github.com/federicobaldo/terraform@v0.6.15-0.20160323222747-b20f680cbf05/terraform/context_validate_test.go (about) 1 package terraform 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 ) 8 9 func TestContext2Validate_badVar(t *testing.T) { 10 p := testProvider("aws") 11 m := testModule(t, "validate-bad-var") 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_varNoDefaultExplicitType(t *testing.T) { 29 m := testModule(t, "validate-var-no-default-explicit-type") 30 c := testContext2(t, &ContextOpts{ 31 Module: m, 32 }) 33 34 w, e := c.Validate() 35 if len(w) > 0 { 36 t.Fatalf("bad: %#v", w) 37 } 38 if len(e) == 0 { 39 t.Fatalf("bad: %#v", e) 40 } 41 } 42 43 func TestContext2Validate_computedVar(t *testing.T) { 44 p := testProvider("aws") 45 m := testModule(t, "validate-computed-var") 46 c := testContext2(t, &ContextOpts{ 47 Module: m, 48 Providers: map[string]ResourceProviderFactory{ 49 "aws": testProviderFuncFixed(p), 50 "test": testProviderFuncFixed(testProvider("test")), 51 }, 52 }) 53 54 p.ValidateFn = func(c *ResourceConfig) ([]string, []error) { 55 if !c.IsComputed("value") { 56 return nil, []error{fmt.Errorf("value isn't computed")} 57 } 58 59 return nil, c.CheckSet([]string{"value"}) 60 } 61 62 p.ConfigureFn = func(c *ResourceConfig) error { 63 return fmt.Errorf("Configure should not be called for provider") 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: %#v", e) 72 } 73 } 74 75 func TestContext2Validate_countNegative(t *testing.T) { 76 p := testProvider("aws") 77 m := testModule(t, "validate-count-negative") 78 c := testContext2(t, &ContextOpts{ 79 Module: m, 80 Providers: map[string]ResourceProviderFactory{ 81 "aws": testProviderFuncFixed(p), 82 }, 83 }) 84 85 w, e := c.Validate() 86 if len(w) > 0 { 87 t.Fatalf("bad: %#v", w) 88 } 89 if len(e) == 0 { 90 t.Fatalf("bad: %#v", e) 91 } 92 } 93 94 func TestContext2Validate_countVariable(t *testing.T) { 95 p := testProvider("aws") 96 m := testModule(t, "apply-count-variable") 97 c := testContext2(t, &ContextOpts{ 98 Module: m, 99 Providers: map[string]ResourceProviderFactory{ 100 "aws": testProviderFuncFixed(p), 101 }, 102 }) 103 104 w, e := c.Validate() 105 if len(w) > 0 { 106 t.Fatalf("bad: %#v", w) 107 } 108 if len(e) > 0 { 109 t.Fatalf("bad: %s", e) 110 } 111 } 112 113 func TestContext2Validate_countVariableNoDefault(t *testing.T) { 114 p := testProvider("aws") 115 m := testModule(t, "validate-count-variable") 116 c := testContext2(t, &ContextOpts{ 117 Module: m, 118 Providers: map[string]ResourceProviderFactory{ 119 "aws": testProviderFuncFixed(p), 120 }, 121 }) 122 123 w, e := c.Validate() 124 if len(w) > 0 { 125 t.Fatalf("bad: %#v", w) 126 } 127 if len(e) != 1 { 128 t.Fatalf("bad: %s", e) 129 } 130 } 131 132 /* 133 TODO: What should we do here? 134 func TestContext2Validate_cycle(t *testing.T) { 135 p := testProvider("aws") 136 m := testModule(t, "validate-cycle") 137 c := testContext2(t, &ContextOpts{ 138 Module: m, 139 Providers: map[string]ResourceProviderFactory{ 140 "aws": testProviderFuncFixed(p), 141 }, 142 }) 143 144 w, e := c.Validate() 145 if len(w) > 0 { 146 t.Fatalf("expected no warns, got: %#v", w) 147 } 148 if len(e) != 1 { 149 t.Fatalf("expected 1 err, got: %s", e) 150 } 151 } 152 */ 153 154 func TestContext2Validate_moduleBadOutput(t *testing.T) { 155 p := testProvider("aws") 156 m := testModule(t, "validate-bad-module-output") 157 c := testContext2(t, &ContextOpts{ 158 Module: m, 159 Providers: map[string]ResourceProviderFactory{ 160 "aws": testProviderFuncFixed(p), 161 }, 162 }) 163 164 w, e := c.Validate() 165 if len(w) > 0 { 166 t.Fatalf("bad: %#v", w) 167 } 168 if len(e) == 0 { 169 t.Fatalf("bad: %s", e) 170 } 171 } 172 173 func TestContext2Validate_moduleGood(t *testing.T) { 174 p := testProvider("aws") 175 m := testModule(t, "validate-good-module") 176 c := testContext2(t, &ContextOpts{ 177 Module: m, 178 Providers: map[string]ResourceProviderFactory{ 179 "aws": testProviderFuncFixed(p), 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: %#v", e) 189 } 190 } 191 192 func TestContext2Validate_moduleBadResource(t *testing.T) { 193 m := testModule(t, "validate-module-bad-rc") 194 p := testProvider("aws") 195 c := testContext2(t, &ContextOpts{ 196 Module: m, 197 Providers: map[string]ResourceProviderFactory{ 198 "aws": testProviderFuncFixed(p), 199 }, 200 }) 201 202 p.ValidateResourceReturnErrors = []error{fmt.Errorf("bad")} 203 204 w, e := c.Validate() 205 if len(w) > 0 { 206 t.Fatalf("bad: %#v", w) 207 } 208 if len(e) == 0 { 209 t.Fatalf("bad: %#v", e) 210 } 211 } 212 213 func TestContext2Validate_moduleDepsShouldNotCycle(t *testing.T) { 214 m := testModule(t, "validate-module-deps-cycle") 215 p := testProvider("aws") 216 ctx := testContext2(t, &ContextOpts{ 217 Module: m, 218 Providers: map[string]ResourceProviderFactory{ 219 "aws": testProviderFuncFixed(p), 220 }, 221 }) 222 223 w, e := ctx.Validate() 224 225 if len(w) > 0 { 226 t.Fatalf("expected no warnings, got: %s", w) 227 } 228 if len(e) > 0 { 229 t.Fatalf("expected no errors, got: %s", e) 230 } 231 } 232 233 func TestContext2Validate_moduleProviderInherit(t *testing.T) { 234 m := testModule(t, "validate-module-pc-inherit") 235 p := testProvider("aws") 236 c := testContext2(t, &ContextOpts{ 237 Module: m, 238 Providers: map[string]ResourceProviderFactory{ 239 "aws": testProviderFuncFixed(p), 240 }, 241 }) 242 243 p.ValidateFn = func(c *ResourceConfig) ([]string, []error) { 244 return nil, c.CheckSet([]string{"set"}) 245 } 246 247 w, e := c.Validate() 248 if len(w) > 0 { 249 t.Fatalf("bad: %#v", w) 250 } 251 if len(e) > 0 { 252 t.Fatalf("bad: %s", e) 253 } 254 } 255 256 func TestContext2Validate_moduleProviderInheritOrphan(t *testing.T) { 257 m := testModule(t, "validate-module-pc-inherit-orphan") 258 p := testProvider("aws") 259 c := testContext2(t, &ContextOpts{ 260 Module: m, 261 Providers: map[string]ResourceProviderFactory{ 262 "aws": testProviderFuncFixed(p), 263 }, 264 State: &State{ 265 Modules: []*ModuleState{ 266 &ModuleState{ 267 Path: []string{"root", "child"}, 268 Resources: map[string]*ResourceState{ 269 "aws_instance.bar": &ResourceState{ 270 Type: "aws_instance", 271 Primary: &InstanceState{ 272 ID: "bar", 273 }, 274 }, 275 }, 276 }, 277 }, 278 }, 279 }) 280 281 p.ValidateFn = func(c *ResourceConfig) ([]string, []error) { 282 v, ok := c.Get("set") 283 if !ok { 284 return nil, []error{fmt.Errorf("not set")} 285 } 286 if v != "bar" { 287 return nil, []error{fmt.Errorf("bad: %#v", v)} 288 } 289 290 return nil, nil 291 } 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: %s", e) 299 } 300 } 301 302 func TestContext2Validate_moduleProviderVar(t *testing.T) { 303 m := testModule(t, "validate-module-pc-vars") 304 p := testProvider("aws") 305 c := testContext2(t, &ContextOpts{ 306 Module: m, 307 Providers: map[string]ResourceProviderFactory{ 308 "aws": testProviderFuncFixed(p), 309 }, 310 Variables: map[string]string{ 311 "provider_var": "bar", 312 }, 313 }) 314 315 p.ValidateFn = func(c *ResourceConfig) ([]string, []error) { 316 return nil, c.CheckSet([]string{"foo"}) 317 } 318 319 w, e := c.Validate() 320 if len(w) > 0 { 321 t.Fatalf("bad: %#v", w) 322 } 323 if len(e) > 0 { 324 t.Fatalf("bad: %s", e) 325 } 326 } 327 328 func TestContext2Validate_moduleProviderInheritUnused(t *testing.T) { 329 m := testModule(t, "validate-module-pc-inherit-unused") 330 p := testProvider("aws") 331 c := testContext2(t, &ContextOpts{ 332 Module: m, 333 Providers: map[string]ResourceProviderFactory{ 334 "aws": testProviderFuncFixed(p), 335 }, 336 }) 337 338 p.ValidateFn = func(c *ResourceConfig) ([]string, []error) { 339 return nil, c.CheckSet([]string{"foo"}) 340 } 341 342 w, e := c.Validate() 343 if len(w) > 0 { 344 t.Fatalf("bad: %#v", w) 345 } 346 if len(e) > 0 { 347 t.Fatalf("bad: %s", e) 348 } 349 } 350 351 func TestContext2Validate_orphans(t *testing.T) { 352 p := testProvider("aws") 353 m := testModule(t, "validate-good") 354 state := &State{ 355 Modules: []*ModuleState{ 356 &ModuleState{ 357 Path: rootModulePath, 358 Resources: map[string]*ResourceState{ 359 "aws_instance.web": &ResourceState{ 360 Type: "aws_instance", 361 Primary: &InstanceState{ 362 ID: "bar", 363 }, 364 }, 365 }, 366 }, 367 }, 368 } 369 c := testContext2(t, &ContextOpts{ 370 Module: m, 371 Providers: map[string]ResourceProviderFactory{ 372 "aws": testProviderFuncFixed(p), 373 }, 374 State: state, 375 }) 376 377 p.ValidateResourceFn = func( 378 t string, c *ResourceConfig) ([]string, []error) { 379 return nil, c.CheckSet([]string{"foo"}) 380 } 381 382 w, e := c.Validate() 383 if len(w) > 0 { 384 t.Fatalf("bad: %#v", w) 385 } 386 if len(e) > 0 { 387 t.Fatalf("bad: %s", e) 388 } 389 } 390 391 func TestContext2Validate_providerConfig_bad(t *testing.T) { 392 m := testModule(t, "validate-bad-pc") 393 p := testProvider("aws") 394 c := testContext2(t, &ContextOpts{ 395 Module: m, 396 Providers: map[string]ResourceProviderFactory{ 397 "aws": testProviderFuncFixed(p), 398 }, 399 }) 400 401 p.ValidateReturnErrors = []error{fmt.Errorf("bad")} 402 403 w, e := c.Validate() 404 if len(w) > 0 { 405 t.Fatalf("bad: %#v", w) 406 } 407 if len(e) == 0 { 408 t.Fatalf("bad: %s", e) 409 } 410 if !strings.Contains(fmt.Sprintf("%s", e), "bad") { 411 t.Fatalf("bad: %s", e) 412 } 413 } 414 415 func TestContext2Validate_providerConfig_badEmpty(t *testing.T) { 416 m := testModule(t, "validate-bad-pc-empty") 417 p := testProvider("aws") 418 c := testContext2(t, &ContextOpts{ 419 Module: m, 420 Providers: map[string]ResourceProviderFactory{ 421 "aws": testProviderFuncFixed(p), 422 }, 423 }) 424 425 p.ValidateReturnErrors = []error{fmt.Errorf("bad")} 426 427 w, e := c.Validate() 428 if len(w) > 0 { 429 t.Fatalf("bad: %#v", w) 430 } 431 if len(e) == 0 { 432 t.Fatalf("bad: %#v", e) 433 } 434 } 435 436 func TestContext2Validate_providerConfig_good(t *testing.T) { 437 m := testModule(t, "validate-bad-pc") 438 p := testProvider("aws") 439 c := testContext2(t, &ContextOpts{ 440 Module: m, 441 Providers: map[string]ResourceProviderFactory{ 442 "aws": testProviderFuncFixed(p), 443 }, 444 }) 445 446 w, e := c.Validate() 447 if len(w) > 0 { 448 t.Fatalf("bad: %#v", w) 449 } 450 if len(e) > 0 { 451 t.Fatalf("bad: %#v", e) 452 } 453 } 454 455 func TestContext2Validate_provisionerConfig_bad(t *testing.T) { 456 m := testModule(t, "validate-bad-prov-conf") 457 p := testProvider("aws") 458 pr := testProvisioner() 459 c := testContext2(t, &ContextOpts{ 460 Module: m, 461 Providers: map[string]ResourceProviderFactory{ 462 "aws": testProviderFuncFixed(p), 463 }, 464 Provisioners: map[string]ResourceProvisionerFactory{ 465 "shell": testProvisionerFuncFixed(pr), 466 }, 467 }) 468 469 pr.ValidateReturnErrors = []error{fmt.Errorf("bad")} 470 471 w, e := c.Validate() 472 if len(w) > 0 { 473 t.Fatalf("bad: %#v", w) 474 } 475 if len(e) == 0 { 476 t.Fatalf("bad: %#v", e) 477 } 478 } 479 480 func TestContext2Validate_provisionerConfig_good(t *testing.T) { 481 m := testModule(t, "validate-bad-prov-conf") 482 p := testProvider("aws") 483 pr := testProvisioner() 484 pr.ValidateFn = func(c *ResourceConfig) ([]string, []error) { 485 if c == nil { 486 t.Fatalf("missing resource config for provisioner") 487 } 488 return nil, c.CheckSet([]string{"command"}) 489 } 490 c := testContext2(t, &ContextOpts{ 491 Module: m, 492 Providers: map[string]ResourceProviderFactory{ 493 "aws": testProviderFuncFixed(p), 494 }, 495 Provisioners: map[string]ResourceProvisionerFactory{ 496 "shell": testProvisionerFuncFixed(pr), 497 }, 498 }) 499 500 w, e := c.Validate() 501 if len(w) > 0 { 502 t.Fatalf("bad: %#v", w) 503 } 504 if len(e) > 0 { 505 t.Fatalf("bad: %#v", e) 506 } 507 } 508 509 func TestContext2Validate_requiredVar(t *testing.T) { 510 m := testModule(t, "validate-required-var") 511 p := testProvider("aws") 512 c := testContext2(t, &ContextOpts{ 513 Module: m, 514 Providers: map[string]ResourceProviderFactory{ 515 "aws": testProviderFuncFixed(p), 516 }, 517 }) 518 519 w, e := c.Validate() 520 if len(w) > 0 { 521 t.Fatalf("bad: %#v", w) 522 } 523 if len(e) == 0 { 524 t.Fatalf("bad: %s", e) 525 } 526 } 527 528 func TestContext2Validate_resourceConfig_bad(t *testing.T) { 529 m := testModule(t, "validate-bad-rc") 530 p := testProvider("aws") 531 c := testContext2(t, &ContextOpts{ 532 Module: m, 533 Providers: map[string]ResourceProviderFactory{ 534 "aws": testProviderFuncFixed(p), 535 }, 536 }) 537 538 p.ValidateResourceReturnErrors = []error{fmt.Errorf("bad")} 539 540 w, e := c.Validate() 541 if len(w) > 0 { 542 t.Fatalf("bad: %#v", w) 543 } 544 if len(e) == 0 { 545 t.Fatalf("bad: %s", e) 546 } 547 } 548 549 func TestContext2Validate_resourceConfig_good(t *testing.T) { 550 m := testModule(t, "validate-bad-rc") 551 p := testProvider("aws") 552 c := testContext2(t, &ContextOpts{ 553 Module: m, 554 Providers: map[string]ResourceProviderFactory{ 555 "aws": testProviderFuncFixed(p), 556 }, 557 }) 558 559 w, e := c.Validate() 560 if len(w) > 0 { 561 t.Fatalf("bad: %#v", w) 562 } 563 if len(e) > 0 { 564 t.Fatalf("bad: %#v", e) 565 } 566 } 567 568 func TestContext2Validate_resourceNameSymbol(t *testing.T) { 569 p := testProvider("aws") 570 m := testModule(t, "validate-resource-name-symbol") 571 c := testContext2(t, &ContextOpts{ 572 Module: m, 573 Providers: map[string]ResourceProviderFactory{ 574 "aws": testProviderFuncFixed(p), 575 }, 576 }) 577 578 w, e := c.Validate() 579 if len(w) > 0 { 580 t.Fatalf("bad: %#v", w) 581 } 582 if len(e) == 0 { 583 t.Fatalf("bad: %s", e) 584 } 585 } 586 587 func TestContext2Validate_selfRef(t *testing.T) { 588 p := testProvider("aws") 589 m := testModule(t, "validate-self-ref") 590 c := testContext2(t, &ContextOpts{ 591 Module: m, 592 Providers: map[string]ResourceProviderFactory{ 593 "aws": testProviderFuncFixed(p), 594 }, 595 }) 596 597 w, e := c.Validate() 598 if len(w) > 0 { 599 t.Fatalf("bad: %#v", w) 600 } 601 if len(e) == 0 { 602 t.Fatalf("bad: %s", e) 603 } 604 } 605 606 func TestContext2Validate_selfRefMulti(t *testing.T) { 607 p := testProvider("aws") 608 m := testModule(t, "validate-self-ref-multi") 609 c := testContext2(t, &ContextOpts{ 610 Module: m, 611 Providers: map[string]ResourceProviderFactory{ 612 "aws": testProviderFuncFixed(p), 613 }, 614 }) 615 616 w, e := c.Validate() 617 if len(w) > 0 { 618 t.Fatalf("bad: %#v", w) 619 } 620 if len(e) == 0 { 621 t.Fatalf("bad: %#v", e) 622 } 623 } 624 625 func TestContext2Validate_selfRefMultiAll(t *testing.T) { 626 p := testProvider("aws") 627 m := testModule(t, "validate-self-ref-multi-all") 628 c := testContext2(t, &ContextOpts{ 629 Module: m, 630 Providers: map[string]ResourceProviderFactory{ 631 "aws": testProviderFuncFixed(p), 632 }, 633 }) 634 635 w, e := c.Validate() 636 if len(w) > 0 { 637 t.Fatalf("bad: %#v", w) 638 } 639 if len(e) == 0 { 640 t.Fatalf("bad: %#v", e) 641 } 642 } 643 644 func TestContext2Validate_tainted(t *testing.T) { 645 p := testProvider("aws") 646 m := testModule(t, "validate-good") 647 state := &State{ 648 Modules: []*ModuleState{ 649 &ModuleState{ 650 Path: rootModulePath, 651 Resources: map[string]*ResourceState{ 652 "aws_instance.foo": &ResourceState{ 653 Type: "aws_instance", 654 Tainted: []*InstanceState{ 655 &InstanceState{ 656 ID: "bar", 657 }, 658 }, 659 }, 660 }, 661 }, 662 }, 663 } 664 c := testContext2(t, &ContextOpts{ 665 Module: m, 666 Providers: map[string]ResourceProviderFactory{ 667 "aws": testProviderFuncFixed(p), 668 }, 669 State: state, 670 }) 671 672 p.ValidateResourceFn = func( 673 t string, c *ResourceConfig) ([]string, []error) { 674 return nil, c.CheckSet([]string{"foo"}) 675 } 676 677 w, e := c.Validate() 678 if len(w) > 0 { 679 t.Fatalf("bad: %#v", w) 680 } 681 if len(e) > 0 { 682 t.Fatalf("bad: %#v", e) 683 } 684 } 685 686 func TestContext2Validate_targetedDestroy(t *testing.T) { 687 m := testModule(t, "validate-targeted") 688 p := testProvider("aws") 689 pr := testProvisioner() 690 p.ApplyFn = testApplyFn 691 p.DiffFn = testDiffFn 692 ctx := testContext2(t, &ContextOpts{ 693 Module: m, 694 Providers: map[string]ResourceProviderFactory{ 695 "aws": testProviderFuncFixed(p), 696 }, 697 Provisioners: map[string]ResourceProvisionerFactory{ 698 "shell": testProvisionerFuncFixed(pr), 699 }, 700 State: &State{ 701 Modules: []*ModuleState{ 702 &ModuleState{ 703 Path: rootModulePath, 704 Resources: map[string]*ResourceState{ 705 "aws_instance.foo": resourceState("aws_instance", "i-bcd345"), 706 "aws_instance.bar": resourceState("aws_instance", "i-abc123"), 707 }, 708 }, 709 }, 710 }, 711 Targets: []string{"aws_instance.foo"}, 712 Destroy: true, 713 }) 714 715 w, e := ctx.Validate() 716 if len(w) > 0 { 717 warnStr := "" 718 for _, v := range w { 719 warnStr = warnStr + " " + v 720 } 721 t.Fatalf("bad: %s", warnStr) 722 } 723 if len(e) > 0 { 724 t.Fatalf("bad: %s", e) 725 } 726 } 727 728 func TestContext2Validate_varRefFilled(t *testing.T) { 729 m := testModule(t, "validate-variable-ref") 730 p := testProvider("aws") 731 c := testContext2(t, &ContextOpts{ 732 Module: m, 733 Providers: map[string]ResourceProviderFactory{ 734 "aws": testProviderFuncFixed(p), 735 }, 736 Variables: map[string]string{ 737 "foo": "bar", 738 }, 739 }) 740 741 var value interface{} 742 p.ValidateResourceFn = func(t string, c *ResourceConfig) ([]string, []error) { 743 value, _ = c.Get("foo") 744 return nil, nil 745 } 746 747 c.Validate() 748 if value != "bar" { 749 t.Fatalf("bad: %#v", value) 750 } 751 }