terraform/builtin/providers/aws/resource_aws_autoscaling_gr...

387 lines
11 KiB
Go
Raw Normal View History

2014-07-14 17:36:25 +02:00
package aws
import (
"fmt"
2015-03-03 23:36:25 +01:00
"reflect"
"strings"
2014-07-14 17:36:25 +02:00
"testing"
2015-04-15 22:30:35 +02:00
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/aws/awserr"
2015-04-15 22:30:35 +02:00
"github.com/awslabs/aws-sdk-go/service/autoscaling"
2014-07-14 17:36:25 +02:00
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAWSAutoScalingGroup_basic(t *testing.T) {
var group autoscaling.Group
var lc autoscaling.LaunchConfiguration
2014-07-14 17:36:25 +02:00
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
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),
testAccCheckAWSAutoScalingGroupHealthyCapacity(&group, 2),
2014-07-14 17:36:25 +02:00
testAccCheckAWSAutoScalingGroupAttributes(&group),
resource.TestCheckResourceAttr(
2015-04-01 17:42:53 +02:00
"aws_autoscaling_group.bar", "availability_zones.2487133097", "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(
"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-10-23 23:58:54 +02:00
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "termination_policies.912102603", "OldestInstance"),
2014-07-14 17:36:25 +02:00
),
},
resource.TestStep{
Config: testAccAWSAutoScalingGroupConfigUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.new", &lc),
resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "desired_capacity", "5"),
testLaunchConfigurationName("aws_autoscaling_group.bar", &lc),
testAccCheckAutoscalingTags(&group.Tags, "Bar", map[string]interface{}{
"value": "bar-foo",
"propagate_at_launch": true,
}),
2015-03-03 23:36:25 +01:00
),
},
},
})
}
func TestAccAWSAutoScalingGroup_tags(t *testing.T) {
var group autoscaling.Group
2015-03-03 23:36:25 +01:00
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSAutoScalingGroupConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
testAccCheckAutoscalingTags(&group.Tags, "Foo", map[string]interface{}{
"value": "foo-bar",
2015-03-03 23:36:25 +01:00
"propagate_at_launch": true,
}),
),
},
resource.TestStep{
Config: testAccAWSAutoScalingGroupConfigUpdate,
2015-03-03 23:36:25 +01:00
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
testAccCheckAutoscalingTagNotExists(&group.Tags, "Foo"),
testAccCheckAutoscalingTags(&group.Tags, "Bar", map[string]interface{}{
"value": "bar-foo",
2015-03-03 23:36:25 +01:00
"propagate_at_launch": true,
}),
),
},
2014-07-14 17:36:25 +02:00
},
})
}
func TestAccAWSAutoScalingGroup_WithLoadBalancer(t *testing.T) {
var group autoscaling.Group
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.Meta().(*AWSClient).autoscalingconn
2014-07-14 17:36:25 +02:00
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(
2015-04-15 22:30:35 +02:00
&autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{aws.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 &&
*describeGroups.AutoScalingGroups[0].AutoScalingGroupName == 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.(awserr.Error)
2014-07-14 17:36:25 +02:00
if !ok {
return err
}
if ec2err.Code() != "InvalidGroup.NotFound" {
2014-07-14 17:36:25 +02:00
return err
}
}
return nil
}
func testAccCheckAWSAutoScalingGroupAttributes(group *autoscaling.Group) resource.TestCheckFunc {
2014-07-14 17:36:25 +02:00
return func(s *terraform.State) error {
2015-04-15 22:30:35 +02:00
if *group.AvailabilityZones[0] != "us-west-2a" {
2015-04-15 22:32:20 +02:00
return fmt.Errorf("Bad availability_zones: %#v", group.AvailabilityZones[0])
2014-07-14 17:36:25 +02:00
}
if *group.AutoScalingGroupName != "foobar3-terraform-test" {
return fmt.Errorf("Bad name: %s", *group.AutoScalingGroupName)
2014-07-14 17:36:25 +02:00
}
if *group.MaxSize != 5 {
return fmt.Errorf("Bad max_size: %d", *group.MaxSize)
2014-07-14 17:36:25 +02:00
}
if *group.MinSize != 2 {
return fmt.Errorf("Bad max_size: %d", *group.MinSize)
2014-07-14 17:36:25 +02:00
}
if *group.HealthCheckType != "ELB" {
return fmt.Errorf("Bad health_check_type,\nexpected: %s\ngot: %s", "ELB", *group.HealthCheckType)
2014-07-14 17:36:25 +02:00
}
if *group.HealthCheckGracePeriod != 300 {
return fmt.Errorf("Bad health_check_grace_period: %d", *group.HealthCheckGracePeriod)
2014-07-14 17:36:25 +02:00
}
if *group.DesiredCapacity != 4 {
return fmt.Errorf("Bad desired_capacity: %d", *group.DesiredCapacity)
2014-07-14 17:36:25 +02:00
}
if *group.LaunchConfigurationName == "" {
return fmt.Errorf("Bad launch configuration name: %s", *group.LaunchConfigurationName)
2014-07-14 17:36:25 +02:00
}
2015-04-15 22:30:35 +02:00
t := &autoscaling.TagDescription{
Key: aws.String("Foo"),
2015-03-03 23:36:25 +01:00
Value: aws.String("foo-bar"),
PropagateAtLaunch: aws.Boolean(true),
ResourceType: aws.String("auto-scaling-group"),
ResourceID: group.AutoScalingGroupName,
2015-03-03 23:36:25 +01:00
}
if !reflect.DeepEqual(group.Tags[0], t) {
return fmt.Errorf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
group.Tags[0],
t)
}
2014-07-14 17:36:25 +02:00
return nil
}
}
func testAccCheckAWSAutoScalingGroupAttributesLoadBalancer(group *autoscaling.Group) resource.TestCheckFunc {
return func(s *terraform.State) error {
2015-04-15 22:30:35 +02:00
if *group.LoadBalancerNames[0] != "foobar-terraform-test" {
2015-04-15 22:32:20 +02:00
return fmt.Errorf("Bad load_balancers: %#v", group.LoadBalancerNames[0])
}
return nil
}
}
func testAccCheckAWSAutoScalingGroupExists(n string, group *autoscaling.Group) resource.TestCheckFunc {
2014-07-14 17:36:25 +02:00
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.Meta().(*AWSClient).autoscalingconn
2014-07-14 17:36:25 +02:00
2015-04-15 22:30:35 +02:00
describeGroups, err := conn.DescribeAutoScalingGroups(
&autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{aws.String(rs.Primary.ID)},
})
2014-07-14 17:36:25 +02:00
if err != nil {
return err
}
if len(describeGroups.AutoScalingGroups) != 1 ||
*describeGroups.AutoScalingGroups[0].AutoScalingGroupName != rs.Primary.ID {
2014-07-14 17:36:25 +02:00
return fmt.Errorf("AutoScaling Group not found")
}
2015-04-15 22:30:35 +02:00
*group = *describeGroups.AutoScalingGroups[0]
2014-07-14 17:36:25 +02:00
return nil
}
}
func testLaunchConfigurationName(n string, lc *autoscaling.LaunchConfiguration) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if *lc.LaunchConfigurationName != rs.Primary.Attributes["launch_configuration"] {
return fmt.Errorf("Launch configuration names do not match")
}
return nil
}
}
func testAccCheckAWSAutoScalingGroupHealthyCapacity(
g *autoscaling.Group, exp int) resource.TestCheckFunc {
return func(s *terraform.State) error {
healthy := 0
for _, i := range g.Instances {
if i.HealthStatus == nil {
continue
}
if strings.EqualFold(*i.HealthStatus, "Healthy") {
healthy++
}
}
if healthy < exp {
return fmt.Errorf("Expected at least %d healthy, got %d.", exp, healthy)
}
return nil
}
}
2014-07-14 17:36:25 +02:00
const testAccAWSAutoScalingGroupConfig = `
resource "aws_launch_configuration" "foobar" {
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
2014-10-23 23:58:54 +02:00
termination_policies = ["OldestInstance"]
2014-07-14 17:36:25 +02:00
launch_configuration = "${aws_launch_configuration.foobar.name}"
2015-03-03 23:36:25 +01:00
tag {
key = "Foo"
2015-03-03 23:36:25 +01:00
value = "foo-bar"
propagate_at_launch = true
}
2014-07-14 17:36:25 +02:00
}
`
const testAccAWSAutoScalingGroupConfigUpdate = `
resource "aws_launch_configuration" "foobar" {
image_id = "ami-21f78e11"
instance_type = "t1.micro"
}
resource "aws_launch_configuration" "new" {
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 = 5
force_delete = true
launch_configuration = "${aws_launch_configuration.new.name}"
2015-03-03 23:36:25 +01:00
tag {
key = "Bar"
2015-03-03 23:36:25 +01:00
value = "bar-foo"
propagate_at_launch = true
}
}
`
const testAccAWSAutoScalingGroupConfigWithLoadBalancer = `
resource "aws_elb" "bar" {
name = "foobar-terraform-test"
availability_zones = ["us-west-2a"]
listener {
instance_port = 80
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
health_check {
healthy_threshold = 2
unhealthy_threshold = 2
target = "HTTP:80/"
interval = 5
timeout = 2
}
}
resource "aws_launch_configuration" "foobar" {
// need an AMI that listens on :80 at boot, this is:
// bitnami-nginxstack-1.6.1-0-linux-ubuntu-14.04.1-x86_64-hvm-ebs-ami-99f5b1a9-3
image_id = "ami-b5b3fc85"
instance_type = "t2.micro"
}
resource "aws_autoscaling_group" "bar" {
availability_zones = ["us-west-2a"]
name = "foobar3-terraform-test"
max_size = 2
min_size = 2
health_check_grace_period = 300
health_check_type = "ELB"
min_elb_capacity = 1
force_delete = true
launch_configuration = "${aws_launch_configuration.foobar.name}"
load_balancers = ["${aws_elb.bar.name}"]
}
`