github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_autoscaling_group_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "reflect" 6 "regexp" 7 "strings" 8 "testing" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/aws/awserr" 12 "github.com/aws/aws-sdk-go/service/autoscaling" 13 "github.com/hashicorp/terraform/helper/resource" 14 "github.com/hashicorp/terraform/terraform" 15 ) 16 17 func TestAccAWSAutoScalingGroup_basic(t *testing.T) { 18 var group autoscaling.Group 19 var lc autoscaling.LaunchConfiguration 20 21 resource.Test(t, resource.TestCase{ 22 PreCheck: func() { testAccPreCheck(t) }, 23 Providers: testAccProviders, 24 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 25 Steps: []resource.TestStep{ 26 resource.TestStep{ 27 Config: testAccAWSAutoScalingGroupConfig, 28 Check: resource.ComposeTestCheckFunc( 29 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 30 testAccCheckAWSAutoScalingGroupHealthyCapacity(&group, 2), 31 testAccCheckAWSAutoScalingGroupAttributes(&group), 32 resource.TestCheckResourceAttr( 33 "aws_autoscaling_group.bar", "availability_zones.2487133097", "us-west-2a"), 34 resource.TestCheckResourceAttr( 35 "aws_autoscaling_group.bar", "name", "foobar3-terraform-test"), 36 resource.TestCheckResourceAttr( 37 "aws_autoscaling_group.bar", "max_size", "5"), 38 resource.TestCheckResourceAttr( 39 "aws_autoscaling_group.bar", "min_size", "2"), 40 resource.TestCheckResourceAttr( 41 "aws_autoscaling_group.bar", "health_check_grace_period", "300"), 42 resource.TestCheckResourceAttr( 43 "aws_autoscaling_group.bar", "health_check_type", "ELB"), 44 resource.TestCheckResourceAttr( 45 "aws_autoscaling_group.bar", "desired_capacity", "4"), 46 resource.TestCheckResourceAttr( 47 "aws_autoscaling_group.bar", "force_delete", "true"), 48 resource.TestCheckResourceAttr( 49 "aws_autoscaling_group.bar", "termination_policies.0", "OldestInstance"), 50 resource.TestCheckResourceAttr( 51 "aws_autoscaling_group.bar", "termination_policies.1", "ClosestToNextInstanceHour"), 52 ), 53 }, 54 55 resource.TestStep{ 56 Config: testAccAWSAutoScalingGroupConfigUpdate, 57 Check: resource.ComposeTestCheckFunc( 58 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 59 testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.new", &lc), 60 resource.TestCheckResourceAttr( 61 "aws_autoscaling_group.bar", "desired_capacity", "5"), 62 resource.TestCheckResourceAttr( 63 "aws_autoscaling_group.bar", "termination_policies.0", "ClosestToNextInstanceHour"), 64 testLaunchConfigurationName("aws_autoscaling_group.bar", &lc), 65 testAccCheckAutoscalingTags(&group.Tags, "Bar", map[string]interface{}{ 66 "value": "bar-foo", 67 "propagate_at_launch": true, 68 }), 69 ), 70 }, 71 }, 72 }) 73 } 74 75 func TestAccAWSAutoScalingGroup_autoGeneratedName(t *testing.T) { 76 asgNameRegexp := regexp.MustCompile("^tf-asg-") 77 78 resource.Test(t, resource.TestCase{ 79 PreCheck: func() { testAccPreCheck(t) }, 80 Providers: testAccProviders, 81 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 82 Steps: []resource.TestStep{ 83 resource.TestStep{ 84 Config: testAccAWSAutoScalingGroupConfig_autoGeneratedName, 85 Check: resource.ComposeTestCheckFunc( 86 resource.TestMatchResourceAttr( 87 "aws_autoscaling_group.bar", "name", asgNameRegexp), 88 ), 89 }, 90 }, 91 }) 92 93 } 94 95 func TestAccAWSAutoScalingGroup_tags(t *testing.T) { 96 var group autoscaling.Group 97 98 resource.Test(t, resource.TestCase{ 99 PreCheck: func() { testAccPreCheck(t) }, 100 Providers: testAccProviders, 101 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 102 Steps: []resource.TestStep{ 103 resource.TestStep{ 104 Config: testAccAWSAutoScalingGroupConfig, 105 Check: resource.ComposeTestCheckFunc( 106 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 107 testAccCheckAutoscalingTags(&group.Tags, "Foo", map[string]interface{}{ 108 "value": "foo-bar", 109 "propagate_at_launch": true, 110 }), 111 ), 112 }, 113 114 resource.TestStep{ 115 Config: testAccAWSAutoScalingGroupConfigUpdate, 116 Check: resource.ComposeTestCheckFunc( 117 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 118 testAccCheckAutoscalingTagNotExists(&group.Tags, "Foo"), 119 testAccCheckAutoscalingTags(&group.Tags, "Bar", map[string]interface{}{ 120 "value": "bar-foo", 121 "propagate_at_launch": true, 122 }), 123 ), 124 }, 125 }, 126 }) 127 } 128 129 func TestAccAWSAutoScalingGroup_VpcUpdates(t *testing.T) { 130 var group autoscaling.Group 131 132 resource.Test(t, resource.TestCase{ 133 PreCheck: func() { testAccPreCheck(t) }, 134 Providers: testAccProviders, 135 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 136 Steps: []resource.TestStep{ 137 resource.TestStep{ 138 Config: testAccAWSAutoScalingGroupConfigWithAZ, 139 Check: resource.ComposeTestCheckFunc( 140 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 141 ), 142 }, 143 144 resource.TestStep{ 145 Config: testAccAWSAutoScalingGroupConfigWithVPCIdent, 146 Check: resource.ComposeTestCheckFunc( 147 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 148 testAccCheckAWSAutoScalingGroupAttributesVPCZoneIdentifer(&group), 149 ), 150 }, 151 }, 152 }) 153 } 154 155 func TestAccAWSAutoScalingGroup_WithLoadBalancer(t *testing.T) { 156 var group autoscaling.Group 157 158 resource.Test(t, resource.TestCase{ 159 PreCheck: func() { testAccPreCheck(t) }, 160 Providers: testAccProviders, 161 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 162 Steps: []resource.TestStep{ 163 resource.TestStep{ 164 Config: testAccAWSAutoScalingGroupConfigWithLoadBalancer, 165 Check: resource.ComposeTestCheckFunc( 166 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 167 testAccCheckAWSAutoScalingGroupAttributesLoadBalancer(&group), 168 ), 169 }, 170 }, 171 }) 172 } 173 174 func TestAccAWSAutoScalingGroup_withPlacementGroup(t *testing.T) { 175 var group autoscaling.Group 176 177 resource.Test(t, resource.TestCase{ 178 PreCheck: func() { testAccPreCheck(t) }, 179 Providers: testAccProviders, 180 CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, 181 Steps: []resource.TestStep{ 182 resource.TestStep{ 183 Config: testAccAWSAutoScalingGroupConfig_withPlacementGroup, 184 Check: resource.ComposeTestCheckFunc( 185 testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group), 186 resource.TestCheckResourceAttr( 187 "aws_autoscaling_group.bar", "placement_group", "test"), 188 ), 189 }, 190 }, 191 }) 192 } 193 194 func testAccCheckAWSAutoScalingGroupDestroy(s *terraform.State) error { 195 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 196 197 for _, rs := range s.RootModule().Resources { 198 if rs.Type != "aws_autoscaling_group" { 199 continue 200 } 201 202 // Try to find the Group 203 describeGroups, err := conn.DescribeAutoScalingGroups( 204 &autoscaling.DescribeAutoScalingGroupsInput{ 205 AutoScalingGroupNames: []*string{aws.String(rs.Primary.ID)}, 206 }) 207 208 if err == nil { 209 if len(describeGroups.AutoScalingGroups) != 0 && 210 *describeGroups.AutoScalingGroups[0].AutoScalingGroupName == rs.Primary.ID { 211 return fmt.Errorf("AutoScaling Group still exists") 212 } 213 } 214 215 // Verify the error 216 ec2err, ok := err.(awserr.Error) 217 if !ok { 218 return err 219 } 220 if ec2err.Code() != "InvalidGroup.NotFound" { 221 return err 222 } 223 } 224 225 return nil 226 } 227 228 func testAccCheckAWSAutoScalingGroupAttributes(group *autoscaling.Group) resource.TestCheckFunc { 229 return func(s *terraform.State) error { 230 if *group.AvailabilityZones[0] != "us-west-2a" { 231 return fmt.Errorf("Bad availability_zones: %#v", group.AvailabilityZones[0]) 232 } 233 234 if *group.AutoScalingGroupName != "foobar3-terraform-test" { 235 return fmt.Errorf("Bad name: %s", *group.AutoScalingGroupName) 236 } 237 238 if *group.MaxSize != 5 { 239 return fmt.Errorf("Bad max_size: %d", *group.MaxSize) 240 } 241 242 if *group.MinSize != 2 { 243 return fmt.Errorf("Bad max_size: %d", *group.MinSize) 244 } 245 246 if *group.HealthCheckType != "ELB" { 247 return fmt.Errorf("Bad health_check_type,\nexpected: %s\ngot: %s", "ELB", *group.HealthCheckType) 248 } 249 250 if *group.HealthCheckGracePeriod != 300 { 251 return fmt.Errorf("Bad health_check_grace_period: %d", *group.HealthCheckGracePeriod) 252 } 253 254 if *group.DesiredCapacity != 4 { 255 return fmt.Errorf("Bad desired_capacity: %d", *group.DesiredCapacity) 256 } 257 258 if *group.LaunchConfigurationName == "" { 259 return fmt.Errorf("Bad launch configuration name: %s", *group.LaunchConfigurationName) 260 } 261 262 t := &autoscaling.TagDescription{ 263 Key: aws.String("Foo"), 264 Value: aws.String("foo-bar"), 265 PropagateAtLaunch: aws.Bool(true), 266 ResourceType: aws.String("auto-scaling-group"), 267 ResourceId: group.AutoScalingGroupName, 268 } 269 270 if !reflect.DeepEqual(group.Tags[0], t) { 271 return fmt.Errorf( 272 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 273 group.Tags[0], 274 t) 275 } 276 277 return nil 278 } 279 } 280 281 func testAccCheckAWSAutoScalingGroupAttributesLoadBalancer(group *autoscaling.Group) resource.TestCheckFunc { 282 return func(s *terraform.State) error { 283 if *group.LoadBalancerNames[0] != "foobar-terraform-test" { 284 return fmt.Errorf("Bad load_balancers: %#v", group.LoadBalancerNames[0]) 285 } 286 287 return nil 288 } 289 } 290 291 func testAccCheckAWSAutoScalingGroupExists(n string, group *autoscaling.Group) resource.TestCheckFunc { 292 return func(s *terraform.State) error { 293 rs, ok := s.RootModule().Resources[n] 294 if !ok { 295 return fmt.Errorf("Not found: %s", n) 296 } 297 298 if rs.Primary.ID == "" { 299 return fmt.Errorf("No AutoScaling Group ID is set") 300 } 301 302 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 303 304 describeGroups, err := conn.DescribeAutoScalingGroups( 305 &autoscaling.DescribeAutoScalingGroupsInput{ 306 AutoScalingGroupNames: []*string{aws.String(rs.Primary.ID)}, 307 }) 308 309 if err != nil { 310 return err 311 } 312 313 if len(describeGroups.AutoScalingGroups) != 1 || 314 *describeGroups.AutoScalingGroups[0].AutoScalingGroupName != rs.Primary.ID { 315 return fmt.Errorf("AutoScaling Group not found") 316 } 317 318 *group = *describeGroups.AutoScalingGroups[0] 319 320 return nil 321 } 322 } 323 324 func testLaunchConfigurationName(n string, lc *autoscaling.LaunchConfiguration) resource.TestCheckFunc { 325 return func(s *terraform.State) error { 326 rs, ok := s.RootModule().Resources[n] 327 if !ok { 328 return fmt.Errorf("Not found: %s", n) 329 } 330 331 if *lc.LaunchConfigurationName != rs.Primary.Attributes["launch_configuration"] { 332 return fmt.Errorf("Launch configuration names do not match") 333 } 334 335 return nil 336 } 337 } 338 339 func testAccCheckAWSAutoScalingGroupHealthyCapacity( 340 g *autoscaling.Group, exp int) resource.TestCheckFunc { 341 return func(s *terraform.State) error { 342 healthy := 0 343 for _, i := range g.Instances { 344 if i.HealthStatus == nil { 345 continue 346 } 347 if strings.EqualFold(*i.HealthStatus, "Healthy") { 348 healthy++ 349 } 350 } 351 if healthy < exp { 352 return fmt.Errorf("Expected at least %d healthy, got %d.", exp, healthy) 353 } 354 return nil 355 } 356 } 357 358 func testAccCheckAWSAutoScalingGroupAttributesVPCZoneIdentifer(group *autoscaling.Group) resource.TestCheckFunc { 359 return func(s *terraform.State) error { 360 // Grab Subnet Ids 361 var subnets []string 362 for _, rs := range s.RootModule().Resources { 363 if rs.Type != "aws_subnet" { 364 continue 365 } 366 subnets = append(subnets, rs.Primary.Attributes["id"]) 367 } 368 369 if group.VPCZoneIdentifier == nil { 370 return fmt.Errorf("Bad VPC Zone Identifier\nexpected: %s\ngot nil", subnets) 371 } 372 373 zones := strings.Split(*group.VPCZoneIdentifier, ",") 374 375 remaining := len(zones) 376 for _, z := range zones { 377 for _, s := range subnets { 378 if z == s { 379 remaining-- 380 } 381 } 382 } 383 384 if remaining != 0 { 385 return fmt.Errorf("Bad VPC Zone Identifier match\nexpected: %s\ngot:%s", zones, subnets) 386 } 387 388 return nil 389 } 390 } 391 392 const testAccAWSAutoScalingGroupConfig_autoGeneratedName = ` 393 resource "aws_launch_configuration" "foobar" { 394 image_id = "ami-21f78e11" 395 instance_type = "t1.micro" 396 } 397 398 resource "aws_autoscaling_group" "bar" { 399 availability_zones = ["us-west-2a"] 400 max_size = 1 401 min_size = 1 402 health_check_grace_period = 300 403 health_check_type = "ELB" 404 desired_capacity = 1 405 force_delete = true 406 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 407 408 launch_configuration = "${aws_launch_configuration.foobar.name}" 409 410 tag { 411 key = "Foo" 412 value = "foo-bar" 413 propagate_at_launch = true 414 } 415 } 416 ` 417 418 const testAccAWSAutoScalingGroupConfig = ` 419 resource "aws_launch_configuration" "foobar" { 420 image_id = "ami-21f78e11" 421 instance_type = "t1.micro" 422 } 423 424 resource "aws_placement_group" "test" { 425 name = "test" 426 strategy = "cluster" 427 } 428 429 resource "aws_autoscaling_group" "bar" { 430 availability_zones = ["us-west-2a"] 431 name = "foobar3-terraform-test" 432 max_size = 5 433 min_size = 2 434 health_check_grace_period = 300 435 health_check_type = "ELB" 436 desired_capacity = 4 437 force_delete = true 438 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 439 440 launch_configuration = "${aws_launch_configuration.foobar.name}" 441 442 tag { 443 key = "Foo" 444 value = "foo-bar" 445 propagate_at_launch = true 446 } 447 } 448 ` 449 450 const testAccAWSAutoScalingGroupConfigUpdate = ` 451 resource "aws_launch_configuration" "foobar" { 452 image_id = "ami-21f78e11" 453 instance_type = "t1.micro" 454 } 455 456 resource "aws_launch_configuration" "new" { 457 image_id = "ami-21f78e11" 458 instance_type = "t1.micro" 459 } 460 461 resource "aws_autoscaling_group" "bar" { 462 availability_zones = ["us-west-2a"] 463 name = "foobar3-terraform-test" 464 max_size = 5 465 min_size = 2 466 health_check_grace_period = 300 467 health_check_type = "ELB" 468 desired_capacity = 5 469 force_delete = true 470 termination_policies = ["ClosestToNextInstanceHour"] 471 472 launch_configuration = "${aws_launch_configuration.new.name}" 473 474 tag { 475 key = "Bar" 476 value = "bar-foo" 477 propagate_at_launch = true 478 } 479 } 480 ` 481 482 const testAccAWSAutoScalingGroupConfigWithLoadBalancer = ` 483 resource "aws_vpc" "foo" { 484 cidr_block = "10.1.0.0/16" 485 tags { Name = "tf-asg-test" } 486 } 487 488 resource "aws_internet_gateway" "gw" { 489 vpc_id = "${aws_vpc.foo.id}" 490 } 491 492 resource "aws_subnet" "foo" { 493 cidr_block = "10.1.1.0/24" 494 vpc_id = "${aws_vpc.foo.id}" 495 } 496 497 resource "aws_security_group" "foo" { 498 vpc_id="${aws_vpc.foo.id}" 499 500 ingress { 501 protocol = "-1" 502 from_port = 0 503 to_port = 0 504 cidr_blocks = ["0.0.0.0/0"] 505 } 506 507 egress { 508 protocol = "-1" 509 from_port = 0 510 to_port = 0 511 cidr_blocks = ["0.0.0.0/0"] 512 } 513 } 514 515 resource "aws_elb" "bar" { 516 name = "foobar-terraform-test" 517 subnets = ["${aws_subnet.foo.id}"] 518 security_groups = ["${aws_security_group.foo.id}"] 519 520 listener { 521 instance_port = 80 522 instance_protocol = "http" 523 lb_port = 80 524 lb_protocol = "http" 525 } 526 527 health_check { 528 healthy_threshold = 2 529 unhealthy_threshold = 2 530 target = "HTTP:80/" 531 interval = 5 532 timeout = 2 533 } 534 535 depends_on = ["aws_internet_gateway.gw"] 536 } 537 538 resource "aws_launch_configuration" "foobar" { 539 // need an AMI that listens on :80 at boot, this is: 540 // bitnami-nginxstack-1.6.1-0-linux-ubuntu-14.04.1-x86_64-hvm-ebs-ami-99f5b1a9-3 541 image_id = "ami-b5b3fc85" 542 instance_type = "t2.micro" 543 security_groups = ["${aws_security_group.foo.id}"] 544 } 545 546 resource "aws_autoscaling_group" "bar" { 547 availability_zones = ["${aws_subnet.foo.availability_zone}"] 548 vpc_zone_identifier = ["${aws_subnet.foo.id}"] 549 name = "foobar3-terraform-test" 550 max_size = 2 551 min_size = 2 552 health_check_grace_period = 300 553 health_check_type = "ELB" 554 min_elb_capacity = 2 555 force_delete = true 556 557 launch_configuration = "${aws_launch_configuration.foobar.name}" 558 load_balancers = ["${aws_elb.bar.name}"] 559 } 560 ` 561 562 const testAccAWSAutoScalingGroupConfigWithAZ = ` 563 resource "aws_vpc" "default" { 564 cidr_block = "10.0.0.0/16" 565 tags { 566 Name = "terraform-test" 567 } 568 } 569 570 resource "aws_subnet" "main" { 571 vpc_id = "${aws_vpc.default.id}" 572 cidr_block = "10.0.1.0/24" 573 availability_zone = "us-west-2a" 574 tags { 575 Name = "terraform-test" 576 } 577 } 578 579 resource "aws_subnet" "alt" { 580 vpc_id = "${aws_vpc.default.id}" 581 cidr_block = "10.0.2.0/24" 582 availability_zone = "us-west-2b" 583 tags { 584 Name = "asg-vpc-thing" 585 } 586 } 587 588 resource "aws_launch_configuration" "foobar" { 589 name = "vpc-asg-test" 590 image_id = "ami-b5b3fc85" 591 instance_type = "t2.micro" 592 } 593 594 resource "aws_autoscaling_group" "bar" { 595 availability_zones = ["us-west-2a"] 596 name = "vpc-asg-test" 597 max_size = 2 598 min_size = 1 599 health_check_grace_period = 300 600 health_check_type = "ELB" 601 desired_capacity = 1 602 force_delete = true 603 termination_policies = ["OldestInstance"] 604 launch_configuration = "${aws_launch_configuration.foobar.name}" 605 } 606 ` 607 608 const testAccAWSAutoScalingGroupConfigWithVPCIdent = ` 609 resource "aws_vpc" "default" { 610 cidr_block = "10.0.0.0/16" 611 tags { 612 Name = "terraform-test" 613 } 614 } 615 616 resource "aws_subnet" "main" { 617 vpc_id = "${aws_vpc.default.id}" 618 cidr_block = "10.0.1.0/24" 619 availability_zone = "us-west-2a" 620 tags { 621 Name = "terraform-test" 622 } 623 } 624 625 resource "aws_subnet" "alt" { 626 vpc_id = "${aws_vpc.default.id}" 627 cidr_block = "10.0.2.0/24" 628 availability_zone = "us-west-2b" 629 tags { 630 Name = "asg-vpc-thing" 631 } 632 } 633 634 resource "aws_launch_configuration" "foobar" { 635 name = "vpc-asg-test" 636 image_id = "ami-b5b3fc85" 637 instance_type = "t2.micro" 638 } 639 640 resource "aws_autoscaling_group" "bar" { 641 vpc_zone_identifier = [ 642 "${aws_subnet.main.id}", 643 "${aws_subnet.alt.id}", 644 ] 645 name = "vpc-asg-test" 646 max_size = 2 647 min_size = 1 648 health_check_grace_period = 300 649 health_check_type = "ELB" 650 desired_capacity = 1 651 force_delete = true 652 termination_policies = ["OldestInstance"] 653 launch_configuration = "${aws_launch_configuration.foobar.name}" 654 } 655 ` 656 657 const testAccAWSAutoScalingGroupConfig_withPlacementGroup = ` 658 resource "aws_launch_configuration" "foobar" { 659 image_id = "ami-21f78e11" 660 instance_type = "c3.large" 661 } 662 663 resource "aws_placement_group" "test" { 664 name = "test" 665 strategy = "cluster" 666 } 667 668 resource "aws_autoscaling_group" "bar" { 669 availability_zones = ["us-west-2a"] 670 name = "foobar3-terraform-test" 671 max_size = 1 672 min_size = 1 673 health_check_grace_period = 300 674 health_check_type = "ELB" 675 desired_capacity = 1 676 force_delete = true 677 termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] 678 placement_group = "${aws_placement_group.test.name}" 679 680 launch_configuration = "${aws_launch_configuration.foobar.name}" 681 682 tag { 683 key = "Foo" 684 value = "foo-bar" 685 propagate_at_launch = true 686 } 687 } 688 `