Skip to content

Commit 01bdce9

Browse files
modular-magiciannat-henderson
authored andcommitted
insist that project org policy tests run serially (#2654)
<!-- This change is generated by MagicModules. --> /cc @danawillow
1 parent 44f462d commit 01bdce9

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

google/resource_google_project_organization_policy_test.go

+43-27
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,31 @@ import (
1212
"google.golang.org/api/cloudresourcemanager/v1"
1313
)
1414

15-
/*
16-
Tests for `google_project_organization_policy`
15+
// Since each test here is acting on the same project, run the tests serially to
16+
// avoid race conditions and aborted operations.
17+
func TestAccProjectOrganizationPolicy(t *testing.T) {
18+
testCases := map[string]func(t *testing.T){
19+
"boolean": testAccProjectOrganizationPolicy_boolean,
20+
"list_allowAll": testAccProjectOrganizationPolicy_list_allowAll,
21+
"list_allowSome": testAccProjectOrganizationPolicy_list_allowSome,
22+
"list_denySome": testAccProjectOrganizationPolicy_list_denySome,
23+
"list_update": testAccProjectOrganizationPolicy_list_update,
24+
"restore_policy": testAccProjectOrganizationPolicy_restore_defaultTrue,
25+
}
1726

18-
These are *not* run in parallel, as they all use the same project
19-
and I end up with 409 Conflict errors from the API when they are
20-
run in parallel.
21-
*/
27+
for name, tc := range testCases {
28+
// shadow the tc variable into scope so that when
29+
// the loop continues, if t.Run hasn't executed tc(t)
30+
// yet, we don't have a race condition
31+
// see https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
32+
tc := tc
33+
t.Run(name, func(t *testing.T) {
34+
tc(t)
35+
})
36+
}
37+
}
2238

