@@ -145,13 +145,13 @@ func BootstrapKMSKeyWithPurposeInLocationAndName(t *testing.T, purpose, location
145145 }
146146}
147147
148- var serviceAccountEmail = "tf-bootstrap-service-account "
148+ var serviceAccountPrefix = "tf-bootstrap-sa- "
149149var serviceAccountDisplay = "Bootstrapped Service Account for Terraform tests"
150150
151151// Some tests need a second service account, other than the test runner, to assert functionality on.
152152// This provides a well-known service account that can be used when dynamically creating a service
153153// account isn't an option.
154- func getOrCreateServiceAccount (config * transport_tpg.Config , project string ) (* iam.ServiceAccount , error ) {
154+ func getOrCreateServiceAccount (config * transport_tpg.Config , project , serviceAccountEmail string ) (* iam.ServiceAccount , error ) {
155155 name := fmt .Sprintf ("projects/%s/serviceAccounts/%s@%s.iam.gserviceaccount.com" , project , serviceAccountEmail , project )
156156 log .Printf ("[DEBUG] Verifying %s as bootstrapped service account.\n " , name )
157157
@@ -208,13 +208,19 @@ func impersonationServiceAccountPermissions(config *transport_tpg.Config, sa *ia
208208 return nil
209209}
210210
211- func BootstrapServiceAccount (t * testing.T , project , testRunner string ) string {
211+ // A separate testId should be used for each test, to create separate service accounts for each,
212+ // and avoid race conditions where the policy of the same service account is being modified by 2
213+ // tests at once. This is needed as long as the function overwrites the policy on every run.
214+ func BootstrapServiceAccount (t * testing.T , testId , testRunner string ) string {
215+ project := envvar .GetTestProjectFromEnv ()
216+ serviceAccountEmail := serviceAccountPrefix + testId
217+
212218 config := BootstrapConfig (t )
213219 if config == nil {
214220 return ""
215221 }
216222
217- sa , err := getOrCreateServiceAccount (config , project )
223+ sa , err := getOrCreateServiceAccount (config , project , serviceAccountEmail )
218224 if err != nil {
219225 t .Fatalf ("Bootstrapping failed. Cannot retrieve service account, %s" , err )
220226 }
@@ -1230,7 +1236,8 @@ func SetupProjectsAndGetAccessToken(org, billing, pid, service string, config *t
12301236 }
12311237
12321238 // Create a service account for project-1
1233- sa1 , err := getOrCreateServiceAccount (config , pid )
1239+ serviceAccountEmail := serviceAccountPrefix + service
1240+ sa1 , err := getOrCreateServiceAccount (config , pid , serviceAccountEmail )
12341241 if err != nil {
12351242 return "" , err
12361243 }
0 commit comments