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.
This commit is contained in:
Martin Atkins 2018-11-13 15:51:01 -08:00
parent aacbe1d14b
commit 985b414dca
3 changed files with 46 additions and 4 deletions

View File

@ -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
}
}

View File

@ -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

View File

@ -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.