diff --git a/backend/local/backend.go b/backend/local/backend.go index 61df56bde..063766b1e 100644 --- a/backend/local/backend.go +++ b/backend/local/backend.go @@ -127,7 +127,7 @@ func (b *Local) States() ([]string, error) { // the listing always start with "default" envs := []string{backend.DefaultStateName} - entries, err := ioutil.ReadDir(DefaultEnvDir) + entries, err := ioutil.ReadDir(b.stateEnvDir()) // no error if there's no envs configured if os.IsNotExist(err) { return envs, nil @@ -166,7 +166,7 @@ func (b *Local) DeleteState(name string) error { } delete(b.states, name) - return os.RemoveAll(filepath.Join(DefaultEnvDir, name)) + return os.RemoveAll(filepath.Join(b.stateEnvDir(), name)) } func (b *Local) State(name string) (state.State, error) { @@ -320,17 +320,12 @@ func (b *Local) StatePaths(name string) (string, string, string) { name = backend.DefaultStateName } - envDir := DefaultEnvDir - if b.StateEnvDir != "" { - envDir = b.StateEnvDir - } - if name == backend.DefaultStateName { if statePath == "" { statePath = DefaultStateFilename } } else { - statePath = filepath.Join(envDir, name, DefaultStateFilename) + statePath = filepath.Join(b.stateEnvDir(), name, DefaultStateFilename) } if stateOutPath == "" { @@ -353,12 +348,7 @@ func (b *Local) createState(name string) error { return nil } - envDir := DefaultEnvDir - if b.StateEnvDir != "" { - envDir = b.StateEnvDir - } - - stateDir := filepath.Join(envDir, name) + stateDir := filepath.Join(b.stateEnvDir(), name) s, err := os.Stat(stateDir) if err == nil && s.IsDir() { // no need to check for os.IsNotExist, since that is covered by os.MkdirAll @@ -374,6 +364,15 @@ func (b *Local) createState(name string) error { return nil } +// stateEnvDir returns the directory where state environments are stored. +func (b *Local) stateEnvDir() string { + if b.StateEnvDir != "" { + return b.StateEnvDir + } + + return DefaultEnvDir +} + // currentStateName returns the name of the current named state as set in the // configuration files. // If there are no configured environments, currentStateName returns "default" diff --git a/backend/local/backend_test.go b/backend/local/backend_test.go index f929e7441..459cfb37f 100644 --- a/backend/local/backend_test.go +++ b/backend/local/backend_test.go @@ -20,6 +20,10 @@ func TestLocal_impl(t *testing.T) { var _ backend.CLI = new(Local) } +func TestLocal_backend(t *testing.T) { + backend.TestBackend(t, TestLocal(t)) +} + func checkState(t *testing.T, path, expected string) { // Read the state f, err := os.Open(path) diff --git a/backend/local/testing.go b/backend/local/testing.go index 67048766f..91ba0f900 100644 --- a/backend/local/testing.go +++ b/backend/local/testing.go @@ -21,6 +21,7 @@ func TestLocal(t *testing.T) *Local { StatePath: filepath.Join(tempDir, "state.tfstate"), StateOutPath: filepath.Join(tempDir, "state.tfstate"), StateBackupPath: filepath.Join(tempDir, "state.tfstate.bak"), + StateEnvDir: filepath.Join(tempDir, "state.tfstate.d"), ContextOpts: &terraform.ContextOpts{}, } } diff --git a/backend/testing.go b/backend/testing.go index 7039de5ad..09c3079cf 100644 --- a/backend/testing.go +++ b/backend/testing.go @@ -90,6 +90,10 @@ func testBackendStates(t *testing.T, b Backend) { // Verify they are distinct states { s := barState.State() + if s == nil { + s = terraform.NewState() + } + s.Lineage = "bar" if err := barState.WriteState(s); err != nil { t.Fatalf("bad: %s", err) @@ -101,7 +105,7 @@ func testBackendStates(t *testing.T, b Backend) { if err := fooState.RefreshState(); err != nil { t.Fatalf("bad: %s", err) } - if v := fooState.State(); v.Lineage == "bar" { + if v := fooState.State(); v != nil && v.Lineage == "bar" { t.Fatalf("bad: %#v", v) } }