Skip to content

Commit

Permalink
Fix tests to use dedicated bootstrapped service accounts instead of o…
Browse files Browse the repository at this point in the history
…ne shared account
  • Loading branch information
roaks3 committed Apr 11, 2024
1 parent 01ead35 commit 04780b4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
17 changes: 12 additions & 5 deletions mmv1/third_party/terraform/acctest/bootstrap_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ func BootstrapKMSKeyWithPurposeInLocationAndName(t *testing.T, purpose, location
}
}

var serviceAccountEmail = "tf-bootstrap-service-account"
var serviceAccountPrefix = "tf-bootstrap-sa-"
var serviceAccountDisplay = "Bootstrapped Service Account for Terraform tests"

// Some tests need a second service account, other than the test runner, to assert functionality on.
// This provides a well-known service account that can be used when dynamically creating a service
// account isn't an option.
func getOrCreateServiceAccount(config *transport_tpg.Config, project string) (*iam.ServiceAccount, error) {
func getOrCreateServiceAccount(config *transport_tpg.Config, project, serviceAccountEmail string) (*iam.ServiceAccount, error) {
name := fmt.Sprintf("projects/%s/serviceAccounts/%s@%s.iam.gserviceaccount.com", project, serviceAccountEmail, project)
log.Printf("[DEBUG] Verifying %s as bootstrapped service account.\n", name)

Expand Down Expand Up @@ -206,13 +206,19 @@ func impersonationServiceAccountPermissions(config *transport_tpg.Config, sa *ia
return nil
}

func BootstrapServiceAccount(t *testing.T, project, testRunner string) string {
// A separate testId should be used for each test, to create separate service accounts for each,
// and avoid race conditions where the policy of the same service account is being modified by 2
// tests at once. This is needed as long as the function overwrites the policy on every run.
func BootstrapServiceAccount(t *testing.T, testId, testRunner string) string {
project := envvar.GetTestProjectFromEnv()
serviceAccountEmail := serviceAccountPrefix + testId

config := BootstrapConfig(t)
if config == nil {
return ""
}

sa, err := getOrCreateServiceAccount(config, project)
sa, err := getOrCreateServiceAccount(config, project, serviceAccountEmail)
if err != nil {
t.Fatalf("Bootstrapping failed. Cannot retrieve service account, %s", err)
}
Expand Down Expand Up @@ -1228,7 +1234,8 @@ func SetupProjectsAndGetAccessToken(org, billing, pid, service string, config *t
}

// Create a service account for project-1
sa1, err := getOrCreateServiceAccount(config, pid)
serviceAccountEmail := serviceAccountPrefix + service
sa1, err := getOrCreateServiceAccount(config, pid, serviceAccountEmail)
if err != nil {
return "", err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestAccDataSourceGoogleServiceAccountAccessToken_basic(t *testing.T) {

resourceName := "data.google_service_account_access_token.default"
serviceAccount := envvar.GetTestServiceAccountFromEnv(t)
targetServiceAccountEmail := acctest.BootstrapServiceAccount(t, envvar.GetTestProjectFromEnv(), serviceAccount)
targetServiceAccountEmail := acctest.BootstrapServiceAccount(t, "access-token-basic", serviceAccount)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestAccDataSourceGoogleServiceAccountIdToken_impersonation(t *testing.T) {

resourceName := "data.google_service_account_id_token.default"
serviceAccount := envvar.GetTestServiceAccountFromEnv(t)
targetServiceAccountEmail := acctest.BootstrapServiceAccount(t, envvar.GetTestProjectFromEnv(), serviceAccount)
targetServiceAccountEmail := acctest.BootstrapServiceAccount(t, "id-token-impersonate", serviceAccount)

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestAccDataSourceGoogleServiceAccountJwt(t *testing.T) {

resourceName := "data.google_service_account_jwt.default"
serviceAccount := envvar.GetTestServiceAccountFromEnv(t)
targetServiceAccountEmail := acctest.BootstrapServiceAccount(t, envvar.GetTestProjectFromEnv(), serviceAccount)
targetServiceAccountEmail := acctest.BootstrapServiceAccount(t, "jwt", serviceAccount)

staticTime := time.Now()

Expand Down

0 comments on commit 04780b4

Please sign in to comment.