github.com/ns1/terraform@v0.7.10-0.20161109153551-8949419bef40/terraform/context_plan_test.go (about) 1 package terraform 2 3 import ( 4 "bytes" 5 "fmt" 6 "os" 7 "reflect" 8 "sort" 9 "strings" 10 "sync" 11 "testing" 12 ) 13 14 func TestContext2Plan(t *testing.T) { 15 m := testModule(t, "plan-good") 16 p := testProvider("aws") 17 p.DiffFn = testDiffFn 18 ctx := testContext2(t, &ContextOpts{ 19 Module: m, 20 Providers: map[string]ResourceProviderFactory{ 21 "aws": testProviderFuncFixed(p), 22 }, 23 }) 24 25 plan, err := ctx.Plan() 26 if err != nil { 27 t.Fatalf("err: %s", err) 28 } 29 30 if len(plan.Diff.RootModule().Resources) < 2 { 31 t.Fatalf("bad: %#v", plan.Diff.RootModule().Resources) 32 } 33 34 actual := strings.TrimSpace(plan.String()) 35 expected := strings.TrimSpace(testTerraformPlanStr) 36 if actual != expected { 37 t.Fatalf("bad:\n%s", actual) 38 } 39 } 40 41 func TestContext2Plan_createBefore_maintainRoot(t *testing.T) { 42 m := testModule(t, "plan-cbd-maintain-root") 43 p := testProvider("aws") 44 p.DiffFn = testDiffFn 45 ctx := testContext2(t, &ContextOpts{ 46 Module: m, 47 Providers: map[string]ResourceProviderFactory{ 48 "aws": testProviderFuncFixed(p), 49 }, 50 Variables: map[string]interface{}{ 51 "in": "a,b,c", 52 }, 53 }) 54 55 plan, err := ctx.Plan() 56 if err != nil { 57 t.Fatalf("err: %s", err) 58 } 59 60 actual := strings.TrimSpace(plan.String()) 61 expected := strings.TrimSpace(` 62 DIFF: 63 64 CREATE: aws_instance.bar.0 65 CREATE: aws_instance.bar.1 66 CREATE: aws_instance.foo.0 67 CREATE: aws_instance.foo.1 68 69 STATE: 70 71 <no state> 72 `) 73 if actual != expected { 74 t.Fatalf("expected:\n%s, got:\n%s", expected, actual) 75 } 76 } 77 78 func TestContext2Plan_emptyDiff(t *testing.T) { 79 m := testModule(t, "plan-empty") 80 p := testProvider("aws") 81 p.DiffFn = func( 82 info *InstanceInfo, 83 s *InstanceState, 84 c *ResourceConfig) (*InstanceDiff, error) { 85 return nil, nil 86 } 87 88 ctx := testContext2(t, &ContextOpts{ 89 Module: m, 90 Providers: map[string]ResourceProviderFactory{ 91 "aws": testProviderFuncFixed(p), 92 }, 93 }) 94 95 plan, err := ctx.Plan() 96 if err != nil { 97 t.Fatalf("err: %s", err) 98 } 99 100 actual := strings.TrimSpace(plan.String()) 101 expected := strings.TrimSpace(testTerraformPlanEmptyStr) 102 if actual != expected { 103 t.Fatalf("bad:\n%s", actual) 104 } 105 } 106 107 func TestContext2Plan_escapedVar(t *testing.T) { 108 m := testModule(t, "plan-escaped-var") 109 p := testProvider("aws") 110 p.DiffFn = testDiffFn 111 ctx := testContext2(t, &ContextOpts{ 112 Module: m, 113 Providers: map[string]ResourceProviderFactory{ 114 "aws": testProviderFuncFixed(p), 115 }, 116 }) 117 118 plan, err := ctx.Plan() 119 if err != nil { 120 t.Fatalf("err: %s", err) 121 } 122 123 actual := strings.TrimSpace(plan.String()) 124 expected := strings.TrimSpace(testTerraformPlanEscapedVarStr) 125 if actual != expected { 126 t.Fatalf("bad:\n%s", actual) 127 } 128 } 129 130 func TestContext2Plan_minimal(t *testing.T) { 131 m := testModule(t, "plan-empty") 132 p := testProvider("aws") 133 p.DiffFn = testDiffFn 134 ctx := testContext2(t, &ContextOpts{ 135 Module: m, 136 Providers: map[string]ResourceProviderFactory{ 137 "aws": testProviderFuncFixed(p), 138 }, 139 }) 140 141 plan, err := ctx.Plan() 142 if err != nil { 143 t.Fatalf("err: %s", err) 144 } 145 146 actual := strings.TrimSpace(plan.String()) 147 expected := strings.TrimSpace(testTerraformPlanEmptyStr) 148 if actual != expected { 149 t.Fatalf("bad:\n%s", actual) 150 } 151 } 152 153 func TestContext2Plan_modules(t *testing.T) { 154 m := testModule(t, "plan-modules") 155 p := testProvider("aws") 156 p.DiffFn = testDiffFn 157 ctx := testContext2(t, &ContextOpts{ 158 Module: m, 159 Providers: map[string]ResourceProviderFactory{ 160 "aws": testProviderFuncFixed(p), 161 }, 162 }) 163 164 plan, err := ctx.Plan() 165 if err != nil { 166 t.Fatalf("err: %s", err) 167 } 168 169 actual := strings.TrimSpace(plan.String()) 170 expected := strings.TrimSpace(testTerraformPlanModulesStr) 171 if actual != expected { 172 t.Fatalf("bad:\n%s", actual) 173 } 174 } 175 176 // GH-1475 177 func TestContext2Plan_moduleCycle(t *testing.T) { 178 m := testModule(t, "plan-module-cycle") 179 p := testProvider("aws") 180 p.DiffFn = testDiffFn 181 ctx := testContext2(t, &ContextOpts{ 182 Module: m, 183 Providers: map[string]ResourceProviderFactory{ 184 "aws": testProviderFuncFixed(p), 185 }, 186 }) 187 188 plan, err := ctx.Plan() 189 if err != nil { 190 t.Fatalf("err: %s", err) 191 } 192 193 actual := strings.TrimSpace(plan.String()) 194 expected := strings.TrimSpace(testTerraformPlanModuleCycleStr) 195 if actual != expected { 196 t.Fatalf("bad:\n%s", actual) 197 } 198 } 199 200 func TestContext2Plan_moduleDeadlock(t *testing.T) { 201 testCheckDeadlock(t, func() { 202 m := testModule(t, "plan-module-deadlock") 203 p := testProvider("aws") 204 p.DiffFn = testDiffFn 205 206 ctx := testContext2(t, &ContextOpts{ 207 Module: m, 208 Providers: map[string]ResourceProviderFactory{ 209 "aws": testProviderFuncFixed(p), 210 }, 211 }) 212 213 plan, err := ctx.Plan() 214 if err != nil { 215 t.Fatalf("err: %s", err) 216 } 217 218 actual := strings.TrimSpace(plan.String()) 219 expected := strings.TrimSpace(` 220 DIFF: 221 222 module.child: 223 CREATE: aws_instance.foo.0 224 CREATE: aws_instance.foo.1 225 CREATE: aws_instance.foo.2 226 227 STATE: 228 229 <no state> 230 `) 231 if actual != expected { 232 t.Fatalf("expected:\n%sgot:\n%s", expected, actual) 233 } 234 }) 235 } 236 237 func TestContext2Plan_moduleInput(t *testing.T) { 238 m := testModule(t, "plan-module-input") 239 p := testProvider("aws") 240 p.DiffFn = testDiffFn 241 ctx := testContext2(t, &ContextOpts{ 242 Module: m, 243 Providers: map[string]ResourceProviderFactory{ 244 "aws": testProviderFuncFixed(p), 245 }, 246 }) 247 248 plan, err := ctx.Plan() 249 if err != nil { 250 t.Fatalf("err: %s", err) 251 } 252 253 actual := strings.TrimSpace(plan.String()) 254 expected := strings.TrimSpace(testTerraformPlanModuleInputStr) 255 if actual != expected { 256 t.Fatalf("bad:\n%s", actual) 257 } 258 } 259 260 func TestContext2Plan_moduleInputComputed(t *testing.T) { 261 m := testModule(t, "plan-module-input-computed") 262 p := testProvider("aws") 263 p.DiffFn = testDiffFn 264 ctx := testContext2(t, &ContextOpts{ 265 Module: m, 266 Providers: map[string]ResourceProviderFactory{ 267 "aws": testProviderFuncFixed(p), 268 }, 269 }) 270 271 plan, err := ctx.Plan() 272 if err != nil { 273 t.Fatalf("err: %s", err) 274 } 275 276 actual := strings.TrimSpace(plan.String()) 277 expected := strings.TrimSpace(testTerraformPlanModuleInputComputedStr) 278 if actual != expected { 279 t.Fatalf("bad:\n%s", actual) 280 } 281 } 282 283 func TestContext2Plan_moduleInputFromVar(t *testing.T) { 284 m := testModule(t, "plan-module-input-var") 285 p := testProvider("aws") 286 p.DiffFn = testDiffFn 287 ctx := testContext2(t, &ContextOpts{ 288 Module: m, 289 Providers: map[string]ResourceProviderFactory{ 290 "aws": testProviderFuncFixed(p), 291 }, 292 Variables: map[string]interface{}{ 293 "foo": "52", 294 }, 295 }) 296 297 plan, err := ctx.Plan() 298 if err != nil { 299 t.Fatalf("err: %s", err) 300 } 301 302 actual := strings.TrimSpace(plan.String()) 303 expected := strings.TrimSpace(testTerraformPlanModuleInputVarStr) 304 if actual != expected { 305 t.Fatalf("bad:\n%s", actual) 306 } 307 } 308 309 func TestContext2Plan_moduleMultiVar(t *testing.T) { 310 m := testModule(t, "plan-module-multi-var") 311 p := testProvider("aws") 312 p.DiffFn = testDiffFn 313 ctx := testContext2(t, &ContextOpts{ 314 Module: m, 315 Providers: map[string]ResourceProviderFactory{ 316 "aws": testProviderFuncFixed(p), 317 }, 318 }) 319 320 plan, err := ctx.Plan() 321 if err != nil { 322 t.Fatalf("err: %s", err) 323 } 324 325 actual := strings.TrimSpace(plan.String()) 326 expected := strings.TrimSpace(testTerraformPlanModuleMultiVarStr) 327 if actual != expected { 328 t.Fatalf("bad:\n%s", actual) 329 } 330 } 331 332 func TestContext2Plan_moduleOrphans(t *testing.T) { 333 m := testModule(t, "plan-modules-remove") 334 p := testProvider("aws") 335 p.DiffFn = testDiffFn 336 s := &State{ 337 Modules: []*ModuleState{ 338 &ModuleState{ 339 Path: []string{"root", "child"}, 340 Resources: map[string]*ResourceState{ 341 "aws_instance.foo": &ResourceState{ 342 Type: "aws_instance", 343 Primary: &InstanceState{ 344 ID: "baz", 345 }, 346 }, 347 }, 348 }, 349 }, 350 } 351 ctx := testContext2(t, &ContextOpts{ 352 Module: m, 353 Providers: map[string]ResourceProviderFactory{ 354 "aws": testProviderFuncFixed(p), 355 }, 356 State: s, 357 }) 358 359 plan, err := ctx.Plan() 360 if err != nil { 361 t.Fatalf("err: %s", err) 362 } 363 364 actual := strings.TrimSpace(plan.String()) 365 expected := strings.TrimSpace(testTerraformPlanModuleOrphansStr) 366 if actual != expected { 367 t.Fatalf("bad:\n%s", actual) 368 } 369 } 370 371 // https://github.com/hashicorp/terraform/issues/3114 372 func TestContext2Plan_moduleOrphansWithProvisioner(t *testing.T) { 373 m := testModule(t, "plan-modules-remove-provisioners") 374 p := testProvider("aws") 375 pr := testProvisioner() 376 p.DiffFn = testDiffFn 377 s := &State{ 378 Modules: []*ModuleState{ 379 &ModuleState{ 380 Path: []string{"root"}, 381 Resources: map[string]*ResourceState{ 382 "aws_instance.top": &ResourceState{ 383 Type: "aws_instance", 384 Primary: &InstanceState{ 385 ID: "top", 386 }, 387 }, 388 }, 389 }, 390 &ModuleState{ 391 Path: []string{"root", "parent", "childone"}, 392 Resources: map[string]*ResourceState{ 393 "aws_instance.foo": &ResourceState{ 394 Type: "aws_instance", 395 Primary: &InstanceState{ 396 ID: "baz", 397 }, 398 }, 399 }, 400 }, 401 &ModuleState{ 402 Path: []string{"root", "parent", "childtwo"}, 403 Resources: map[string]*ResourceState{ 404 "aws_instance.foo": &ResourceState{ 405 Type: "aws_instance", 406 Primary: &InstanceState{ 407 ID: "baz", 408 }, 409 }, 410 }, 411 }, 412 }, 413 } 414 ctx := testContext2(t, &ContextOpts{ 415 Module: m, 416 Providers: map[string]ResourceProviderFactory{ 417 "aws": testProviderFuncFixed(p), 418 }, 419 Provisioners: map[string]ResourceProvisionerFactory{ 420 "shell": testProvisionerFuncFixed(pr), 421 }, 422 State: s, 423 }) 424 425 plan, err := ctx.Plan() 426 if err != nil { 427 t.Fatalf("err: %s", err) 428 } 429 430 actual := strings.TrimSpace(plan.String()) 431 expected := strings.TrimSpace(` 432 DIFF: 433 434 module.parent.childone: 435 DESTROY: aws_instance.foo 436 module.parent.childtwo: 437 DESTROY: aws_instance.foo 438 439 STATE: 440 441 aws_instance.top: 442 ID = top 443 444 module.parent.childone: 445 aws_instance.foo: 446 ID = baz 447 module.parent.childtwo: 448 aws_instance.foo: 449 ID = baz 450 `) 451 if actual != expected { 452 t.Fatalf("bad:\n%s", actual) 453 } 454 } 455 456 func TestContext2Plan_moduleProviderInherit(t *testing.T) { 457 var l sync.Mutex 458 var calls []string 459 460 m := testModule(t, "plan-module-provider-inherit") 461 ctx := testContext2(t, &ContextOpts{ 462 Module: m, 463 Providers: map[string]ResourceProviderFactory{ 464 "aws": func() (ResourceProvider, error) { 465 l.Lock() 466 defer l.Unlock() 467 468 p := testProvider("aws") 469 p.ConfigureFn = func(c *ResourceConfig) error { 470 if v, ok := c.Get("from"); !ok || v.(string) != "root" { 471 return fmt.Errorf("bad") 472 } 473 474 return nil 475 } 476 p.DiffFn = func( 477 info *InstanceInfo, 478 state *InstanceState, 479 c *ResourceConfig) (*InstanceDiff, error) { 480 v, _ := c.Get("from") 481 calls = append(calls, v.(string)) 482 return testDiffFn(info, state, c) 483 } 484 return p, nil 485 }, 486 }, 487 }) 488 489 _, err := ctx.Plan() 490 if err != nil { 491 t.Fatalf("err: %s", err) 492 } 493 494 actual := calls 495 sort.Strings(actual) 496 expected := []string{"child", "root"} 497 if !reflect.DeepEqual(actual, expected) { 498 t.Fatalf("bad: %#v", actual) 499 } 500 } 501 502 func TestContext2Plan_moduleProviderDefaults(t *testing.T) { 503 var l sync.Mutex 504 var calls []string 505 toCount := 0 506 507 m := testModule(t, "plan-module-provider-defaults") 508 ctx := testContext2(t, &ContextOpts{ 509 Module: m, 510 Providers: map[string]ResourceProviderFactory{ 511 "aws": func() (ResourceProvider, error) { 512 l.Lock() 513 defer l.Unlock() 514 515 p := testProvider("aws") 516 p.ConfigureFn = func(c *ResourceConfig) error { 517 if v, ok := c.Get("from"); !ok || v.(string) != "root" { 518 return fmt.Errorf("bad") 519 } 520 if v, ok := c.Get("to"); ok && v.(string) == "child" { 521 toCount++ 522 } 523 524 return nil 525 } 526 p.DiffFn = func( 527 info *InstanceInfo, 528 state *InstanceState, 529 c *ResourceConfig) (*InstanceDiff, error) { 530 v, _ := c.Get("from") 531 calls = append(calls, v.(string)) 532 return testDiffFn(info, state, c) 533 } 534 return p, nil 535 }, 536 }, 537 }) 538 539 _, err := ctx.Plan() 540 if err != nil { 541 t.Fatalf("err: %s", err) 542 } 543 544 if toCount != 1 { 545 t.Fatalf( 546 "provider in child didn't set proper config\n\n"+ 547 "toCount: %d", toCount) 548 } 549 550 actual := calls 551 sort.Strings(actual) 552 expected := []string{"child", "root"} 553 if !reflect.DeepEqual(actual, expected) { 554 t.Fatalf("bad: %#v", actual) 555 } 556 } 557 558 func TestContext2Plan_moduleProviderDefaultsVar(t *testing.T) { 559 var l sync.Mutex 560 var calls []string 561 562 m := testModule(t, "plan-module-provider-defaults-var") 563 ctx := testContext2(t, &ContextOpts{ 564 Module: m, 565 Providers: map[string]ResourceProviderFactory{ 566 "aws": func() (ResourceProvider, error) { 567 l.Lock() 568 defer l.Unlock() 569 570 p := testProvider("aws") 571 p.ConfigureFn = func(c *ResourceConfig) error { 572 var buf bytes.Buffer 573 if v, ok := c.Get("from"); ok { 574 buf.WriteString(v.(string) + "\n") 575 } 576 if v, ok := c.Get("to"); ok { 577 buf.WriteString(v.(string) + "\n") 578 } 579 580 calls = append(calls, buf.String()) 581 return nil 582 } 583 p.DiffFn = testDiffFn 584 return p, nil 585 }, 586 }, 587 Variables: map[string]interface{}{ 588 "foo": "root", 589 }, 590 }) 591 592 _, err := ctx.Plan() 593 if err != nil { 594 t.Fatalf("err: %s", err) 595 } 596 597 expected := []string{ 598 "root\n", 599 "root\nchild\n", 600 } 601 if !reflect.DeepEqual(calls, expected) { 602 t.Fatalf("BAD: %#v", calls) 603 } 604 } 605 606 func TestContext2Plan_moduleVar(t *testing.T) { 607 m := testModule(t, "plan-module-var") 608 p := testProvider("aws") 609 p.DiffFn = testDiffFn 610 ctx := testContext2(t, &ContextOpts{ 611 Module: m, 612 Providers: map[string]ResourceProviderFactory{ 613 "aws": testProviderFuncFixed(p), 614 }, 615 }) 616 617 plan, err := ctx.Plan() 618 if err != nil { 619 t.Fatalf("err: %s", err) 620 } 621 622 actual := strings.TrimSpace(plan.String()) 623 expected := strings.TrimSpace(testTerraformPlanModuleVarStr) 624 if actual != expected { 625 t.Fatalf("bad:\n%s", actual) 626 } 627 } 628 629 func TestContext2Plan_moduleVarWrongType(t *testing.T) { 630 m := testModule(t, "plan-module-wrong-var-type") 631 p := testProvider("aws") 632 p.DiffFn = testDiffFn 633 ctx := testContext2(t, &ContextOpts{ 634 Module: m, 635 Providers: map[string]ResourceProviderFactory{ 636 "aws": testProviderFuncFixed(p), 637 }, 638 }) 639 640 _, err := ctx.Plan() 641 if err == nil { 642 t.Fatalf("should error") 643 } 644 } 645 646 func TestContext2Plan_moduleVarWrongTypeNested(t *testing.T) { 647 m := testModule(t, "plan-module-wrong-var-type-nested") 648 p := testProvider("aws") 649 p.DiffFn = testDiffFn 650 ctx := testContext2(t, &ContextOpts{ 651 Module: m, 652 Providers: map[string]ResourceProviderFactory{ 653 "aws": testProviderFuncFixed(p), 654 }, 655 }) 656 657 _, err := ctx.Plan() 658 if err == nil { 659 t.Fatalf("should error") 660 } 661 } 662 663 func TestContext2Plan_moduleVarWithDefaultValue(t *testing.T) { 664 m := testModule(t, "plan-module-var-with-default-value") 665 p := testProvider("null") 666 p.DiffFn = testDiffFn 667 ctx := testContext2(t, &ContextOpts{ 668 Module: m, 669 Providers: map[string]ResourceProviderFactory{ 670 "null": testProviderFuncFixed(p), 671 }, 672 }) 673 674 _, err := ctx.Plan() 675 if err != nil { 676 t.Fatalf("bad: %s", err) 677 } 678 } 679 680 func TestContext2Plan_moduleVarComputed(t *testing.T) { 681 m := testModule(t, "plan-module-var-computed") 682 p := testProvider("aws") 683 p.DiffFn = testDiffFn 684 ctx := testContext2(t, &ContextOpts{ 685 Module: m, 686 Providers: map[string]ResourceProviderFactory{ 687 "aws": testProviderFuncFixed(p), 688 }, 689 }) 690 691 plan, err := ctx.Plan() 692 if err != nil { 693 t.Fatalf("err: %s", err) 694 } 695 696 actual := strings.TrimSpace(plan.String()) 697 expected := strings.TrimSpace(testTerraformPlanModuleVarComputedStr) 698 if actual != expected { 699 t.Fatalf("bad:\n%s", actual) 700 } 701 } 702 703 func TestContext2Plan_nil(t *testing.T) { 704 m := testModule(t, "plan-nil") 705 p := testProvider("aws") 706 p.DiffFn = testDiffFn 707 ctx := testContext2(t, &ContextOpts{ 708 Module: m, 709 Providers: map[string]ResourceProviderFactory{ 710 "aws": testProviderFuncFixed(p), 711 }, 712 State: &State{ 713 Modules: []*ModuleState{ 714 &ModuleState{ 715 Path: rootModulePath, 716 Resources: map[string]*ResourceState{ 717 "aws_instance.foo": &ResourceState{ 718 Type: "aws_instance", 719 Primary: &InstanceState{ 720 ID: "bar", 721 }, 722 }, 723 }, 724 }, 725 }, 726 }, 727 }) 728 729 plan, err := ctx.Plan() 730 if err != nil { 731 t.Fatalf("err: %s", err) 732 } 733 if len(plan.Diff.RootModule().Resources) != 0 { 734 t.Fatalf("bad: %#v", plan.Diff.RootModule().Resources) 735 } 736 } 737 738 func TestContext2Plan_preventDestroy_bad(t *testing.T) { 739 m := testModule(t, "plan-prevent-destroy-bad") 740 p := testProvider("aws") 741 p.DiffFn = testDiffFn 742 ctx := testContext2(t, &ContextOpts{ 743 Module: m, 744 Providers: map[string]ResourceProviderFactory{ 745 "aws": testProviderFuncFixed(p), 746 }, 747 State: &State{ 748 Modules: []*ModuleState{ 749 &ModuleState{ 750 Path: rootModulePath, 751 Resources: map[string]*ResourceState{ 752 "aws_instance.foo": &ResourceState{ 753 Type: "aws_instance", 754 Primary: &InstanceState{ 755 ID: "i-abc123", 756 }, 757 }, 758 }, 759 }, 760 }, 761 }, 762 }) 763 764 plan, err := ctx.Plan() 765 766 expectedErr := "aws_instance.foo: the plan would destroy" 767 if !strings.Contains(fmt.Sprintf("%s", err), expectedErr) { 768 t.Fatalf("expected err would contain %q\nerr: %s\nplan: %s", 769 expectedErr, err, plan) 770 } 771 } 772 773 func TestContext2Plan_preventDestroy_good(t *testing.T) { 774 m := testModule(t, "plan-prevent-destroy-good") 775 p := testProvider("aws") 776 p.DiffFn = testDiffFn 777 ctx := testContext2(t, &ContextOpts{ 778 Module: m, 779 Providers: map[string]ResourceProviderFactory{ 780 "aws": testProviderFuncFixed(p), 781 }, 782 State: &State{ 783 Modules: []*ModuleState{ 784 &ModuleState{ 785 Path: rootModulePath, 786 Resources: map[string]*ResourceState{ 787 "aws_instance.foo": &ResourceState{ 788 Type: "aws_instance", 789 Primary: &InstanceState{ 790 ID: "i-abc123", 791 }, 792 }, 793 }, 794 }, 795 }, 796 }, 797 }) 798 799 plan, err := ctx.Plan() 800 if err != nil { 801 t.Fatalf("err: %s", err) 802 } 803 if !plan.Diff.Empty() { 804 t.Fatalf("Expected empty plan, got %s", plan.String()) 805 } 806 } 807 808 func TestContext2Plan_preventDestroy_countBad(t *testing.T) { 809 m := testModule(t, "plan-prevent-destroy-count-bad") 810 p := testProvider("aws") 811 p.DiffFn = testDiffFn 812 ctx := testContext2(t, &ContextOpts{ 813 Module: m, 814 Providers: map[string]ResourceProviderFactory{ 815 "aws": testProviderFuncFixed(p), 816 }, 817 State: &State{ 818 Modules: []*ModuleState{ 819 &ModuleState{ 820 Path: rootModulePath, 821 Resources: map[string]*ResourceState{ 822 "aws_instance.foo.0": &ResourceState{ 823 Type: "aws_instance", 824 Primary: &InstanceState{ 825 ID: "i-abc123", 826 }, 827 }, 828 "aws_instance.foo.1": &ResourceState{ 829 Type: "aws_instance", 830 Primary: &InstanceState{ 831 ID: "i-abc345", 832 }, 833 }, 834 }, 835 }, 836 }, 837 }, 838 }) 839 840 plan, err := ctx.Plan() 841 842 expectedErr := "aws_instance.foo.1: the plan would destroy" 843 if !strings.Contains(fmt.Sprintf("%s", err), expectedErr) { 844 t.Fatalf("expected err would contain %q\nerr: %s\nplan: %s", 845 expectedErr, err, plan) 846 } 847 } 848 849 func TestContext2Plan_preventDestroy_countGood(t *testing.T) { 850 m := testModule(t, "plan-prevent-destroy-count-good") 851 p := testProvider("aws") 852 p.DiffFn = testDiffFn 853 ctx := testContext2(t, &ContextOpts{ 854 Module: m, 855 Providers: map[string]ResourceProviderFactory{ 856 "aws": testProviderFuncFixed(p), 857 }, 858 State: &State{ 859 Modules: []*ModuleState{ 860 &ModuleState{ 861 Path: rootModulePath, 862 Resources: map[string]*ResourceState{ 863 "aws_instance.foo.0": &ResourceState{ 864 Type: "aws_instance", 865 Primary: &InstanceState{ 866 ID: "i-abc123", 867 }, 868 }, 869 "aws_instance.foo.1": &ResourceState{ 870 Type: "aws_instance", 871 Primary: &InstanceState{ 872 ID: "i-abc345", 873 }, 874 }, 875 }, 876 }, 877 }, 878 }, 879 }) 880 881 plan, err := ctx.Plan() 882 if err != nil { 883 t.Fatalf("err: %s", err) 884 } 885 if plan.Diff.Empty() { 886 t.Fatalf("Expected non-empty plan, got %s", plan.String()) 887 } 888 } 889 890 func TestContext2Plan_preventDestroy_countGoodNoChange(t *testing.T) { 891 m := testModule(t, "plan-prevent-destroy-count-good") 892 p := testProvider("aws") 893 p.DiffFn = testDiffFn 894 ctx := testContext2(t, &ContextOpts{ 895 Module: m, 896 Providers: map[string]ResourceProviderFactory{ 897 "aws": testProviderFuncFixed(p), 898 }, 899 State: &State{ 900 Modules: []*ModuleState{ 901 &ModuleState{ 902 Path: rootModulePath, 903 Resources: map[string]*ResourceState{ 904 "aws_instance.foo.0": &ResourceState{ 905 Type: "aws_instance", 906 Primary: &InstanceState{ 907 ID: "i-abc123", 908 Attributes: map[string]string{ 909 "current": "0", 910 "type": "aws_instance", 911 }, 912 }, 913 }, 914 }, 915 }, 916 }, 917 }, 918 }) 919 920 plan, err := ctx.Plan() 921 if err != nil { 922 t.Fatalf("err: %s", err) 923 } 924 if !plan.Diff.Empty() { 925 t.Fatalf("Expected empty plan, got %s", plan.String()) 926 } 927 } 928 929 func TestContext2Plan_preventDestroy_destroyPlan(t *testing.T) { 930 m := testModule(t, "plan-prevent-destroy-good") 931 p := testProvider("aws") 932 p.DiffFn = testDiffFn 933 ctx := testContext2(t, &ContextOpts{ 934 Module: m, 935 Providers: map[string]ResourceProviderFactory{ 936 "aws": testProviderFuncFixed(p), 937 }, 938 State: &State{ 939 Modules: []*ModuleState{ 940 &ModuleState{ 941 Path: rootModulePath, 942 Resources: map[string]*ResourceState{ 943 "aws_instance.foo": &ResourceState{ 944 Type: "aws_instance", 945 Primary: &InstanceState{ 946 ID: "i-abc123", 947 }, 948 }, 949 }, 950 }, 951 }, 952 }, 953 Destroy: true, 954 }) 955 956 plan, err := ctx.Plan() 957 958 expectedErr := "aws_instance.foo: the plan would destroy" 959 if !strings.Contains(fmt.Sprintf("%s", err), expectedErr) { 960 t.Fatalf("expected err would contain %q\nerr: %s\nplan: %s", 961 expectedErr, err, plan) 962 } 963 } 964 965 func TestContext2Plan_provisionerCycle(t *testing.T) { 966 m := testModule(t, "plan-provisioner-cycle") 967 p := testProvider("aws") 968 p.DiffFn = testDiffFn 969 pr := testProvisioner() 970 ctx := testContext2(t, &ContextOpts{ 971 Module: m, 972 Providers: map[string]ResourceProviderFactory{ 973 "aws": testProviderFuncFixed(p), 974 }, 975 Provisioners: map[string]ResourceProvisionerFactory{ 976 "local-exec": testProvisionerFuncFixed(pr), 977 }, 978 }) 979 980 _, err := ctx.Plan() 981 if err == nil { 982 t.Fatalf("should error") 983 } 984 } 985 986 func TestContext2Plan_computed(t *testing.T) { 987 m := testModule(t, "plan-computed") 988 p := testProvider("aws") 989 p.DiffFn = testDiffFn 990 ctx := testContext2(t, &ContextOpts{ 991 Module: m, 992 Providers: map[string]ResourceProviderFactory{ 993 "aws": testProviderFuncFixed(p), 994 }, 995 }) 996 997 plan, err := ctx.Plan() 998 if err != nil { 999 t.Fatalf("err: %s", err) 1000 } 1001 1002 actual := strings.TrimSpace(plan.String()) 1003 expected := strings.TrimSpace(testTerraformPlanComputedStr) 1004 if actual != expected { 1005 t.Fatalf("bad:\n%s", actual) 1006 } 1007 } 1008 1009 func TestContext2Plan_computedDataResource(t *testing.T) { 1010 m := testModule(t, "plan-computed-data-resource") 1011 p := testProvider("aws") 1012 p.DiffFn = testDiffFn 1013 ctx := testContext2(t, &ContextOpts{ 1014 Module: m, 1015 Providers: map[string]ResourceProviderFactory{ 1016 "aws": testProviderFuncFixed(p), 1017 }, 1018 }) 1019 1020 plan, err := ctx.Plan() 1021 if err != nil { 1022 t.Fatalf("err: %s", err) 1023 } 1024 1025 if got := len(plan.Diff.Modules); got != 1 { 1026 t.Fatalf("got %d modules; want 1", got) 1027 } 1028 1029 moduleDiff := plan.Diff.Modules[0] 1030 1031 if _, ok := moduleDiff.Resources["aws_instance.foo"]; !ok { 1032 t.Fatalf("missing diff for aws_instance.foo") 1033 } 1034 iDiff, ok := moduleDiff.Resources["data.aws_vpc.bar"] 1035 if !ok { 1036 t.Fatalf("missing diff for data.aws_vpc.bar") 1037 } 1038 1039 expectedDiff := &InstanceDiff{ 1040 Attributes: map[string]*ResourceAttrDiff{ 1041 "id": { 1042 NewComputed: true, 1043 RequiresNew: true, 1044 Type: DiffAttrOutput, 1045 }, 1046 }, 1047 } 1048 if same, _ := expectedDiff.Same(iDiff); !same { 1049 t.Fatalf( 1050 "incorrect diff for data.aws_vpc.bar\ngot: %#v\nwant: %#v", 1051 iDiff, expectedDiff, 1052 ) 1053 } 1054 } 1055 1056 func TestContext2Plan_computedDataCountResource(t *testing.T) { 1057 m := testModule(t, "plan-computed-data-count") 1058 p := testProvider("aws") 1059 p.DiffFn = testDiffFn 1060 ctx := testContext2(t, &ContextOpts{ 1061 Module: m, 1062 Providers: map[string]ResourceProviderFactory{ 1063 "aws": testProviderFuncFixed(p), 1064 }, 1065 }) 1066 1067 plan, err := ctx.Plan() 1068 if err != nil { 1069 t.Fatalf("err: %s", err) 1070 } 1071 1072 if got := len(plan.Diff.Modules); got != 1 { 1073 t.Fatalf("got %d modules; want 1", got) 1074 } 1075 1076 moduleDiff := plan.Diff.Modules[0] 1077 1078 // make sure we created 3 "bar"s 1079 for i := 0; i < 3; i++ { 1080 resource := fmt.Sprintf("data.aws_vpc.bar.%d", i) 1081 if _, ok := moduleDiff.Resources[resource]; !ok { 1082 t.Fatalf("missing diff for %s", resource) 1083 } 1084 } 1085 } 1086 1087 // Higher level test at TestResource_dataSourceListPlanPanic 1088 func TestContext2Plan_dataSourceTypeMismatch(t *testing.T) { 1089 m := testModule(t, "plan-data-source-type-mismatch") 1090 p := testProvider("aws") 1091 p.ValidateResourceFn = func(t string, c *ResourceConfig) (ws []string, es []error) { 1092 // Emulate the type checking behavior of helper/schema based validation 1093 if t == "aws_instance" { 1094 ami, _ := c.Get("ami") 1095 switch a := ami.(type) { 1096 case string: 1097 // ok 1098 default: 1099 es = append(es, fmt.Errorf("Expected ami to be string, got %T", a)) 1100 } 1101 } 1102 return 1103 } 1104 p.DiffFn = func( 1105 info *InstanceInfo, 1106 state *InstanceState, 1107 c *ResourceConfig) (*InstanceDiff, error) { 1108 if info.Type == "aws_instance" { 1109 // If we get to the diff, we should be able to assume types 1110 ami, _ := c.Get("ami") 1111 _ = ami.(string) 1112 } 1113 return nil, nil 1114 } 1115 ctx := testContext2(t, &ContextOpts{ 1116 Module: m, 1117 // Pretend like we ran a Refresh and the AZs data source was populated. 1118 State: &State{ 1119 Modules: []*ModuleState{ 1120 &ModuleState{ 1121 Path: rootModulePath, 1122 Resources: map[string]*ResourceState{ 1123 "data.aws_availability_zones.azs": &ResourceState{ 1124 Type: "aws_availability_zones", 1125 Primary: &InstanceState{ 1126 ID: "i-abc123", 1127 Attributes: map[string]string{ 1128 "names.#": "2", 1129 "names.0": "us-east-1a", 1130 "names.1": "us-east-1b", 1131 }, 1132 }, 1133 }, 1134 }, 1135 }, 1136 }, 1137 }, 1138 Providers: map[string]ResourceProviderFactory{ 1139 "aws": testProviderFuncFixed(p), 1140 }, 1141 }) 1142 1143 _, err := ctx.Plan() 1144 1145 if err == nil { 1146 t.Fatalf("Expected err, got none!") 1147 } 1148 expected := "Expected ami to be string" 1149 if !strings.Contains(err.Error(), expected) { 1150 t.Fatalf("expected:\n\n%s\n\nto contain:\n\n%s", err, expected) 1151 } 1152 } 1153 1154 func TestContext2Plan_dataResourceBecomesComputed(t *testing.T) { 1155 m := testModule(t, "plan-data-resource-becomes-computed") 1156 p := testProvider("aws") 1157 1158 p.DiffFn = func(info *InstanceInfo, state *InstanceState, config *ResourceConfig) (*InstanceDiff, error) { 1159 if info.Type != "aws_instance" { 1160 t.Fatalf("don't know how to diff %s", info.Id) 1161 return nil, nil 1162 } 1163 1164 return &InstanceDiff{ 1165 Attributes: map[string]*ResourceAttrDiff{ 1166 "computed": &ResourceAttrDiff{ 1167 Old: "", 1168 New: "", 1169 NewComputed: true, 1170 }, 1171 }, 1172 }, nil 1173 } 1174 p.ReadDataDiffReturn = &InstanceDiff{ 1175 Attributes: map[string]*ResourceAttrDiff{ 1176 "foo": &ResourceAttrDiff{ 1177 Old: "", 1178 New: "", 1179 NewComputed: true, 1180 }, 1181 }, 1182 } 1183 1184 ctx := testContext2(t, &ContextOpts{ 1185 Module: m, 1186 Providers: map[string]ResourceProviderFactory{ 1187 "aws": testProviderFuncFixed(p), 1188 }, 1189 State: &State{ 1190 Modules: []*ModuleState{ 1191 &ModuleState{ 1192 Path: rootModulePath, 1193 Resources: map[string]*ResourceState{ 1194 "data.aws_data_resource.foo": &ResourceState{ 1195 Type: "aws_data_resource", 1196 Primary: &InstanceState{ 1197 ID: "i-abc123", 1198 Attributes: map[string]string{ 1199 "id": "i-abc123", 1200 "value": "baz", 1201 }, 1202 }, 1203 }, 1204 }, 1205 }, 1206 }, 1207 }, 1208 }) 1209 1210 plan, err := ctx.Plan() 1211 if err != nil { 1212 t.Fatalf("err: %s", err) 1213 } 1214 1215 if got := len(plan.Diff.Modules); got != 1 { 1216 t.Fatalf("got %d modules; want 1", got) 1217 } 1218 1219 if !p.ReadDataDiffCalled { 1220 t.Fatal("ReadDataDiff wasn't called, but should've been") 1221 } 1222 if got, want := p.ReadDataDiffInfo.Id, "data.aws_data_resource.foo"; got != want { 1223 t.Fatalf("ReadDataDiff info id is %s; want %s", got, want) 1224 } 1225 1226 moduleDiff := plan.Diff.Modules[0] 1227 1228 iDiff, ok := moduleDiff.Resources["data.aws_data_resource.foo"] 1229 if !ok { 1230 t.Fatalf("missing diff for data.aws_data_resource.foo") 1231 } 1232 1233 // This is added by the diff but we want to verify that we got 1234 // the same diff as above minus the dynamic stuff. 1235 delete(iDiff.Attributes, "id") 1236 1237 if same, _ := p.ReadDataDiffReturn.Same(iDiff); !same { 1238 t.Fatalf( 1239 "incorrect diff for data.data_resource.foo\ngot: %#v\nwant: %#v", 1240 iDiff, p.ReadDataDiffReturn, 1241 ) 1242 } 1243 } 1244 1245 func TestContext2Plan_computedList(t *testing.T) { 1246 m := testModule(t, "plan-computed-list") 1247 p := testProvider("aws") 1248 p.DiffFn = testDiffFn 1249 ctx := testContext2(t, &ContextOpts{ 1250 Module: m, 1251 Providers: map[string]ResourceProviderFactory{ 1252 "aws": testProviderFuncFixed(p), 1253 }, 1254 }) 1255 1256 plan, err := ctx.Plan() 1257 if err != nil { 1258 t.Fatalf("err: %s", err) 1259 } 1260 1261 actual := strings.TrimSpace(plan.String()) 1262 expected := strings.TrimSpace(testTerraformPlanComputedListStr) 1263 if actual != expected { 1264 t.Fatalf("bad:\n%s", actual) 1265 } 1266 } 1267 1268 func TestContext2Plan_count(t *testing.T) { 1269 m := testModule(t, "plan-count") 1270 p := testProvider("aws") 1271 p.DiffFn = testDiffFn 1272 ctx := testContext2(t, &ContextOpts{ 1273 Module: m, 1274 Providers: map[string]ResourceProviderFactory{ 1275 "aws": testProviderFuncFixed(p), 1276 }, 1277 }) 1278 1279 plan, err := ctx.Plan() 1280 if err != nil { 1281 t.Fatalf("err: %s", err) 1282 } 1283 1284 if len(plan.Diff.RootModule().Resources) < 6 { 1285 t.Fatalf("bad: %#v", plan.Diff.RootModule().Resources) 1286 } 1287 1288 actual := strings.TrimSpace(plan.String()) 1289 expected := strings.TrimSpace(testTerraformPlanCountStr) 1290 if actual != expected { 1291 t.Fatalf("bad:\n%s", actual) 1292 } 1293 } 1294 1295 func TestContext2Plan_countComputed(t *testing.T) { 1296 m := testModule(t, "plan-count-computed") 1297 p := testProvider("aws") 1298 p.DiffFn = testDiffFn 1299 ctx := testContext2(t, &ContextOpts{ 1300 Module: m, 1301 Providers: map[string]ResourceProviderFactory{ 1302 "aws": testProviderFuncFixed(p), 1303 }, 1304 }) 1305 1306 _, err := ctx.Plan() 1307 if err == nil { 1308 t.Fatal("should error") 1309 } 1310 } 1311 1312 func TestContext2Plan_countIndex(t *testing.T) { 1313 m := testModule(t, "plan-count-index") 1314 p := testProvider("aws") 1315 p.DiffFn = testDiffFn 1316 ctx := testContext2(t, &ContextOpts{ 1317 Module: m, 1318 Providers: map[string]ResourceProviderFactory{ 1319 "aws": testProviderFuncFixed(p), 1320 }, 1321 }) 1322 1323 plan, err := ctx.Plan() 1324 if err != nil { 1325 t.Fatalf("err: %s", err) 1326 } 1327 1328 actual := strings.TrimSpace(plan.String()) 1329 expected := strings.TrimSpace(testTerraformPlanCountIndexStr) 1330 if actual != expected { 1331 t.Fatalf("bad:\n%s", actual) 1332 } 1333 } 1334 1335 func TestContext2Plan_countIndexZero(t *testing.T) { 1336 m := testModule(t, "plan-count-index-zero") 1337 p := testProvider("aws") 1338 p.DiffFn = testDiffFn 1339 ctx := testContext2(t, &ContextOpts{ 1340 Module: m, 1341 Providers: map[string]ResourceProviderFactory{ 1342 "aws": testProviderFuncFixed(p), 1343 }, 1344 }) 1345 1346 plan, err := ctx.Plan() 1347 if err != nil { 1348 t.Fatalf("err: %s", err) 1349 } 1350 1351 actual := strings.TrimSpace(plan.String()) 1352 expected := strings.TrimSpace(testTerraformPlanCountIndexZeroStr) 1353 if actual != expected { 1354 t.Fatalf("bad:\n%s", actual) 1355 } 1356 } 1357 1358 func TestContext2Plan_countVar(t *testing.T) { 1359 m := testModule(t, "plan-count-var") 1360 p := testProvider("aws") 1361 p.DiffFn = testDiffFn 1362 ctx := testContext2(t, &ContextOpts{ 1363 Module: m, 1364 Providers: map[string]ResourceProviderFactory{ 1365 "aws": testProviderFuncFixed(p), 1366 }, 1367 Variables: map[string]interface{}{ 1368 "count": "3", 1369 }, 1370 }) 1371 1372 plan, err := ctx.Plan() 1373 if err != nil { 1374 t.Fatalf("err: %s", err) 1375 } 1376 1377 actual := strings.TrimSpace(plan.String()) 1378 expected := strings.TrimSpace(testTerraformPlanCountVarStr) 1379 if actual != expected { 1380 t.Fatalf("bad:\n%s", actual) 1381 } 1382 } 1383 1384 func TestContext2Plan_countZero(t *testing.T) { 1385 m := testModule(t, "plan-count-zero") 1386 p := testProvider("aws") 1387 p.DiffFn = testDiffFn 1388 ctx := testContext2(t, &ContextOpts{ 1389 Module: m, 1390 Providers: map[string]ResourceProviderFactory{ 1391 "aws": testProviderFuncFixed(p), 1392 }, 1393 }) 1394 1395 plan, err := ctx.Plan() 1396 if err != nil { 1397 t.Fatalf("err: %s", err) 1398 } 1399 1400 actual := strings.TrimSpace(plan.String()) 1401 expected := strings.TrimSpace(testTerraformPlanCountZeroStr) 1402 if actual != expected { 1403 t.Logf("expected:\n%s", expected) 1404 t.Fatalf("bad:\n%s", actual) 1405 } 1406 } 1407 1408 func TestContext2Plan_countOneIndex(t *testing.T) { 1409 m := testModule(t, "plan-count-one-index") 1410 p := testProvider("aws") 1411 p.DiffFn = testDiffFn 1412 ctx := testContext2(t, &ContextOpts{ 1413 Module: m, 1414 Providers: map[string]ResourceProviderFactory{ 1415 "aws": testProviderFuncFixed(p), 1416 }, 1417 }) 1418 1419 plan, err := ctx.Plan() 1420 if err != nil { 1421 t.Fatalf("err: %s", err) 1422 } 1423 1424 actual := strings.TrimSpace(plan.String()) 1425 expected := strings.TrimSpace(testTerraformPlanCountOneIndexStr) 1426 if actual != expected { 1427 t.Fatalf("bad:\n%s", actual) 1428 } 1429 } 1430 1431 func TestContext2Plan_countDecreaseToOne(t *testing.T) { 1432 m := testModule(t, "plan-count-dec") 1433 p := testProvider("aws") 1434 p.DiffFn = testDiffFn 1435 s := &State{ 1436 Modules: []*ModuleState{ 1437 &ModuleState{ 1438 Path: rootModulePath, 1439 Resources: map[string]*ResourceState{ 1440 "aws_instance.foo.0": &ResourceState{ 1441 Type: "aws_instance", 1442 Primary: &InstanceState{ 1443 ID: "bar", 1444 Attributes: map[string]string{ 1445 "foo": "foo", 1446 "type": "aws_instance", 1447 }, 1448 }, 1449 }, 1450 "aws_instance.foo.1": &ResourceState{ 1451 Type: "aws_instance", 1452 Primary: &InstanceState{ 1453 ID: "bar", 1454 }, 1455 }, 1456 "aws_instance.foo.2": &ResourceState{ 1457 Type: "aws_instance", 1458 Primary: &InstanceState{ 1459 ID: "bar", 1460 }, 1461 }, 1462 }, 1463 }, 1464 }, 1465 } 1466 ctx := testContext2(t, &ContextOpts{ 1467 Module: m, 1468 Providers: map[string]ResourceProviderFactory{ 1469 "aws": testProviderFuncFixed(p), 1470 }, 1471 State: s, 1472 }) 1473 1474 plan, err := ctx.Plan() 1475 if err != nil { 1476 t.Fatalf("err: %s", err) 1477 } 1478 1479 actual := strings.TrimSpace(plan.String()) 1480 expected := strings.TrimSpace(testTerraformPlanCountDecreaseStr) 1481 if actual != expected { 1482 t.Fatalf("bad:\n%s", actual) 1483 } 1484 } 1485 1486 func TestContext2Plan_countIncreaseFromNotSet(t *testing.T) { 1487 m := testModule(t, "plan-count-inc") 1488 p := testProvider("aws") 1489 p.DiffFn = testDiffFn 1490 s := &State{ 1491 Modules: []*ModuleState{ 1492 &ModuleState{ 1493 Path: rootModulePath, 1494 Resources: map[string]*ResourceState{ 1495 "aws_instance.foo": &ResourceState{ 1496 Type: "aws_instance", 1497 Primary: &InstanceState{ 1498 ID: "bar", 1499 Attributes: map[string]string{ 1500 "foo": "foo", 1501 "type": "aws_instance", 1502 }, 1503 }, 1504 }, 1505 }, 1506 }, 1507 }, 1508 } 1509 ctx := testContext2(t, &ContextOpts{ 1510 Module: m, 1511 Providers: map[string]ResourceProviderFactory{ 1512 "aws": testProviderFuncFixed(p), 1513 }, 1514 State: s, 1515 }) 1516 1517 plan, err := ctx.Plan() 1518 if err != nil { 1519 t.Fatalf("err: %s", err) 1520 } 1521 1522 actual := strings.TrimSpace(plan.String()) 1523 expected := strings.TrimSpace(testTerraformPlanCountIncreaseStr) 1524 if actual != expected { 1525 t.Fatalf("bad:\n%s", actual) 1526 } 1527 } 1528 1529 func TestContext2Plan_countIncreaseFromOne(t *testing.T) { 1530 m := testModule(t, "plan-count-inc") 1531 p := testProvider("aws") 1532 p.DiffFn = testDiffFn 1533 s := &State{ 1534 Modules: []*ModuleState{ 1535 &ModuleState{ 1536 Path: rootModulePath, 1537 Resources: map[string]*ResourceState{ 1538 "aws_instance.foo.0": &ResourceState{ 1539 Type: "aws_instance", 1540 Primary: &InstanceState{ 1541 ID: "bar", 1542 Attributes: map[string]string{ 1543 "foo": "foo", 1544 "type": "aws_instance", 1545 }, 1546 }, 1547 }, 1548 }, 1549 }, 1550 }, 1551 } 1552 ctx := testContext2(t, &ContextOpts{ 1553 Module: m, 1554 Providers: map[string]ResourceProviderFactory{ 1555 "aws": testProviderFuncFixed(p), 1556 }, 1557 State: s, 1558 }) 1559 1560 plan, err := ctx.Plan() 1561 if err != nil { 1562 t.Fatalf("err: %s", err) 1563 } 1564 1565 actual := strings.TrimSpace(plan.String()) 1566 expected := strings.TrimSpace(testTerraformPlanCountIncreaseFromOneStr) 1567 if actual != expected { 1568 t.Fatalf("bad:\n%s", actual) 1569 } 1570 } 1571 1572 // https://github.com/PeoplePerHour/terraform/pull/11 1573 // 1574 // This tests a case where both a "resource" and "resource.0" are in 1575 // the state file, which apparently is a reasonable backwards compatibility 1576 // concern found in the above 3rd party repo. 1577 func TestContext2Plan_countIncreaseFromOneCorrupted(t *testing.T) { 1578 m := testModule(t, "plan-count-inc") 1579 p := testProvider("aws") 1580 p.DiffFn = testDiffFn 1581 s := &State{ 1582 Modules: []*ModuleState{ 1583 &ModuleState{ 1584 Path: rootModulePath, 1585 Resources: map[string]*ResourceState{ 1586 "aws_instance.foo": &ResourceState{ 1587 Type: "aws_instance", 1588 Primary: &InstanceState{ 1589 ID: "bar", 1590 Attributes: map[string]string{ 1591 "foo": "foo", 1592 "type": "aws_instance", 1593 }, 1594 }, 1595 }, 1596 "aws_instance.foo.0": &ResourceState{ 1597 Type: "aws_instance", 1598 Primary: &InstanceState{ 1599 ID: "bar", 1600 Attributes: map[string]string{ 1601 "foo": "foo", 1602 "type": "aws_instance", 1603 }, 1604 }, 1605 }, 1606 }, 1607 }, 1608 }, 1609 } 1610 ctx := testContext2(t, &ContextOpts{ 1611 Module: m, 1612 Providers: map[string]ResourceProviderFactory{ 1613 "aws": testProviderFuncFixed(p), 1614 }, 1615 State: s, 1616 }) 1617 1618 plan, err := ctx.Plan() 1619 if err != nil { 1620 t.Fatalf("err: %s", err) 1621 } 1622 1623 actual := strings.TrimSpace(plan.String()) 1624 expected := strings.TrimSpace(testTerraformPlanCountIncreaseFromOneCorruptedStr) 1625 if actual != expected { 1626 t.Fatalf("bad:\n%s", actual) 1627 } 1628 } 1629 1630 func TestContext2Plan_destroy(t *testing.T) { 1631 m := testModule(t, "plan-destroy") 1632 p := testProvider("aws") 1633 p.DiffFn = testDiffFn 1634 s := &State{ 1635 Modules: []*ModuleState{ 1636 &ModuleState{ 1637 Path: rootModulePath, 1638 Resources: map[string]*ResourceState{ 1639 "aws_instance.one": &ResourceState{ 1640 Type: "aws_instance", 1641 Primary: &InstanceState{ 1642 ID: "bar", 1643 }, 1644 }, 1645 "aws_instance.two": &ResourceState{ 1646 Type: "aws_instance", 1647 Primary: &InstanceState{ 1648 ID: "baz", 1649 }, 1650 }, 1651 }, 1652 }, 1653 }, 1654 } 1655 ctx := testContext2(t, &ContextOpts{ 1656 Module: m, 1657 Providers: map[string]ResourceProviderFactory{ 1658 "aws": testProviderFuncFixed(p), 1659 }, 1660 State: s, 1661 Destroy: true, 1662 }) 1663 1664 plan, err := ctx.Plan() 1665 if err != nil { 1666 t.Fatalf("err: %s", err) 1667 } 1668 1669 if len(plan.Diff.RootModule().Resources) != 2 { 1670 t.Fatalf("bad: %#v", plan.Diff.RootModule().Resources) 1671 } 1672 1673 actual := strings.TrimSpace(plan.String()) 1674 expected := strings.TrimSpace(testTerraformPlanDestroyStr) 1675 if actual != expected { 1676 t.Fatalf("bad:\n%s", actual) 1677 } 1678 } 1679 1680 func TestContext2Plan_moduleDestroy(t *testing.T) { 1681 m := testModule(t, "plan-module-destroy") 1682 p := testProvider("aws") 1683 p.DiffFn = testDiffFn 1684 s := &State{ 1685 Modules: []*ModuleState{ 1686 &ModuleState{ 1687 Path: rootModulePath, 1688 Resources: map[string]*ResourceState{ 1689 "aws_instance.foo": &ResourceState{ 1690 Type: "aws_instance", 1691 Primary: &InstanceState{ 1692 ID: "bar", 1693 }, 1694 }, 1695 }, 1696 }, 1697 &ModuleState{ 1698 Path: []string{"root", "child"}, 1699 Resources: map[string]*ResourceState{ 1700 "aws_instance.foo": &ResourceState{ 1701 Type: "aws_instance", 1702 Primary: &InstanceState{ 1703 ID: "bar", 1704 }, 1705 }, 1706 }, 1707 }, 1708 }, 1709 } 1710 ctx := testContext2(t, &ContextOpts{ 1711 Module: m, 1712 Providers: map[string]ResourceProviderFactory{ 1713 "aws": testProviderFuncFixed(p), 1714 }, 1715 State: s, 1716 Destroy: true, 1717 }) 1718 1719 plan, err := ctx.Plan() 1720 if err != nil { 1721 t.Fatalf("err: %s", err) 1722 } 1723 1724 actual := strings.TrimSpace(plan.String()) 1725 expected := strings.TrimSpace(testTerraformPlanModuleDestroyStr) 1726 if actual != expected { 1727 t.Fatalf("bad:\n%s\n\nexpected:\n\n%s", actual, expected) 1728 } 1729 } 1730 1731 // GH-1835 1732 func TestContext2Plan_moduleDestroyCycle(t *testing.T) { 1733 m := testModule(t, "plan-module-destroy-gh-1835") 1734 p := testProvider("aws") 1735 p.DiffFn = testDiffFn 1736 s := &State{ 1737 Modules: []*ModuleState{ 1738 &ModuleState{ 1739 Path: []string{"root", "a_module"}, 1740 Resources: map[string]*ResourceState{ 1741 "aws_instance.a": &ResourceState{ 1742 Type: "aws_instance", 1743 Primary: &InstanceState{ 1744 ID: "a", 1745 }, 1746 }, 1747 }, 1748 }, 1749 &ModuleState{ 1750 Path: []string{"root", "b_module"}, 1751 Resources: map[string]*ResourceState{ 1752 "aws_instance.b": &ResourceState{ 1753 Type: "aws_instance", 1754 Primary: &InstanceState{ 1755 ID: "b", 1756 }, 1757 }, 1758 }, 1759 }, 1760 }, 1761 } 1762 ctx := testContext2(t, &ContextOpts{ 1763 Module: m, 1764 Providers: map[string]ResourceProviderFactory{ 1765 "aws": testProviderFuncFixed(p), 1766 }, 1767 State: s, 1768 Destroy: true, 1769 }) 1770 1771 plan, err := ctx.Plan() 1772 if err != nil { 1773 t.Fatalf("err: %s", err) 1774 } 1775 1776 actual := strings.TrimSpace(plan.String()) 1777 expected := strings.TrimSpace(testTerraformPlanModuleDestroyCycleStr) 1778 if actual != expected { 1779 t.Fatalf("bad:\n%s\n\nexpected:\n\n%s", actual, expected) 1780 } 1781 } 1782 1783 func TestContext2Plan_moduleDestroyMultivar(t *testing.T) { 1784 m := testModule(t, "plan-module-destroy-multivar") 1785 p := testProvider("aws") 1786 p.DiffFn = testDiffFn 1787 s := &State{ 1788 Modules: []*ModuleState{ 1789 &ModuleState{ 1790 Path: rootModulePath, 1791 Resources: map[string]*ResourceState{}, 1792 }, 1793 &ModuleState{ 1794 Path: []string{"root", "child"}, 1795 Resources: map[string]*ResourceState{ 1796 "aws_instance.foo.0": &ResourceState{ 1797 Type: "aws_instance", 1798 Primary: &InstanceState{ 1799 ID: "bar0", 1800 }, 1801 }, 1802 "aws_instance.foo.1": &ResourceState{ 1803 Type: "aws_instance", 1804 Primary: &InstanceState{ 1805 ID: "bar1", 1806 }, 1807 }, 1808 }, 1809 }, 1810 }, 1811 } 1812 ctx := testContext2(t, &ContextOpts{ 1813 Module: m, 1814 Providers: map[string]ResourceProviderFactory{ 1815 "aws": testProviderFuncFixed(p), 1816 }, 1817 State: s, 1818 Destroy: true, 1819 }) 1820 1821 plan, err := ctx.Plan() 1822 if err != nil { 1823 t.Fatalf("err: %s", err) 1824 } 1825 1826 actual := strings.TrimSpace(plan.String()) 1827 expected := strings.TrimSpace(testTerraformPlanModuleDestroyMultivarStr) 1828 if actual != expected { 1829 t.Fatalf("bad:\n%s\n\nexpected:\n\n%s", actual, expected) 1830 } 1831 } 1832 1833 func TestContext2Plan_pathVar(t *testing.T) { 1834 cwd, err := os.Getwd() 1835 if err != nil { 1836 t.Fatalf("err: %s", err) 1837 } 1838 1839 m := testModule(t, "plan-path-var") 1840 p := testProvider("aws") 1841 p.DiffFn = testDiffFn 1842 ctx := testContext2(t, &ContextOpts{ 1843 Module: m, 1844 Providers: map[string]ResourceProviderFactory{ 1845 "aws": testProviderFuncFixed(p), 1846 }, 1847 }) 1848 1849 plan, err := ctx.Plan() 1850 if err != nil { 1851 t.Fatalf("err: %s", err) 1852 } 1853 1854 actual := strings.TrimSpace(plan.String()) 1855 expected := strings.TrimSpace(testTerraformPlanPathVarStr) 1856 1857 // Warning: this ordering REALLY matters for this test. The 1858 // order is: cwd, module, root. 1859 expected = fmt.Sprintf( 1860 expected, 1861 cwd, 1862 m.Config().Dir, 1863 m.Config().Dir) 1864 1865 if actual != expected { 1866 t.Fatalf("bad:\n%s\n\nexpected:\n\n%s", actual, expected) 1867 } 1868 } 1869 1870 func TestContext2Plan_diffVar(t *testing.T) { 1871 m := testModule(t, "plan-diffvar") 1872 p := testProvider("aws") 1873 s := &State{ 1874 Modules: []*ModuleState{ 1875 &ModuleState{ 1876 Path: rootModulePath, 1877 Resources: map[string]*ResourceState{ 1878 "aws_instance.foo": &ResourceState{ 1879 Primary: &InstanceState{ 1880 ID: "bar", 1881 Attributes: map[string]string{ 1882 "num": "2", 1883 }, 1884 }, 1885 }, 1886 }, 1887 }, 1888 }, 1889 } 1890 ctx := testContext2(t, &ContextOpts{ 1891 Module: m, 1892 Providers: map[string]ResourceProviderFactory{ 1893 "aws": testProviderFuncFixed(p), 1894 }, 1895 State: s, 1896 }) 1897 1898 p.DiffFn = func( 1899 info *InstanceInfo, 1900 s *InstanceState, 1901 c *ResourceConfig) (*InstanceDiff, error) { 1902 if s.ID != "bar" { 1903 return testDiffFn(info, s, c) 1904 } 1905 1906 return &InstanceDiff{ 1907 Attributes: map[string]*ResourceAttrDiff{ 1908 "num": &ResourceAttrDiff{ 1909 Old: "2", 1910 New: "3", 1911 }, 1912 }, 1913 }, nil 1914 } 1915 1916 plan, err := ctx.Plan() 1917 if err != nil { 1918 t.Fatalf("err: %s", err) 1919 } 1920 1921 actual := strings.TrimSpace(plan.String()) 1922 expected := strings.TrimSpace(testTerraformPlanDiffVarStr) 1923 if actual != expected { 1924 t.Fatalf("actual:\n%s\n\nexpected:\n%s", actual, expected) 1925 } 1926 } 1927 1928 func TestContext2Plan_hook(t *testing.T) { 1929 m := testModule(t, "plan-good") 1930 h := new(MockHook) 1931 p := testProvider("aws") 1932 p.DiffFn = testDiffFn 1933 ctx := testContext2(t, &ContextOpts{ 1934 Module: m, 1935 Hooks: []Hook{h}, 1936 Providers: map[string]ResourceProviderFactory{ 1937 "aws": testProviderFuncFixed(p), 1938 }, 1939 }) 1940 1941 _, err := ctx.Plan() 1942 if err != nil { 1943 t.Fatalf("err: %s", err) 1944 } 1945 1946 if !h.PreDiffCalled { 1947 t.Fatal("should be called") 1948 } 1949 if !h.PostDiffCalled { 1950 t.Fatal("should be called") 1951 } 1952 } 1953 1954 func TestContext2Plan_orphan(t *testing.T) { 1955 m := testModule(t, "plan-orphan") 1956 p := testProvider("aws") 1957 p.DiffFn = testDiffFn 1958 s := &State{ 1959 Modules: []*ModuleState{ 1960 &ModuleState{ 1961 Path: rootModulePath, 1962 Resources: map[string]*ResourceState{ 1963 "aws_instance.baz": &ResourceState{ 1964 Type: "aws_instance", 1965 Primary: &InstanceState{ 1966 ID: "bar", 1967 }, 1968 }, 1969 }, 1970 }, 1971 }, 1972 } 1973 ctx := testContext2(t, &ContextOpts{ 1974 Module: m, 1975 Providers: map[string]ResourceProviderFactory{ 1976 "aws": testProviderFuncFixed(p), 1977 }, 1978 State: s, 1979 }) 1980 1981 plan, err := ctx.Plan() 1982 if err != nil { 1983 t.Fatalf("err: %s", err) 1984 } 1985 1986 actual := strings.TrimSpace(plan.String()) 1987 expected := strings.TrimSpace(testTerraformPlanOrphanStr) 1988 if actual != expected { 1989 t.Fatalf("bad:\n%s", actual) 1990 } 1991 } 1992 1993 func TestContext2Plan_state(t *testing.T) { 1994 m := testModule(t, "plan-good") 1995 p := testProvider("aws") 1996 p.DiffFn = testDiffFn 1997 s := &State{ 1998 Modules: []*ModuleState{ 1999 &ModuleState{ 2000 Path: rootModulePath, 2001 Resources: map[string]*ResourceState{ 2002 "aws_instance.foo": &ResourceState{ 2003 Primary: &InstanceState{ 2004 ID: "bar", 2005 }, 2006 }, 2007 }, 2008 }, 2009 }, 2010 } 2011 ctx := testContext2(t, &ContextOpts{ 2012 Module: m, 2013 Providers: map[string]ResourceProviderFactory{ 2014 "aws": testProviderFuncFixed(p), 2015 }, 2016 State: s, 2017 }) 2018 2019 plan, err := ctx.Plan() 2020 if err != nil { 2021 t.Fatalf("err: %s", err) 2022 } 2023 2024 if len(plan.Diff.RootModule().Resources) < 2 { 2025 t.Fatalf("bad: %#v", plan.Diff.RootModule().Resources) 2026 } 2027 2028 actual := strings.TrimSpace(plan.String()) 2029 expected := strings.TrimSpace(testTerraformPlanStateStr) 2030 if actual != expected { 2031 t.Fatalf("bad:\n%s\n\nexpected:\n\n%s", actual, expected) 2032 } 2033 } 2034 2035 func TestContext2Plan_taint(t *testing.T) { 2036 m := testModule(t, "plan-taint") 2037 p := testProvider("aws") 2038 p.DiffFn = testDiffFn 2039 s := &State{ 2040 Modules: []*ModuleState{ 2041 &ModuleState{ 2042 Path: rootModulePath, 2043 Resources: map[string]*ResourceState{ 2044 "aws_instance.foo": &ResourceState{ 2045 Type: "aws_instance", 2046 Primary: &InstanceState{ 2047 ID: "bar", 2048 Attributes: map[string]string{"num": "2"}, 2049 }, 2050 }, 2051 "aws_instance.bar": &ResourceState{ 2052 Type: "aws_instance", 2053 Primary: &InstanceState{ 2054 ID: "baz", 2055 Tainted: true, 2056 }, 2057 }, 2058 }, 2059 }, 2060 }, 2061 } 2062 ctx := testContext2(t, &ContextOpts{ 2063 Module: m, 2064 Providers: map[string]ResourceProviderFactory{ 2065 "aws": testProviderFuncFixed(p), 2066 }, 2067 State: s, 2068 }) 2069 2070 plan, err := ctx.Plan() 2071 if err != nil { 2072 t.Fatalf("err: %s", err) 2073 } 2074 2075 actual := strings.TrimSpace(plan.String()) 2076 expected := strings.TrimSpace(testTerraformPlanTaintStr) 2077 if actual != expected { 2078 t.Fatalf("bad:\n%s", actual) 2079 } 2080 } 2081 2082 func TestContext2Apply_taintIgnoreChanges(t *testing.T) { 2083 m := testModule(t, "plan-taint-ignore-changes") 2084 p := testProvider("aws") 2085 p.ApplyFn = testApplyFn 2086 p.DiffFn = testDiffFn 2087 s := &State{ 2088 Modules: []*ModuleState{ 2089 &ModuleState{ 2090 Path: rootModulePath, 2091 Resources: map[string]*ResourceState{ 2092 "aws_instance.foo": &ResourceState{ 2093 Type: "aws_instance", 2094 Primary: &InstanceState{ 2095 ID: "foo", 2096 Attributes: map[string]string{ 2097 "vars": "foo", 2098 "type": "aws_instance", 2099 }, 2100 Tainted: true, 2101 }, 2102 }, 2103 }, 2104 }, 2105 }, 2106 } 2107 ctx := testContext2(t, &ContextOpts{ 2108 Module: m, 2109 Providers: map[string]ResourceProviderFactory{ 2110 "aws": testProviderFuncFixed(p), 2111 }, 2112 State: s, 2113 }) 2114 2115 plan, err := ctx.Plan() 2116 if err != nil { 2117 t.Fatalf("err: %s", err) 2118 } 2119 2120 actual := strings.TrimSpace(plan.String()) 2121 expected := strings.TrimSpace(testTerraformPlanTaintIgnoreChangesStr) 2122 if actual != expected { 2123 t.Fatalf("bad:\n%s", actual) 2124 } 2125 } 2126 2127 // Fails about 50% of the time before the fix for GH-4982, covers the fix. 2128 func TestContext2Plan_taintDestroyInterpolatedCountRace(t *testing.T) { 2129 m := testModule(t, "plan-taint-interpolated-count") 2130 p := testProvider("aws") 2131 p.DiffFn = testDiffFn 2132 s := &State{ 2133 Modules: []*ModuleState{ 2134 &ModuleState{ 2135 Path: rootModulePath, 2136 Resources: map[string]*ResourceState{ 2137 "aws_instance.foo.0": &ResourceState{ 2138 Type: "aws_instance", 2139 Primary: &InstanceState{ 2140 ID: "bar", 2141 Tainted: true, 2142 }, 2143 }, 2144 "aws_instance.foo.1": &ResourceState{ 2145 Type: "aws_instance", 2146 Primary: &InstanceState{ID: "bar"}, 2147 }, 2148 "aws_instance.foo.2": &ResourceState{ 2149 Type: "aws_instance", 2150 Primary: &InstanceState{ID: "bar"}, 2151 }, 2152 }, 2153 }, 2154 }, 2155 } 2156 ctx := testContext2(t, &ContextOpts{ 2157 Module: m, 2158 Providers: map[string]ResourceProviderFactory{ 2159 "aws": testProviderFuncFixed(p), 2160 }, 2161 State: s, 2162 }) 2163 2164 for i := 0; i < 100; i++ { 2165 plan, err := ctx.Plan() 2166 if err != nil { 2167 t.Fatalf("err: %s", err) 2168 } 2169 2170 actual := strings.TrimSpace(plan.String()) 2171 expected := strings.TrimSpace(` 2172 DIFF: 2173 2174 DESTROY/CREATE: aws_instance.foo.0 2175 type: "" => "aws_instance" 2176 2177 STATE: 2178 2179 aws_instance.foo.0: (tainted) 2180 ID = bar 2181 aws_instance.foo.1: 2182 ID = bar 2183 aws_instance.foo.2: 2184 ID = bar 2185 `) 2186 if actual != expected { 2187 t.Fatalf("[%d] bad:\n%s\nexpected:\n%s\n", i, actual, expected) 2188 } 2189 } 2190 } 2191 2192 func TestContext2Plan_targeted(t *testing.T) { 2193 m := testModule(t, "plan-targeted") 2194 p := testProvider("aws") 2195 p.DiffFn = testDiffFn 2196 ctx := testContext2(t, &ContextOpts{ 2197 Module: m, 2198 Providers: map[string]ResourceProviderFactory{ 2199 "aws": testProviderFuncFixed(p), 2200 }, 2201 Targets: []string{"aws_instance.foo"}, 2202 }) 2203 2204 plan, err := ctx.Plan() 2205 if err != nil { 2206 t.Fatalf("err: %s", err) 2207 } 2208 2209 actual := strings.TrimSpace(plan.String()) 2210 expected := strings.TrimSpace(` 2211 DIFF: 2212 2213 CREATE: aws_instance.foo 2214 num: "" => "2" 2215 type: "" => "aws_instance" 2216 2217 STATE: 2218 2219 <no state> 2220 `) 2221 if actual != expected { 2222 t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actual) 2223 } 2224 } 2225 2226 func TestContext2Plan_targetedOrphan(t *testing.T) { 2227 m := testModule(t, "plan-targeted-orphan") 2228 p := testProvider("aws") 2229 p.DiffFn = testDiffFn 2230 ctx := testContext2(t, &ContextOpts{ 2231 Module: m, 2232 Providers: map[string]ResourceProviderFactory{ 2233 "aws": testProviderFuncFixed(p), 2234 }, 2235 State: &State{ 2236 Modules: []*ModuleState{ 2237 &ModuleState{ 2238 Path: rootModulePath, 2239 Resources: map[string]*ResourceState{ 2240 "aws_instance.orphan": &ResourceState{ 2241 Type: "aws_instance", 2242 Primary: &InstanceState{ 2243 ID: "i-789xyz", 2244 }, 2245 }, 2246 "aws_instance.nottargeted": &ResourceState{ 2247 Type: "aws_instance", 2248 Primary: &InstanceState{ 2249 ID: "i-abc123", 2250 }, 2251 }, 2252 }, 2253 }, 2254 }, 2255 }, 2256 Destroy: true, 2257 Targets: []string{"aws_instance.orphan"}, 2258 }) 2259 2260 plan, err := ctx.Plan() 2261 if err != nil { 2262 t.Fatalf("err: %s", err) 2263 } 2264 2265 actual := strings.TrimSpace(plan.String()) 2266 expected := strings.TrimSpace(`DIFF: 2267 2268 DESTROY: aws_instance.orphan 2269 2270 STATE: 2271 2272 aws_instance.nottargeted: 2273 ID = i-abc123 2274 aws_instance.orphan: 2275 ID = i-789xyz 2276 `) 2277 if actual != expected { 2278 t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actual) 2279 } 2280 } 2281 2282 // https://github.com/hashicorp/terraform/issues/2538 2283 func TestContext2Plan_targetedModuleOrphan(t *testing.T) { 2284 m := testModule(t, "plan-targeted-module-orphan") 2285 p := testProvider("aws") 2286 p.DiffFn = testDiffFn 2287 ctx := testContext2(t, &ContextOpts{ 2288 Module: m, 2289 Providers: map[string]ResourceProviderFactory{ 2290 "aws": testProviderFuncFixed(p), 2291 }, 2292 State: &State{ 2293 Modules: []*ModuleState{ 2294 &ModuleState{ 2295 Path: []string{"root", "child"}, 2296 Resources: map[string]*ResourceState{ 2297 "aws_instance.orphan": &ResourceState{ 2298 Type: "aws_instance", 2299 Primary: &InstanceState{ 2300 ID: "i-789xyz", 2301 }, 2302 }, 2303 "aws_instance.nottargeted": &ResourceState{ 2304 Type: "aws_instance", 2305 Primary: &InstanceState{ 2306 ID: "i-abc123", 2307 }, 2308 }, 2309 }, 2310 }, 2311 }, 2312 }, 2313 Destroy: true, 2314 Targets: []string{"module.child.aws_instance.orphan"}, 2315 }) 2316 2317 plan, err := ctx.Plan() 2318 if err != nil { 2319 t.Fatalf("err: %s", err) 2320 } 2321 2322 actual := strings.TrimSpace(plan.String()) 2323 expected := strings.TrimSpace(`DIFF: 2324 2325 module.child: 2326 DESTROY: aws_instance.orphan 2327 2328 STATE: 2329 2330 module.child: 2331 aws_instance.nottargeted: 2332 ID = i-abc123 2333 aws_instance.orphan: 2334 ID = i-789xyz 2335 `) 2336 if actual != expected { 2337 t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actual) 2338 } 2339 } 2340 2341 func TestContext2Plan_targetedModuleUntargetedVariable(t *testing.T) { 2342 m := testModule(t, "plan-targeted-module-untargeted-variable") 2343 p := testProvider("aws") 2344 p.DiffFn = testDiffFn 2345 ctx := testContext2(t, &ContextOpts{ 2346 Module: m, 2347 Providers: map[string]ResourceProviderFactory{ 2348 "aws": testProviderFuncFixed(p), 2349 }, 2350 Targets: []string{"aws_instance.blue", "module.blue_mod"}, 2351 }) 2352 2353 plan, err := ctx.Plan() 2354 if err != nil { 2355 t.Fatalf("err: %s", err) 2356 } 2357 2358 actual := strings.TrimSpace(plan.String()) 2359 expected := strings.TrimSpace(` 2360 DIFF: 2361 2362 CREATE: aws_instance.blue 2363 2364 module.blue_mod: 2365 CREATE: aws_instance.mod 2366 type: "" => "aws_instance" 2367 value: "" => "<computed>" 2368 2369 STATE: 2370 2371 <no state> 2372 `) 2373 if actual != expected { 2374 t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actual) 2375 } 2376 } 2377 2378 // https://github.com/hashicorp/terraform/issues/4515 2379 func TestContext2Plan_targetedOverTen(t *testing.T) { 2380 m := testModule(t, "plan-targeted-over-ten") 2381 p := testProvider("aws") 2382 p.DiffFn = testDiffFn 2383 2384 resources := make(map[string]*ResourceState) 2385 var expectedState []string 2386 for i := 0; i < 13; i++ { 2387 key := fmt.Sprintf("aws_instance.foo.%d", i) 2388 id := fmt.Sprintf("i-abc%d", i) 2389 resources[key] = &ResourceState{ 2390 Type: "aws_instance", 2391 Primary: &InstanceState{ID: id}, 2392 } 2393 expectedState = append(expectedState, 2394 fmt.Sprintf("%s:\n ID = %s\n", key, id)) 2395 } 2396 ctx := testContext2(t, &ContextOpts{ 2397 Module: m, 2398 Providers: map[string]ResourceProviderFactory{ 2399 "aws": testProviderFuncFixed(p), 2400 }, 2401 State: &State{ 2402 Modules: []*ModuleState{ 2403 &ModuleState{ 2404 Path: rootModulePath, 2405 Resources: resources, 2406 }, 2407 }, 2408 }, 2409 Targets: []string{"aws_instance.foo[1]"}, 2410 }) 2411 2412 plan, err := ctx.Plan() 2413 if err != nil { 2414 t.Fatalf("err: %s", err) 2415 } 2416 2417 actual := strings.TrimSpace(plan.String()) 2418 sort.Strings(expectedState) 2419 expected := strings.TrimSpace(` 2420 DIFF: 2421 2422 2423 2424 STATE: 2425 2426 aws_instance.foo.0: 2427 ID = i-abc0 2428 aws_instance.foo.1: 2429 ID = i-abc1 2430 aws_instance.foo.10: 2431 ID = i-abc10 2432 aws_instance.foo.11: 2433 ID = i-abc11 2434 aws_instance.foo.12: 2435 ID = i-abc12 2436 aws_instance.foo.2: 2437 ID = i-abc2 2438 aws_instance.foo.3: 2439 ID = i-abc3 2440 aws_instance.foo.4: 2441 ID = i-abc4 2442 aws_instance.foo.5: 2443 ID = i-abc5 2444 aws_instance.foo.6: 2445 ID = i-abc6 2446 aws_instance.foo.7: 2447 ID = i-abc7 2448 aws_instance.foo.8: 2449 ID = i-abc8 2450 aws_instance.foo.9: 2451 ID = i-abc9 2452 `) 2453 if actual != expected { 2454 t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actual) 2455 } 2456 } 2457 2458 func TestContext2Plan_provider(t *testing.T) { 2459 m := testModule(t, "plan-provider") 2460 p := testProvider("aws") 2461 p.DiffFn = testDiffFn 2462 2463 var value interface{} 2464 p.ConfigureFn = func(c *ResourceConfig) error { 2465 value, _ = c.Get("foo") 2466 return nil 2467 } 2468 2469 ctx := testContext2(t, &ContextOpts{ 2470 Module: m, 2471 Providers: map[string]ResourceProviderFactory{ 2472 "aws": testProviderFuncFixed(p), 2473 }, 2474 Variables: map[string]interface{}{ 2475 "foo": "bar", 2476 }, 2477 }) 2478 2479 if _, err := ctx.Plan(); err != nil { 2480 t.Fatalf("err: %s", err) 2481 } 2482 2483 if value != "bar" { 2484 t.Fatalf("bad: %#v", value) 2485 } 2486 } 2487 2488 func TestContext2Plan_varListErr(t *testing.T) { 2489 m := testModule(t, "plan-var-list-err") 2490 p := testProvider("aws") 2491 ctx := testContext2(t, &ContextOpts{ 2492 Module: m, 2493 Providers: map[string]ResourceProviderFactory{ 2494 "aws": testProviderFuncFixed(p), 2495 }, 2496 }) 2497 2498 _, err := ctx.Plan() 2499 2500 if err == nil { 2501 t.Fatal("should error") 2502 } 2503 } 2504 2505 func TestContext2Plan_ignoreChanges(t *testing.T) { 2506 m := testModule(t, "plan-ignore-changes") 2507 p := testProvider("aws") 2508 p.DiffFn = testDiffFn 2509 s := &State{ 2510 Modules: []*ModuleState{ 2511 &ModuleState{ 2512 Path: rootModulePath, 2513 Resources: map[string]*ResourceState{ 2514 "aws_instance.foo": &ResourceState{ 2515 Primary: &InstanceState{ 2516 ID: "bar", 2517 Attributes: map[string]string{"ami": "ami-abcd1234"}, 2518 }, 2519 }, 2520 }, 2521 }, 2522 }, 2523 } 2524 ctx := testContext2(t, &ContextOpts{ 2525 Module: m, 2526 Providers: map[string]ResourceProviderFactory{ 2527 "aws": testProviderFuncFixed(p), 2528 }, 2529 Variables: map[string]interface{}{ 2530 "foo": "ami-1234abcd", 2531 }, 2532 State: s, 2533 }) 2534 2535 plan, err := ctx.Plan() 2536 if err != nil { 2537 t.Fatalf("err: %s", err) 2538 } 2539 2540 if len(plan.Diff.RootModule().Resources) < 1 { 2541 t.Fatalf("bad: %#v", plan.Diff.RootModule().Resources) 2542 } 2543 2544 actual := strings.TrimSpace(plan.String()) 2545 expected := strings.TrimSpace(testTerraformPlanIgnoreChangesStr) 2546 if actual != expected { 2547 t.Fatalf("bad:\n%s\n\nexpected\n\n%s", actual, expected) 2548 } 2549 } 2550 2551 func TestContext2Plan_ignoreChangesWildcard(t *testing.T) { 2552 m := testModule(t, "plan-ignore-changes-wildcard") 2553 p := testProvider("aws") 2554 p.DiffFn = testDiffFn 2555 s := &State{ 2556 Modules: []*ModuleState{ 2557 &ModuleState{ 2558 Path: rootModulePath, 2559 Resources: map[string]*ResourceState{ 2560 "aws_instance.foo": &ResourceState{ 2561 Primary: &InstanceState{ 2562 ID: "bar", 2563 Attributes: map[string]string{ 2564 "ami": "ami-abcd1234", 2565 "instance_type": "t2.micro", 2566 }, 2567 }, 2568 }, 2569 }, 2570 }, 2571 }, 2572 } 2573 ctx := testContext2(t, &ContextOpts{ 2574 Module: m, 2575 Providers: map[string]ResourceProviderFactory{ 2576 "aws": testProviderFuncFixed(p), 2577 }, 2578 Variables: map[string]interface{}{ 2579 "foo": "ami-1234abcd", 2580 "bar": "t2.small", 2581 }, 2582 State: s, 2583 }) 2584 2585 plan, err := ctx.Plan() 2586 if err != nil { 2587 t.Fatalf("err: %s", err) 2588 } 2589 2590 if len(plan.Diff.RootModule().Resources) > 0 { 2591 t.Fatalf("bad: %#v", plan.Diff.RootModule().Resources) 2592 } 2593 2594 actual := strings.TrimSpace(plan.String()) 2595 expected := strings.TrimSpace(testTerraformPlanIgnoreChangesWildcardStr) 2596 if actual != expected { 2597 t.Fatalf("bad:\n%s\n\nexpected\n\n%s", actual, expected) 2598 } 2599 } 2600 2601 func TestContext2Plan_moduleMapLiteral(t *testing.T) { 2602 m := testModule(t, "plan-module-map-literal") 2603 p := testProvider("aws") 2604 p.ApplyFn = testApplyFn 2605 p.DiffFn = func(i *InstanceInfo, s *InstanceState, c *ResourceConfig) (*InstanceDiff, error) { 2606 // Here we verify that both the populated and empty map literals made it 2607 // through to the resource attributes 2608 val, _ := c.Get("tags") 2609 m, ok := val.(map[string]interface{}) 2610 if !ok { 2611 t.Fatalf("Tags attr not map: %#v", val) 2612 } 2613 if m["foo"] != "bar" { 2614 t.Fatalf("Bad value in tags attr: %#v", m) 2615 } 2616 { 2617 val, _ := c.Get("meta") 2618 m, ok := val.(map[string]interface{}) 2619 if !ok { 2620 t.Fatalf("Meta attr not map: %#v", val) 2621 } 2622 if len(m) != 0 { 2623 t.Fatalf("Meta attr not empty: %#v", val) 2624 } 2625 } 2626 return nil, nil 2627 } 2628 ctx := testContext2(t, &ContextOpts{ 2629 Module: m, 2630 Providers: map[string]ResourceProviderFactory{ 2631 "aws": testProviderFuncFixed(p), 2632 }, 2633 }) 2634 2635 if _, err := ctx.Plan(); err != nil { 2636 t.Fatalf("err: %s", err) 2637 } 2638 } 2639 2640 func TestContext2Plan_computedValueInMap(t *testing.T) { 2641 m := testModule(t, "plan-computed-value-in-map") 2642 p := testProvider("aws") 2643 p.DiffFn = func(info *InstanceInfo, state *InstanceState, c *ResourceConfig) (*InstanceDiff, error) { 2644 switch info.Type { 2645 case "aws_computed_source": 2646 return &InstanceDiff{ 2647 Attributes: map[string]*ResourceAttrDiff{ 2648 "computed_read_only": &ResourceAttrDiff{ 2649 NewComputed: true, 2650 }, 2651 }, 2652 }, nil 2653 } 2654 2655 return testDiffFn(info, state, c) 2656 } 2657 ctx := testContext2(t, &ContextOpts{ 2658 Module: m, 2659 Providers: map[string]ResourceProviderFactory{ 2660 "aws": testProviderFuncFixed(p), 2661 }, 2662 }) 2663 2664 if _, err := ctx.Plan(); err != nil { 2665 t.Fatalf("err: %s", err) 2666 } 2667 2668 plan, err := ctx.Plan() 2669 if err != nil { 2670 t.Fatalf("err: %s", err) 2671 } 2672 2673 actual := strings.TrimSpace(plan.String()) 2674 expected := strings.TrimSpace(testTerraformPlanComputedValueInMap) 2675 if actual != expected { 2676 t.Fatalf("bad:\n%s\n\nexpected\n\n%s", actual, expected) 2677 } 2678 } 2679 2680 func TestContext2Plan_moduleVariableFromSplat(t *testing.T) { 2681 m := testModule(t, "plan-module-variable-from-splat") 2682 p := testProvider("aws") 2683 p.DiffFn = testDiffFn 2684 ctx := testContext2(t, &ContextOpts{ 2685 Module: m, 2686 Providers: map[string]ResourceProviderFactory{ 2687 "aws": testProviderFuncFixed(p), 2688 }, 2689 }) 2690 2691 if _, err := ctx.Plan(); err != nil { 2692 t.Fatalf("err: %s", err) 2693 } 2694 2695 plan, err := ctx.Plan() 2696 if err != nil { 2697 t.Fatalf("err: %s", err) 2698 } 2699 2700 actual := strings.TrimSpace(plan.String()) 2701 expected := strings.TrimSpace(testTerraformPlanModuleVariableFromSplat) 2702 if actual != expected { 2703 t.Fatalf("bad:\n%s\n\nexpected\n\n%s", actual, expected) 2704 } 2705 } 2706 2707 func TestContext2Plan_createBeforeDestroy_depends_datasource(t *testing.T) { 2708 m := testModule(t, "plan-cdb-depends-datasource") 2709 p := testProvider("aws") 2710 p.DiffFn = testDiffFn 2711 ctx := testContext2(t, &ContextOpts{ 2712 Module: m, 2713 Providers: map[string]ResourceProviderFactory{ 2714 "aws": testProviderFuncFixed(p), 2715 }, 2716 }) 2717 2718 plan, err := ctx.Plan() 2719 if err != nil { 2720 t.Fatalf("err: %s", err) 2721 } 2722 2723 if got := len(plan.Diff.Modules); got != 1 { 2724 t.Fatalf("got %d modules; want 1", got) 2725 } 2726 2727 moduleDiff := plan.Diff.Modules[0] 2728 2729 if _, ok := moduleDiff.Resources["aws_instance.foo.0"]; !ok { 2730 t.Fatalf("missing diff for aws_instance.foo.0") 2731 } 2732 if _, ok := moduleDiff.Resources["aws_instance.foo.1"]; !ok { 2733 t.Fatalf("missing diff for aws_instance.foo.1") 2734 } 2735 if _, ok := moduleDiff.Resources["data.aws_vpc.bar.0"]; !ok { 2736 t.Fatalf("missing diff for data.aws_vpc.bar.0") 2737 } 2738 if _, ok := moduleDiff.Resources["data.aws_vpc.bar.1"]; !ok { 2739 t.Fatalf("missing diff for data.aws_vpc.bar.1") 2740 } 2741 }