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