Skip to content

Commit

Permalink
Add test that asserts that setting credentials to "" in the config …
Browse files Browse the repository at this point in the history
…doesn't affect use of ENV-supplied credentials
  • Loading branch information
SarahFrench committed Apr 25, 2024
1 parent 55e2496 commit ed83722
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions internal/backend/remote-state/gcs/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,74 @@ func TestBackendConfig_kmsKey(t *testing.T) {
}
}

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

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.Setenv("GOOGLE_BACKEND_CREDENTIALS", "") // unset value
t.Setenv("GOOGLE_CREDENTIALS", "") // unset value

cases := map[string]struct {
config map[string]interface{}
envs map[string]string
want string
}{
"empty credentials in config doesn't affect use of GOOGLE_BACKEND_CREDENTIALS": {
config: map[string]interface{}{
"bucket": "tf-test-testaccbackendconfig_credentials_1",
"credentials": "",
},
envs: map[string]string{
"GOOGLE_BACKEND_CREDENTIALS": credentials,
},
},
"empty credentials in config doesn't affect use of GOOGLE_CREDENTIALS": {
config: map[string]interface{}{
"bucket": "tf-test-testaccbackendconfig_credentials_2",
"credentials": "",
},
envs: map[string]string{
"GOOGLE_CREDENTIALS": credentials,
},
},
// Uncomment below for sanity checking
// Testing with TestBackendConfig currently doesn't let us assert for errors, so instead
// run the below and expect it to fail.
// "nonsense in config causes an error and GOOGLE_CREDENTIALS isn't used": {
// config: map[string]interface{}{
// "bucket": "tf-test-testaccbackendconfig_credentials_3",
// "credentials": "foobar",
// },
// envs: map[string]string{
// "GOOGLE_CREDENTIALS": credentials,
// },
// },
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
for k, v := range tc.envs {
t.Setenv(k, v)
}

be0 := setupBackend(t, tc.config)
defer teardownBackend(t, be0, noPrefix)

be1 := setupBackend(t, tc.config)

backend.TestBackendStates(t, be0)
backend.TestBackendStateLocks(t, be0, be1)
})
}

}

func TestStateFile(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit ed83722

Please sign in to comment.