Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests to use dedicated bootstrapped service accounts instead of one shared account #10418

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, "acctoken", 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, "idtoken-imp", 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