From 63e006adb8843f9ec0f238e0623e4fd8a0e9a2c4 Mon Sep 17 00:00:00 2001 From: CJ Horton Date: Tue, 7 Feb 2023 18:09:46 -0800 Subject: [PATCH] fix issue when localBinPath is set When running e2e tests against a locally built Terraform binary, the runTestWithVersions helper has some surprising behavior: it throws away the selected versions and runs the test against the local binary instead. This seems fairly undesirable, since the tests using that helper are testing version-specific behavior (and thus fail when a local bin path is set). This commit clarifies some naming and ensures that version override behavior is confined to `runTest`, which makes no assumptions about which versions it's using. --- tfexec/internal/e2etest/apply_test.go | 4 ++-- tfexec/internal/e2etest/destroy_test.go | 4 ++-- tfexec/internal/e2etest/plan_test.go | 4 ++-- tfexec/internal/e2etest/refresh_test.go | 4 ++-- tfexec/internal/e2etest/show_test.go | 22 +++++++++++----------- tfexec/internal/e2etest/upgrade012_test.go | 2 +- tfexec/internal/e2etest/util_test.go | 10 ++-------- 7 files changed, 22 insertions(+), 28 deletions(-) diff --git a/tfexec/internal/e2etest/apply_test.go b/tfexec/internal/e2etest/apply_test.go index 5211a3db..967e5969 100644 --- a/tfexec/internal/e2etest/apply_test.go +++ b/tfexec/internal/e2etest/apply_test.go @@ -29,7 +29,7 @@ func TestApply(t *testing.T) { func TestApplyJSON_TF014AndEarlier(t *testing.T) { versions := []string{testutil.Latest011, testutil.Latest012, testutil.Latest013, testutil.Latest014} - runTestWithVersions(t, "basic", versions, func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, versions, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { err := tf.Init(context.Background()) if err != nil { t.Fatalf("error running Init in test directory: %s", err) @@ -47,7 +47,7 @@ func TestApplyJSON_TF014AndEarlier(t *testing.T) { func TestApplyJSON_TF015AndLater(t *testing.T) { versions := []string{testutil.Latest015, testutil.Latest_v1, testutil.Latest_v1_1} - runTestWithVersions(t, "basic", versions, func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, versions, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { err := tf.Init(context.Background()) if err != nil { t.Fatalf("error running Init in test directory: %s", err) diff --git a/tfexec/internal/e2etest/destroy_test.go b/tfexec/internal/e2etest/destroy_test.go index 9d157fd9..bd7124f2 100644 --- a/tfexec/internal/e2etest/destroy_test.go +++ b/tfexec/internal/e2etest/destroy_test.go @@ -34,7 +34,7 @@ func TestDestroy(t *testing.T) { func TestDestroyJSON_TF014AndEarlier(t *testing.T) { versions := []string{testutil.Latest011, testutil.Latest012, testutil.Latest013, testutil.Latest014} - runTestWithVersions(t, "basic", versions, func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, versions, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { err := tf.Init(context.Background()) if err != nil { t.Fatalf("error running Init in test directory: %s", err) @@ -52,7 +52,7 @@ func TestDestroyJSON_TF014AndEarlier(t *testing.T) { func TestDestroyJSON_TF015AndLater(t *testing.T) { versions := []string{testutil.Latest015, testutil.Latest_v1, testutil.Latest_v1_1} - runTestWithVersions(t, "basic", versions, func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, versions, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { err := tf.Init(context.Background()) if err != nil { t.Fatalf("error running Init in test directory: %s", err) diff --git a/tfexec/internal/e2etest/plan_test.go b/tfexec/internal/e2etest/plan_test.go index 83364fd6..ca6dbcd9 100644 --- a/tfexec/internal/e2etest/plan_test.go +++ b/tfexec/internal/e2etest/plan_test.go @@ -53,7 +53,7 @@ func TestPlanWithState(t *testing.T) { func TestPlanJSON_TF014AndEarlier(t *testing.T) { versions := []string{testutil.Latest011, testutil.Latest012, testutil.Latest013, testutil.Latest014} - runTestWithVersions(t, "basic", versions, func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, versions, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { err := tf.Init(context.Background()) if err != nil { t.Fatalf("error running Init in test directory: %s", err) @@ -74,7 +74,7 @@ func TestPlanJSON_TF014AndEarlier(t *testing.T) { func TestPlanJSON_TF015AndLater(t *testing.T) { versions := []string{testutil.Latest015, testutil.Latest_v1, testutil.Latest_v1_1} - runTestWithVersions(t, "basic", versions, func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, versions, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { err := tf.Init(context.Background()) if err != nil { t.Fatalf("error running Init in test directory: %s", err) diff --git a/tfexec/internal/e2etest/refresh_test.go b/tfexec/internal/e2etest/refresh_test.go index 1cea79e4..500d3f7f 100644 --- a/tfexec/internal/e2etest/refresh_test.go +++ b/tfexec/internal/e2etest/refresh_test.go @@ -34,7 +34,7 @@ func TestRefresh(t *testing.T) { func TestRefreshJSON_TF014AndEarlier(t *testing.T) { versions := []string{testutil.Latest011, testutil.Latest012, testutil.Latest013, testutil.Latest014} - runTestWithVersions(t, "basic", versions, func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, versions, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { err := tf.Init(context.Background()) if err != nil { t.Fatalf("error running Init in test directory: %s", err) @@ -52,7 +52,7 @@ func TestRefreshJSON_TF014AndEarlier(t *testing.T) { func TestRefreshJSON_TF015AndLater(t *testing.T) { versions := []string{testutil.Latest015, testutil.Latest_v1, testutil.Latest_v1_1} - runTestWithVersions(t, "basic", versions, func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, versions, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { err := tf.Init(context.Background()) if err != nil { t.Fatalf("error running Init in test directory: %s", err) diff --git a/tfexec/internal/e2etest/show_test.go b/tfexec/internal/e2etest/show_test.go index 29e77118..e1cb92b6 100644 --- a/tfexec/internal/e2etest/show_test.go +++ b/tfexec/internal/e2etest/show_test.go @@ -116,7 +116,7 @@ func TestShow_noInitBasic(t *testing.T) { // no providers to download, this is unintended behaviour, as // init is not actually necessary. This is considered a known issue in // pre-1.2.0 versions. - runTestVersions(t, []string{testutil.Latest012, testutil.Latest013, testutil.Latest014, testutil.Latest015, testutil.Latest_v1, testutil.Latest_v1_1}, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, []string{testutil.Latest012, testutil.Latest013, testutil.Latest014, testutil.Latest015, testutil.Latest_v1, testutil.Latest_v1_1}, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { var noInit *tfexec.ErrNoInit _, err := tf.Show(context.Background()) if !errors.As(err, &noInit) { @@ -154,7 +154,7 @@ func TestShow_noInitModule(t *testing.T) { // no providers to download, this is unintended behaviour, as // init is not actually necessary. This is considered a known issue in // pre-1.2.0 versions. - runTestVersions(t, []string{testutil.Latest012, testutil.Latest013, testutil.Latest014, testutil.Latest015, testutil.Latest_v1, testutil.Latest_v1_1}, "registry_module", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, []string{testutil.Latest012, testutil.Latest013, testutil.Latest014, testutil.Latest015, testutil.Latest_v1, testutil.Latest_v1_1}, "registry_module", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { var noInit *tfexec.ErrNoInit _, err := tf.Show(context.Background()) if !errors.As(err, &noInit) { @@ -306,7 +306,7 @@ func TestShow_versionMismatch(t *testing.T) { // so we maintain one fixture per supported version. // See github.com/hashicorp/terraform/25920 func TestShowStateFile012(t *testing.T) { - runTestVersions(t, []string{testutil.Latest012}, "non_default_statefile_012", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, []string{testutil.Latest012}, "non_default_statefile_012", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { expected := &tfjson.State{ FormatVersion: "0.1", // TerraformVersion is ignored to facilitate latest version testing @@ -344,7 +344,7 @@ func TestShowStateFile012(t *testing.T) { } func TestShowStateFile013(t *testing.T) { - runTestVersions(t, []string{testutil.Latest013, testutil.Latest014}, "non_default_statefile_013", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, []string{testutil.Latest013, testutil.Latest014}, "non_default_statefile_013", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { expected := &tfjson.State{ FormatVersion: "0.1", // TerraformVersion is ignored to facilitate latest version testing @@ -382,7 +382,7 @@ func TestShowStateFile013(t *testing.T) { } func TestShowStateFile014(t *testing.T) { - runTestVersions(t, []string{testutil.Latest014}, "non_default_statefile_014", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, []string{testutil.Latest014}, "non_default_statefile_014", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { expected := &tfjson.State{ FormatVersion: "0.1", // TerraformVersion is ignored to facilitate latest version testing @@ -422,7 +422,7 @@ func TestShowStateFile014(t *testing.T) { // Plan files cannot be transferred between different Terraform versions, // so we maintain one fixture per supported version func TestShowPlanFile012_linux(t *testing.T) { - runTestVersions(t, []string{testutil.Latest012}, "non_default_planfile_012", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, []string{testutil.Latest012}, "non_default_planfile_012", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { if runtime.GOOS != "linux" { t.Skip("plan file created in 0.12 on Linux is not compatible with other systems") } @@ -488,7 +488,7 @@ func TestShowPlanFile012_linux(t *testing.T) { } func TestShowPlanFile013(t *testing.T) { - runTestVersions(t, []string{testutil.Latest013}, "non_default_planfile_013", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, []string{testutil.Latest013}, "non_default_planfile_013", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { providerName := "registry.terraform.io/hashicorp/null" expected := &tfjson.Plan{ @@ -550,7 +550,7 @@ func TestShowPlanFile013(t *testing.T) { } func TestShowPlanFile014(t *testing.T) { - runTestVersions(t, []string{testutil.Latest014}, "non_default_planfile_014", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, []string{testutil.Latest014}, "non_default_planfile_014", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { providerName := "registry.terraform.io/hashicorp/null" expected := &tfjson.Plan{ @@ -615,7 +615,7 @@ func TestShowPlanFile014(t *testing.T) { } func TestShowPlanFileRaw012_linux(t *testing.T) { - runTestVersions(t, []string{testutil.Latest012}, "non_default_planfile_012", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, []string{testutil.Latest012}, "non_default_planfile_012", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { if runtime.GOOS != "linux" { t.Skip("plan file created in 0.12 on Linux is not compatible with other systems") } @@ -647,7 +647,7 @@ func TestShowPlanFileRaw012_linux(t *testing.T) { } func TestShowPlanFileRaw013(t *testing.T) { - runTestVersions(t, []string{testutil.Latest013}, "non_default_planfile_013", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, []string{testutil.Latest013}, "non_default_planfile_013", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { f, err := os.Open("testdata/non_default_planfile_013/human_readable_output.txt") if err != nil { t.Fatal(err) @@ -675,7 +675,7 @@ func TestShowPlanFileRaw013(t *testing.T) { } func TestShowPlanFileRaw014(t *testing.T) { - runTestVersions(t, []string{testutil.Latest014}, "non_default_planfile_014", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, []string{testutil.Latest014}, "non_default_planfile_014", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { f, err := os.Open("testdata/non_default_planfile_013/human_readable_output.txt") if err != nil { t.Fatal(err) diff --git a/tfexec/internal/e2etest/upgrade012_test.go b/tfexec/internal/e2etest/upgrade012_test.go index c40de0c0..0cdc14e4 100644 --- a/tfexec/internal/e2etest/upgrade012_test.go +++ b/tfexec/internal/e2etest/upgrade012_test.go @@ -12,7 +12,7 @@ import ( ) func TestUpgrade012(t *testing.T) { - runTestVersions(t, []string{testutil.Latest012}, "pre_011_syntax", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + runTestWithVersions(t, []string{testutil.Latest012}, "pre_011_syntax", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { err := tf.Init(context.Background()) if err != nil { t.Fatalf("error running Init in test directory: %s", err) diff --git a/tfexec/internal/e2etest/util_test.go b/tfexec/internal/e2etest/util_test.go index 36959ef3..113cb4da 100644 --- a/tfexec/internal/e2etest/util_test.go +++ b/tfexec/internal/e2etest/util_test.go @@ -42,12 +42,6 @@ func runTest(t *testing.T, fixtureName string, cb func(t *testing.T, tfVersion * versions = strings.Split(override, ",") } - runTestWithVersions(t, fixtureName, versions, cb) -} - -func runTestWithVersions(t *testing.T, fixtureName string, versions []string, cb func(t *testing.T, tfVersion *version.Version, tf *tfexec.Terraform)) { - t.Helper() - // If the env var TFEXEC_E2ETEST_TERRAFORM_PATH is set to the path of a // valid Terraform executable, only tests appropriate to that // executable's version will be run. @@ -79,10 +73,10 @@ func runTestWithVersions(t *testing.T, fixtureName string, versions []string, cb versions = []string{lVersion.String()} } - runTestVersions(t, versions, fixtureName, cb) + runTestWithVersions(t, versions, fixtureName, cb) } -func runTestVersions(t *testing.T, versions []string, fixtureName string, cb func(t *testing.T, tfVersion *version.Version, tf *tfexec.Terraform)) { +func runTestWithVersions(t *testing.T, versions []string, fixtureName string, cb func(t *testing.T, tfVersion *version.Version, tf *tfexec.Terraform)) { t.Helper() alreadyRunVersions := map[string]bool{}