Skip to content

Commit

Permalink
states/statemgr: Fix the Filesystem state manager tests
Browse files Browse the repository at this point in the history
Now that we're verifying the Terraform version during state loading, we
need to force a particular Terraform version to use during these tests.
  • Loading branch information
apparentlymart committed Nov 19, 2018
1 parent aacbe1d commit 985b414
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
40 changes: 39 additions & 1 deletion states/statemgr/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 6 additions & 2 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit 985b414

Please sign in to comment.