Skip to content

Commit

Permalink
Skip state push -force tests due to a bug in Terraform v0.12
Browse files Browse the repository at this point in the history
Terraform >= v0.12.25 and < v0.13 has a bug for state push -force.
hashicorp/terraform#25761
I added a version check in acceptance tests to minimize the patch.
Ideally, the version check process should be implemented in TerraformCLI
instead of the test helper function because supported features may be
different for some Terraform versions. I'll follow up a separated PR to
add some version checks in runtime.
  • Loading branch information
minamijoyo committed Aug 11, 2021
1 parent fa66cfd commit 6f610a5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/google/go-cmp v0.5.2
github.com/hashicorp/aws-sdk-go-base v0.6.0
github.com/hashicorp/go-version v1.3.0
github.com/hashicorp/hcl/v2 v2.6.0
github.com/hashicorp/logutils v1.0.0
github.com/mattn/go-shellwords v1.0.10
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ github.com/hashicorp/go-cleanhttp v0.5.0 h1:wvCrVc9TjDls6+YGAF2hAifE1E5U1+b4tH6K
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw=
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/hcl/v2 v2.6.0 h1:3krZOfGY6SziUXa6H9PJU6TyohHn7I+ARYnhbeNBz+o=
github.com/hashicorp/hcl/v2 v2.6.0/go.mod h1:bQTN5mpo+jewjJgh8jr0JUguIi7qPHUF6yIfAEN3jqY=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
Expand Down
19 changes: 19 additions & 0 deletions tfexec/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"runtime"
"strings"
"testing"

"github.com/hashicorp/go-version"
)

// mockExecutor impolements the Executor interface for testing.
Expand Down Expand Up @@ -325,3 +327,20 @@ func UpdateTestAccSource(t *testing.T, tf TerraformCLI, source string) {
t.Fatalf("failed to update source: %s", err)
}
}

// MatchTerraformVersion returns true if terraform version matches a given constraints.
func MatchTerraformVersion(ctx context.Context, tf TerraformCLI, constraints string) (bool, error) {
tfVersionRaw, err := tf.Version(ctx)
if err != nil {
return false, fmt.Errorf("failed to get terraform version: %s", err)
}
v, err := version.NewVersion(tfVersionRaw)
if err != nil {
return false, fmt.Errorf("failed to parse terraform version: %s", err)
}
c, err := version.NewConstraint(constraints)
if err != nil {
return false, fmt.Errorf("failed to new version constraint: %s", err)
}
return c.Check(v), nil
}
17 changes: 17 additions & 0 deletions tfmigrate/multi_state_migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,23 @@ func TestAccMultiStateMigratorApply(t *testing.T) {
t.Fatalf("failed to apply the saved plan file in toDir: %s", err)
}

// Terraform >= v0.12.25 and < v0.13 has a bug for state push -force
// https://github.com/hashicorp/terraform/issues/25761
fromTfVersionMatched, err := tfexec.MatchTerraformVersion(ctx, fromTf, ">= 0.12.25, < 0.13")
if err != nil {
t.Fatalf("failed to check terraform version constraints in fromDir: %s", err)
}
if fromTfVersionMatched {
t.Skip("skip the following test due to a bug in Terraform v0.12")
}
toTfVersionMatched, err := tfexec.MatchTerraformVersion(ctx, toTf, ">= 0.12.25, < 0.13")
if err != nil {
t.Fatalf("failed to check terraform version constraints in toDir: %s", err)
}
if toTfVersionMatched {
t.Skip("skip the following test due to a bug in Terraform v0.12")
}

// Note that applying the plan file only affects a local state,
// make sure to force push it to remote after terraform apply.
// The -force flag is required here because the lineage of the state was changed.
Expand Down
10 changes: 10 additions & 0 deletions tfmigrate/state_migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,16 @@ resource "aws_security_group" "baz" {}
t.Fatalf("failed to apply the saved plan file: %s", err)
}

// Terraform >= v0.12.25 and < v0.13 has a bug for state push -force
// https://github.com/hashicorp/terraform/issues/25761
tfVersionMatched, err := tfexec.MatchTerraformVersion(ctx, tf, ">= 0.12.25, < 0.13")
if err != nil {
t.Fatalf("failed to check terraform version constraints: %s", err)
}
if tfVersionMatched {
t.Skip("skip the following test due to a bug in Terraform v0.12")
}

// Note that applying the plan file only affects a local state,
// make sure to force push it to remote after terraform apply.
// The -force flag is required here because the lineage of the state was changed.
Expand Down

0 comments on commit 6f610a5

Please sign in to comment.