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: auto-discover workspaces when in subdirectories #4363

Merged
merged 1 commit into from
Mar 16, 2024

Conversation

marcoceppi
Copy link
Contributor

what

Detect workspaces from subdirectories during autodiscovery.

why

  • When using terraform cloud configurations for multiple projects / modules in a single repo the default Atlantis workflow will fail

tests

  • I have tested my changes by building a local version of Atlantis and deploying it against a repo that previously failed auto discovery
  • I have created unit tests to verify this functionality continues to work going forward

references

closes #3252
supersedes #3253

@marcoceppi marcoceppi requested review from a team as code owners March 16, 2024 12:00
@marcoceppi marcoceppi requested review from chenrui333, nitrocode and X-Guardian and removed request for a team March 16, 2024 12:00
@github-actions github-actions bot added the go Pull requests that update Go code label Mar 16, 2024
@GenPage GenPage self-assigned this Mar 16, 2024
@GenPage GenPage added bug Something isn't working quick-win Is obviously something Atlantis should do and will take <4 hrs work provider/terraform-cloud labels Mar 16, 2024
@GenPage GenPage added this to the v0.28.0 milestone Mar 16, 2024
Copy link
Member

@GenPage GenPage left a comment

Choose a reason for hiding this comment

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

💯 Thanks Marco!

@GenPage GenPage merged commit f214105 into runatlantis:main Mar 16, 2024
23 of 24 checks passed
@marcoceppi marcoceppi deleted the fix/3252 branch March 16, 2024 22:50
@X-Guardian
Copy link
Contributor

These tests are flaky on Linux and fail consistently on Windows.

On Linux sometimes fails with:

--- FAIL: TestDefaultProjectCommandBuilder_BuildAutoplanCommands/workspaces_from_subdirectories_detected (0.01s)
        project_command_builder_test.go:292: [work != test]

            exp: work******
            got: test

On Windows, always fails with:

--- FAIL: TestDefaultProjectCommandBuilder_BuildAutoplanCommands/workspaces_from_subdirectories_detected (0.00s)
        project_command_builder_test.go:288: [2 != 1]

            exp: int(2)******
            got: int(1)

Can we revert the merge of this PR and have this looked at.

@GenPage
Copy link
Member

GenPage commented Mar 25, 2024

I think I could see where this possibly flaky issue is coming from in the tests. Here's my running theory.

Description: "workspaces from subdirectories detected",
TestDirStructure: map[string]interface{}{
"work": map[string]interface{}{
"main.tf": `
terraform {
cloud {
organization = "atlantis-test"
workspaces {
name = "test-workspace1"
}
}
}`,
},
"test": map[string]interface{}{
"main.tf": `
terraform {
cloud {
organization = "atlantis-test"
workspaces {
name = "test-workspace12"
}
}
}`,
},
},
exp: []expCtxFields{
{
ProjectName: "",
RepoRelDir: "work",
Workspace: "test-workspace1",
},
{
ProjectName: "",
RepoRelDir: "test",
Workspace: "test-workspace12",
},
},
},

For this specific test case, there is two test directories test and work, from which each have different workspaces.

ctxs, err := builder.BuildAutoplanCommands(&command.Context{
PullRequestStatus: models.PullReqStatus{
Mergeable: true,
},
Log: logger,
Scope: scope,
})
Ok(t, err)
Equals(t, len(c.exp), len(ctxs))
for i, actCtx := range ctxs {
expCtx := c.exp[i]
Equals(t, expCtx.ProjectName, actCtx.ProjectName)
Equals(t, expCtx.RepoRelDir, actCtx.RepoRelDir)
Equals(t, expCtx.Workspace, actCtx.Workspace)
}

Here is the logic to compare the actual contexts which the ones that are expected set in an array. The camparsion being done is the string key of the map interface for TestDirStructure compared to the RepoRelDir in the expCtxFields array.

I believe the flaky test is when the arrays get out of order compared to one another. So test != work happens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working go Pull requests that update Go code provider/terraform-cloud quick-win Is obviously something Atlantis should do and will take <4 hrs work
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

Workspace name isn't detected for Terraform Cloud in subfolders
3 participants