2014-07-14 17:36:25 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/goamz/autoscaling"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAWSAutoScalingGroup(t *testing.T) {
|
|
|
|
var group autoscaling.AutoScalingGroup
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2014-07-14 17:44:15 +02:00
|
|
|
CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy,
|
2014-07-14 17:36:25 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSAutoScalingGroupConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
|
|
|
|
testAccCheckAWSAutoScalingGroupAttributes(&group),
|
|
|
|
resource.TestCheckResourceAttr(
|
2014-07-28 17:27:50 +02:00
|
|
|
"aws_autoscaling_group.bar", "availability_zones.#.0", "us-west-2a"),
|
2014-07-15 00:48:22 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_autoscaling_group.bar", "name", "foobar3-terraform-test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_autoscaling_group.bar", "max_size", "5"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_autoscaling_group.bar", "min_size", "2"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_autoscaling_group.bar", "health_check_grace_period", "300"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_autoscaling_group.bar", "health_check_type", "ELB"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2014-07-29 16:42:31 +02:00
|
|
|
"aws_autoscaling_group.bar", "desired_capacity", "4"),
|
2014-07-15 00:48:22 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2014-07-14 17:41:49 +02:00
|
|
|
"aws_autoscaling_group.bar", "force_delete", "true"),
|
2014-07-14 17:36:25 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-08-20 19:40:43 +02:00
|
|
|
func TestAccAWSAutoScalingGroupWithLoadBalancer(t *testing.T) {
|
|
|
|
var group autoscaling.AutoScalingGroup
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSAutoScalingGroupConfigWithLoadBalancer,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
|
|
|
|
testAccCheckAWSAutoScalingGroupAttributesLoadBalancer(&group),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2014-07-14 17:36:25 +02:00
|
|
|
func testAccCheckAWSAutoScalingGroupDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.autoscalingconn
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-14 17:36:25 +02:00
|
|
|
if rs.Type != "aws_autoscaling_group" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the Group
|
2014-07-15 00:48:22 +02:00
|
|
|
describeGroups, err := conn.DescribeAutoScalingGroups(
|
|
|
|
&autoscaling.DescribeAutoScalingGroups{
|
2014-09-17 02:44:42 +02:00
|
|
|
Names: []string{rs.Primary.ID},
|
2014-07-15 00:48:22 +02:00
|
|
|
})
|
2014-07-14 17:36:25 +02:00
|
|
|
|
2014-07-15 00:48:22 +02:00
|
|
|
if err == nil {
|
|
|
|
if len(describeGroups.AutoScalingGroups) != 0 &&
|
2014-09-17 02:44:42 +02:00
|
|
|
describeGroups.AutoScalingGroups[0].Name == rs.Primary.ID {
|
2014-07-15 00:48:22 +02:00
|
|
|
return fmt.Errorf("AutoScaling Group still exists")
|
|
|
|
}
|
|
|
|
}
|
2014-07-14 17:36:25 +02:00
|
|
|
|
|
|
|
// Verify the error
|
|
|
|
ec2err, ok := err.(*autoscaling.Error)
|
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ec2err.Code != "InvalidGroup.NotFound" {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSAutoScalingGroupAttributes(group *autoscaling.AutoScalingGroup) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-07-28 17:27:50 +02:00
|
|
|
if group.AvailabilityZones[0].AvailabilityZone != "us-west-2a" {
|
2014-07-14 17:36:25 +02:00
|
|
|
return fmt.Errorf("Bad availability_zones: %s", group.AvailabilityZones[0].AvailabilityZone)
|
|
|
|
}
|
|
|
|
|
|
|
|
if group.Name != "foobar3-terraform-test" {
|
|
|
|
return fmt.Errorf("Bad name: %s", group.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if group.MaxSize != 5 {
|
2014-08-25 06:50:35 +02:00
|
|
|
return fmt.Errorf("Bad max_size: %d", group.MaxSize)
|
2014-07-14 17:36:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if group.MinSize != 2 {
|
2014-08-25 06:50:35 +02:00
|
|
|
return fmt.Errorf("Bad max_size: %d", group.MinSize)
|
2014-07-14 17:36:25 +02:00
|
|
|
}
|
|
|
|
|
2014-07-15 17:13:18 +02:00
|
|
|
if group.HealthCheckType != "ELB" {
|
2014-07-14 17:36:25 +02:00
|
|
|
return fmt.Errorf("Bad health_check_type: %s", group.HealthCheckType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if group.HealthCheckGracePeriod != 300 {
|
2014-08-25 06:50:35 +02:00
|
|
|
return fmt.Errorf("Bad health_check_grace_period: %d", group.HealthCheckGracePeriod)
|
2014-07-14 17:36:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if group.DesiredCapacity != 4 {
|
2014-08-25 06:50:35 +02:00
|
|
|
return fmt.Errorf("Bad desired_capacity: %d", group.DesiredCapacity)
|
2014-07-14 17:36:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if group.LaunchConfigurationName != "" {
|
2014-08-25 06:50:35 +02:00
|
|
|
return fmt.Errorf("Bad desired_capacity: %d", group.DesiredCapacity)
|
2014-07-14 17:36:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 19:40:43 +02:00
|
|
|
func testAccCheckAWSAutoScalingGroupAttributesLoadBalancer(group *autoscaling.AutoScalingGroup) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if group.LoadBalancerNames[0].LoadBalancerName != "foobar-terraform-test" {
|
|
|
|
return fmt.Errorf("Bad load_balancers: %s", group.LoadBalancerNames[0].LoadBalancerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-14 17:36:25 +02:00
|
|
|
func testAccCheckAWSAutoScalingGroupExists(n string, group *autoscaling.AutoScalingGroup) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:44:42 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-07-14 17:36:25 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-07-14 17:36:25 +02:00
|
|
|
return fmt.Errorf("No AutoScaling Group ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.autoscalingconn
|
|
|
|
|
|
|
|
describeOpts := autoscaling.DescribeAutoScalingGroups{
|
2014-09-17 02:44:42 +02:00
|
|
|
Names: []string{rs.Primary.ID},
|
2014-07-14 17:36:25 +02:00
|
|
|
}
|
|
|
|
describeGroups, err := conn.DescribeAutoScalingGroups(&describeOpts)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(describeGroups.AutoScalingGroups) != 1 ||
|
2014-09-17 02:44:42 +02:00
|
|
|
describeGroups.AutoScalingGroups[0].Name != rs.Primary.ID {
|
2014-07-14 17:36:25 +02:00
|
|
|
return fmt.Errorf("AutoScaling Group not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*group = describeGroups.AutoScalingGroups[0]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccAWSAutoScalingGroupConfig = `
|
|
|
|
resource "aws_launch_configuration" "foobar" {
|
2014-07-15 17:13:18 +02:00
|
|
|
name = "foobarautoscaling-terraform-test"
|
2014-07-28 17:27:50 +02:00
|
|
|
image_id = "ami-21f78e11"
|
2014-07-14 17:36:25 +02:00
|
|
|
instance_type = "t1.micro"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_autoscaling_group" "bar" {
|
2014-07-28 17:27:50 +02:00
|
|
|
availability_zones = ["us-west-2a"]
|
2014-07-14 17:36:25 +02:00
|
|
|
name = "foobar3-terraform-test"
|
|
|
|
max_size = 5
|
|
|
|
min_size = 2
|
|
|
|
health_check_grace_period = 300
|
|
|
|
health_check_type = "ELB"
|
2014-07-28 17:27:50 +02:00
|
|
|
desired_capacity = 4
|
2014-07-14 17:36:25 +02:00
|
|
|
force_delete = true
|
|
|
|
|
|
|
|
launch_configuration = "${aws_launch_configuration.foobar.name}"
|
|
|
|
}
|
|
|
|
`
|
2014-08-20 19:40:43 +02:00
|
|
|
|
|
|
|
const testAccAWSAutoScalingGroupConfigWithLoadBalancer = `
|
|
|
|
resource "aws_elb" "bar" {
|
|
|
|
name = "foobar-terraform-test"
|
|
|
|
availability_zones = ["us-west-2a"]
|
|
|
|
|
|
|
|
listener {
|
|
|
|
instance_port = 8000
|
|
|
|
instance_protocol = "http"
|
|
|
|
lb_port = 80
|
|
|
|
lb_protocol = "http"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_launch_configuration" "foobar" {
|
|
|
|
name = "foobarautoscaling-terraform-test"
|
|
|
|
image_id = "ami-21f78e11"
|
|
|
|
instance_type = "t1.micro"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_autoscaling_group" "bar" {
|
|
|
|
availability_zones = ["us-west-2a"]
|
|
|
|
name = "foobar3-terraform-test"
|
|
|
|
max_size = 5
|
|
|
|
min_size = 2
|
|
|
|
health_check_grace_period = 300
|
|
|
|
health_check_type = "ELB"
|
|
|
|
desired_capacity = 4
|
|
|
|
force_delete = true
|
|
|
|
|
|
|
|
launch_configuration = "${aws_launch_configuration.foobar.name}"
|
|
|
|
load_balancers = ["${aws_elb.bar.name}"]
|
|
|
|
}
|
|
|
|
`
|