github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/command/taint_test.go (about) 1 package command 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/hashicorp/terraform/terraform" 8 "github.com/mitchellh/cli" 9 ) 10 11 func TestTaint(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 }, 22 }, 23 }, 24 }, 25 }, 26 } 27 statePath := testStateFile(t, state) 28 29 ui := new(cli.MockUi) 30 c := &TaintCommand{ 31 Meta: Meta{ 32 Ui: ui, 33 }, 34 } 35 36 args := []string{ 37 "-state", statePath, 38 "test_instance.foo", 39 } 40 if code := c.Run(args); code != 0 { 41 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 42 } 43 44 testStateOutput(t, statePath, testTaintStr) 45 } 46 47 func TestTaint_backup(t *testing.T) { 48 // Get a temp cwd 49 tmp, cwd := testCwd(t) 50 defer testFixCwd(t, tmp, cwd) 51 52 // Write the temp state 53 state := &terraform.State{ 54 Modules: []*terraform.ModuleState{ 55 &terraform.ModuleState{ 56 Path: []string{"root"}, 57 Resources: map[string]*terraform.ResourceState{ 58 "test_instance.foo": &terraform.ResourceState{ 59 Type: "test_instance", 60 Primary: &terraform.InstanceState{ 61 ID: "bar", 62 }, 63 }, 64 }, 65 }, 66 }, 67 } 68 path := testStateFileDefault(t, state) 69 70 ui := new(cli.MockUi) 71 c := &TaintCommand{ 72 Meta: Meta{ 73 Ui: ui, 74 }, 75 } 76 77 args := []string{ 78 "test_instance.foo", 79 } 80 if code := c.Run(args); code != 0 { 81 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 82 } 83 84 testStateOutput(t, path+".backup", testTaintDefaultStr) 85 testStateOutput(t, path, testTaintStr) 86 } 87 88 func TestTaint_backupDisable(t *testing.T) { 89 // Get a temp cwd 90 tmp, cwd := testCwd(t) 91 defer testFixCwd(t, tmp, cwd) 92 93 // Write the temp state 94 state := &terraform.State{ 95 Modules: []*terraform.ModuleState{ 96 &terraform.ModuleState{ 97 Path: []string{"root"}, 98 Resources: map[string]*terraform.ResourceState{ 99 "test_instance.foo": &terraform.ResourceState{ 100 Type: "test_instance", 101 Primary: &terraform.InstanceState{ 102 ID: "bar", 103 }, 104 }, 105 }, 106 }, 107 }, 108 } 109 path := testStateFileDefault(t, state) 110 111 ui := new(cli.MockUi) 112 c := &TaintCommand{ 113 Meta: Meta{ 114 Ui: ui, 115 }, 116 } 117 118 args := []string{ 119 "-backup", "-", 120 "test_instance.foo", 121 } 122 if code := c.Run(args); code != 0 { 123 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 124 } 125 126 if _, err := os.Stat(path + ".backup"); err == nil { 127 t.Fatal("backup path should not exist") 128 } 129 130 testStateOutput(t, path, testTaintStr) 131 } 132 133 func TestTaint_badState(t *testing.T) { 134 ui := new(cli.MockUi) 135 c := &TaintCommand{ 136 Meta: Meta{ 137 Ui: ui, 138 }, 139 } 140 141 args := []string{ 142 "-state", "i-should-not-exist-ever", 143 "foo", 144 } 145 if code := c.Run(args); code != 1 { 146 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 147 } 148 } 149 150 func TestTaint_defaultState(t *testing.T) { 151 // Get a temp cwd 152 tmp, cwd := testCwd(t) 153 defer testFixCwd(t, tmp, cwd) 154 155 // Write the temp state 156 state := &terraform.State{ 157 Modules: []*terraform.ModuleState{ 158 &terraform.ModuleState{ 159 Path: []string{"root"}, 160 Resources: map[string]*terraform.ResourceState{ 161 "test_instance.foo": &terraform.ResourceState{ 162 Type: "test_instance", 163 Primary: &terraform.InstanceState{ 164 ID: "bar", 165 }, 166 }, 167 }, 168 }, 169 }, 170 } 171 path := testStateFileDefault(t, state) 172 173 ui := new(cli.MockUi) 174 c := &TaintCommand{ 175 Meta: Meta{ 176 Ui: ui, 177 }, 178 } 179 180 args := []string{ 181 "test_instance.foo", 182 } 183 if code := c.Run(args); code != 0 { 184 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 185 } 186 187 testStateOutput(t, path, testTaintStr) 188 } 189 190 func TestTaint_missing(t *testing.T) { 191 state := &terraform.State{ 192 Modules: []*terraform.ModuleState{ 193 &terraform.ModuleState{ 194 Path: []string{"root"}, 195 Resources: map[string]*terraform.ResourceState{ 196 "test_instance.foo": &terraform.ResourceState{ 197 Type: "test_instance", 198 Primary: &terraform.InstanceState{ 199 ID: "bar", 200 }, 201 }, 202 }, 203 }, 204 }, 205 } 206 statePath := testStateFile(t, state) 207 208 ui := new(cli.MockUi) 209 c := &TaintCommand{ 210 Meta: Meta{ 211 Ui: ui, 212 }, 213 } 214 215 args := []string{ 216 "-state", statePath, 217 "test_instance.bar", 218 } 219 if code := c.Run(args); code == 0 { 220 t.Fatalf("bad: %d\n\n%s", code, ui.OutputWriter.String()) 221 } 222 } 223 224 func TestTaint_missingAllow(t *testing.T) { 225 state := &terraform.State{ 226 Modules: []*terraform.ModuleState{ 227 &terraform.ModuleState{ 228 Path: []string{"root"}, 229 Resources: map[string]*terraform.ResourceState{ 230 "test_instance.foo": &terraform.ResourceState{ 231 Type: "test_instance", 232 Primary: &terraform.InstanceState{ 233 ID: "bar", 234 }, 235 }, 236 }, 237 }, 238 }, 239 } 240 statePath := testStateFile(t, state) 241 242 ui := new(cli.MockUi) 243 c := &TaintCommand{ 244 Meta: Meta{ 245 Ui: ui, 246 }, 247 } 248 249 args := []string{ 250 "-allow-missing", 251 "-state", statePath, 252 "test_instance.bar", 253 } 254 if code := c.Run(args); code != 0 { 255 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 256 } 257 } 258 259 func TestTaint_stateOut(t *testing.T) { 260 // Get a temp cwd 261 tmp, cwd := testCwd(t) 262 defer testFixCwd(t, tmp, cwd) 263 264 // Write the temp state 265 state := &terraform.State{ 266 Modules: []*terraform.ModuleState{ 267 &terraform.ModuleState{ 268 Path: []string{"root"}, 269 Resources: map[string]*terraform.ResourceState{ 270 "test_instance.foo": &terraform.ResourceState{ 271 Type: "test_instance", 272 Primary: &terraform.InstanceState{ 273 ID: "bar", 274 }, 275 }, 276 }, 277 }, 278 }, 279 } 280 path := testStateFileDefault(t, state) 281 282 ui := new(cli.MockUi) 283 c := &TaintCommand{ 284 Meta: Meta{ 285 Ui: ui, 286 }, 287 } 288 289 args := []string{ 290 "-state-out", "foo", 291 "test_instance.foo", 292 } 293 if code := c.Run(args); code != 0 { 294 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 295 } 296 297 testStateOutput(t, path, testTaintDefaultStr) 298 testStateOutput(t, "foo", testTaintStr) 299 } 300 301 func TestTaint_module(t *testing.T) { 302 state := &terraform.State{ 303 Modules: []*terraform.ModuleState{ 304 &terraform.ModuleState{ 305 Path: []string{"root"}, 306 Resources: map[string]*terraform.ResourceState{ 307 "test_instance.foo": &terraform.ResourceState{ 308 Type: "test_instance", 309 Primary: &terraform.InstanceState{ 310 ID: "bar", 311 }, 312 }, 313 }, 314 }, 315 &terraform.ModuleState{ 316 Path: []string{"root", "child"}, 317 Resources: map[string]*terraform.ResourceState{ 318 "test_instance.blah": &terraform.ResourceState{ 319 Type: "test_instance", 320 Primary: &terraform.InstanceState{ 321 ID: "blah", 322 }, 323 }, 324 }, 325 }, 326 }, 327 } 328 statePath := testStateFile(t, state) 329 330 ui := new(cli.MockUi) 331 c := &TaintCommand{ 332 Meta: Meta{ 333 Ui: ui, 334 }, 335 } 336 337 args := []string{ 338 "-module=child", 339 "-state", statePath, 340 "test_instance.blah", 341 } 342 if code := c.Run(args); code != 0 { 343 t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) 344 } 345 346 testStateOutput(t, statePath, testTaintModuleStr) 347 } 348 349 const testTaintStr = ` 350 test_instance.foo: (tainted) 351 ID = bar 352 ` 353 354 const testTaintDefaultStr = ` 355 test_instance.foo: 356 ID = bar 357 ` 358 359 const testTaintModuleStr = ` 360 test_instance.foo: 361 ID = bar 362 363 module.child: 364 test_instance.blah: (tainted) 365 ID = blah 366 `