Merge pull request #12558 from hashicorp/f-backend-testing

backend/local: run backend.TestBackend
This commit is contained in:
Mitchell Hashimoto 2017-03-15 08:45:20 -07:00 committed by GitHub
commit 1ca0352e5f
4 changed files with 23 additions and 15 deletions

View File

@ -127,7 +127,7 @@ func (b *Local) States() ([]string, error) {
// the listing always start with "default" // the listing always start with "default"
envs := []string{backend.DefaultStateName} envs := []string{backend.DefaultStateName}
entries, err := ioutil.ReadDir(DefaultEnvDir) entries, err := ioutil.ReadDir(b.stateEnvDir())
// no error if there's no envs configured // no error if there's no envs configured
if os.IsNotExist(err) { if os.IsNotExist(err) {
return envs, nil return envs, nil
@ -166,7 +166,7 @@ func (b *Local) DeleteState(name string) error {
} }
delete(b.states, name) 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) { 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 name = backend.DefaultStateName
} }
envDir := DefaultEnvDir
if b.StateEnvDir != "" {
envDir = b.StateEnvDir
}
if name == backend.DefaultStateName { if name == backend.DefaultStateName {
if statePath == "" { if statePath == "" {
statePath = DefaultStateFilename statePath = DefaultStateFilename
} }
} else { } else {
statePath = filepath.Join(envDir, name, DefaultStateFilename) statePath = filepath.Join(b.stateEnvDir(), name, DefaultStateFilename)
} }
if stateOutPath == "" { if stateOutPath == "" {
@ -353,12 +348,7 @@ func (b *Local) createState(name string) error {
return nil return nil
} }
envDir := DefaultEnvDir stateDir := filepath.Join(b.stateEnvDir(), name)
if b.StateEnvDir != "" {
envDir = b.StateEnvDir
}
stateDir := filepath.Join(envDir, name)
s, err := os.Stat(stateDir) s, err := os.Stat(stateDir)
if err == nil && s.IsDir() { if err == nil && s.IsDir() {
// no need to check for os.IsNotExist, since that is covered by os.MkdirAll // 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 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 // currentStateName returns the name of the current named state as set in the
// configuration files. // configuration files.
// If there are no configured environments, currentStateName returns "default" // If there are no configured environments, currentStateName returns "default"

View File

@ -20,6 +20,10 @@ func TestLocal_impl(t *testing.T) {
var _ backend.CLI = new(Local) var _ backend.CLI = new(Local)
} }
func TestLocal_backend(t *testing.T) {
backend.TestBackend(t, TestLocal(t))
}
func checkState(t *testing.T, path, expected string) { func checkState(t *testing.T, path, expected string) {
// Read the state // Read the state
f, err := os.Open(path) f, err := os.Open(path)

View File

@ -21,6 +21,7 @@ func TestLocal(t *testing.T) *Local {
StatePath: filepath.Join(tempDir, "state.tfstate"), StatePath: filepath.Join(tempDir, "state.tfstate"),
StateOutPath: filepath.Join(tempDir, "state.tfstate"), StateOutPath: filepath.Join(tempDir, "state.tfstate"),
StateBackupPath: filepath.Join(tempDir, "state.tfstate.bak"), StateBackupPath: filepath.Join(tempDir, "state.tfstate.bak"),
StateEnvDir: filepath.Join(tempDir, "state.tfstate.d"),
ContextOpts: &terraform.ContextOpts{}, ContextOpts: &terraform.ContextOpts{},
} }
} }

View File

@ -90,6 +90,10 @@ func testBackendStates(t *testing.T, b Backend) {
// Verify they are distinct states // Verify they are distinct states
{ {
s := barState.State() s := barState.State()
if s == nil {
s = terraform.NewState()
}
s.Lineage = "bar" s.Lineage = "bar"
if err := barState.WriteState(s); err != nil { if err := barState.WriteState(s); err != nil {
t.Fatalf("bad: %s", err) t.Fatalf("bad: %s", err)
@ -101,7 +105,7 @@ func testBackendStates(t *testing.T, b Backend) {
if err := fooState.RefreshState(); err != nil { if err := fooState.RefreshState(); err != nil {
t.Fatalf("bad: %s", err) 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) t.Fatalf("bad: %#v", v)
} }
} }