github.com/ojiry/terraform@v0.8.2-0.20161218223921-e50cec712c4a/command/state_rm_test.go (about) 1 package command 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "github.com/hashicorp/terraform/terraform" 8 "github.com/mitchellh/cli" 9 ) 10 11 func TestStateRm(t *testing.T) { 12 state := &terraform.State{ 13 Modules: []*terraform.ModuleState{ 14 &terraform.ModuleState{ 15 Path: []string{"root"}, 16 Resources: map[string]*terraform.ResourceState{ 17 "test_instance.foo": &terraform.ResourceState{ 18 Type: "test_instance", 19 Primary: &terraform.InstanceState{ 20 ID: "bar", 21 Attributes: map[string]string{ 22 "foo": "value", 23 "bar": "value", 24 }, 25 }, 26 }, 27 28 "test_instance.bar": &terraform.ResourceState{ 29 Type: "test_instance", 30 Primary: &terraform.InstanceState{ 31 ID: "foo", 32 Attributes: map[string]string{ 33 "foo": "value", 34 "bar": "value", 35 }, 36 }, 37 }, 38 }, 39 }, 40 }, 41 } 42 43 statePath := testStateFile(t, state) 44 45 p := testProvider() 46 ui := new(cli.MockUi) 47 c := &StateRmCommand{ 48 Meta: Meta{ 49 ContextOpts: testCtxConfig(p), 50 Ui: ui, 51 }, 52 } 53 54 args := []string{ 55 "-state", statePath, 56 "test_instance.foo", 57 } 58 if code := c.Run(args); code != 0 { 59 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 60 } 61 62 // Test it is correct 63 testStateOutput(t, statePath, testStateRmOutput) 64 65 // Test we have backups 66 backups := testStateBackups(t, filepath.Dir(statePath)) 67 if len(backups) != 1 { 68 t.Fatalf("bad: %#v", backups) 69 } 70 testStateOutput(t, backups[0], testStateRmOutputOriginal) 71 } 72 73 func TestStateRm_noState(t *testing.T) { 74 tmp, cwd := testCwd(t) 75 defer testFixCwd(t, tmp, cwd) 76 77 p := testProvider() 78 ui := new(cli.MockUi) 79 c := &StateRmCommand{ 80 Meta: Meta{ 81 ContextOpts: testCtxConfig(p), 82 Ui: ui, 83 }, 84 } 85 86 args := []string{} 87 if code := c.Run(args); code != 1 { 88 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 89 } 90 } 91 92 const testStateRmOutputOriginal = ` 93 test_instance.bar: 94 ID = foo 95 bar = value 96 foo = value 97 test_instance.foo: 98 ID = bar 99 bar = value 100 foo = value 101 ` 102 103 const testStateRmOutput = ` 104 test_instance.bar: 105 ID = foo 106 bar = value 107 foo = value 108 `