github.com/trawler/terraform@v0.10.8-0.20171106022149-4b1c7a1d9b48/terraform/context_validate_test.go (about) 1 package terraform 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 ) 8 9 func TestContext2Validate_badCount(t *testing.T) { 10 p := testProvider("aws") 11 m := testModule(t, "validate-bad-count") 12 c := testContext2(t, &ContextOpts{ 13 Module: m, 14 ProviderResolver: ResourceProviderResolverFixed( 15 map[string]ResourceProviderFactory{ 16 "aws": testProviderFuncFixed(p), 17 }, 18 ), 19 }) 20 21 w, e := c.Validate() 22 if len(w) > 0 { 23 t.Fatalf("bad: %#v", w) 24 } 25 if len(e) == 0 { 26 t.Fatalf("bad: %#v", e) 27 } 28 } 29 30 func TestContext2Validate_badVar(t *testing.T) { 31 p := testProvider("aws") 32 m := testModule(t, "validate-bad-var") 33 c := testContext2(t, &ContextOpts{ 34 Module: m, 35 ProviderResolver: ResourceProviderResolverFixed( 36 map[string]ResourceProviderFactory{ 37 "aws": testProviderFuncFixed(p), 38 }, 39 ), 40 }) 41 42 w, e := c.Validate() 43 if len(w) > 0 { 44 t.Fatalf("bad: %#v", w) 45 } 46 if len(e) == 0 { 47 t.Fatalf("bad: %#v", e) 48 } 49 } 50 51 func TestContext2Validate_varMapOverrideOld(t *testing.T) { 52 m := testModule(t, "validate-module-pc-vars") 53 p := testProvider("aws") 54 c := testContext2(t, &ContextOpts{ 55 Module: m, 56 ProviderResolver: ResourceProviderResolverFixed( 57 map[string]ResourceProviderFactory{ 58 "aws": testProviderFuncFixed(p), 59 }, 60 ), 61 Variables: map[string]interface{}{ 62 "foo.foo": "bar", 63 }, 64 }) 65 66 w, e := c.Validate() 67 if len(w) > 0 { 68 t.Fatalf("bad: %#v", w) 69 } 70 if len(e) == 0 { 71 t.Fatalf("bad: %s", e) 72 } 73 } 74 75 func TestContext2Validate_varNoDefaultExplicitType(t *testing.T) { 76 m := testModule(t, "validate-var-no-default-explicit-type") 77 c := testContext2(t, &ContextOpts{ 78 Module: m, 79 }) 80 81 w, e := c.Validate() 82 if len(w) > 0 { 83 t.Fatalf("bad: %#v", w) 84 } 85 if len(e) == 0 { 86 t.Fatalf("bad: %#v", e) 87 } 88 } 89 90 func TestContext2Validate_computedVar(t *testing.T) { 91 p := testProvider("aws") 92 m := testModule(t, "validate-computed-var") 93 c := testContext2(t, &ContextOpts{ 94 Module: m, 95 ProviderResolver: ResourceProviderResolverFixed( 96 map[string]ResourceProviderFactory{ 97 "aws": testProviderFuncFixed(p), 98 "test": testProviderFuncFixed(testProvider("test")), 99 }, 100 ), 101 }) 102 103 p.ValidateFn = func(c *ResourceConfig) ([]string, []error) { 104 if !c.IsComputed("value") { 105 return nil, []error{fmt.Errorf("value isn't computed")} 106 } 107 108 return nil, c.CheckSet([]string{"value"}) 109 } 110 111 p.ConfigureFn = func(c *ResourceConfig) error { 112 return fmt.Errorf("Configure should not be called for provider") 113 } 114 115 w, e := c.Validate() 116 if len(w) > 0 { 117 t.Fatalf("bad: %#v", w) 118 } 119 if len(e) > 0 { 120 for _, err := range e { 121 t.Errorf("bad: %s", err) 122 } 123 } 124 } 125 126 // Test that validate allows through computed counts. We do this and allow 127 // them to fail during "plan" since we can't know if the computed values 128 // can be realized during a plan. 129 func TestContext2Validate_countComputed(t *testing.T) { 130 p := testProvider("aws") 131 m := testModule(t, "validate-count-computed") 132 c := testContext2(t, &ContextOpts{ 133 Module: m, 134 ProviderResolver: ResourceProviderResolverFixed( 135 map[string]ResourceProviderFactory{ 136 "aws": testProviderFuncFixed(p), 137 }, 138 ), 139 }) 140 141 w, e := c.Validate() 142 if len(w) > 0 { 143 t.Fatalf("bad: %#v", w) 144 } 145 if len(e) > 0 { 146 t.Fatalf("bad: %s", e) 147 } 148 } 149 150 func TestContext2Validate_countNegative(t *testing.T) { 151 p := testProvider("aws") 152 m := testModule(t, "validate-count-negative") 153 c := testContext2(t, &ContextOpts{ 154 Module: m, 155 ProviderResolver: ResourceProviderResolverFixed( 156 map[string]ResourceProviderFactory{ 157 "aws": testProviderFuncFixed(p), 158 }, 159 ), 160 }) 161 162 w, e := c.Validate() 163 if len(w) > 0 { 164 t.Fatalf("bad: %#v", w) 165 } 166 if len(e) == 0 { 167 t.Fatalf("bad: %#v", e) 168 } 169 } 170 171 func TestContext2Validate_countVariable(t *testing.T) { 172 p := testProvider("aws") 173 m := testModule(t, "apply-count-variable") 174 c := testContext2(t, &ContextOpts{ 175 Module: m, 176 ProviderResolver: ResourceProviderResolverFixed( 177 map[string]ResourceProviderFactory{ 178 "aws": testProviderFuncFixed(p), 179 }, 180 ), 181 }) 182 183 w, e := c.Validate() 184 if len(w) > 0 { 185 t.Fatalf("bad: %#v", w) 186 } 187 if len(e) > 0 { 188 t.Fatalf("bad: %s", e) 189 } 190 } 191 192 func TestContext2Validate_countVariableNoDefault(t *testing.T) { 193 p := testProvider("aws") 194 m := testModule(t, "validate-count-variable") 195 c := testContext2(t, &ContextOpts{ 196 Module: m, 197 ProviderResolver: ResourceProviderResolverFixed( 198 map[string]ResourceProviderFactory{ 199 "aws": testProviderFuncFixed(p), 200 }, 201 ), 202 }) 203 204 w, e := c.Validate() 205 if len(w) > 0 { 206 t.Fatalf("bad: %#v", w) 207 } 208 if len(e) != 1 { 209 t.Fatalf("bad: %s", e) 210 } 211 } 212 213 /* 214 TODO: What should we do here? 215 func TestContext2Validate_cycle(t *testing.T) { 216 p := testProvider("aws") 217 m := testModule(t, "validate-cycle") 218 c := testContext2(t, &ContextOpts{ 219 Module: m, 220 ProviderResolver: ResourceProviderResolverFixed( 221 map[string]ResourceProviderFactory{ 222 "aws": testProviderFuncFixed(p), 223 }, 224 ), 225 }) 226 227 w, e := c.Validate() 228 if len(w) > 0 { 229 t.Fatalf("expected no warns, got: %#v", w) 230 } 231 if len(e) != 1 { 232 t.Fatalf("expected 1 err, got: %s", e) 233 } 234 } 235 */ 236 237 func TestContext2Validate_moduleBadOutput(t *testing.T) { 238 p := testProvider("aws") 239 m := testModule(t, "validate-bad-module-output") 240 c := testContext2(t, &ContextOpts{ 241 Module: m, 242 ProviderResolver: ResourceProviderResolverFixed( 243 map[string]ResourceProviderFactory{ 244 "aws": testProviderFuncFixed(p), 245 }, 246 ), 247 }) 248 249 w, e := c.Validate() 250 if len(w) > 0 { 251 t.Fatalf("bad: %#v", w) 252 } 253 if len(e) == 0 { 254 t.Fatalf("bad: %s", e) 255 } 256 } 257 258 func TestContext2Validate_moduleGood(t *testing.T) { 259 p := testProvider("aws") 260 m := testModule(t, "validate-good-module") 261 c := testContext2(t, &ContextOpts{ 262 Module: m, 263 ProviderResolver: ResourceProviderResolverFixed( 264 map[string]ResourceProviderFactory{ 265 "aws": testProviderFuncFixed(p), 266 }, 267 ), 268 }) 269 270 w, e := c.Validate() 271 if len(w) > 0 { 272 t.Fatalf("bad: %#v", w) 273 } 274 if len(e) > 0 { 275 t.Fatalf("bad: %#v", e) 276 } 277 } 278 279 func TestContext2Validate_moduleBadResource(t *testing.T) { 280 m := testModule(t, "validate-module-bad-rc") 281 p := testProvider("aws") 282 c := testContext2(t, &ContextOpts{ 283 Module: m, 284 ProviderResolver: ResourceProviderResolverFixed( 285 map[string]ResourceProviderFactory{ 286 "aws": testProviderFuncFixed(p), 287 }, 288 ), 289 }) 290 291 p.ValidateResourceReturnErrors = []error{fmt.Errorf("bad")} 292 293 w, e := c.Validate() 294 if len(w) > 0 { 295 t.Fatalf("bad: %#v", w) 296 } 297 if len(e) == 0 { 298 t.Fatalf("bad: %#v", e) 299 } 300 } 301 302 func TestContext2Validate_moduleDepsShouldNotCycle(t *testing.T) { 303 m := testModule(t, "validate-module-deps-cycle") 304 p := testProvider("aws") 305 ctx := testContext2(t, &ContextOpts{ 306 Module: m, 307 ProviderResolver: ResourceProviderResolverFixed( 308 map[string]ResourceProviderFactory{ 309 "aws": testProviderFuncFixed(p), 310 }, 311 ), 312 }) 313 314 w, e := ctx.Validate() 315 316 if len(w) > 0 { 317 t.Fatalf("expected no warnings, got: %s", w) 318 } 319 if len(e) > 0 { 320 t.Fatalf("expected no errors, got: %s", e) 321 } 322 } 323 324 func TestContext2Validate_moduleProviderInheritOrphan(t *testing.T) { 325 m := testModule(t, "validate-module-pc-inherit-orphan") 326 p := testProvider("aws") 327 c := testContext2(t, &ContextOpts{ 328 Module: m, 329 ProviderResolver: ResourceProviderResolverFixed( 330 map[string]ResourceProviderFactory{ 331 "aws": testProviderFuncFixed(p), 332 }, 333 ), 334 State: &State{ 335 Modules: []*ModuleState{ 336 &ModuleState{ 337 Path: []string{"root", "child"}, 338 Resources: map[string]*ResourceState{ 339 "aws_instance.bar": &ResourceState{ 340 Type: "aws_instance", 341 Primary: &InstanceState{ 342 ID: "bar", 343 }, 344 }, 345 }, 346 }, 347 }, 348 }, 349 }) 350 351 p.ValidateFn = func(c *ResourceConfig) ([]string, []error) { 352 v, ok := c.Get("set") 353 if !ok { 354 return nil, []error{fmt.Errorf("not set")} 355 } 356 if v != "bar" { 357 return nil, []error{fmt.Errorf("bad: %#v", v)} 358 } 359 360 return nil, nil 361 } 362 363 w, e := c.Validate() 364 if len(w) > 0 { 365 t.Fatalf("bad: %#v", w) 366 } 367 if len(e) > 0 { 368 t.Fatalf("bad: %s", e) 369 } 370 } 371 372 func TestContext2Validate_moduleProviderVar(t *testing.T) { 373 m := testModule(t, "validate-module-pc-vars") 374 p := testProvider("aws") 375 c := testContext2(t, &ContextOpts{ 376 Module: m, 377 ProviderResolver: ResourceProviderResolverFixed( 378 map[string]ResourceProviderFactory{ 379 "aws": testProviderFuncFixed(p), 380 }, 381 ), 382 Variables: map[string]interface{}{ 383 "provider_var": "bar", 384 }, 385 }) 386 387 p.ValidateFn = func(c *ResourceConfig) ([]string, []error) { 388 return nil, c.CheckSet([]string{"foo"}) 389 } 390 391 w, e := c.Validate() 392 if len(w) > 0 { 393 t.Fatalf("bad: %#v", w) 394 } 395 if len(e) > 0 { 396 t.Fatalf("bad: %s", e) 397 } 398 } 399 400 func TestContext2Validate_moduleProviderInheritUnused(t *testing.T) { 401 m := testModule(t, "validate-module-pc-inherit-unused") 402 p := testProvider("aws") 403 c := testContext2(t, &ContextOpts{ 404 Module: m, 405 ProviderResolver: ResourceProviderResolverFixed( 406 map[string]ResourceProviderFactory{ 407 "aws": testProviderFuncFixed(p), 408 }, 409 ), 410 }) 411 412 p.ValidateFn = func(c *ResourceConfig) ([]string, []error) { 413 return nil, c.CheckSet([]string{"foo"}) 414 } 415 416 w, e := c.Validate() 417 if len(w) > 0 { 418 t.Fatalf("bad: %#v", w) 419 } 420 if len(e) > 0 { 421 t.Fatalf("bad: %s", e) 422 } 423 } 424 425 func TestContext2Validate_orphans(t *testing.T) { 426 p := testProvider("aws") 427 m := testModule(t, "validate-good") 428 state := &State{ 429 Modules: []*ModuleState{ 430 &ModuleState{ 431 Path: rootModulePath, 432 Resources: map[string]*ResourceState{ 433 "aws_instance.web": &ResourceState{ 434 Type: "aws_instance", 435 Primary: &InstanceState{ 436 ID: "bar", 437 }, 438 }, 439 }, 440 }, 441 }, 442 } 443 c := testContext2(t, &ContextOpts{ 444 Module: m, 445 ProviderResolver: ResourceProviderResolverFixed( 446 map[string]ResourceProviderFactory{ 447 "aws": testProviderFuncFixed(p), 448 }, 449 ), 450 State: state, 451 }) 452 453 p.ValidateResourceFn = func( 454 t string, c *ResourceConfig) ([]string, []error) { 455 return nil, c.CheckSet([]string{"foo"}) 456 } 457 458 w, e := c.Validate() 459 if len(w) > 0 { 460 t.Fatalf("bad: %#v", w) 461 } 462 if len(e) > 0 { 463 t.Fatalf("bad: %s", e) 464 } 465 } 466 467 func TestContext2Validate_providerConfig_bad(t *testing.T) { 468 m := testModule(t, "validate-bad-pc") 469 p := testProvider("aws") 470 c := testContext2(t, &ContextOpts{ 471 Module: m, 472 ProviderResolver: ResourceProviderResolverFixed( 473 map[string]ResourceProviderFactory{ 474 "aws": testProviderFuncFixed(p), 475 }, 476 ), 477 }) 478 479 p.ValidateReturnErrors = []error{fmt.Errorf("bad")} 480 481 w, e := c.Validate() 482 if len(w) > 0 { 483 t.Fatalf("bad: %#v", w) 484 } 485 if len(e) == 0 { 486 t.Fatalf("bad: %s", e) 487 } 488 if !strings.Contains(fmt.Sprintf("%s", e), "bad") { 489 t.Fatalf("bad: %s", e) 490 } 491 } 492 493 func TestContext2Validate_providerConfig_badEmpty(t *testing.T) { 494 m := testModule(t, "validate-bad-pc-empty") 495 p := testProvider("aws") 496 c := testContext2(t, &ContextOpts{ 497 Module: m, 498 ProviderResolver: ResourceProviderResolverFixed( 499 map[string]ResourceProviderFactory{ 500 "aws": testProviderFuncFixed(p), 501 }, 502 ), 503 }) 504 505 p.ValidateReturnErrors = []error{fmt.Errorf("bad")} 506 507 w, e := c.Validate() 508 if len(w) > 0 { 509 t.Fatalf("bad: %#v", w) 510 } 511 if len(e) == 0 { 512 t.Fatalf("bad: %#v", e) 513 } 514 } 515 516 func TestContext2Validate_providerConfig_good(t *testing.T) { 517 m := testModule(t, "validate-bad-pc") 518 p := testProvider("aws") 519 c := testContext2(t, &ContextOpts{ 520 Module: m, 521 ProviderResolver: ResourceProviderResolverFixed( 522 map[string]ResourceProviderFactory{ 523 "aws": testProviderFuncFixed(p), 524 }, 525 ), 526 }) 527 528 w, e := c.Validate() 529 if len(w) > 0 { 530 t.Fatalf("bad: %#v", w) 531 } 532 if len(e) > 0 { 533 t.Fatalf("bad: %#v", e) 534 } 535 } 536 537 func TestContext2Validate_provisionerConfig_bad(t *testing.T) { 538 m := testModule(t, "validate-bad-prov-conf") 539 p := testProvider("aws") 540 pr := testProvisioner() 541 c := testContext2(t, &ContextOpts{ 542 Module: m, 543 ProviderResolver: ResourceProviderResolverFixed( 544 map[string]ResourceProviderFactory{ 545 "aws": testProviderFuncFixed(p), 546 }, 547 ), 548 Provisioners: map[string]ResourceProvisionerFactory{ 549 "shell": testProvisionerFuncFixed(pr), 550 }, 551 }) 552 553 pr.ValidateReturnErrors = []error{fmt.Errorf("bad")} 554 555 w, e := c.Validate() 556 if len(w) > 0 { 557 t.Fatalf("bad: %#v", w) 558 } 559 if len(e) == 0 { 560 t.Fatalf("bad: %#v", e) 561 } 562 } 563 564 func TestContext2Validate_provisionerConfig_good(t *testing.T) { 565 m := testModule(t, "validate-bad-prov-conf") 566 p := testProvider("aws") 567 pr := testProvisioner() 568 pr.ValidateFn = func(c *ResourceConfig) ([]string, []error) { 569 if c == nil { 570 t.Fatalf("missing resource config for provisioner") 571 } 572 return nil, c.CheckSet([]string{"command"}) 573 } 574 c := testContext2(t, &ContextOpts{ 575 Module: m, 576 ProviderResolver: ResourceProviderResolverFixed( 577 map[string]ResourceProviderFactory{ 578 "aws": testProviderFuncFixed(p), 579 }, 580 ), 581 Provisioners: map[string]ResourceProvisionerFactory{ 582 "shell": testProvisionerFuncFixed(pr), 583 }, 584 }) 585 586 w, e := c.Validate() 587 if len(w) > 0 { 588 t.Fatalf("bad: %#v", w) 589 } 590 if len(e) > 0 { 591 t.Fatalf("bad: %#v", e) 592 } 593 } 594 595 func TestContext2Validate_requiredVar(t *testing.T) { 596 m := testModule(t, "validate-required-var") 597 p := testProvider("aws") 598 c := testContext2(t, &ContextOpts{ 599 Module: m, 600 ProviderResolver: ResourceProviderResolverFixed( 601 map[string]ResourceProviderFactory{ 602 "aws": testProviderFuncFixed(p), 603 }, 604 ), 605 }) 606 607 w, e := c.Validate() 608 if len(w) > 0 { 609 t.Fatalf("bad: %#v", w) 610 } 611 if len(e) == 0 { 612 t.Fatalf("bad: %s", e) 613 } 614 } 615 616 func TestContext2Validate_resourceConfig_bad(t *testing.T) { 617 m := testModule(t, "validate-bad-rc") 618 p := testProvider("aws") 619 c := testContext2(t, &ContextOpts{ 620 Module: m, 621 ProviderResolver: ResourceProviderResolverFixed( 622 map[string]ResourceProviderFactory{ 623 "aws": testProviderFuncFixed(p), 624 }, 625 ), 626 }) 627 628 p.ValidateResourceReturnErrors = []error{fmt.Errorf("bad")} 629 630 w, e := c.Validate() 631 if len(w) > 0 { 632 t.Fatalf("bad: %#v", w) 633 } 634 if len(e) == 0 { 635 t.Fatalf("bad: %s", e) 636 } 637 } 638 639 func TestContext2Validate_resourceConfig_good(t *testing.T) { 640 m := testModule(t, "validate-bad-rc") 641 p := testProvider("aws") 642 c := testContext2(t, &ContextOpts{ 643 Module: m, 644 ProviderResolver: ResourceProviderResolverFixed( 645 map[string]ResourceProviderFactory{ 646 "aws": testProviderFuncFixed(p), 647 }, 648 ), 649 }) 650 651 w, e := c.Validate() 652 if len(w) > 0 { 653 t.Fatalf("bad: %#v", w) 654 } 655 if len(e) > 0 { 656 t.Fatalf("bad: %#v", e) 657 } 658 } 659 660 func TestContext2Validate_resourceNameSymbol(t *testing.T) { 661 p := testProvider("aws") 662 m := testModule(t, "validate-resource-name-symbol") 663 c := testContext2(t, &ContextOpts{ 664 Module: m, 665 ProviderResolver: ResourceProviderResolverFixed( 666 map[string]ResourceProviderFactory{ 667 "aws": testProviderFuncFixed(p), 668 }, 669 ), 670 }) 671 672 w, e := c.Validate() 673 if len(w) > 0 { 674 t.Fatalf("bad: %#v", w) 675 } 676 if len(e) == 0 { 677 t.Fatalf("bad: %s", e) 678 } 679 } 680 681 func TestContext2Validate_selfRef(t *testing.T) { 682 p := testProvider("aws") 683 m := testModule(t, "validate-self-ref") 684 c := testContext2(t, &ContextOpts{ 685 Module: m, 686 ProviderResolver: ResourceProviderResolverFixed( 687 map[string]ResourceProviderFactory{ 688 "aws": testProviderFuncFixed(p), 689 }, 690 ), 691 }) 692 693 w, e := c.Validate() 694 if len(w) > 0 { 695 t.Fatalf("bad: %#v", w) 696 } 697 if len(e) == 0 { 698 t.Fatalf("bad: %s", e) 699 } 700 } 701 702 func TestContext2Validate_selfRefMulti(t *testing.T) { 703 p := testProvider("aws") 704 m := testModule(t, "validate-self-ref-multi") 705 c := testContext2(t, &ContextOpts{ 706 Module: m, 707 ProviderResolver: ResourceProviderResolverFixed( 708 map[string]ResourceProviderFactory{ 709 "aws": testProviderFuncFixed(p), 710 }, 711 ), 712 }) 713 714 w, e := c.Validate() 715 if len(w) > 0 { 716 t.Fatalf("bad: %#v", w) 717 } 718 if len(e) == 0 { 719 t.Fatalf("bad: %#v", e) 720 } 721 } 722 723 func TestContext2Validate_selfRefMultiAll(t *testing.T) { 724 p := testProvider("aws") 725 m := testModule(t, "validate-self-ref-multi-all") 726 c := testContext2(t, &ContextOpts{ 727 Module: m, 728 ProviderResolver: ResourceProviderResolverFixed( 729 map[string]ResourceProviderFactory{ 730 "aws": testProviderFuncFixed(p), 731 }, 732 ), 733 }) 734 735 w, e := c.Validate() 736 if len(w) > 0 { 737 t.Fatalf("bad: %#v", w) 738 } 739 if len(e) == 0 { 740 t.Fatalf("bad: %#v", e) 741 } 742 } 743 744 func TestContext2Validate_tainted(t *testing.T) { 745 p := testProvider("aws") 746 m := testModule(t, "validate-good") 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: "bar", 756 Tainted: true, 757 }, 758 }, 759 }, 760 }, 761 }, 762 } 763 c := testContext2(t, &ContextOpts{ 764 Module: m, 765 ProviderResolver: ResourceProviderResolverFixed( 766 map[string]ResourceProviderFactory{ 767 "aws": testProviderFuncFixed(p), 768 }, 769 ), 770 State: state, 771 }) 772 773 p.ValidateResourceFn = func( 774 t string, c *ResourceConfig) ([]string, []error) { 775 return nil, c.CheckSet([]string{"foo"}) 776 } 777 778 w, e := c.Validate() 779 if len(w) > 0 { 780 t.Fatalf("bad: %#v", w) 781 } 782 if len(e) > 0 { 783 t.Fatalf("bad: %#v", e) 784 } 785 } 786 787 func TestContext2Validate_targetedDestroy(t *testing.T) { 788 m := testModule(t, "validate-targeted") 789 p := testProvider("aws") 790 pr := testProvisioner() 791 p.ApplyFn = testApplyFn 792 p.DiffFn = testDiffFn 793 ctx := testContext2(t, &ContextOpts{ 794 Module: m, 795 ProviderResolver: ResourceProviderResolverFixed( 796 map[string]ResourceProviderFactory{ 797 "aws": testProviderFuncFixed(p), 798 }, 799 ), 800 Provisioners: map[string]ResourceProvisionerFactory{ 801 "shell": testProvisionerFuncFixed(pr), 802 }, 803 State: &State{ 804 Modules: []*ModuleState{ 805 &ModuleState{ 806 Path: rootModulePath, 807 Resources: map[string]*ResourceState{ 808 "aws_instance.foo": resourceState("aws_instance", "i-bcd345"), 809 "aws_instance.bar": resourceState("aws_instance", "i-abc123"), 810 }, 811 }, 812 }, 813 }, 814 Targets: []string{"aws_instance.foo"}, 815 Destroy: true, 816 }) 817 818 w, e := ctx.Validate() 819 if len(w) > 0 { 820 warnStr := "" 821 for _, v := range w { 822 warnStr = warnStr + " " + v 823 } 824 t.Fatalf("bad: %s", warnStr) 825 } 826 if len(e) > 0 { 827 t.Fatalf("bad: %s", e) 828 } 829 } 830 831 func TestContext2Validate_varRefFilled(t *testing.T) { 832 m := testModule(t, "validate-variable-ref") 833 p := testProvider("aws") 834 c := testContext2(t, &ContextOpts{ 835 Module: m, 836 ProviderResolver: ResourceProviderResolverFixed( 837 map[string]ResourceProviderFactory{ 838 "aws": testProviderFuncFixed(p), 839 }, 840 ), 841 Variables: map[string]interface{}{ 842 "foo": "bar", 843 }, 844 }) 845 846 var value interface{} 847 p.ValidateResourceFn = func(t string, c *ResourceConfig) ([]string, []error) { 848 value, _ = c.Get("foo") 849 return nil, nil 850 } 851 852 c.Validate() 853 if value != "bar" { 854 t.Fatalf("bad: %#v", value) 855 } 856 } 857 858 // Module variables weren't being interpolated during Validate phase. 859 // related to https://github.com/hashicorp/terraform/issues/5322 860 func TestContext2Validate_interpolateVar(t *testing.T) { 861 input := new(MockUIInput) 862 863 m := testModule(t, "input-interpolate-var") 864 p := testProvider("null") 865 p.ApplyFn = testApplyFn 866 p.DiffFn = testDiffFn 867 868 ctx := testContext2(t, &ContextOpts{ 869 Module: m, 870 ProviderResolver: ResourceProviderResolverFixed( 871 map[string]ResourceProviderFactory{ 872 "template": testProviderFuncFixed(p), 873 }, 874 ), 875 UIInput: input, 876 }) 877 878 w, e := ctx.Validate() 879 if w != nil { 880 t.Log("warnings:", w) 881 } 882 if e != nil { 883 t.Fatal("err:", e) 884 } 885 } 886 887 // When module vars reference something that is actually computed, this 888 // shouldn't cause validation to fail. 889 func TestContext2Validate_interpolateComputedModuleVarDef(t *testing.T) { 890 input := new(MockUIInput) 891 892 m := testModule(t, "validate-computed-module-var-ref") 893 p := testProvider("aws") 894 p.ApplyFn = testApplyFn 895 p.DiffFn = testDiffFn 896 897 ctx := testContext2(t, &ContextOpts{ 898 Module: m, 899 ProviderResolver: ResourceProviderResolverFixed( 900 map[string]ResourceProviderFactory{ 901 "aws": testProviderFuncFixed(p), 902 }, 903 ), 904 UIInput: input, 905 }) 906 907 w, e := ctx.Validate() 908 if w != nil { 909 t.Log("warnings:", w) 910 } 911 if e != nil { 912 t.Fatal("err:", e) 913 } 914 } 915 916 // Computed values are lost when a map is output from a module 917 func TestContext2Validate_interpolateMap(t *testing.T) { 918 input := new(MockUIInput) 919 920 m := testModule(t, "issue-9549") 921 p := testProvider("null") 922 p.ApplyFn = testApplyFn 923 p.DiffFn = testDiffFn 924 925 ctx := testContext2(t, &ContextOpts{ 926 Module: m, 927 ProviderResolver: ResourceProviderResolverFixed( 928 map[string]ResourceProviderFactory{ 929 "template": testProviderFuncFixed(p), 930 }, 931 ), 932 UIInput: input, 933 }) 934 935 w, e := ctx.Validate() 936 if w != nil { 937 t.Log("warnings:", w) 938 } 939 if e != nil { 940 t.Fatal("err:", e) 941 } 942 } 943 944 // Manually validate using the new PlanGraphBuilder 945 func TestContext2Validate_PlanGraphBuilder(t *testing.T) { 946 m := testModule(t, "apply-vars") 947 p := testProvider("aws") 948 p.ApplyFn = testApplyFn 949 p.DiffFn = testDiffFn 950 c := testContext2(t, &ContextOpts{ 951 Module: m, 952 ProviderResolver: ResourceProviderResolverFixed( 953 map[string]ResourceProviderFactory{ 954 "aws": testProviderFuncFixed(p), 955 }, 956 ), 957 Variables: map[string]interface{}{ 958 "foo": "us-west-2", 959 "test_list": []interface{}{"Hello", "World"}, 960 "test_map": map[string]interface{}{ 961 "Hello": "World", 962 "Foo": "Bar", 963 "Baz": "Foo", 964 }, 965 "amis": []map[string]interface{}{ 966 map[string]interface{}{ 967 "us-east-1": "override", 968 }, 969 }, 970 }, 971 }) 972 973 graph, err := (&PlanGraphBuilder{ 974 Module: c.module, 975 State: NewState(), 976 Providers: c.components.ResourceProviders(), 977 Targets: c.targets, 978 }).Build(RootModulePath) 979 if err != nil { 980 t.Fatalf("error attmepting to Build PlanGraphBuilder: %s", err) 981 } 982 defer c.acquireRun("validate-test")() 983 walker, err := c.walk(graph, walkValidate) 984 if err != nil { 985 t.Fatal(err) 986 } 987 if len(walker.ValidationErrors) > 0 { 988 t.Fatal(walker.ValidationErrors) 989 } 990 }