github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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 rs = rs 38 return fmt.Errorf("Not found: %s", n) 39 } 40 41 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 42 params := &autoscaling.DescribePoliciesInput{ 43 AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]), 44 PolicyNames: []*string{aws.String(rs.Primary.ID)}, 45 } 46 resp, err := conn.DescribePolicies(params) 47 if err != nil { 48 return err 49 } 50 if len(resp.ScalingPolicies) == 0 { 51 return fmt.Errorf("ScalingPolicy not found") 52 } 53 54 return nil 55 } 56 } 57 58 func testAccCheckAWSAutoscalingPolicyDestroy(s *terraform.State) error { 59 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 60 61 for _, rs := range s.RootModule().Resources { 62 if rs.Type != "aws_autoscaling_group" { 63 continue 64 } 65 66 params := autoscaling.DescribePoliciesInput{ 67 AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]), 68 PolicyNames: []*string{aws.String(rs.Primary.ID)}, 69 } 70 71 resp, err := conn.DescribePolicies(¶ms) 72 73 if err == nil { 74 if len(resp.ScalingPolicies) != 0 && 75 *resp.ScalingPolicies[0].PolicyName == rs.Primary.ID { 76 return fmt.Errorf("Scaling Policy Still Exists: %s", rs.Primary.ID) 77 } 78 } 79 } 80 81 return nil 82 } 83 84 var testAccAWSAutoscalingPolicyConfig = fmt.Sprintf(` 85 resource "aws_launch_configuration" "foobar" { 86 name = "terraform-test-foobar5" 87 image_id = "ami-21f78e11" 88 instance_type = "t1.micro" 89 } 90 91 resource "aws_autoscaling_group" "foobar" { 92 availability_zones = ["us-west-2a"] 93 name = "terraform-test-foobar5" 94 max_size = 5 95 min_size = 2 96 health_check_grace_period = 300 97 health_check_type = "ELB" 98 force_delete = true 99 termination_policies = ["OldestInstance"] 100 launch_configuration = "${aws_launch_configuration.foobar.name}" 101 tag { 102 key = "Foo" 103 value = "foo-bar" 104 propagate_at_launch = true 105 } 106 } 107 108 resource "aws_autoscaling_policy" "foobar" { 109 name = "foobar" 110 scaling_adjustment = 4 111 adjustment_type = "ChangeInCapacity" 112 cooldown = 300 113 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 114 } 115 `)