23-
func TestAccProjectOrganizationPolicy_boolean(t *testing.T) {
39+
func testAccProjectOrganizationPolicy_boolean(t *testing.T) {
2440
projectId := getTestProjectFromEnv()
2541

2642
resource.Test(t, resource.TestCase{
@@ -30,12 +46,12 @@ func TestAccProjectOrganizationPolicy_boolean(t *testing.T) {
3046
Steps: []resource.TestStep{
3147
{
3248
// Test creation of an enforced boolean policy
33-
Config: testAccProjectOrganizationPolicy_boolean(projectId, true),
49+
Config: testAccProjectOrganizationPolicyConfig_boolean(projectId, true),
3450
Check: testAccCheckGoogleProjectOrganizationBooleanPolicy("bool", true),
3551
},
3652
{
3753
// Test update from enforced to not
38-
Config: testAccProjectOrganizationPolicy_boolean(projectId, false),
54+
Config: testAccProjectOrganizationPolicyConfig_boolean(projectId, false),
3955
Check: testAccCheckGoogleProjectOrganizationBooleanPolicy("bool", false),
4056
},
4157
{
@@ -44,19 +60,19 @@ func TestAccProjectOrganizationPolicy_boolean(t *testing.T) {
4460
},
4561
{
4662
// Test creation of a not enforced boolean policy
47-
Config: testAccProjectOrganizationPolicy_boolean(projectId, false),
63+
Config: testAccProjectOrganizationPolicyConfig_boolean(projectId, false),
4864
Check: testAccCheckGoogleProjectOrganizationBooleanPolicy("bool", false),
4965
},
5066
{
5167
// Test update from not enforced to enforced
52-
Config: testAccProjectOrganizationPolicy_boolean(projectId, true),
68+
Config: testAccProjectOrganizationPolicyConfig_boolean(projectId, true),
5369
Check: testAccCheckGoogleProjectOrganizationBooleanPolicy("bool", true),
5470
},
5571
},
5672
})
5773
}
5874

59-
func TestAccProjectOrganizationPolicy_list_allowAll(t *testing.T) {
75+
func testAccProjectOrganizationPolicy_list_allowAll(t *testing.T) {
6076
projectId := getTestProjectFromEnv()
6177

6278
resource.Test(t, resource.TestCase{
@@ -65,14 +81,14 @@ func TestAccProjectOrganizationPolicy_list_allowAll(t *testing.T) {
6581
CheckDestroy: testAccCheckGoogleProjectOrganizationPolicyDestroy,
6682
Steps: []resource.TestStep{
6783
{
68-
Config: testAccProjectOrganizationPolicy_list_allowAll(projectId),
84+
Config: testAccProjectOrganizationPolicyConfig_list_allowAll(projectId),
6985
Check: testAccCheckGoogleProjectOrganizationListPolicyAll("list", "ALLOW"),
7086
},
7187
},
7288
})
7389
}
7490

75-
func TestAccProjectOrganizationPolicy_list_allowSome(t *testing.T) {
91+
func testAccProjectOrganizationPolicy_list_allowSome(t *testing.T) {
7692
project := getTestProjectFromEnv()
7793
canonicalProject := canonicalProjectId(project)
7894
resource.Test(t, resource.TestCase{
@@ -81,48 +97,48 @@ func TestAccProjectOrganizationPolicy_list_allowSome(t *testing.T) {
8197
CheckDestroy: testAccCheckGoogleProjectOrganizationPolicyDestroy,
8298
Steps: []resource.TestStep{
8399
{
84-
Config: testAccProjectOrganizationPolicy_list_allowSome(project),
100+
Config: testAccProjectOrganizationPolicyConfig_list_allowSome(project),
85101
Check: testAccCheckGoogleProjectOrganizationListPolicyAllowedValues("list", []string{canonicalProject}),
86102
},
87103
},
88104
})
89105
}
90106

91-
func TestAccProjectOrganizationPolicy_list_denySome(t *testing.T) {
107+
func testAccProjectOrganizationPolicy_list_denySome(t *testing.T) {
92108
projectId := getTestProjectFromEnv()
93109
resource.Test(t, resource.TestCase{
94110
PreCheck: func() { testAccPreCheck(t) },
95111
Providers: testAccProviders,
96112
CheckDestroy: testAccCheckGoogleProjectOrganizationPolicyDestroy,
97113
Steps: []resource.TestStep{
98114
{
99-
Config: testAccProjectOrganizationPolicy_list_denySome(projectId),
115+
Config: testAccProjectOrganizationPolicyConfig_list_denySome(projectId),
100116
Check: testAccCheckGoogleProjectOrganizationListPolicyDeniedValues("list", DENIED_ORG_POLICIES),
101117
},
102118
},
103119
})
104120
}
105121

106-
func TestAccProjectOrganizationPolicy_list_update(t *testing.T) {
122+
func testAccProjectOrganizationPolicy_list_update(t *testing.T) {
107123
projectId := getTestProjectFromEnv()
108124
resource.Test(t, resource.TestCase{
109125
PreCheck: func() { testAccPreCheck(t) },
110126
Providers: testAccProviders,
111127
CheckDestroy: testAccCheckGoogleProjectOrganizationPolicyDestroy,
112128
Steps: []resource.TestStep{
113129
{
114-
Config: testAccProjectOrganizationPolicy_list_allowAll(projectId),
130+
Config: testAccProjectOrganizationPolicyConfig_list_allowAll(projectId),
115131
Check: testAccCheckGoogleProjectOrganizationListPolicyAll("list", "ALLOW"),
116132
},
117133
{
118-
Config: testAccProjectOrganizationPolicy_list_denySome(projectId),
134+
Config: testAccProjectOrganizationPolicyConfig_list_denySome(projectId),
119135
Check: testAccCheckGoogleProjectOrganizationListPolicyDeniedValues("list", DENIED_ORG_POLICIES),
120136
},
121137
},
122138
})
123139
}
124140

125-
func TestAccProjectOrganizationPolicy_restore_defaultTrue(t *testing.T) {
141+
func testAccProjectOrganizationPolicy_restore_defaultTrue(t *testing.T) {
126142
projectId := getTestProjectFromEnv()
127143

128144
resource.Test(t, resource.TestCase{
@@ -131,7 +147,7 @@ func TestAccProjectOrganizationPolicy_restore_defaultTrue(t *testing.T) {
131147
CheckDestroy: testAccCheckGoogleProjectOrganizationPolicyDestroy,
132148
Steps: []resource.TestStep{
133149
{
134-
Config: testAccProjectOrganizationPolicy_restore_defaultTrue(projectId),
150+
Config: testAccProjectOrganizationPolicyConfig_restore_defaultTrue(projectId),
135151
Check: getGoogleProjectOrganizationRestoreDefaultTrue("restore", &cloudresourcemanager.RestoreDefault{}),
136152
},
137153
},
@@ -270,7 +286,7 @@ func getGoogleProjectOrganizationPolicyTestResource(s *terraform.State, n string
270286
}).Do()
271287
}
272288

273-
func testAccProjectOrganizationPolicy_boolean(pid string, enforced bool) string {
289+
func testAccProjectOrganizationPolicyConfig_boolean(pid string, enforced bool) string {
274290
return fmt.Sprintf(`
275291
resource "google_project_organization_policy" "bool" {
276292
project = "%s"
@@ -283,7 +299,7 @@ resource "google_project_organization_policy" "bool" {
283299
`, pid, enforced)
284300
}
285301

286-
func testAccProjectOrganizationPolicy_list_allowAll(pid string) string {
302+
func testAccProjectOrganizationPolicyConfig_list_allowAll(pid string) string {
287303
return fmt.Sprintf(`
288304
resource "google_project_organization_policy" "list" {
289305
project = "%s"
@@ -298,7 +314,7 @@ resource "google_project_organization_policy" "list" {
298314
`, pid)
299315
}
300316

301-
func testAccProjectOrganizationPolicy_list_allowSome(pid string) string {
317+
func testAccProjectOrganizationPolicyConfig_list_allowSome(pid string) string {
302318
return fmt.Sprintf(`
303319
304320
resource "google_project_organization_policy" "list" {
@@ -314,7 +330,7 @@ resource "google_project_organization_policy" "list" {
314330
`, pid, pid)
315331
}
316332

317-
func testAccProjectOrganizationPolicy_list_denySome(pid string) string {
333+
func testAccProjectOrganizationPolicyConfig_list_denySome(pid string) string {
318334
return fmt.Sprintf(`
319335
320336
resource "google_project_organization_policy" "list" {
@@ -333,7 +349,7 @@ resource "google_project_organization_policy" "list" {
333349
`, pid)
334350
}
335351

336-
func testAccProjectOrganizationPolicy_restore_defaultTrue(pid string) string {
352+
func testAccProjectOrganizationPolicyConfig_restore_defaultTrue(pid string) string {
337353
return fmt.Sprintf(`
338354
resource "google_project_organization_policy" "restore" {
339355
project = "%s"

0 commit comments

Comments
 (0)