github.com/vic3lord/terraform@v0.8.0-rc1.0.20170626102919-16c6dd2cb372/command/state_rm_test.go (about) 1 package command 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/hashicorp/terraform/terraform" 9 "github.com/mitchellh/cli" 10 ) 11 12 func TestStateRm(t *testing.T) { 13 state := &terraform.State{ 14 Modules: []*terraform.ModuleState{ 15 &terraform.ModuleState{ 16 Path: []string{"root"}, 17 Resources: map[string]*terraform.ResourceState{ 18 "test_instance.foo": &terraform.ResourceState{ 19 Type: "test_instance", 20 Primary: &terraform.InstanceState{ 21 ID: "bar", 22 Attributes: map[string]string{ 23 "foo": "value", 24 "bar": "value", 25 }, 26 }, 27 }, 28 29 "test_instance.bar": &terraform.ResourceState{ 30 Type: "test_instance", 31 Primary: &terraform.InstanceState{ 32 ID: "foo", 33 Attributes: map[string]string{ 34 "foo": "value", 35 "bar": "value", 36 }, 37 }, 38 }, 39 }, 40 }, 41 }, 42 } 43 44 statePath := testStateFile(t, state) 45 46 p := testProvider() 47 ui := new(cli.MockUi) 48 c := &StateRmCommand{ 49 Meta: Meta{ 50 testingOverrides: metaOverridesForProvider(p), 51 Ui: ui, 52 }, 53 } 54 55 args := []string{ 56 "-state", statePath, 57 "test_instance.foo", 58 } 59 if code := c.Run(args); code != 0 { 60 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 61 } 62 63 // Test it is correct 64 testStateOutput(t, statePath, testStateRmOutput) 65 66 // Test we have backups 67 backups := testStateBackups(t, filepath.Dir(statePath)) 68 if len(backups) != 1 { 69 t.Fatalf("bad: %#v", backups) 70 } 71 testStateOutput(t, backups[0], testStateRmOutputOriginal) 72 } 73 74 func TestStateRm_backupExplicit(t *testing.T) { 75 td := tempDir(t) 76 defer os.RemoveAll(td) 77 backupPath := filepath.Join(td, "backup") 78 79 state := &terraform.State{ 80 Modules: []*terraform.ModuleState{ 81 &terraform.ModuleState{ 82 Path: []string{"root"}, 83 Resources: map[string]*terraform.ResourceState{ 84 "test_instance.foo": &terraform.ResourceState{ 85 Type: "test_instance", 86 Primary: &terraform.InstanceState{ 87 ID: "bar", 88 Attributes: map[string]string{ 89 "foo": "value", 90 "bar": "value", 91 }, 92 }, 93 }, 94 95 "test_instance.bar": &terraform.ResourceState{ 96 Type: "test_instance", 97 Primary: &terraform.InstanceState{ 98 ID: "foo", 99 Attributes: map[string]string{ 100 "foo": "value", 101 "bar": "value", 102 }, 103 }, 104 }, 105 }, 106 }, 107 }, 108 } 109 110 statePath := testStateFile(t, state) 111 112 p := testProvider() 113 ui := new(cli.MockUi) 114 c := &StateRmCommand{ 115 Meta: Meta{ 116 testingOverrides: metaOverridesForProvider(p), 117 Ui: ui, 118 }, 119 } 120 121 args := []string{ 122 "-backup", backupPath, 123 "-state", statePath, 124 "test_instance.foo", 125 } 126 if code := c.Run(args); code != 0 { 127 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 128 } 129 130 // Test it is correct 131 testStateOutput(t, statePath, testStateRmOutput) 132 133 // Test backup 134 testStateOutput(t, backupPath, testStateRmOutputOriginal) 135 } 136 137 func TestStateRm_noState(t *testing.T) { 138 tmp, cwd := testCwd(t) 139 defer testFixCwd(t, tmp, cwd) 140 141 p := testProvider() 142 ui := new(cli.MockUi) 143 c := &StateRmCommand{ 144 Meta: Meta{ 145 testingOverrides: metaOverridesForProvider(p), 146 Ui: ui, 147 }, 148 } 149 150 args := []string{} 151 if code := c.Run(args); code != 1 { 152 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 153 } 154 } 155 156 const testStateRmOutputOriginal = ` 157 test_instance.bar: 158 ID = foo 159 bar = value 160 foo = value 161 test_instance.foo: 162 ID = bar 163 bar = value 164 foo = value 165 ` 166 167 const testStateRmOutput = ` 168 test_instance.bar: 169 ID = foo 170 bar = value 171 foo = value 172 `