-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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 error reading remote workspace with version constraint #36356
base: main
Are you sure you want to change the base?
Conversation
dc706ad
to
8323e45
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👋🏻 Hi Shweta! I think this PR looks good, though I've not worked on the cloud/remote backends before so needed to dig in a little.
I believe before your PR users would see a "Malformed version"
error if the workspace's Terraform version is a version constraint string, and after your PR's fixes users will only get an error if their Terraform version doesn't match the version constraint ("Terraform version mismatch"
).
Could you please add a new test case to cover this scenario? E.g. you could set the workspace's Terraform version to something like ">= 9.9.9" and assert about the error string in TestRemote_VerifyWorkspaceTerraformVersion_workspaceErrors
. However if I've overlooked existing test coverage for the problem this PR is fixing please let me know.
Also, could you please resolve the CLA issue mentioned on the PR? Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the test! There's an edit required in the change file, but once that's done I'm happy to approve
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really nice work! 😄
@SarahFrench's initial suggestion was fantastic. I think we can further improve our tests to make sure we have all our bases covered.
if _, err := b.client.Workspaces.Update( | ||
context.Background(), | ||
b.organization, | ||
b.workspace, | ||
tfe.WorkspaceUpdateOptions{ | ||
TerraformVersion: tfe.String(">= 9.9.9"), | ||
}, | ||
); err != nil { | ||
t.Fatalf("error: %v", err) | ||
} | ||
diags := b.VerifyWorkspaceTerraformVersion(backend.DefaultStateName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should create a table test to validate different version constraint scenarios. This style of structuring tests is a very common pattern when unit testing in Go. Example:
func TestRemote_VerifyWorkspaceTerraformVersion_versionConstraint(t *testing.T) {
// configure backend
// Define our test case struct
type testCase struct {
terraformVersion string
versionConstraint string
shouldSatisfy bool
}
// Create a slice of test cases
testCases := []testCase{
{
terraformVersion: "1.8.0",
terraformVersionConstraint: "> 1.9.0",
shouldSatisfy: false,
},
{
terraformVersion: "1.10.1",
terraformVersionConstraint: "~> 1.10.0",
shouldSatisfy: true,
},
{
// etc
},
}
// Now we loop through each test case, utilizing the values of each case
// to setup our test and assert accordingly.
for _, tc := range testCases {
// Mock the Terraform version
// Update the mocked workspace with the constraint
// Call `VerifyWorkspaceTerraformVersion` and assert any diagnostics depending on whether
// shouldSatisfy is true or not
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, if these tests are applicable to both the remote and cloud backends then maybe those changes should be propagated to the cloud backend? That could be saved for a separate PR, though.
Co-authored-by: Sebastian Rivera <[email protected]>
Implementing the functionality that exists in cloud backend to remote as well to avoid errors on certain terraform commands when a version constraint is present.
CHANGELOG entry