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 error reading remote workspace with version constraint #36356

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

Conversation

shwetamurali
Copy link

@shwetamurali shwetamurali commented Jan 16, 2025

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

  • This change is user-facing and I added a changelog entry.
  • This change is not user-facing.

Copy link

hashicorp-cla-app bot commented Jan 16, 2025

CLA assistant check
All committers have signed the CLA.

@shwetamurali shwetamurali marked this pull request as ready for review January 17, 2025 22:48
@shwetamurali shwetamurali requested a review from a team as a code owner January 17, 2025 22:48
Copy link
Member

@SarahFrench SarahFrench left a 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!

Copy link
Member

@SarahFrench SarahFrench left a 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

.changes/unreleased/BUG FIXES-20250123-135228.yaml Outdated Show resolved Hide resolved
Copy link
Contributor

@sebasslash sebasslash left a 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.

.changes/unreleased/BUG FIXES-20250123-135228.yaml Outdated Show resolved Hide resolved
Comment on lines +679 to +689
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)
Copy link
Contributor

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
  }
}

Copy link
Member

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.

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.

3 participants