github.com/alexissmirnov/terraform@v0.4.3-0.20150423153700-1ef9731a2f14/builtin/providers/aws/resource_aws_elb_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "os" 6 "reflect" 7 "sort" 8 "testing" 9 10 "github.com/awslabs/aws-sdk-go/aws" 11 "github.com/awslabs/aws-sdk-go/service/elb" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSELB_basic(t *testing.T) { 17 var conf elb.LoadBalancerDescription 18 ssl_certificate_id := os.Getenv("AWS_SSL_CERTIFICATE_ID") 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testAccCheckAWSELBDestroy, 24 Steps: []resource.TestStep{ 25 resource.TestStep{ 26 Config: testAccAWSELBConfig, 27 Check: resource.ComposeTestCheckFunc( 28 testAccCheckAWSELBExists("aws_elb.bar", &conf), 29 testAccCheckAWSELBAttributes(&conf), 30 resource.TestCheckResourceAttr( 31 "aws_elb.bar", "name", "foobar-terraform-test"), 32 resource.TestCheckResourceAttr( 33 "aws_elb.bar", "availability_zones.2487133097", "us-west-2a"), 34 resource.TestCheckResourceAttr( 35 "aws_elb.bar", "availability_zones.221770259", "us-west-2b"), 36 resource.TestCheckResourceAttr( 37 "aws_elb.bar", "availability_zones.2050015877", "us-west-2c"), 38 resource.TestCheckResourceAttr( 39 "aws_elb.bar", "listener.206423021.instance_port", "8000"), 40 resource.TestCheckResourceAttr( 41 "aws_elb.bar", "listener.206423021.instance_protocol", "http"), 42 resource.TestCheckResourceAttr( 43 "aws_elb.bar", "listener.206423021.ssl_certificate_id", ssl_certificate_id), 44 resource.TestCheckResourceAttr( 45 "aws_elb.bar", "listener.206423021.lb_port", "80"), 46 resource.TestCheckResourceAttr( 47 "aws_elb.bar", "listener.206423021.lb_protocol", "http"), 48 resource.TestCheckResourceAttr( 49 "aws_elb.bar", "cross_zone_load_balancing", "true"), 50 ), 51 }, 52 }, 53 }) 54 } 55 56 func TestAccAWSELB_tags(t *testing.T) { 57 var conf elb.LoadBalancerDescription 58 var td elb.TagDescription 59 60 resource.Test(t, resource.TestCase{ 61 PreCheck: func() { testAccPreCheck(t) }, 62 Providers: testAccProviders, 63 CheckDestroy: testAccCheckAWSELBDestroy, 64 Steps: []resource.TestStep{ 65 resource.TestStep{ 66 Config: testAccAWSELBConfig, 67 Check: resource.ComposeTestCheckFunc( 68 testAccCheckAWSELBExists("aws_elb.bar", &conf), 69 testAccCheckAWSELBAttributes(&conf), 70 resource.TestCheckResourceAttr( 71 "aws_elb.bar", "name", "foobar-terraform-test"), 72 testAccLoadTags(&conf, &td), 73 testAccCheckELBTags(&td.Tags, "bar", "baz"), 74 ), 75 }, 76 77 resource.TestStep{ 78 Config: testAccAWSELBConfig_TagUpdate, 79 Check: resource.ComposeTestCheckFunc( 80 testAccCheckAWSELBExists("aws_elb.bar", &conf), 81 testAccCheckAWSELBAttributes(&conf), 82 resource.TestCheckResourceAttr( 83 "aws_elb.bar", "name", "foobar-terraform-test"), 84 testAccLoadTags(&conf, &td), 85 testAccCheckELBTags(&td.Tags, "foo", "bar"), 86 testAccCheckELBTags(&td.Tags, "new", "type"), 87 ), 88 }, 89 }, 90 }) 91 } 92 93 func testAccLoadTags(conf *elb.LoadBalancerDescription, td *elb.TagDescription) resource.TestCheckFunc { 94 return func(s *terraform.State) error { 95 conn := testAccProvider.Meta().(*AWSClient).elbconn 96 97 describe, err := conn.DescribeTags(&elb.DescribeTagsInput{ 98 LoadBalancerNames: []*string{conf.LoadBalancerName}, 99 }) 100 101 if err != nil { 102 return err 103 } 104 if len(describe.TagDescriptions) > 0 { 105 *td = *describe.TagDescriptions[0] 106 } 107 return nil 108 } 109 } 110 111 func TestAccAWSELB_InstanceAttaching(t *testing.T) { 112 var conf elb.LoadBalancerDescription 113 114 testCheckInstanceAttached := func(count int) resource.TestCheckFunc { 115 return func(*terraform.State) error { 116 if len(conf.Instances) != count { 117 return fmt.Errorf("instance count does not match") 118 } 119 return nil 120 } 121 } 122 123 resource.Test(t, resource.TestCase{ 124 PreCheck: func() { testAccPreCheck(t) }, 125 Providers: testAccProviders, 126 CheckDestroy: testAccCheckAWSELBDestroy, 127 Steps: []resource.TestStep{ 128 resource.TestStep{ 129 Config: testAccAWSELBConfig, 130 Check: resource.ComposeTestCheckFunc( 131 testAccCheckAWSELBExists("aws_elb.bar", &conf), 132 testAccCheckAWSELBAttributes(&conf), 133 ), 134 }, 135 136 resource.TestStep{ 137 Config: testAccAWSELBConfigNewInstance, 138 Check: resource.ComposeTestCheckFunc( 139 testAccCheckAWSELBExists("aws_elb.bar", &conf), 140 testCheckInstanceAttached(1), 141 ), 142 }, 143 }, 144 }) 145 } 146 147 func TestAccAWSELBUpdate_Listener(t *testing.T) { 148 var conf elb.LoadBalancerDescription 149 150 resource.Test(t, resource.TestCase{ 151 PreCheck: func() { testAccPreCheck(t) }, 152 Providers: testAccProviders, 153 CheckDestroy: testAccCheckAWSELBDestroy, 154 Steps: []resource.TestStep{ 155 resource.TestStep{ 156 Config: testAccAWSELBConfig, 157 Check: resource.ComposeTestCheckFunc( 158 testAccCheckAWSELBExists("aws_elb.bar", &conf), 159 testAccCheckAWSELBAttributes(&conf), 160 resource.TestCheckResourceAttr( 161 "aws_elb.bar", "listener.206423021.instance_port", "8000"), 162 ), 163 }, 164 165 resource.TestStep{ 166 Config: testAccAWSELBConfigListener_update, 167 Check: resource.ComposeTestCheckFunc( 168 testAccCheckAWSELBExists("aws_elb.bar", &conf), 169 resource.TestCheckResourceAttr( 170 "aws_elb.bar", "listener.3931999347.instance_port", "8080"), 171 ), 172 }, 173 }, 174 }) 175 } 176 177 func TestAccAWSELB_HealthCheck(t *testing.T) { 178 var conf elb.LoadBalancerDescription 179 180 resource.Test(t, resource.TestCase{ 181 PreCheck: func() { testAccPreCheck(t) }, 182 Providers: testAccProviders, 183 CheckDestroy: testAccCheckAWSELBDestroy, 184 Steps: []resource.TestStep{ 185 resource.TestStep{ 186 Config: testAccAWSELBConfigHealthCheck, 187 Check: resource.ComposeTestCheckFunc( 188 testAccCheckAWSELBExists("aws_elb.bar", &conf), 189 testAccCheckAWSELBAttributesHealthCheck(&conf), 190 resource.TestCheckResourceAttr( 191 "aws_elb.bar", "health_check.3484319807.healthy_threshold", "5"), 192 resource.TestCheckResourceAttr( 193 "aws_elb.bar", "health_check.3484319807.unhealthy_threshold", "5"), 194 resource.TestCheckResourceAttr( 195 "aws_elb.bar", "health_check.3484319807.target", "HTTP:8000/"), 196 resource.TestCheckResourceAttr( 197 "aws_elb.bar", "health_check.3484319807.timeout", "30"), 198 resource.TestCheckResourceAttr( 199 "aws_elb.bar", "health_check.3484319807.interval", "60"), 200 ), 201 }, 202 }, 203 }) 204 } 205 206 func TestAccAWSELBUpdate_HealthCheck(t *testing.T) { 207 resource.Test(t, resource.TestCase{ 208 PreCheck: func() { testAccPreCheck(t) }, 209 Providers: testAccProviders, 210 CheckDestroy: testAccCheckAWSELBDestroy, 211 Steps: []resource.TestStep{ 212 resource.TestStep{ 213 Config: testAccAWSELBConfigHealthCheck, 214 Check: resource.ComposeTestCheckFunc( 215 resource.TestCheckResourceAttr( 216 "aws_elb.bar", "health_check.3484319807.healthy_threshold", "5"), 217 ), 218 }, 219 resource.TestStep{ 220 Config: testAccAWSELBConfigHealthCheck_update, 221 Check: resource.ComposeTestCheckFunc( 222 resource.TestCheckResourceAttr( 223 "aws_elb.bar", "health_check.2648756019.healthy_threshold", "10"), 224 ), 225 }, 226 }, 227 }) 228 } 229 230 func TestAccAWSELB_Timeout(t *testing.T) { 231 var conf elb.LoadBalancerDescription 232 233 resource.Test(t, resource.TestCase{ 234 PreCheck: func() { testAccPreCheck(t) }, 235 Providers: testAccProviders, 236 CheckDestroy: testAccCheckAWSELBDestroy, 237 Steps: []resource.TestStep{ 238 resource.TestStep{ 239 Config: testAccAWSELBConfigIdleTimeout, 240 Check: resource.ComposeTestCheckFunc( 241 testAccCheckAWSELBExists("aws_elb.bar", &conf), 242 resource.TestCheckResourceAttr( 243 "aws_elb.bar", "idle_timeout", "200", 244 ), 245 ), 246 }, 247 }, 248 }) 249 } 250 251 func TestAccAWSELBUpdate_Timeout(t *testing.T) { 252 resource.Test(t, resource.TestCase{ 253 PreCheck: func() { testAccPreCheck(t) }, 254 Providers: testAccProviders, 255 CheckDestroy: testAccCheckAWSELBDestroy, 256 Steps: []resource.TestStep{ 257 resource.TestStep{ 258 Config: testAccAWSELBConfigIdleTimeout, 259 Check: resource.ComposeTestCheckFunc( 260 resource.TestCheckResourceAttr( 261 "aws_elb.bar", "idle_timeout", "200", 262 ), 263 ), 264 }, 265 resource.TestStep{ 266 Config: testAccAWSELBConfigIdleTimeout_update, 267 Check: resource.ComposeTestCheckFunc( 268 resource.TestCheckResourceAttr( 269 "aws_elb.bar", "idle_timeout", "400", 270 ), 271 ), 272 }, 273 }, 274 }) 275 } 276 277 func TestAccAWSELB_ConnectionDraining(t *testing.T) { 278 resource.Test(t, resource.TestCase{ 279 PreCheck: func() { testAccPreCheck(t) }, 280 Providers: testAccProviders, 281 CheckDestroy: testAccCheckAWSELBDestroy, 282 Steps: []resource.TestStep{ 283 resource.TestStep{ 284 Config: testAccAWSELBConfigConnectionDraining, 285 Check: resource.ComposeTestCheckFunc( 286 resource.TestCheckResourceAttr( 287 "aws_elb.bar", "connection_draining", "true", 288 ), 289 resource.TestCheckResourceAttr( 290 "aws_elb.bar", "connection_draining_timeout", "400", 291 ), 292 ), 293 }, 294 }, 295 }) 296 } 297 298 func TestAccAWSELBUpdate_ConnectionDraining(t *testing.T) { 299 resource.Test(t, resource.TestCase{ 300 PreCheck: func() { testAccPreCheck(t) }, 301 Providers: testAccProviders, 302 CheckDestroy: testAccCheckAWSELBDestroy, 303 Steps: []resource.TestStep{ 304 resource.TestStep{ 305 Config: testAccAWSELBConfigConnectionDraining, 306 Check: resource.ComposeTestCheckFunc( 307 resource.TestCheckResourceAttr( 308 "aws_elb.bar", "connection_draining", "true", 309 ), 310 resource.TestCheckResourceAttr( 311 "aws_elb.bar", "connection_draining_timeout", "400", 312 ), 313 ), 314 }, 315 resource.TestStep{ 316 Config: testAccAWSELBConfigConnectionDraining_update_timeout, 317 Check: resource.ComposeTestCheckFunc( 318 resource.TestCheckResourceAttr( 319 "aws_elb.bar", "connection_draining", "true", 320 ), 321 resource.TestCheckResourceAttr( 322 "aws_elb.bar", "connection_draining_timeout", "600", 323 ), 324 ), 325 }, 326 resource.TestStep{ 327 Config: testAccAWSELBConfigConnectionDraining_update_disable, 328 Check: resource.ComposeTestCheckFunc( 329 resource.TestCheckResourceAttr( 330 "aws_elb.bar", "connection_draining", "false", 331 ), 332 ), 333 }, 334 }, 335 }) 336 } 337 338 func testAccCheckAWSELBDestroy(s *terraform.State) error { 339 conn := testAccProvider.Meta().(*AWSClient).elbconn 340 341 for _, rs := range s.RootModule().Resources { 342 if rs.Type != "aws_elb" { 343 continue 344 } 345 346 describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancersInput{ 347 LoadBalancerNames: []*string{aws.String(rs.Primary.ID)}, 348 }) 349 350 if err == nil { 351 if len(describe.LoadBalancerDescriptions) != 0 && 352 *describe.LoadBalancerDescriptions[0].LoadBalancerName == rs.Primary.ID { 353 return fmt.Errorf("ELB still exists") 354 } 355 } 356 357 // Verify the error 358 providerErr, ok := err.(aws.APIError) 359 if !ok { 360 return err 361 } 362 363 if providerErr.Code != "InvalidLoadBalancerName.NotFound" { 364 return fmt.Errorf("Unexpected error: %s", err) 365 } 366 } 367 368 return nil 369 } 370 371 func testAccCheckAWSELBAttributes(conf *elb.LoadBalancerDescription) resource.TestCheckFunc { 372 return func(s *terraform.State) error { 373 zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"} 374 azs := make([]string, 0, len(conf.AvailabilityZones)) 375 for _, x := range conf.AvailabilityZones { 376 azs = append(azs, *x) 377 } 378 sort.StringSlice(azs).Sort() 379 if !reflect.DeepEqual(azs, zones) { 380 return fmt.Errorf("bad availability_zones") 381 } 382 383 if *conf.LoadBalancerName != "foobar-terraform-test" { 384 return fmt.Errorf("bad name") 385 } 386 387 l := elb.Listener{ 388 InstancePort: aws.Long(int64(8000)), 389 InstanceProtocol: aws.String("HTTP"), 390 LoadBalancerPort: aws.Long(int64(80)), 391 Protocol: aws.String("HTTP"), 392 } 393 394 if !reflect.DeepEqual(conf.ListenerDescriptions[0].Listener, &l) { 395 return fmt.Errorf( 396 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 397 conf.ListenerDescriptions[0].Listener, 398 l) 399 } 400 401 if *conf.DNSName == "" { 402 return fmt.Errorf("empty dns_name") 403 } 404 405 return nil 406 } 407 } 408 409 func testAccCheckAWSELBAttributesHealthCheck(conf *elb.LoadBalancerDescription) resource.TestCheckFunc { 410 return func(s *terraform.State) error { 411 zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"} 412 azs := make([]string, 0, len(conf.AvailabilityZones)) 413 for _, x := range conf.AvailabilityZones { 414 azs = append(azs, *x) 415 } 416 sort.StringSlice(azs).Sort() 417 if !reflect.DeepEqual(azs, zones) { 418 return fmt.Errorf("bad availability_zones") 419 } 420 421 if *conf.LoadBalancerName != "foobar-terraform-test" { 422 return fmt.Errorf("bad name") 423 } 424 425 check := &elb.HealthCheck{ 426 Timeout: aws.Long(int64(30)), 427 UnhealthyThreshold: aws.Long(int64(5)), 428 HealthyThreshold: aws.Long(int64(5)), 429 Interval: aws.Long(int64(60)), 430 Target: aws.String("HTTP:8000/"), 431 } 432 433 if !reflect.DeepEqual(conf.HealthCheck, check) { 434 return fmt.Errorf( 435 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 436 conf.HealthCheck, 437 check) 438 } 439 440 if *conf.DNSName == "" { 441 return fmt.Errorf("empty dns_name") 442 } 443 444 return nil 445 } 446 } 447 448 func testAccCheckAWSELBExists(n string, res *elb.LoadBalancerDescription) resource.TestCheckFunc { 449 return func(s *terraform.State) error { 450 rs, ok := s.RootModule().Resources[n] 451 if !ok { 452 return fmt.Errorf("Not found: %s", n) 453 } 454 455 if rs.Primary.ID == "" { 456 return fmt.Errorf("No ELB ID is set") 457 } 458 459 conn := testAccProvider.Meta().(*AWSClient).elbconn 460 461 describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancersInput{ 462 LoadBalancerNames: []*string{aws.String(rs.Primary.ID)}, 463 }) 464 465 if err != nil { 466 return err 467 } 468 469 if len(describe.LoadBalancerDescriptions) != 1 || 470 *describe.LoadBalancerDescriptions[0].LoadBalancerName != rs.Primary.ID { 471 return fmt.Errorf("ELB not found") 472 } 473 474 *res = *describe.LoadBalancerDescriptions[0] 475 476 return nil 477 } 478 } 479 480 const testAccAWSELBConfig = ` 481 resource "aws_elb" "bar" { 482 name = "foobar-terraform-test" 483 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 484 485 listener { 486 instance_port = 8000 487 instance_protocol = "http" 488 lb_port = 80 489 lb_protocol = "http" 490 } 491 492 tags { 493 bar = "baz" 494 } 495 496 cross_zone_load_balancing = true 497 } 498 ` 499 500 const testAccAWSELBConfig_TagUpdate = ` 501 resource "aws_elb" "bar" { 502 name = "foobar-terraform-test" 503 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 504 505 listener { 506 instance_port = 8000 507 instance_protocol = "http" 508 lb_port = 80 509 lb_protocol = "http" 510 } 511 512 tags { 513 foo = "bar" 514 new = "type" 515 } 516 517 cross_zone_load_balancing = true 518 } 519 ` 520 521 const testAccAWSELBConfigNewInstance = ` 522 resource "aws_elb" "bar" { 523 name = "foobar-terraform-test" 524 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 525 526 listener { 527 instance_port = 8000 528 instance_protocol = "http" 529 lb_port = 80 530 lb_protocol = "http" 531 } 532 533 instances = ["${aws_instance.foo.id}"] 534 } 535 536 resource "aws_instance" "foo" { 537 # us-west-2 538 ami = "ami-043a5034" 539 instance_type = "t1.micro" 540 } 541 ` 542 543 const testAccAWSELBConfigListenerSSLCertificateId = ` 544 resource "aws_elb" "bar" { 545 name = "foobar-terraform-test" 546 availability_zones = ["us-west-2a"] 547 548 listener { 549 instance_port = 8000 550 instance_protocol = "http" 551 ssl_certificate_id = "%s" 552 lb_port = 443 553 lb_protocol = "https" 554 } 555 } 556 ` 557 558 const testAccAWSELBConfigHealthCheck = ` 559 resource "aws_elb" "bar" { 560 name = "foobar-terraform-test" 561 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 562 563 listener { 564 instance_port = 8000 565 instance_protocol = "http" 566 lb_port = 80 567 lb_protocol = "http" 568 } 569 570 health_check { 571 healthy_threshold = 5 572 unhealthy_threshold = 5 573 target = "HTTP:8000/" 574 interval = 60 575 timeout = 30 576 } 577 } 578 ` 579 580 const testAccAWSELBConfigHealthCheck_update = ` 581 resource "aws_elb" "bar" { 582 name = "foobar-terraform-test" 583 availability_zones = ["us-west-2a"] 584 585 listener { 586 instance_port = 8000 587 instance_protocol = "http" 588 lb_port = 80 589 lb_protocol = "http" 590 } 591 592 health_check { 593 healthy_threshold = 10 594 unhealthy_threshold = 5 595 target = "HTTP:8000/" 596 interval = 60 597 timeout = 30 598 } 599 } 600 ` 601 602 const testAccAWSELBConfigListener_update = ` 603 resource "aws_elb" "bar" { 604 name = "foobar-terraform-test" 605 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 606 607 listener { 608 instance_port = 8080 609 instance_protocol = "http" 610 lb_port = 80 611 lb_protocol = "http" 612 } 613 } 614 ` 615 616 const testAccAWSELBConfigIdleTimeout = ` 617 resource "aws_elb" "bar" { 618 name = "foobar-terraform-test" 619 availability_zones = ["us-west-2a"] 620 621 listener { 622 instance_port = 8000 623 instance_protocol = "http" 624 lb_port = 80 625 lb_protocol = "http" 626 } 627 628 idle_timeout = 200 629 } 630 ` 631 632 const testAccAWSELBConfigIdleTimeout_update = ` 633 resource "aws_elb" "bar" { 634 name = "foobar-terraform-test" 635 availability_zones = ["us-west-2a"] 636 637 listener { 638 instance_port = 8000 639 instance_protocol = "http" 640 lb_port = 80 641 lb_protocol = "http" 642 } 643 644 idle_timeout = 400 645 } 646 ` 647 648 const testAccAWSELBConfigConnectionDraining = ` 649 resource "aws_elb" "bar" { 650 name = "foobar-terraform-test" 651 availability_zones = ["us-west-2a"] 652 653 listener { 654 instance_port = 8000 655 instance_protocol = "http" 656 lb_port = 80 657 lb_protocol = "http" 658 } 659 660 connection_draining = true 661 connection_draining_timeout = 400 662 } 663 ` 664 665 const testAccAWSELBConfigConnectionDraining_update_timeout = ` 666 resource "aws_elb" "bar" { 667 name = "foobar-terraform-test" 668 availability_zones = ["us-west-2a"] 669 670 listener { 671 instance_port = 8000 672 instance_protocol = "http" 673 lb_port = 80 674 lb_protocol = "http" 675 } 676 677 connection_draining = true 678 connection_draining_timeout = 400 679 } 680 ` 681 682 const testAccAWSELBConfigConnectionDraining_update_disable = ` 683 resource "aws_elb" "bar" { 684 name = "foobar-terraform-test" 685 availability_zones = ["us-west-2a"] 686 687 listener { 688 instance_port = 8000 689 instance_protocol = "http" 690 lb_port = 80 691 lb_protocol = "http" 692 } 693 694 connection_draining = false 695 } 696 `