providers/aws: autoscaling tests

This commit is contained in:
Jack Pearkes 2014-07-14 11:36:25 -04:00
parent bd2fe4d145
commit b9dfe9dc38
2 changed files with 179 additions and 10 deletions

View File

@ -41,8 +41,8 @@ func resource_aws_autoscaling_group_create(
autoScalingGroupOpts.DesiredCapacity, err = strconv.Atoi(rs.Attributes["desired_capicity"]) autoScalingGroupOpts.DesiredCapacity, err = strconv.Atoi(rs.Attributes["desired_capicity"])
} }
if rs.Attributes["healthcheck_grace_period"] != "" { if rs.Attributes["health_check_grace_period"] != "" {
autoScalingGroupOpts.HealthCheckGracePeriod, err = strconv.Atoi(rs.Attributes["healthcheck_grace_period"]) autoScalingGroupOpts.HealthCheckGracePeriod, err = strconv.Atoi(rs.Attributes["health_check_grace_period"])
} }
if err != nil { if err != nil {
@ -68,15 +68,15 @@ func resource_aws_autoscaling_group_create(
autoScalingGroupOpts.HealthCheckType = rs.Attributes["health_check_type"] autoScalingGroupOpts.HealthCheckType = rs.Attributes["health_check_type"]
autoScalingGroupOpts.LaunchConfigurationName = rs.Attributes["launch_configuration"] autoScalingGroupOpts.LaunchConfigurationName = rs.Attributes["launch_configuration"]
log.Printf("[DEBUG] autoscaling Group create configuration: %#v", autoScalingGroupOpts) log.Printf("[DEBUG] AutoScaling Group create configuration: %#v", autoScalingGroupOpts)
_, err = autoscalingconn.CreateAutoScalingGroup(&autoScalingGroupOpts) _, err = autoscalingconn.CreateAutoScalingGroup(&autoScalingGroupOpts)
if err != nil { if err != nil {
return nil, fmt.Errorf("Error creating autoscaling Group: %s", err) return nil, fmt.Errorf("Error creating AutoScaling Group: %s", err)
} }
rs.ID = rs.Attributes["name"] rs.ID = rs.Attributes["name"]
log.Printf("[INFO] autoscaling Group ID: %s", rs.ID) log.Printf("[INFO] AutoScaling Group ID: %s", rs.ID)
g, err := resource_aws_autoscaling_group_retrieve(rs.ID, autoscalingconn) g, err := resource_aws_autoscaling_group_retrieve(rs.ID, autoscalingconn)
if err != nil { if err != nil {
@ -105,9 +105,20 @@ func resource_aws_autoscaling_group_destroy(
p := meta.(*ResourceProvider) p := meta.(*ResourceProvider)
autoscalingconn := p.autoscalingconn autoscalingconn := p.autoscalingconn
log.Printf("[DEBUG] autoscaling Group destroy: %v", s.ID) log.Printf("[DEBUG] AutoScaling Group destroy: %v", s.ID)
_, err := autoscalingconn.DeleteAutoScalingGroup(&autoscaling.DeleteAutoScalingGroup{Name: s.ID}) deleteopts := autoscaling.DeleteAutoScalingGroup{Name: s.ID}
// You can force an autoscaling group to delete
// even if it's in the process of scaling a resource.
// Normally, you would set the min-size and max-size to 0,0
// and then delete the group. This bypasses that and leaves
// resources potentially dangling.
if s.Attributes["force_delete"] != "" {
deleteopts.ForceDelete = true
}
_, err := autoscalingconn.DeleteAutoScalingGroup(&deleteopts)
if err != nil { if err != nil {
autoscalingerr, ok := err.(*autoscaling.Error) autoscalingerr, ok := err.(*autoscaling.Error)
@ -153,6 +164,7 @@ func resource_aws_autoscaling_group_diff(
"vpc_zone_identifier": diff.AttrTypeCreate, "vpc_zone_identifier": diff.AttrTypeCreate,
"load_balancers": diff.AttrTypeCreate, "load_balancers": diff.AttrTypeCreate,
"availability_zone": diff.AttrTypeCreate, "availability_zone": diff.AttrTypeCreate,
"force_delete": diff.AttrTypeCreate,
}, },
ComputedAttrs: []string{}, ComputedAttrs: []string{},
@ -199,19 +211,19 @@ func resource_aws_autoscaling_group_retrieve(id string, autoscalingconn *autosca
Names: []string{id}, Names: []string{id},
} }
log.Printf("[DEBUG] autoscaling Group describe configuration: %#v", describeOpts) log.Printf("[DEBUG] AutoScaling Group describe configuration: %#v", describeOpts)
describeGroups, err := autoscalingconn.DescribeAutoScalingGroups(&describeOpts) describeGroups, err := autoscalingconn.DescribeAutoScalingGroups(&describeOpts)
if err != nil { if err != nil {
return nil, fmt.Errorf("Error retrieving autoscaling groups: %s", err) return nil, fmt.Errorf("Error retrieving AutoScaling groups: %s", err)
} }
// Verify AWS returned our sg // Verify AWS returned our sg
if len(describeGroups.AutoScalingGroups) != 1 || if len(describeGroups.AutoScalingGroups) != 1 ||
describeGroups.AutoScalingGroups[0].Name != id { describeGroups.AutoScalingGroups[0].Name != id {
if err != nil { if err != nil {
return nil, fmt.Errorf("Unable to find autoscaling group: %#v", describeGroups.AutoScalingGroups) return nil, fmt.Errorf("Unable to find AutoScaling group: %#v", describeGroups.AutoScalingGroups)
} }
} }

View File

@ -0,0 +1,157 @@
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,
CheckDestroy: testAccCheckVpcDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSAutoScalingGroupConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
testAccCheckAWSAutoScalingGroupAttributes(&group),
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "availability_zones.#.0", "us-east-1a"),
),
},
},
})
}
func testAccCheckAWSAutoScalingGroupDestroy(s *terraform.State) error {
conn := testAccProvider.autoscalingconn
for _, rs := range s.Resources {
if rs.Type != "aws_autoscaling_group" {
continue
}
// Try to find the Group
describeGroups, err := conn.DescribeAutoScalingGroups(
&autoscaling.DescribeAutoScalingGroups{
Names: []string{rs.ID},
})
if err == nil {
if len(describeGroups.AutoScalingGroups) != 0 &&
describeGroups.AutoScalingGroups[0].Name == rs.ID {
return fmt.Errorf("AutoScaling Group still exists")
}
}
// 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 {
if group.AvailabilityZones[0].AvailabilityZone != "us-east-1a" {
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 {
return fmt.Errorf("Bad max_size: %s", group.MaxSize)
}
if group.MinSize != 2 {
return fmt.Errorf("Bad max_size: %s", group.MinSize)
}
if group.HealthCheckType != "elb" {
return fmt.Errorf("Bad health_check_type: %s", group.HealthCheckType)
}
if group.HealthCheckGracePeriod != 300 {
return fmt.Errorf("Bad health_check_grace_period: %s", group.HealthCheckGracePeriod)
}
if group.DesiredCapacity != 4 {
return fmt.Errorf("Bad desired_capicity: %s", group.DesiredCapacity)
}
if group.LaunchConfigurationName != "" {
return fmt.Errorf("Bad desired_capicity: %s", group.DesiredCapacity)
}
return nil
}
}
func testAccCheckAWSAutoScalingGroupExists(n string, group *autoscaling.AutoScalingGroup) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.ID == "" {
return fmt.Errorf("No AutoScaling Group ID is set")
}
conn := testAccProvider.autoscalingconn
describeOpts := autoscaling.DescribeAutoScalingGroups{
Names: []string{rs.ID},
}
describeGroups, err := conn.DescribeAutoScalingGroups(&describeOpts)
if err != nil {
return err
}
if len(describeGroups.AutoScalingGroups) != 1 ||
describeGroups.AutoScalingGroups[0].Name != rs.ID {
return fmt.Errorf("AutoScaling Group not found")
}
*group = describeGroups.AutoScalingGroups[0]
return nil
}
}
const testAccAWSAutoScalingGroupConfig = `
resource "aws_launch_configuration" "foobar" {
name = "foobar-terraform-test"
image_id = "ami-fb8e9292"
instance_type = "t1.micro"
}
resource "aws_autoscaling_group" "bar" {
availability_zones = ["us-east-1a"]
name = "foobar3-terraform-test"
max_size = 5
min_size = 2
health_check_grace_period = 300
health_check_type = "ELB"
desired_capicity = 4
force_delete = true
launch_configuration = "${aws_launch_configuration.foobar.name}"
}
`