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