Skip to content

Commit f38e30f

Browse files
Fix tests to use dedicated bootstrapped service accounts instead of one shared account (#10418) (#7244)
[upstream:7041d8052bd61acf8a05feb51cdccf950ff072e6] Signed-off-by: Modular Magician <[email protected]>
1 parent c0488e8 commit f38e30f

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

.changelog/10418.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:none
2+
3+
```

google-beta/acctest/bootstrap_test_utils.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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-"
149149
var 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
}

google-beta/services/resourcemanager/data_source_google_service_account_access_token_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestAccDataSourceGoogleServiceAccountAccessToken_basic(t *testing.T) {
3636

3737
resourceName := "data.google_service_account_access_token.default"
3838
serviceAccount := envvar.GetTestServiceAccountFromEnv(t)
39-
targetServiceAccountEmail := acctest.BootstrapServiceAccount(t, envvar.GetTestProjectFromEnv(), serviceAccount)
39+
targetServiceAccountEmail := acctest.BootstrapServiceAccount(t, "acctoken", serviceAccount)
4040

4141
acctest.VcrTest(t, resource.TestCase{
4242
PreCheck: func() { acctest.AccTestPreCheck(t) },

google-beta/services/resourcemanager/data_source_google_service_account_id_token_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func TestAccDataSourceGoogleServiceAccountIdToken_impersonation(t *testing.T) {
7575

7676
resourceName := "data.google_service_account_id_token.default"
7777
serviceAccount := envvar.GetTestServiceAccountFromEnv(t)
78-
targetServiceAccountEmail := acctest.BootstrapServiceAccount(t, envvar.GetTestProjectFromEnv(), serviceAccount)
78+
targetServiceAccountEmail := acctest.BootstrapServiceAccount(t, "idtoken-imp", serviceAccount)
7979

8080
resource.Test(t, resource.TestCase{
8181
PreCheck: func() { acctest.AccTestPreCheck(t) },

google-beta/services/resourcemanager/data_source_google_service_account_jwt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func TestAccDataSourceGoogleServiceAccountJwt(t *testing.T) {
102102

103103
resourceName := "data.google_service_account_jwt.default"
104104
serviceAccount := envvar.GetTestServiceAccountFromEnv(t)
105-
targetServiceAccountEmail := acctest.BootstrapServiceAccount(t, envvar.GetTestProjectFromEnv(), serviceAccount)
105+
targetServiceAccountEmail := acctest.BootstrapServiceAccount(t, "jwt", serviceAccount)
106106

107107
staticTime := time.Now()
108108

0 commit comments

Comments
 (0)