github.com/sathiyas/terraform@v0.6.9-0.20151210233947-3330da00b997/builtin/providers/aws/resource_aws_autoscaling_policy_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/autoscaling"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSAutoscalingPolicy_basic(t *testing.T) {
    14  	var policy autoscaling.ScalingPolicy
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAWSAutoscalingPolicyDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAWSAutoscalingPolicyConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar", &policy),
    25  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar", "adjustment_type", "ChangeInCapacity"),
    26  					resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar", "cooldown", "300"),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func testAccCheckScalingPolicyExists(n string, policy *autoscaling.ScalingPolicy) resource.TestCheckFunc {
    34  	return func(s *terraform.State) error {
    35  		rs, ok := s.RootModule().Resources[n]
    36  		if !ok {
    37  			return fmt.Errorf("Not found: %s", n)
    38  		}
    39  
    40  		conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
    41  		params := &autoscaling.DescribePoliciesInput{
    42  			AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]),
    43  			PolicyNames:          []*string{aws.String(rs.Primary.ID)},
    44  		}
    45  		resp, err := conn.DescribePolicies(params)
    46  		if err != nil {
    47  			return err
    48  		}
    49  		if len(resp.ScalingPolicies) == 0 {
    50  			return fmt.Errorf("ScalingPolicy not found")
    51  		}
    52  
    53  		return nil
    54  	}
    55  }
    56  
    57  func testAccCheckAWSAutoscalingPolicyDestroy(s *terraform.State) error {
    58  	conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
    59  
    60  	for _, rs := range s.RootModule().Resources {
    61  		if rs.Type != "aws_autoscaling_group" {
    62  			continue
    63  		}
    64  
    65  		params := autoscaling.DescribePoliciesInput{
    66  			AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]),
    67  			PolicyNames:          []*string{aws.String(rs.Primary.ID)},
    68  		}
    69  
    70  		resp, err := conn.DescribePolicies(&params)
    71  
    72  		if err == nil {
    73  			if len(resp.ScalingPolicies) != 0 &&
    74  				*resp.ScalingPolicies[0].PolicyName == rs.Primary.ID {
    75  				return fmt.Errorf("Scaling Policy Still Exists: %s", rs.Primary.ID)
    76  			}
    77  		}
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  var testAccAWSAutoscalingPolicyConfig = fmt.Sprintf(`
    84  resource "aws_launch_configuration" "foobar" {
    85      name = "terraform-test-foobar5"
    86      image_id = "ami-21f78e11"
    87      instance_type = "t1.micro"
    88  }
    89  
    90  resource "aws_autoscaling_group" "foobar" {
    91      availability_zones = ["us-west-2a"]
    92      name = "terraform-test-foobar5"
    93      max_size = 5
    94      min_size = 2
    95      health_check_grace_period = 300
    96      health_check_type = "ELB"
    97      force_delete = true
    98      termination_policies = ["OldestInstance"]
    99      launch_configuration = "${aws_launch_configuration.foobar.name}"
   100      tag {
   101          key = "Foo"
   102          value = "foo-bar"
   103          propagate_at_launch = true
   104      }
   105  }
   106  
   107  resource "aws_autoscaling_policy" "foobar" {
   108      name = "foobar"
   109      scaling_adjustment = 4
   110      adjustment_type = "ChangeInCapacity"
   111      cooldown = 300
   112      autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
   113  }
   114  `)