@@ -12,15 +12,31 @@ import (
12
12
"google.golang.org/api/cloudresourcemanager/v1"
13
13
)
14
14
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
+ }
17
26
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
+ }
22
38
23
- func TestAccProjectOrganizationPolicy_boolean (t * testing.T ) {
39
+ func testAccProjectOrganizationPolicy_boolean (t * testing.T ) {
24
40
projectId := getTestProjectFromEnv ()
25
41
26
42
resource .Test (t , resource.TestCase {
@@ -30,12 +46,12 @@ func TestAccProjectOrganizationPolicy_boolean(t *testing.T) {
30
46
Steps : []resource.TestStep {
31
47
{
32
48
// Test creation of an enforced boolean policy
33
- Config : testAccProjectOrganizationPolicy_boolean (projectId , true ),
49
+ Config : testAccProjectOrganizationPolicyConfig_boolean (projectId , true ),
34
50
Check : testAccCheckGoogleProjectOrganizationBooleanPolicy ("bool" , true ),
35
51
},
36
52
{
37
53
// Test update from enforced to not
38
- Config : testAccProjectOrganizationPolicy_boolean (projectId , false ),
54
+ Config : testAccProjectOrganizationPolicyConfig_boolean (projectId , false ),
39
55
Check : testAccCheckGoogleProjectOrganizationBooleanPolicy ("bool" , false ),
40
56
},
41
57
{
@@ -44,19 +60,19 @@ func TestAccProjectOrganizationPolicy_boolean(t *testing.T) {
44
60
},
45
61
{
46
62
// Test creation of a not enforced boolean policy
47
- Config : testAccProjectOrganizationPolicy_boolean (projectId , false ),
63
+ Config : testAccProjectOrganizationPolicyConfig_boolean (projectId , false ),
48
64
Check : testAccCheckGoogleProjectOrganizationBooleanPolicy ("bool" , false ),
49
65
},
50
66
{
51
67
// Test update from not enforced to enforced
52
- Config : testAccProjectOrganizationPolicy_boolean (projectId , true ),
68
+ Config : testAccProjectOrganizationPolicyConfig_boolean (projectId , true ),
53
69
Check : testAccCheckGoogleProjectOrganizationBooleanPolicy ("bool" , true ),
54
70
},
55
71
},
56
72
})
57
73
}
58
74
59
- func TestAccProjectOrganizationPolicy_list_allowAll (t * testing.T ) {
75
+ func testAccProjectOrganizationPolicy_list_allowAll (t * testing.T ) {
60
76
projectId := getTestProjectFromEnv ()
61
77
62
78
resource .Test (t , resource.TestCase {
@@ -65,14 +81,14 @@ func TestAccProjectOrganizationPolicy_list_allowAll(t *testing.T) {
65
81
CheckDestroy : testAccCheckGoogleProjectOrganizationPolicyDestroy ,
66
82
Steps : []resource.TestStep {
67
83
{
68
- Config : testAccProjectOrganizationPolicy_list_allowAll (projectId ),
84
+ Config : testAccProjectOrganizationPolicyConfig_list_allowAll (projectId ),
69
85
Check : testAccCheckGoogleProjectOrganizationListPolicyAll ("list" , "ALLOW" ),
70
86
},
71
87
},
72
88
})
73
89
}
74
90
75
- func TestAccProjectOrganizationPolicy_list_allowSome (t * testing.T ) {
91
+ func testAccProjectOrganizationPolicy_list_allowSome (t * testing.T ) {
76
92
project := getTestProjectFromEnv ()
77
93
canonicalProject := canonicalProjectId (project )
78
94
resource .Test (t , resource.TestCase {
@@ -81,48 +97,48 @@ func TestAccProjectOrganizationPolicy_list_allowSome(t *testing.T) {
81
97
CheckDestroy : testAccCheckGoogleProjectOrganizationPolicyDestroy ,
82
98
Steps : []resource.TestStep {
83
99
{
84
- Config : testAccProjectOrganizationPolicy_list_allowSome (project ),
100
+ Config : testAccProjectOrganizationPolicyConfig_list_allowSome (project ),
85
101
Check : testAccCheckGoogleProjectOrganizationListPolicyAllowedValues ("list" , []string {canonicalProject }),
86
102
},
87
103
},
88
104
})
89
105
}
90
106
91
- func TestAccProjectOrganizationPolicy_list_denySome (t * testing.T ) {
107
+ func testAccProjectOrganizationPolicy_list_denySome (t * testing.T ) {
92
108
projectId := getTestProjectFromEnv ()
93
109
resource .Test (t , resource.TestCase {
94
110
PreCheck : func () { testAccPreCheck (t ) },
95
111
Providers : testAccProviders ,
96
112
CheckDestroy : testAccCheckGoogleProjectOrganizationPolicyDestroy ,
97
113
Steps : []resource.TestStep {
98
114
{
99
- Config : testAccProjectOrganizationPolicy_list_denySome (projectId ),
115
+ Config : testAccProjectOrganizationPolicyConfig_list_denySome (projectId ),
100
116
Check : testAccCheckGoogleProjectOrganizationListPolicyDeniedValues ("list" , DENIED_ORG_POLICIES ),
101
117
},
102
118
},
103
119
})
104
120
}
105
121
106
- func TestAccProjectOrganizationPolicy_list_update (t * testing.T ) {
122
+ func testAccProjectOrganizationPolicy_list_update (t * testing.T ) {
107
123
projectId := getTestProjectFromEnv ()
108
124
resource .Test (t , resource.TestCase {
109
125
PreCheck : func () { testAccPreCheck (t ) },
110
126
Providers : testAccProviders ,
111
127
CheckDestroy : testAccCheckGoogleProjectOrganizationPolicyDestroy ,
112
128
Steps : []resource.TestStep {
113
129
{
114
- Config : testAccProjectOrganizationPolicy_list_allowAll (projectId ),
130
+ Config : testAccProjectOrganizationPolicyConfig_list_allowAll (projectId ),
115
131
Check : testAccCheckGoogleProjectOrganizationListPolicyAll ("list" , "ALLOW" ),
116
132
},
117
133
{
118
- Config : testAccProjectOrganizationPolicy_list_denySome (projectId ),
134
+ Config : testAccProjectOrganizationPolicyConfig_list_denySome (projectId ),
119
135
Check : testAccCheckGoogleProjectOrganizationListPolicyDeniedValues ("list" , DENIED_ORG_POLICIES ),
120
136
},
121
137
},
122
138
})
123
139
}
124
140
125
- func TestAccProjectOrganizationPolicy_restore_defaultTrue (t * testing.T ) {
141
+ func testAccProjectOrganizationPolicy_restore_defaultTrue (t * testing.T ) {
126
142
projectId := getTestProjectFromEnv ()
127
143
128
144
resource .Test (t , resource.TestCase {
@@ -131,7 +147,7 @@ func TestAccProjectOrganizationPolicy_restore_defaultTrue(t *testing.T) {
131
147
CheckDestroy : testAccCheckGoogleProjectOrganizationPolicyDestroy ,
132
148
Steps : []resource.TestStep {
133
149
{
134
- Config : testAccProjectOrganizationPolicy_restore_defaultTrue (projectId ),
150
+ Config : testAccProjectOrganizationPolicyConfig_restore_defaultTrue (projectId ),
135
151
Check : getGoogleProjectOrganizationRestoreDefaultTrue ("restore" , & cloudresourcemanager.RestoreDefault {}),
136
152
},
137
153
},
@@ -270,7 +286,7 @@ func getGoogleProjectOrganizationPolicyTestResource(s *terraform.State, n string
270
286
}).Do ()
271
287
}
272
288
273
- func testAccProjectOrganizationPolicy_boolean (pid string , enforced bool ) string {
289
+ func testAccProjectOrganizationPolicyConfig_boolean (pid string , enforced bool ) string {
274
290
return fmt .Sprintf (`
275
291
resource "google_project_organization_policy" "bool" {
276
292
project = "%s"
@@ -283,7 +299,7 @@ resource "google_project_organization_policy" "bool" {
283
299
` , pid , enforced )
284
300
}
285
301
286
- func testAccProjectOrganizationPolicy_list_allowAll (pid string ) string {
302
+ func testAccProjectOrganizationPolicyConfig_list_allowAll (pid string ) string {
287
303
return fmt .Sprintf (`
288
304
resource "google_project_organization_policy" "list" {
289
305
project = "%s"
@@ -298,7 +314,7 @@ resource "google_project_organization_policy" "list" {
298
314
` , pid )
299
315
}
300
316
301
- func testAccProjectOrganizationPolicy_list_allowSome (pid string ) string {
317
+ func testAccProjectOrganizationPolicyConfig_list_allowSome (pid string ) string {
302
318
return fmt .Sprintf (`
303
319
304
320
resource "google_project_organization_policy" "list" {
@@ -314,7 +330,7 @@ resource "google_project_organization_policy" "list" {
314
330
` , pid , pid )
315
331
}
316
332
317
- func testAccProjectOrganizationPolicy_list_denySome (pid string ) string {
333
+ func testAccProjectOrganizationPolicyConfig_list_denySome (pid string ) string {
318
334
return fmt .Sprintf (`
319
335
320
336
resource "google_project_organization_policy" "list" {
@@ -333,7 +349,7 @@ resource "google_project_organization_policy" "list" {
333
349
` , pid )
334
350
}
335
351
336
- func testAccProjectOrganizationPolicy_restore_defaultTrue (pid string ) string {
352
+ func testAccProjectOrganizationPolicyConfig_restore_defaultTrue (pid string ) string {
337
353
return fmt .Sprintf (`
338
354
resource "google_project_organization_policy" "restore" {
339
355
project = "%s"
0 commit comments