Skip to content

Commit

Permalink
Add precheck for credentials in test environment. Skip if missing, to…
Browse files Browse the repository at this point in the history
… let checks pass in hashicorp/terraform repo.
  • Loading branch information
SarahFrench committed Apr 25, 2024
1 parent 350eb57 commit f93c4f7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
7 changes: 3 additions & 4 deletions internal/backend/remote-state/gcs/backend_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import (
)

func TestBackendConfig_encryptionKey(t *testing.T) {
preCheckEnvironmentVariables(t)
// Cannot use t.Parallel as t.SetEnv used

// TODO - add pre check that asserts ENVs for credentials are set when the test runs

// getWantValue is required because the key input is changed internally in the backend's code
// This function is a quick way to help us get a want value, but ideally in future the test and
// the code under test will use a reusable function to avoid logic duplication.
Expand Down Expand Up @@ -104,8 +103,8 @@ func TestBackendConfig_encryptionKey(t *testing.T) {
}

func TestBackendConfig_kmsKey(t *testing.T) {
t.Parallel()
// TODO - add pre check that asserts ENVs for credentials are set when the test runs
preCheckEnvironmentVariables(t)
// Cannot use t.Parallel() due to t.Setenv

cases := map[string]struct {
config map[string]interface{}
Expand Down
49 changes: 40 additions & 9 deletions internal/backend/remote-state/gcs/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,40 @@ func preCheckTestAcc(t *testing.T) {
}
}

func preCheckEnvironmentVariables(t *testing.T) {

// TODO - implement a separate function specific to credentials which will iterate through a list of ENV names and return first value found.

credentials := []string{
"GOOGLE_BACKEND_CREDENTIALS",
"GOOGLE_CREDENTIALS",
}
credsFound := false
for _, name := range credentials {
v := os.Getenv(name)
if v != "" {
credsFound = true
break
}
}
if !credsFound {
// Skipping tests because hashicorp/terraform repo doesn't have credentials set up.
// In future we should enable tests to run automatically, and make this code fail when no creds are set.
t.Skip("credentials need to be provided via GOOGLE_BACKEND_CREDENTIALS or GOOGLE_CREDENTIALS environment variable but neither is set")
}
}

func TestAccBackendConfig_credentials(t *testing.T) {
preCheckTestAcc(t)
// Cannot use t.Parallel() due to t.Setenv
preCheckTestAcc(t)
preCheckEnvironmentVariables(t)

credentials := os.Getenv("GOOGLE_BACKEND_CREDENTIALS")
if credentials == "" {
credentials = os.Getenv("GOOGLE_CREDENTIALS")
}
if credentials == "" {
t.Fatalf("test requires credentials to be set as either GOOGLE_BACKEND_CREDENTIALS or GOOGLE_CREDENTIALS but neither is set")
t.Fatalf("unable to access credentials from the test environment")
}

t.Setenv("GOOGLE_BACKEND_CREDENTIALS", "") // unset value
Expand Down Expand Up @@ -119,8 +143,9 @@ func TestAccBackendConfig_credentials(t *testing.T) {
}

func TestAccRemoteClient(t *testing.T) {
preCheckTestAcc(t)
t.Parallel()
preCheckTestAcc(t)
preCheckEnvironmentVariables(t)

bucket := bucketName(t)
config := map[string]interface{}{
Expand All @@ -143,8 +168,9 @@ func TestAccRemoteClient(t *testing.T) {
}

func TestAccRemoteClientWithEncryption(t *testing.T) {
preCheckTestAcc(t)
t.Parallel()
preCheckTestAcc(t)
preCheckEnvironmentVariables(t)

bucket := bucketName(t)
config := map[string]interface{}{
Expand All @@ -168,8 +194,9 @@ func TestAccRemoteClientWithEncryption(t *testing.T) {
}

func TestAccRemoteLocks(t *testing.T) {
preCheckTestAcc(t)
t.Parallel()
preCheckTestAcc(t)
preCheckEnvironmentVariables(t)

bucket := bucketName(t)
config := map[string]interface{}{
Expand Down Expand Up @@ -205,8 +232,9 @@ func TestAccRemoteLocks(t *testing.T) {
}

func TestAccBackend(t *testing.T) {
preCheckTestAcc(t)
t.Parallel()
preCheckTestAcc(t)
preCheckEnvironmentVariables(t)

bucket := bucketName(t)

Expand All @@ -224,8 +252,9 @@ func TestAccBackend(t *testing.T) {
}

func TestAccBackendWithPrefix(t *testing.T) {
preCheckTestAcc(t)
t.Parallel()
preCheckTestAcc(t)
preCheckEnvironmentVariables(t)

prefix := "test/prefix"
bucket := bucketName(t)
Expand All @@ -246,8 +275,9 @@ func TestAccBackendWithPrefix(t *testing.T) {
}

func TestAccBackendWithCustomerSuppliedEncryption(t *testing.T) {
preCheckTestAcc(t)
t.Parallel()
preCheckTestAcc(t)
preCheckEnvironmentVariables(t)

bucket := bucketName(t)

Expand All @@ -265,8 +295,9 @@ func TestAccBackendWithCustomerSuppliedEncryption(t *testing.T) {
}

func TestAccBackendWithCustomerManagedKMSEncryption(t *testing.T) {
preCheckTestAcc(t)
t.Parallel()
preCheckTestAcc(t)
preCheckEnvironmentVariables(t)

projectID := os.Getenv("GOOGLE_PROJECT")
bucket := bucketName(t)
Expand Down

0 comments on commit f93c4f7

Please sign in to comment.