add some Serial checks to apply and refresh tests

This commit is contained in:
James Bardin 2017-07-05 18:17:07 -04:00
parent 054716c397
commit fb397060eb
2 changed files with 23 additions and 30 deletions

View File

@ -272,6 +272,14 @@ func TestApply_defaultState(t *testing.T) {
}, },
} }
// create an existing state file
localState := &state.LocalState{Path: statePath}
if err := localState.WriteState(terraform.NewState()); err != nil {
t.Fatal(err)
}
serial := localState.State().Serial
args := []string{ args := []string{
testFixturePath("apply"), testFixturePath("apply"),
} }
@ -287,6 +295,10 @@ func TestApply_defaultState(t *testing.T) {
if state == nil { if state == nil {
t.Fatal("state should not be nil") t.Fatal("state should not be nil")
} }
if state.Serial <= serial {
t.Fatalf("serial was not incremented. previous:%d, current%d", serial, state.Serial)
}
} }
func TestApply_error(t *testing.T) { func TestApply_error(t *testing.T) {
@ -540,10 +552,8 @@ func TestApply_plan_backup(t *testing.T) {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
} }
{ // Should have a backup file
// Should have a backup file testStateRead(t, backupPath)
testStateRead(t, backupPath)
}
} }
func TestApply_plan_noBackup(t *testing.T) { func TestApply_plan_noBackup(t *testing.T) {

View File

@ -10,6 +10,7 @@ import (
"testing" "testing"
"github.com/hashicorp/terraform/helper/copy" "github.com/hashicorp/terraform/helper/copy"
"github.com/hashicorp/terraform/state"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
@ -193,15 +194,11 @@ func TestRefresh_defaultState(t *testing.T) {
} }
statePath := filepath.Join(td, DefaultStateFilename) statePath := filepath.Join(td, DefaultStateFilename)
f, err := os.Create(statePath) localState := &state.LocalState{Path: statePath}
if err != nil { if err := localState.WriteState(originalState); err != nil {
t.Fatalf("err: %s", err) t.Fatal(err)
}
err = terraform.WriteState(originalState, f)
f.Close()
if err != nil {
t.Fatalf("err: %s", err)
} }
serial := localState.State().Serial
// Change to that directory // Change to that directory
cwd, err := os.Getwd() cwd, err := os.Getwd()
@ -236,16 +233,7 @@ func TestRefresh_defaultState(t *testing.T) {
t.Fatal("refresh should be called") t.Fatal("refresh should be called")
} }
f, err = os.Open(statePath) newState := testStateRead(t, statePath)
if err != nil {
t.Fatalf("err: %s", err)
}
newState, err := terraform.ReadState(f)
f.Close()
if err != nil {
t.Fatalf("err: %s", err)
}
actual := newState.RootModule().Resources["test_instance.foo"].Primary actual := newState.RootModule().Resources["test_instance.foo"].Primary
expected := p.RefreshReturn expected := p.RefreshReturn
@ -254,16 +242,11 @@ func TestRefresh_defaultState(t *testing.T) {
t.Fatalf("bad:\n%#v", actual) t.Fatalf("bad:\n%#v", actual)
} }
f, err = os.Open(statePath + DefaultBackupExtension) if newState.Serial <= serial {
if err != nil { t.Fatalf("serial not incremented during refresh. previous:%d, current:%d", serial, newState.Serial)
t.Fatalf("err: %s", err)
} }
backupState, err := terraform.ReadState(f) backupState := testStateRead(t, statePath+DefaultBackupExtension)
f.Close()
if err != nil {
t.Fatalf("err: %s", err)
}
actual = backupState.RootModule().Resources["test_instance.foo"].Primary actual = backupState.RootModule().Resources["test_instance.foo"].Primary
expected = originalState.RootModule().Resources["test_instance.foo"].Primary expected = originalState.RootModule().Resources["test_instance.foo"].Primary