From fb397060eb2b90198a2d808d068a7c69b160f6a4 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 5 Jul 2017 18:17:07 -0400 Subject: [PATCH] add some Serial checks to apply and refresh tests --- command/apply_test.go | 18 ++++++++++++++---- command/refresh_test.go | 35 +++++++++-------------------------- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/command/apply_test.go b/command/apply_test.go index ad6e5a9a0..bab8e0955 100644 --- a/command/apply_test.go +++ b/command/apply_test.go @@ -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{ testFixturePath("apply"), } @@ -287,6 +295,10 @@ func TestApply_defaultState(t *testing.T) { if state == 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) { @@ -540,10 +552,8 @@ func TestApply_plan_backup(t *testing.T) { t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) } - { - // Should have a backup file - testStateRead(t, backupPath) - } + // Should have a backup file + testStateRead(t, backupPath) } func TestApply_plan_noBackup(t *testing.T) { diff --git a/command/refresh_test.go b/command/refresh_test.go index c53c84bd3..5a0cfd357 100644 --- a/command/refresh_test.go +++ b/command/refresh_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/hashicorp/terraform/helper/copy" + "github.com/hashicorp/terraform/state" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/cli" ) @@ -193,15 +194,11 @@ func TestRefresh_defaultState(t *testing.T) { } statePath := filepath.Join(td, DefaultStateFilename) - f, err := os.Create(statePath) - if err != nil { - t.Fatalf("err: %s", err) - } - err = terraform.WriteState(originalState, f) - f.Close() - if err != nil { - t.Fatalf("err: %s", err) + localState := &state.LocalState{Path: statePath} + if err := localState.WriteState(originalState); err != nil { + t.Fatal(err) } + serial := localState.State().Serial // Change to that directory cwd, err := os.Getwd() @@ -236,16 +233,7 @@ func TestRefresh_defaultState(t *testing.T) { t.Fatal("refresh should be called") } - f, err = os.Open(statePath) - if err != nil { - t.Fatalf("err: %s", err) - } - - newState, err := terraform.ReadState(f) - f.Close() - if err != nil { - t.Fatalf("err: %s", err) - } + newState := testStateRead(t, statePath) actual := newState.RootModule().Resources["test_instance.foo"].Primary expected := p.RefreshReturn @@ -254,16 +242,11 @@ func TestRefresh_defaultState(t *testing.T) { t.Fatalf("bad:\n%#v", actual) } - f, err = os.Open(statePath + DefaultBackupExtension) - if err != nil { - t.Fatalf("err: %s", err) + if newState.Serial <= serial { + t.Fatalf("serial not incremented during refresh. previous:%d, current:%d", serial, newState.Serial) } - backupState, err := terraform.ReadState(f) - f.Close() - if err != nil { - t.Fatalf("err: %s", err) - } + backupState := testStateRead(t, statePath+DefaultBackupExtension) actual = backupState.RootModule().Resources["test_instance.foo"].Primary expected = originalState.RootModule().Resources["test_instance.foo"].Primary