From 51547ad2bad5340d9439f70497229a6cd57c8dc8 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 27 Jul 2017 15:31:13 -0400 Subject: [PATCH] add tests for state commands through a backend --- command/state_mv_test.go | 161 +++++++++++++++++++++++++++++++++++++++ command/state_rm_test.go | 105 +++++++++++++++++++++++++ 2 files changed, 266 insertions(+) diff --git a/command/state_mv_test.go b/command/state_mv_test.go index e93d4d190..5a0d2ab44 100644 --- a/command/state_mv_test.go +++ b/command/state_mv_test.go @@ -723,6 +723,160 @@ func TestStateMv_stateOutNew_nestedModule(t *testing.T) { testStateOutput(t, backups[0], testStateMvNestedModule_stateOutOriginal) } +func TestStateMv_withinBackend(t *testing.T) { + td := tempDir(t) + copy.CopyDir(testFixturePath("backend-unchanged"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() + + state := &terraform.State{ + Modules: []*terraform.ModuleState{ + &terraform.ModuleState{ + Path: []string{"root"}, + Resources: map[string]*terraform.ResourceState{ + "test_instance.foo": &terraform.ResourceState{ + Type: "test_instance", + Primary: &terraform.InstanceState{ + ID: "bar", + Attributes: map[string]string{ + "foo": "value", + "bar": "value", + }, + }, + }, + + "test_instance.baz": &terraform.ResourceState{ + Type: "test_instance", + Primary: &terraform.InstanceState{ + ID: "foo", + Attributes: map[string]string{ + "foo": "value", + "bar": "value", + }, + }, + }, + }, + }, + }, + } + + // the local backend state file is "foo" + statePath := "local-state.tfstate" + backupPath := "local-state.backup" + + f, err := os.Create(statePath) + if err != nil { + t.Fatal(err) + } + defer f.Close() + + if err := terraform.WriteState(state, f); err != nil { + t.Fatal(err) + } + + p := testProvider() + ui := new(cli.MockUi) + c := &StateMvCommand{ + StateMeta{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(p), + Ui: ui, + }, + }, + } + + args := []string{ + "-backup", backupPath, + "test_instance.foo", + "test_instance.bar", + } + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + testStateOutput(t, statePath, testStateMvOutput) + testStateOutput(t, backupPath, testStateMvOutputOriginal) +} + +func TestStateMv_fromBackendToLocal(t *testing.T) { + td := tempDir(t) + copy.CopyDir(testFixturePath("backend-unchanged"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() + + state := &terraform.State{ + Modules: []*terraform.ModuleState{ + &terraform.ModuleState{ + Path: []string{"root"}, + Resources: map[string]*terraform.ResourceState{ + "test_instance.foo": &terraform.ResourceState{ + Type: "test_instance", + Primary: &terraform.InstanceState{ + ID: "bar", + Attributes: map[string]string{ + "foo": "value", + "bar": "value", + }, + }, + }, + + "test_instance.baz": &terraform.ResourceState{ + Type: "test_instance", + Primary: &terraform.InstanceState{ + ID: "foo", + Attributes: map[string]string{ + "foo": "value", + "bar": "value", + }, + }, + }, + }, + }, + }, + } + + // the local backend state file is "foo" + statePath := "local-state.tfstate" + + // real "local" state file + statePathOut := "real-local.tfstate" + + f, err := os.Create(statePath) + if err != nil { + t.Fatal(err) + } + defer f.Close() + + if err := terraform.WriteState(state, f); err != nil { + t.Fatal(err) + } + + p := testProvider() + ui := new(cli.MockUi) + c := &StateMvCommand{ + StateMeta{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(p), + Ui: ui, + }, + }, + } + + args := []string{ + "-state-out", statePathOut, + "test_instance.foo", + "test_instance.bar", + } + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + testStateOutput(t, statePathOut, testStateMvCount_stateOutSrc) + + // the backend state should be left with only baz + testStateOutput(t, statePath, testStateMvOriginal_backend) +} + const testStateMvOutputOriginal = ` test_instance.baz: ID = foo @@ -961,3 +1115,10 @@ const testStateMvExisting_stateDstOriginal = ` test_instance.qux: ID = bar ` + +const testStateMvOriginal_backend = ` +test_instance.baz: + ID = foo + bar = value + foo = value +` diff --git a/command/state_rm_test.go b/command/state_rm_test.go index 278867ed4..3a4477793 100644 --- a/command/state_rm_test.go +++ b/command/state_rm_test.go @@ -6,6 +6,7 @@ import ( "strings" "testing" + "github.com/hashicorp/terraform/helper/copy" "github.com/hashicorp/terraform/terraform" "github.com/mitchellh/cli" ) @@ -218,6 +219,110 @@ func TestStateRm_noState(t *testing.T) { } } +func TestStateRm_needsInit(t *testing.T) { + td := tempDir(t) + copy.CopyDir(testFixturePath("backend-change"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() + + p := testProvider() + ui := new(cli.MockUi) + c := &StateRmCommand{ + StateMeta{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(p), + Ui: ui, + }, + }, + } + + args := []string{"foo"} + if code := c.Run(args); code == 0 { + t.Fatal("expected error\noutput:", ui.OutputWriter) + } + + if !strings.Contains(ui.ErrorWriter.String(), "Initialization") { + t.Fatal("expected initialization error, got:\n", ui.ErrorWriter) + } +} + +func TestStateRm_backendState(t *testing.T) { + td := tempDir(t) + copy.CopyDir(testFixturePath("backend-unchanged"), td) + defer os.RemoveAll(td) + defer testChdir(t, td)() + + state := &terraform.State{ + Modules: []*terraform.ModuleState{ + &terraform.ModuleState{ + Path: []string{"root"}, + Resources: map[string]*terraform.ResourceState{ + "test_instance.foo": &terraform.ResourceState{ + Type: "test_instance", + Primary: &terraform.InstanceState{ + ID: "bar", + Attributes: map[string]string{ + "foo": "value", + "bar": "value", + }, + }, + }, + + "test_instance.bar": &terraform.ResourceState{ + Type: "test_instance", + Primary: &terraform.InstanceState{ + ID: "foo", + Attributes: map[string]string{ + "foo": "value", + "bar": "value", + }, + }, + }, + }, + }, + }, + } + + // the local backend state file is "foo" + statePath := "local-state.tfstate" + backupPath := "local-state.backup" + + f, err := os.Create(statePath) + if err != nil { + t.Fatal(err) + } + defer f.Close() + + if err := terraform.WriteState(state, f); err != nil { + t.Fatal(err) + } + + p := testProvider() + ui := new(cli.MockUi) + c := &StateRmCommand{ + StateMeta{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(p), + Ui: ui, + }, + }, + } + + args := []string{ + "-backup", backupPath, + "test_instance.foo", + } + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + // Test it is correct + testStateOutput(t, statePath, testStateRmOutput) + + // Test backup + testStateOutput(t, backupPath, testStateRmOutputOriginal) +} + const testStateRmOutputOriginal = ` test_instance.bar: ID = foo