github.com/rhenning/terraform@v0.8.0-beta2/terraform/context_import_test.go (about) 1 package terraform 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 ) 8 9 func TestContextImport_basic(t *testing.T) { 10 p := testProvider("aws") 11 ctx := testContext2(t, &ContextOpts{ 12 Providers: map[string]ResourceProviderFactory{ 13 "aws": testProviderFuncFixed(p), 14 }, 15 }) 16 17 p.ImportStateReturn = []*InstanceState{ 18 &InstanceState{ 19 ID: "foo", 20 Ephemeral: EphemeralState{Type: "aws_instance"}, 21 }, 22 } 23 24 state, err := ctx.Import(&ImportOpts{ 25 Targets: []*ImportTarget{ 26 &ImportTarget{ 27 Addr: "aws_instance.foo", 28 ID: "bar", 29 }, 30 }, 31 }) 32 if err != nil { 33 t.Fatalf("err: %s", err) 34 } 35 36 actual := strings.TrimSpace(state.String()) 37 expected := strings.TrimSpace(testImportStr) 38 if actual != expected { 39 t.Fatalf("bad: \n%s", actual) 40 } 41 } 42 43 func TestContextImport_countIndex(t *testing.T) { 44 p := testProvider("aws") 45 ctx := testContext2(t, &ContextOpts{ 46 Providers: map[string]ResourceProviderFactory{ 47 "aws": testProviderFuncFixed(p), 48 }, 49 }) 50 51 p.ImportStateReturn = []*InstanceState{ 52 &InstanceState{ 53 ID: "foo", 54 Ephemeral: EphemeralState{Type: "aws_instance"}, 55 }, 56 } 57 58 state, err := ctx.Import(&ImportOpts{ 59 Targets: []*ImportTarget{ 60 &ImportTarget{ 61 Addr: "aws_instance.foo[0]", 62 ID: "bar", 63 }, 64 }, 65 }) 66 if err != nil { 67 t.Fatalf("err: %s", err) 68 } 69 70 actual := strings.TrimSpace(state.String()) 71 expected := strings.TrimSpace(testImportCountIndexStr) 72 if actual != expected { 73 t.Fatalf("bad: \n%s", actual) 74 } 75 } 76 77 func TestContextImport_collision(t *testing.T) { 78 p := testProvider("aws") 79 ctx := testContext2(t, &ContextOpts{ 80 Providers: map[string]ResourceProviderFactory{ 81 "aws": testProviderFuncFixed(p), 82 }, 83 84 State: &State{ 85 Modules: []*ModuleState{ 86 &ModuleState{ 87 Path: []string{"root"}, 88 Resources: map[string]*ResourceState{ 89 "aws_instance.foo": &ResourceState{ 90 Type: "aws_instance", 91 Primary: &InstanceState{ 92 ID: "bar", 93 }, 94 }, 95 }, 96 }, 97 }, 98 }, 99 }) 100 101 p.ImportStateReturn = []*InstanceState{ 102 &InstanceState{ 103 ID: "foo", 104 Ephemeral: EphemeralState{Type: "aws_instance"}, 105 }, 106 } 107 108 state, err := ctx.Import(&ImportOpts{ 109 Targets: []*ImportTarget{ 110 &ImportTarget{ 111 Addr: "aws_instance.foo", 112 ID: "bar", 113 }, 114 }, 115 }) 116 if err == nil { 117 t.Fatalf("err: %s", err) 118 } 119 120 actual := strings.TrimSpace(state.String()) 121 expected := strings.TrimSpace(testImportCollisionStr) 122 if actual != expected { 123 t.Fatalf("bad: \n%s", actual) 124 } 125 } 126 127 func TestContextImport_missingType(t *testing.T) { 128 p := testProvider("aws") 129 ctx := testContext2(t, &ContextOpts{ 130 Providers: map[string]ResourceProviderFactory{ 131 "aws": testProviderFuncFixed(p), 132 }, 133 }) 134 135 p.ImportStateReturn = []*InstanceState{ 136 &InstanceState{ 137 ID: "foo", 138 }, 139 } 140 141 state, err := ctx.Import(&ImportOpts{ 142 Targets: []*ImportTarget{ 143 &ImportTarget{ 144 Addr: "aws_instance.foo", 145 ID: "bar", 146 }, 147 }, 148 }) 149 if err == nil { 150 t.Fatal("should error") 151 } 152 153 actual := strings.TrimSpace(state.String()) 154 expected := "<no state>" 155 if actual != expected { 156 t.Fatalf("bad: \n%s", actual) 157 } 158 } 159 160 func TestContextImport_moduleProvider(t *testing.T) { 161 p := testProvider("aws") 162 ctx := testContext2(t, &ContextOpts{ 163 Providers: map[string]ResourceProviderFactory{ 164 "aws": testProviderFuncFixed(p), 165 }, 166 }) 167 168 p.ImportStateReturn = []*InstanceState{ 169 &InstanceState{ 170 ID: "foo", 171 Ephemeral: EphemeralState{Type: "aws_instance"}, 172 }, 173 } 174 175 configured := false 176 p.ConfigureFn = func(c *ResourceConfig) error { 177 configured = true 178 179 if v, ok := c.Get("foo"); !ok || v.(string) != "bar" { 180 return fmt.Errorf("bad") 181 } 182 183 return nil 184 } 185 186 m := testModule(t, "import-provider") 187 188 state, err := ctx.Import(&ImportOpts{ 189 Module: m, 190 Targets: []*ImportTarget{ 191 &ImportTarget{ 192 Addr: "aws_instance.foo", 193 ID: "bar", 194 }, 195 }, 196 }) 197 if err != nil { 198 t.Fatalf("err: %s", err) 199 } 200 201 if !configured { 202 t.Fatal("didn't configure provider") 203 } 204 205 actual := strings.TrimSpace(state.String()) 206 expected := strings.TrimSpace(testImportStr) 207 if actual != expected { 208 t.Fatalf("bad: \n%s", actual) 209 } 210 } 211 212 // Test that import will interpolate provider configuration and use 213 // that configuration for import. 214 func TestContextImport_providerVarConfig(t *testing.T) { 215 p := testProvider("aws") 216 ctx := testContext2(t, &ContextOpts{ 217 Module: testModule(t, "import-provider-vars"), 218 Providers: map[string]ResourceProviderFactory{ 219 "aws": testProviderFuncFixed(p), 220 }, 221 Variables: map[string]interface{}{ 222 "foo": "bar", 223 }, 224 }) 225 226 configured := false 227 p.ConfigureFn = func(c *ResourceConfig) error { 228 configured = true 229 230 if v, ok := c.Get("foo"); !ok || v.(string) != "bar" { 231 return fmt.Errorf("bad value: %#v", v) 232 } 233 234 return nil 235 } 236 237 p.ImportStateReturn = []*InstanceState{ 238 &InstanceState{ 239 ID: "foo", 240 Ephemeral: EphemeralState{Type: "aws_instance"}, 241 }, 242 } 243 244 state, err := ctx.Import(&ImportOpts{ 245 Targets: []*ImportTarget{ 246 &ImportTarget{ 247 Addr: "aws_instance.foo", 248 ID: "bar", 249 }, 250 }, 251 }) 252 if err != nil { 253 t.Fatalf("err: %s", err) 254 } 255 256 if !configured { 257 t.Fatal("didn't configure provider") 258 } 259 260 actual := strings.TrimSpace(state.String()) 261 expected := strings.TrimSpace(testImportStr) 262 if actual != expected { 263 t.Fatalf("bad: \n%s", actual) 264 } 265 } 266 267 // Test that provider configs can't reference resources. 268 func TestContextImport_providerNonVarConfig(t *testing.T) { 269 p := testProvider("aws") 270 ctx := testContext2(t, &ContextOpts{ 271 Module: testModule(t, "import-provider-non-vars"), 272 Providers: map[string]ResourceProviderFactory{ 273 "aws": testProviderFuncFixed(p), 274 }, 275 }) 276 277 p.ImportStateReturn = []*InstanceState{ 278 &InstanceState{ 279 ID: "foo", 280 Ephemeral: EphemeralState{Type: "aws_instance"}, 281 }, 282 } 283 284 _, err := ctx.Import(&ImportOpts{ 285 Targets: []*ImportTarget{ 286 &ImportTarget{ 287 Addr: "aws_instance.foo", 288 ID: "bar", 289 }, 290 }, 291 }) 292 if err == nil { 293 t.Fatal("should error") 294 } 295 } 296 297 func TestContextImport_refresh(t *testing.T) { 298 p := testProvider("aws") 299 ctx := testContext2(t, &ContextOpts{ 300 Providers: map[string]ResourceProviderFactory{ 301 "aws": testProviderFuncFixed(p), 302 }, 303 }) 304 305 p.ImportStateReturn = []*InstanceState{ 306 &InstanceState{ 307 ID: "foo", 308 Ephemeral: EphemeralState{Type: "aws_instance"}, 309 }, 310 } 311 312 p.RefreshFn = func(info *InstanceInfo, s *InstanceState) (*InstanceState, error) { 313 return &InstanceState{ 314 ID: "foo", 315 Attributes: map[string]string{"foo": "bar"}, 316 }, nil 317 } 318 319 state, err := ctx.Import(&ImportOpts{ 320 Targets: []*ImportTarget{ 321 &ImportTarget{ 322 Addr: "aws_instance.foo", 323 ID: "bar", 324 }, 325 }, 326 }) 327 if err != nil { 328 t.Fatalf("err: %s", err) 329 } 330 331 actual := strings.TrimSpace(state.String()) 332 expected := strings.TrimSpace(testImportRefreshStr) 333 if actual != expected { 334 t.Fatalf("bad: \n%s", actual) 335 } 336 } 337 338 func TestContextImport_refreshNil(t *testing.T) { 339 p := testProvider("aws") 340 ctx := testContext2(t, &ContextOpts{ 341 Providers: map[string]ResourceProviderFactory{ 342 "aws": testProviderFuncFixed(p), 343 }, 344 }) 345 346 p.ImportStateReturn = []*InstanceState{ 347 &InstanceState{ 348 ID: "foo", 349 Ephemeral: EphemeralState{Type: "aws_instance"}, 350 }, 351 } 352 353 p.RefreshFn = func(info *InstanceInfo, s *InstanceState) (*InstanceState, error) { 354 return nil, nil 355 } 356 357 state, err := ctx.Import(&ImportOpts{ 358 Targets: []*ImportTarget{ 359 &ImportTarget{ 360 Addr: "aws_instance.foo", 361 ID: "bar", 362 }, 363 }, 364 }) 365 if err == nil { 366 t.Fatal("should error") 367 } 368 369 actual := strings.TrimSpace(state.String()) 370 expected := "<no state>" 371 if actual != expected { 372 t.Fatalf("bad: \n%s", actual) 373 } 374 } 375 376 func TestContextImport_module(t *testing.T) { 377 p := testProvider("aws") 378 ctx := testContext2(t, &ContextOpts{ 379 Providers: map[string]ResourceProviderFactory{ 380 "aws": testProviderFuncFixed(p), 381 }, 382 }) 383 384 p.ImportStateReturn = []*InstanceState{ 385 &InstanceState{ 386 ID: "foo", 387 Ephemeral: EphemeralState{Type: "aws_instance"}, 388 }, 389 } 390 391 state, err := ctx.Import(&ImportOpts{ 392 Targets: []*ImportTarget{ 393 &ImportTarget{ 394 Addr: "module.foo.aws_instance.foo", 395 ID: "bar", 396 }, 397 }, 398 }) 399 if err != nil { 400 t.Fatalf("err: %s", err) 401 } 402 403 actual := strings.TrimSpace(state.String()) 404 expected := strings.TrimSpace(testImportModuleStr) 405 if actual != expected { 406 t.Fatalf("bad: \n%s", actual) 407 } 408 } 409 410 func TestContextImport_moduleDepth2(t *testing.T) { 411 p := testProvider("aws") 412 ctx := testContext2(t, &ContextOpts{ 413 Providers: map[string]ResourceProviderFactory{ 414 "aws": testProviderFuncFixed(p), 415 }, 416 }) 417 418 p.ImportStateReturn = []*InstanceState{ 419 &InstanceState{ 420 ID: "foo", 421 Ephemeral: EphemeralState{Type: "aws_instance"}, 422 }, 423 } 424 425 state, err := ctx.Import(&ImportOpts{ 426 Targets: []*ImportTarget{ 427 &ImportTarget{ 428 Addr: "module.a.module.b.aws_instance.foo", 429 ID: "bar", 430 }, 431 }, 432 }) 433 if err != nil { 434 t.Fatalf("err: %s", err) 435 } 436 437 actual := strings.TrimSpace(state.String()) 438 expected := strings.TrimSpace(testImportModuleDepth2Str) 439 if actual != expected { 440 t.Fatalf("bad: \n%s", actual) 441 } 442 } 443 444 func TestContextImport_moduleDiff(t *testing.T) { 445 p := testProvider("aws") 446 ctx := testContext2(t, &ContextOpts{ 447 Providers: map[string]ResourceProviderFactory{ 448 "aws": testProviderFuncFixed(p), 449 }, 450 451 State: &State{ 452 Modules: []*ModuleState{ 453 &ModuleState{ 454 Path: []string{"root", "bar"}, 455 Resources: map[string]*ResourceState{ 456 "aws_instance.bar": &ResourceState{ 457 Type: "aws_instance", 458 Primary: &InstanceState{ 459 ID: "bar", 460 }, 461 }, 462 }, 463 }, 464 }, 465 }, 466 }) 467 468 p.ImportStateReturn = []*InstanceState{ 469 &InstanceState{ 470 ID: "foo", 471 Ephemeral: EphemeralState{Type: "aws_instance"}, 472 }, 473 } 474 475 state, err := ctx.Import(&ImportOpts{ 476 Targets: []*ImportTarget{ 477 &ImportTarget{ 478 Addr: "module.foo.aws_instance.foo", 479 ID: "bar", 480 }, 481 }, 482 }) 483 if err != nil { 484 t.Fatalf("err: %s", err) 485 } 486 487 actual := strings.TrimSpace(state.String()) 488 expected := strings.TrimSpace(testImportModuleDiffStr) 489 if actual != expected { 490 t.Fatalf("bad: \n%s", actual) 491 } 492 } 493 494 func TestContextImport_moduleExisting(t *testing.T) { 495 p := testProvider("aws") 496 ctx := testContext2(t, &ContextOpts{ 497 Providers: map[string]ResourceProviderFactory{ 498 "aws": testProviderFuncFixed(p), 499 }, 500 501 State: &State{ 502 Modules: []*ModuleState{ 503 &ModuleState{ 504 Path: []string{"root", "foo"}, 505 Resources: map[string]*ResourceState{ 506 "aws_instance.bar": &ResourceState{ 507 Type: "aws_instance", 508 Primary: &InstanceState{ 509 ID: "bar", 510 }, 511 }, 512 }, 513 }, 514 }, 515 }, 516 }) 517 518 p.ImportStateReturn = []*InstanceState{ 519 &InstanceState{ 520 ID: "foo", 521 Ephemeral: EphemeralState{Type: "aws_instance"}, 522 }, 523 } 524 525 state, err := ctx.Import(&ImportOpts{ 526 Targets: []*ImportTarget{ 527 &ImportTarget{ 528 Addr: "module.foo.aws_instance.foo", 529 ID: "bar", 530 }, 531 }, 532 }) 533 if err != nil { 534 t.Fatalf("err: %s", err) 535 } 536 537 actual := strings.TrimSpace(state.String()) 538 expected := strings.TrimSpace(testImportModuleExistingStr) 539 if actual != expected { 540 t.Fatalf("bad: \n%s", actual) 541 } 542 } 543 544 func TestContextImport_multiState(t *testing.T) { 545 p := testProvider("aws") 546 ctx := testContext2(t, &ContextOpts{ 547 Providers: map[string]ResourceProviderFactory{ 548 "aws": testProviderFuncFixed(p), 549 }, 550 }) 551 552 p.ImportStateReturn = []*InstanceState{ 553 &InstanceState{ 554 ID: "foo", 555 Ephemeral: EphemeralState{Type: "aws_instance"}, 556 }, 557 &InstanceState{ 558 ID: "bar", 559 Ephemeral: EphemeralState{Type: "aws_instance_thing"}, 560 }, 561 } 562 563 state, err := ctx.Import(&ImportOpts{ 564 Targets: []*ImportTarget{ 565 &ImportTarget{ 566 Addr: "aws_instance.foo", 567 ID: "bar", 568 }, 569 }, 570 }) 571 if err != nil { 572 t.Fatalf("err: %s", err) 573 } 574 575 actual := strings.TrimSpace(state.String()) 576 expected := strings.TrimSpace(testImportMultiStr) 577 if actual != expected { 578 t.Fatalf("bad: \n%s", actual) 579 } 580 } 581 582 func TestContextImport_multiStateSame(t *testing.T) { 583 p := testProvider("aws") 584 ctx := testContext2(t, &ContextOpts{ 585 Providers: map[string]ResourceProviderFactory{ 586 "aws": testProviderFuncFixed(p), 587 }, 588 }) 589 590 p.ImportStateReturn = []*InstanceState{ 591 &InstanceState{ 592 ID: "foo", 593 Ephemeral: EphemeralState{Type: "aws_instance"}, 594 }, 595 &InstanceState{ 596 ID: "bar", 597 Ephemeral: EphemeralState{Type: "aws_instance_thing"}, 598 }, 599 &InstanceState{ 600 ID: "qux", 601 Ephemeral: EphemeralState{Type: "aws_instance_thing"}, 602 }, 603 } 604 605 state, err := ctx.Import(&ImportOpts{ 606 Targets: []*ImportTarget{ 607 &ImportTarget{ 608 Addr: "aws_instance.foo", 609 ID: "bar", 610 }, 611 }, 612 }) 613 if err != nil { 614 t.Fatalf("err: %s", err) 615 } 616 617 actual := strings.TrimSpace(state.String()) 618 expected := strings.TrimSpace(testImportMultiSameStr) 619 if actual != expected { 620 t.Fatalf("bad: \n%s", actual) 621 } 622 } 623 624 const testImportStr = ` 625 aws_instance.foo: 626 ID = foo 627 provider = aws 628 ` 629 630 const testImportCountIndexStr = ` 631 aws_instance.foo.0: 632 ID = foo 633 provider = aws 634 ` 635 636 const testImportCollisionStr = ` 637 aws_instance.foo: 638 ID = bar 639 ` 640 641 const testImportModuleStr = ` 642 <no state> 643 module.foo: 644 aws_instance.foo: 645 ID = foo 646 provider = aws 647 ` 648 649 const testImportModuleDepth2Str = ` 650 <no state> 651 module.a.b: 652 aws_instance.foo: 653 ID = foo 654 provider = aws 655 ` 656 657 const testImportModuleDiffStr = ` 658 module.bar: 659 aws_instance.bar: 660 ID = bar 661 module.foo: 662 aws_instance.foo: 663 ID = foo 664 provider = aws 665 ` 666 667 const testImportModuleExistingStr = ` 668 module.foo: 669 aws_instance.bar: 670 ID = bar 671 aws_instance.foo: 672 ID = foo 673 provider = aws 674 ` 675 676 const testImportMultiStr = ` 677 aws_instance.foo: 678 ID = foo 679 provider = aws 680 aws_instance_thing.foo: 681 ID = bar 682 provider = aws 683 ` 684 685 const testImportMultiSameStr = ` 686 aws_instance.foo: 687 ID = foo 688 provider = aws 689 aws_instance_thing.foo: 690 ID = bar 691 provider = aws 692 aws_instance_thing.foo-1: 693 ID = qux 694 provider = aws 695 ` 696 697 const testImportRefreshStr = ` 698 aws_instance.foo: 699 ID = foo 700 provider = aws 701 foo = bar 702 `