github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/resource_aws_autoscaling_group_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/goamz/autoscaling"
    10  )
    11  
    12  func TestAccAWSAutoScalingGroup_basic(t *testing.T) {
    13  	var group autoscaling.AutoScalingGroup
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAWSAutoScalingGroupConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
    24  					testAccCheckAWSAutoScalingGroupAttributes(&group),
    25  					resource.TestCheckResourceAttr(
    26  						"aws_autoscaling_group.bar", "availability_zones.2487133097", "us-west-2a"),
    27  					resource.TestCheckResourceAttr(
    28  						"aws_autoscaling_group.bar", "name", "foobar3-terraform-test"),
    29  					resource.TestCheckResourceAttr(
    30  						"aws_autoscaling_group.bar", "max_size", "5"),
    31  					resource.TestCheckResourceAttr(
    32  						"aws_autoscaling_group.bar", "min_size", "2"),
    33  					resource.TestCheckResourceAttr(
    34  						"aws_autoscaling_group.bar", "health_check_grace_period", "300"),
    35  					resource.TestCheckResourceAttr(
    36  						"aws_autoscaling_group.bar", "health_check_type", "ELB"),
    37  					resource.TestCheckResourceAttr(
    38  						"aws_autoscaling_group.bar", "desired_capacity", "4"),
    39  					resource.TestCheckResourceAttr(
    40  						"aws_autoscaling_group.bar", "force_delete", "true"),
    41  					resource.TestCheckResourceAttr(
    42  						"aws_autoscaling_group.bar", "termination_policies.912102603", "OldestInstance"),
    43  				),
    44  			},
    45  
    46  			resource.TestStep{
    47  				Config: testAccAWSAutoScalingGroupConfigUpdate,
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
    50  					resource.TestCheckResourceAttr(
    51  						"aws_autoscaling_group.bar", "desired_capacity", "5"),
    52  				),
    53  			},
    54  		},
    55  	})
    56  }
    57  
    58  func TestAccAWSAutoScalingGroupWithLoadBalancer(t *testing.T) {
    59  	var group autoscaling.AutoScalingGroup
    60  
    61  	resource.Test(t, resource.TestCase{
    62  		PreCheck:     func() { testAccPreCheck(t) },
    63  		Providers:    testAccProviders,
    64  		CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy,
    65  		Steps: []resource.TestStep{
    66  			resource.TestStep{
    67  				Config: testAccAWSAutoScalingGroupConfigWithLoadBalancer,
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testAccCheckAWSAutoScalingGroupExists("aws_autoscaling_group.bar", &group),
    70  					testAccCheckAWSAutoScalingGroupAttributesLoadBalancer(&group),
    71  				),
    72  			},
    73  		},
    74  	})
    75  }
    76  func testAccCheckAWSAutoScalingGroupDestroy(s *terraform.State) error {
    77  	conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
    78  
    79  	for _, rs := range s.RootModule().Resources {
    80  		if rs.Type != "aws_autoscaling_group" {
    81  			continue
    82  		}
    83  
    84  		// Try to find the Group
    85  		describeGroups, err := conn.DescribeAutoScalingGroups(
    86  			&autoscaling.DescribeAutoScalingGroups{
    87  				Names: []string{rs.Primary.ID},
    88  			})
    89  
    90  		if err == nil {
    91  			if len(describeGroups.AutoScalingGroups) != 0 &&
    92  				describeGroups.AutoScalingGroups[0].Name == rs.Primary.ID {
    93  				return fmt.Errorf("AutoScaling Group still exists")
    94  			}
    95  		}
    96  
    97  		// Verify the error
    98  		ec2err, ok := err.(*autoscaling.Error)
    99  		if !ok {
   100  			return err
   101  		}
   102  		if ec2err.Code != "InvalidGroup.NotFound" {
   103  			return err
   104  		}
   105  	}
   106  
   107  	return nil
   108  }
   109  
   110  func testAccCheckAWSAutoScalingGroupAttributes(group *autoscaling.AutoScalingGroup) resource.TestCheckFunc {
   111  	return func(s *terraform.State) error {
   112  		if group.AvailabilityZones[0] != "us-west-2a" {
   113  			return fmt.Errorf("Bad availability_zones: %s", group.AvailabilityZones[0])
   114  		}
   115  
   116  		if group.Name != "foobar3-terraform-test" {
   117  			return fmt.Errorf("Bad name: %s", group.Name)
   118  		}
   119  
   120  		if group.MaxSize != 5 {
   121  			return fmt.Errorf("Bad max_size: %d", group.MaxSize)
   122  		}
   123  
   124  		if group.MinSize != 2 {
   125  			return fmt.Errorf("Bad max_size: %d", group.MinSize)
   126  		}
   127  
   128  		if group.HealthCheckType != "ELB" {
   129  			return fmt.Errorf("Bad health_check_type: %s", group.HealthCheckType)
   130  		}
   131  
   132  		if group.HealthCheckGracePeriod != 300 {
   133  			return fmt.Errorf("Bad health_check_grace_period: %d", group.HealthCheckGracePeriod)
   134  		}
   135  
   136  		if group.DesiredCapacity != 4 {
   137  			return fmt.Errorf("Bad desired_capacity: %d", group.DesiredCapacity)
   138  		}
   139  
   140  		if group.LaunchConfigurationName == "" {
   141  			return fmt.Errorf("Bad launch configuration name: %s", group.LaunchConfigurationName)
   142  		}
   143  
   144  		return nil
   145  	}
   146  }
   147  
   148  func testAccCheckAWSAutoScalingGroupAttributesLoadBalancer(group *autoscaling.AutoScalingGroup) resource.TestCheckFunc {
   149  	return func(s *terraform.State) error {
   150  		if group.LoadBalancerNames[0] != "foobar-terraform-test" {
   151  			return fmt.Errorf("Bad load_balancers: %s", group.LoadBalancerNames[0])
   152  		}
   153  
   154  		return nil
   155  	}
   156  }
   157  
   158  func testAccCheckAWSAutoScalingGroupExists(n string, group *autoscaling.AutoScalingGroup) resource.TestCheckFunc {
   159  	return func(s *terraform.State) error {
   160  		rs, ok := s.RootModule().Resources[n]
   161  		if !ok {
   162  			return fmt.Errorf("Not found: %s", n)
   163  		}
   164  
   165  		if rs.Primary.ID == "" {
   166  			return fmt.Errorf("No AutoScaling Group ID is set")
   167  		}
   168  
   169  		conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
   170  
   171  		describeOpts := autoscaling.DescribeAutoScalingGroups{
   172  			Names: []string{rs.Primary.ID},
   173  		}
   174  		describeGroups, err := conn.DescribeAutoScalingGroups(&describeOpts)
   175  
   176  		if err != nil {
   177  			return err
   178  		}
   179  
   180  		if len(describeGroups.AutoScalingGroups) != 1 ||
   181  			describeGroups.AutoScalingGroups[0].Name != rs.Primary.ID {
   182  			return fmt.Errorf("AutoScaling Group not found")
   183  		}
   184  
   185  		*group = describeGroups.AutoScalingGroups[0]
   186  
   187  		return nil
   188  	}
   189  }
   190  
   191  const testAccAWSAutoScalingGroupConfig = `
   192  resource "aws_launch_configuration" "foobar" {
   193    name = "foobarautoscaling-terraform-test"
   194    image_id = "ami-21f78e11"
   195    instance_type = "t1.micro"
   196  }
   197  
   198  resource "aws_autoscaling_group" "bar" {
   199    availability_zones = ["us-west-2a"]
   200    name = "foobar3-terraform-test"
   201    max_size = 5
   202    min_size = 2
   203    health_check_grace_period = 300
   204    health_check_type = "ELB"
   205    desired_capacity = 4
   206    force_delete = true
   207    termination_policies = ["OldestInstance"]
   208  
   209    launch_configuration = "${aws_launch_configuration.foobar.name}"
   210  }
   211  `
   212  
   213  const testAccAWSAutoScalingGroupConfigUpdate = `
   214  resource "aws_launch_configuration" "foobar" {
   215    name = "foobarautoscaling-terraform-test"
   216    image_id = "ami-21f78e11"
   217    instance_type = "t1.micro"
   218  }
   219  
   220  resource "aws_autoscaling_group" "bar" {
   221    availability_zones = ["us-west-2a"]
   222    name = "foobar3-terraform-test"
   223    max_size = 5
   224    min_size = 2
   225    health_check_grace_period = 300
   226    health_check_type = "ELB"
   227    desired_capacity = 5
   228    force_delete = true
   229  
   230    launch_configuration = "${aws_launch_configuration.foobar.name}"
   231  }
   232  `
   233  
   234  const testAccAWSAutoScalingGroupConfigWithLoadBalancer = `
   235  resource "aws_elb" "bar" {
   236    name = "foobar-terraform-test"
   237    availability_zones = ["us-west-2a"]
   238  
   239    listener {
   240      instance_port = 8000
   241      instance_protocol = "http"
   242      lb_port = 80
   243      lb_protocol = "http"
   244    }
   245  }
   246  
   247  resource "aws_launch_configuration" "foobar" {
   248    name = "foobarautoscaling-terraform-test"
   249    image_id = "ami-21f78e11"
   250    instance_type = "t1.micro"
   251  }
   252  
   253  resource "aws_autoscaling_group" "bar" {
   254    availability_zones = ["us-west-2a"]
   255    name = "foobar3-terraform-test"
   256    max_size = 5
   257    min_size = 2
   258    health_check_grace_period = 300
   259    health_check_type = "ELB"
   260    desired_capacity = 4
   261    force_delete = true
   262  
   263    launch_configuration = "${aws_launch_configuration.foobar.name}"
   264    load_balancers = ["${aws_elb.bar.name}"]
   265  }
   266  `