Skip to content

Commit

Permalink
Allow pre-release versions to pass version constraints (#3405)
Browse files Browse the repository at this point in the history
* fix: Adding test for pre-release in version constraints

* fix: Test version constraints against `Core` for pre-releases so that pre-release data is ignored.

* fix: Fix race condition in `TestTerragruntVersionConstraints`
  • Loading branch information
yhakbar authored Sep 16, 2024
1 parent 68e95e1 commit 839846c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
13 changes: 12 additions & 1 deletion cli/commands/terraform/version_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,18 @@ func CheckTerragruntVersionMeetsConstraint(currentVersion *version.Version, cons
return err
}

if !versionConstraint.Check(currentVersion) {
checkedVersion := currentVersion

if currentVersion.Prerelease() != "" {
// The logic in hashicorp/go-version is such that it will not consider a prerelease version to be
// compatible with a constraint that does not have a prerelease version. This is not the behavior we want
// for Terragrunt, so we strip the prerelease version before checking the constraint.
//
// https://github.com/hashicorp/go-version/issues/130
checkedVersion = currentVersion.Core()
}

if !versionConstraint.Check(checkedVersion) {
return errors.WithStackTrace(InvalidTerragruntVersion{CurrentVersion: currentVersion, VersionConstraints: versionConstraint})
}

Expand Down
7 changes: 7 additions & 0 deletions cli/commands/terraform/version_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ func testParseTerraformVersion(t *testing.T, versionString string, expectedVersi
}
}

// TODO: Refactor these into a test table.

// Terragrunt Version Checking
func TestCheckTerragruntVersionMeetsConstraintEqual(t *testing.T) {
t.Parallel()
Expand All @@ -146,6 +148,11 @@ func TestCheckTerragruntVersionMeetsConstraintLessMajor(t *testing.T) {
testCheckTerragruntVersionMeetsConstraint(t, "v0.22.15", ">= v0.23.18", false)
}

func TestCheckTerragruntVersionMeetsConstraintPrerelease(t *testing.T) {
t.Parallel()
testCheckTerragruntVersionMeetsConstraint(t, "v0.23.18-alpha202409013", ">= v0.23.18", true)
}

func testCheckTerragruntVersionMeetsConstraint(t *testing.T, currentVersion string, versionConstraint string, versionMeetsConstraint bool) {
t.Helper()

Expand Down
44 changes: 33 additions & 11 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2424,7 +2424,10 @@ func TestTerragruntIncludeParentHclFile(t *testing.T) {
assert.Equal(t, 1, strings.Count(out, "common_hcl"))
}

func TestTerragruntVersionConstraints(t *testing.T) {
// The over all test here can run in parallel, but the subtests cannot.
// This is due to a race condition brought about by overriding `version.Version` in
// runTerragruntVersionCommand
func TestTerragruntVersionConstraints(t *testing.T) { //nolint:tparallel
t.Parallel()

tc := []struct {
Expand All @@ -2440,37 +2443,45 @@ func TestTerragruntVersionConstraints(t *testing.T) {
true,
},
{
"version meets constriant greater patch",
"version meets constraint greater patch",
"v0.23.19",
"terragrunt_version_constraint = \">= v0.23.18\"",
true,
},
{
"version meets constriant greater major",
"version meets constraint greater major",
"v1.0.0",
"terragrunt_version_constraint = \">= v0.23.18\"",
true,
},
{
"version meets constriant less patch",
"version fails constraint less patch",
"v0.23.17",
"terragrunt_version_constraint = \">= v0.23.18\"",
false,
},
{
"version meets constriant less major",
"version fails constraint less major",
"v0.22.18",
"terragrunt_version_constraint = \">= v0.23.18\"",
false,
},
{
"version meets constraint pre-release",
"v0.23.18-alpha2024091301",
"terragrunt_version_constraint = \">= v0.23.18\"",
true,
},
{
"version fails constraint pre-release",
"v0.23.18-alpha2024091301",
"terragrunt_version_constraint = \"< v0.23.18\"",
false,
},
}

for _, tt := range tc {
tt := tt

for _, tt := range tc { //nolint:paralleltest
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

tmpEnvPath := copyEnvironment(t, testFixtureReadConfig)
rootPath := filepath.Join(tmpEnvPath, testFixtureReadConfig, "with_constraints")

Expand All @@ -2479,7 +2490,18 @@ func TestTerragruntVersionConstraints(t *testing.T) {
stdout := bytes.Buffer{}
stderr := bytes.Buffer{}

err := runTerragruntVersionCommand(t, tt.terragruntVersion, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-config %s --terragrunt-working-dir %s", tmpTerragruntConfigPath, rootPath), &stdout, &stderr)
err := runTerragruntVersionCommand(
t,
tt.terragruntVersion,
fmt.Sprintf(
"terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-config %s --terragrunt-working-dir %s",
tmpTerragruntConfigPath,
rootPath,
),
&stdout,
&stderr,
)

logBufferContentsLineByLine(t, stdout, "stdout")
logBufferContentsLineByLine(t, stderr, "stderr")

Expand Down

0 comments on commit 839846c

Please sign in to comment.