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

Add acceptance and internal tests for gcs backend, add some test helper functions #35026

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

SarahFrench
Copy link
Member

@SarahFrench SarahFrench commented Apr 18, 2024

Historically the gcs backend hasn't been touched much. In the past I've added a few features but largely left the existing codebase as it was previously. Recently the gcs backend was refactored in #34989 to avoid using the old SDK.

As part of reviewing that PR I started adding a few acceptance tests in this PR. There's a lot more work to be done but I want to avoid this PR getting too big.

This PR:

  • Separates acceptance tests and 'internal' tests into separate _test.go files
  • Implements some pre-check functions to test for TF_ACC being set and credentials being supplied by the test environment
    • When this info is missing the tests are skipped, to enable checks passing in this repo. This repo has no automated testing of the gcs backend set up (//TODO!)
  • Adds internal tests that assert:
    • TestBackendConfig_encryptionKey - how the config and ENVs for customer-supplied encryption keys are used
    • TestBackendConfig_kmsKey - how the config and ENVs for customer-managed (KMS) encryption keys are used
  • Adds acc tests the assert:
    • TestAccBackendConfig_credentials - empty strings in the backend config don't interrupt use of ENVs for creds.
    • Note: we cannot have tests where we assert a failure, due to legacy reasons.

Target Release

1.8.x

Draft CHANGELOG entry

NEW FEATURES | UPGRADE NOTES | ENHANCEMENTS | BUG FIXES | EXPERIMENTS

@SarahFrench
Copy link
Member Author

Just rebased to include the latest gcs PR

Move `TestBackendEncryptionKeyEmptyConflict` test to internal test file
@SarahFrench SarahFrench marked this pull request as ready for review April 26, 2024 11:25
@SarahFrench SarahFrench requested a review from a team as a code owner April 26, 2024 11:25
@SarahFrench SarahFrench changed the title Add tests for gcs backend Add acceptance and internal tests for gcs backend, add some test helper functions Apr 26, 2024
Copy link
Contributor

@jrhouston jrhouston left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to approve this, the comments in my review are only superficial.

// 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.
getWantValue := func(key string) []byte {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you've made this a closure because you want access to the t variable but you could just make this it's own func with testing.T as a parameter.

edit: ah, you mention this in the comment above

getWantValue := func(key string) []byte {
var want []byte
if key == "" {
want = nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could just return nil here since the next conditional is key != ""?

if key == "" {
want = nil
}
if key != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could just be else or not even be a conditional if we just return early in the conditional above.

"GOOGLE_CREDENTIALS": credentials,
},
},
// Uncomment below for sanity checking
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably best not to merge in lines of commented out code – if this is a purposeful test it would be better to formulate it in a way that a failing test isn't the desired outcome.

preCheckEnvironmentVariables(t)
// Cannot use t.Parallel as t.SetEnv used

// getWantValue is required because the key input is changed internally in the backend's code
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we are just copying convention used elsewhere in the codebase, but trying to parse "get want value" hurt my brain. We can leave as is, but I suspect the language we wanted to use here was expected instead of want.

if got := b.stateFile(c.name); got != c.wantStateFile {
t.Errorf("stateFile(%q) = %q, want %q", c.name, got, c.wantStateFile)
}
// TODO - implement a separate function specific to credentials which will iterate through a list of ENV names and return first value found.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a note to self to be answered in this PR, or for later?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants