From 94f2f4d6ae93e0fbf869c98b39055a30d0009e7f Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 3 Feb 2017 13:07:34 -0500 Subject: [PATCH] Create state files first for backup tests Previously when runnign a plan with no exitsing state, the plan would be written out and then backed up on the next WriteState by another BackupState instance. Since we now maintain a single State instance thoughout an operation, the backup happens before any state exists so no backup file is created. This is OK, as the backup state the tests were checking for is from the plan file, which already exists separate from the state. --- command/apply_test.go | 10 +++++++++- command/meta_backend_test.go | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/command/apply_test.go b/command/apply_test.go index 77667ae3e7..72d52ddaea 100644 --- a/command/apply_test.go +++ b/command/apply_test.go @@ -15,6 +15,7 @@ import ( "testing" "time" + "github.com/hashicorp/terraform/state" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/cli" ) @@ -550,7 +551,8 @@ func TestApply_plan(t *testing.T) { } func TestApply_plan_backup(t *testing.T) { - planPath := testPlanFile(t, testPlan(t)) + plan := testPlan(t) + planPath := testPlanFile(t, plan) statePath := testTempFile(t) backupPath := testTempFile(t) @@ -563,6 +565,12 @@ func TestApply_plan_backup(t *testing.T) { }, } + // create a state file that needs to be backed up + err := (&state.LocalState{Path: statePath}).WriteState(plan.State) + if err != nil { + t.Fatal(err) + } + args := []string{ "-state-out", statePath, "-backup", backupPath, diff --git a/command/meta_backend_test.go b/command/meta_backend_test.go index c9cff296a0..e73fa6b311 100644 --- a/command/meta_backend_test.go +++ b/command/meta_backend_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/hashicorp/terraform/helper/copy" + "github.com/hashicorp/terraform/state" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/cli" ) @@ -2208,6 +2209,12 @@ func TestMetaBackend_planLocalStatePath(t *testing.T) { // Create an alternate output path statePath := "foo.tfstate" + // put a initial state there that needs to be backed up + err := (&state.LocalState{Path: statePath}).WriteState(original) + if err != nil { + t.Fatal(err) + } + // Setup the meta m := testMetaBackend(t, nil) m.stateOutPath = statePath