From 985b414dca721a5e2f35812856c74227a6bfc0ee Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 13 Nov 2018 15:51:01 -0800 Subject: [PATCH] states/statemgr: Fix the Filesystem state manager tests Now that we're verifying the Terraform version during state loading, we need to force a particular Terraform version to use during these tests. --- states/statemgr/filesystem_test.go | 40 +++++++++++++++++++++++++++++- version.go | 2 +- version/version.go | 8 ++++-- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/states/statemgr/filesystem_test.go b/states/statemgr/filesystem_test.go index 4e50b4a43..7977a6f8d 100644 --- a/states/statemgr/filesystem_test.go +++ b/states/statemgr/filesystem_test.go @@ -4,23 +4,26 @@ import ( "io/ioutil" "os" "os/exec" + "strings" "sync" "testing" "github.com/go-test/deep" - version "github.com/hashicorp/go-version" "github.com/hashicorp/terraform/states/statefile" + tfversion "github.com/hashicorp/terraform/version" ) func TestFilesystem(t *testing.T) { + defer testOverrideVersion(t, "1.2.3")() ls := testFilesystem(t) defer os.Remove(ls.readPath) TestFull(t, ls) } func TestFilesystemRace(t *testing.T) { + defer testOverrideVersion(t, "1.2.3")() ls := testFilesystem(t) defer os.Remove(ls.readPath) @@ -37,6 +40,7 @@ func TestFilesystemRace(t *testing.T) { } func TestFilesystemLocks(t *testing.T) { + defer testOverrideVersion(t, "1.2.3")() s := testFilesystem(t) defer os.Remove(s.readPath) @@ -97,6 +101,7 @@ func TestFilesystemLocks(t *testing.T) { // Verify that we can write to the state file, as Windows' mandatory locking // will prevent writing to a handle different than the one that hold the lock. func TestFilesystem_writeWhileLocked(t *testing.T) { + defer testOverrideVersion(t, "1.2.3")() s := testFilesystem(t) defer os.Remove(s.readPath) @@ -119,6 +124,7 @@ func TestFilesystem_writeWhileLocked(t *testing.T) { } func TestFilesystem_pathOut(t *testing.T) { + defer testOverrideVersion(t, "1.2.3")() f, err := ioutil.TempFile("", "tf") if err != nil { t.Fatalf("err: %s", err) @@ -134,6 +140,7 @@ func TestFilesystem_pathOut(t *testing.T) { } func TestFilesystem_backup(t *testing.T) { + defer testOverrideVersion(t, "1.2.3")() f, err := ioutil.TempFile("", "tf") if err != nil { t.Fatalf("err: %s", err) @@ -166,6 +173,7 @@ func TestFilesystem_backup(t *testing.T) { } func TestFilesystem_nonExist(t *testing.T) { + defer testOverrideVersion(t, "1.2.3")() ls := NewFilesystem("ishouldntexist") if err := ls.RefreshState(); err != nil { t.Fatalf("err: %s", err) @@ -177,6 +185,7 @@ func TestFilesystem_nonExist(t *testing.T) { } func TestFilesystem_impl(t *testing.T) { + defer testOverrideVersion(t, "1.2.3")() var _ Reader = new(Filesystem) var _ Writer = new(Filesystem) var _ Persister = new(Filesystem) @@ -212,6 +221,7 @@ func testFilesystem(t *testing.T) *Filesystem { // Make sure we can refresh while the state is locked func TestFilesystem_refreshWhileLocked(t *testing.T) { + defer testOverrideVersion(t, "1.2.3")() f, err := ioutil.TempFile("", "tf") if err != nil { t.Fatalf("err: %s", err) @@ -253,3 +263,31 @@ func TestFilesystem_refreshWhileLocked(t *testing.T) { t.Fatal("missing state") } } + +func testOverrideVersion(t *testing.T, v string) func() { + oldVersionStr := tfversion.Version + oldPrereleaseStr := tfversion.Prerelease + oldSemVer := tfversion.SemVer + + var newPrereleaseStr string + if dash := strings.Index(v, "-"); dash != -1 { + newPrereleaseStr = v[dash+1:] + v = v[:dash] + } + + newSemVer, err := version.NewVersion(v) + if err != nil { + t.Errorf("invalid override version %q: %s", v, err) + } + newVersionStr := newSemVer.String() + + tfversion.Version = newVersionStr + tfversion.Prerelease = newPrereleaseStr + tfversion.SemVer = newSemVer + + return func() { // reset function + tfversion.Version = oldVersionStr + tfversion.Prerelease = oldPrereleaseStr + tfversion.SemVer = oldSemVer + } +} diff --git a/version.go b/version.go index baefdc2d9..36d16cb28 100644 --- a/version.go +++ b/version.go @@ -7,6 +7,6 @@ import ( // The git commit that was compiled. This will be filled in by the compiler. var GitCommit string -const Version = version.Version +var Version = version.Version var VersionPrerelease = version.Prerelease diff --git a/version/version.go b/version/version.go index 0cfac684e..e6cc16506 100644 --- a/version/version.go +++ b/version/version.go @@ -11,7 +11,7 @@ import ( ) // The main version number that is being run at the moment. -const Version = "0.12.0" +var Version = "0.12.0" // A pre-release marker for the version. If this is "" (empty string) // then it means that it is a final release. Otherwise, this is a pre-release @@ -21,7 +21,11 @@ var Prerelease = "dev" // SemVer is an instance of version.Version. This has the secondary // benefit of verifying during tests and init time that our version is a // proper semantic version, which should always be the case. -var SemVer = version.Must(version.NewVersion(Version)) +var SemVer *version.Version + +func init() { + SemVer = version.Must(version.NewVersion(Version)) +} // Header is the header name used to send the current terraform version // in http requests.