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