github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/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/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSAutoscalingPolicy_basic(t *testing.T) { 15 var policy autoscaling.ScalingPolicy 16 17 name := fmt.Sprintf("terraform-test-foobar-%s", acctest.RandString(5)) 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckAWSAutoscalingPolicyDestroy, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: testAccAWSAutoscalingPolicyConfig(name), 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_simple", &policy), 28 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "adjustment_type", "ChangeInCapacity"), 29 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "policy_type", "SimpleScaling"), 30 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "cooldown", "300"), 31 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "name", "foobar_simple"), 32 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "scaling_adjustment", "2"), 33 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "autoscaling_group_name", name), 34 testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_step", &policy), 35 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "adjustment_type", "ChangeInCapacity"), 36 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "policy_type", "StepScaling"), 37 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "name", "foobar_step"), 38 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "metric_aggregation_type", "Minimum"), 39 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "estimated_instance_warmup", "200"), 40 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "autoscaling_group_name", name), 41 ), 42 }, 43 }, 44 }) 45 } 46 47 func TestAccAWSAutoscalingPolicy_upgrade(t *testing.T) { 48 var policy autoscaling.ScalingPolicy 49 50 name := acctest.RandString(5) 51 52 resource.Test(t, resource.TestCase{ 53 PreCheck: func() { testAccPreCheck(t) }, 54 Providers: testAccProviders, 55 CheckDestroy: testAccCheckAWSAutoscalingPolicyDestroy, 56 Steps: []resource.TestStep{ 57 resource.TestStep{ 58 Config: testAccAWSAutoscalingPolicyConfig_upgrade_614(name), 59 Check: resource.ComposeTestCheckFunc( 60 testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_simple", &policy), 61 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "min_adjustment_step", "0"), 62 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "min_adjustment_magnitude", "1"), 63 ), 64 ExpectNonEmptyPlan: true, 65 }, 66 67 resource.TestStep{ 68 Config: testAccAWSAutoscalingPolicyConfig_upgrade_615(name), 69 Check: resource.ComposeTestCheckFunc( 70 testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_simple", &policy), 71 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "min_adjustment_step", "0"), 72 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "min_adjustment_magnitude", "1"), 73 ), 74 }, 75 }, 76 }) 77 } 78 79 func testAccCheckScalingPolicyExists(n string, policy *autoscaling.ScalingPolicy) resource.TestCheckFunc { 80 return func(s *terraform.State) error { 81 rs, ok := s.RootModule().Resources[n] 82 if !ok { 83 return fmt.Errorf("Not found: %s", n) 84 } 85 86 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 87 params := &autoscaling.DescribePoliciesInput{ 88 AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]), 89 PolicyNames: []*string{aws.String(rs.Primary.ID)}, 90 } 91 resp, err := conn.DescribePolicies(params) 92 if err != nil { 93 return err 94 } 95 if len(resp.ScalingPolicies) == 0 { 96 return fmt.Errorf("ScalingPolicy not found") 97 } 98 99 return nil 100 } 101 } 102 103 func testAccCheckAWSAutoscalingPolicyDestroy(s *terraform.State) error { 104 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 105 106 for _, rs := range s.RootModule().Resources { 107 if rs.Type != "aws_autoscaling_group" { 108 continue 109 } 110 111 params := autoscaling.DescribePoliciesInput{ 112 AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]), 113 PolicyNames: []*string{aws.String(rs.Primary.ID)}, 114 } 115 116 resp, err := conn.DescribePolicies(¶ms) 117 118 if err == nil { 119 if len(resp.ScalingPolicies) != 0 && 120 *resp.ScalingPolicies[0].PolicyName == rs.Primary.ID { 121 return fmt.Errorf("Scaling Policy Still Exists: %s", rs.Primary.ID) 122 } 123 } 124 } 125 126 return nil 127 } 128 129 func testAccAWSAutoscalingPolicyConfig(name string) string { 130 return fmt.Sprintf(` 131 resource "aws_launch_configuration" "foobar" { 132 name = "%s" 133 image_id = "ami-21f78e11" 134 instance_type = "t1.micro" 135 } 136 137 resource "aws_autoscaling_group" "foobar" { 138 availability_zones = ["us-west-2a"] 139 name = "%s" 140 max_size = 5 141 min_size = 2 142 health_check_grace_period = 300 143 health_check_type = "ELB" 144 force_delete = true 145 termination_policies = ["OldestInstance"] 146 launch_configuration = "${aws_launch_configuration.foobar.name}" 147 tag { 148 key = "Foo" 149 value = "foo-bar" 150 propagate_at_launch = true 151 } 152 } 153 154 resource "aws_autoscaling_policy" "foobar_simple" { 155 name = "foobar_simple" 156 adjustment_type = "ChangeInCapacity" 157 cooldown = 300 158 policy_type = "SimpleScaling" 159 scaling_adjustment = 2 160 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 161 } 162 163 resource "aws_autoscaling_policy" "foobar_step" { 164 name = "foobar_step" 165 adjustment_type = "ChangeInCapacity" 166 policy_type = "StepScaling" 167 estimated_instance_warmup = 200 168 metric_aggregation_type = "Minimum" 169 step_adjustment { 170 scaling_adjustment = 1 171 metric_interval_lower_bound = 2.0 172 } 173 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 174 } 175 `, name, name) 176 } 177 178 func testAccAWSAutoscalingPolicyConfig_upgrade_614(name string) string { 179 return fmt.Sprintf(` 180 resource "aws_launch_configuration" "foobar" { 181 name = "tf-test-%s" 182 image_id = "ami-21f78e11" 183 instance_type = "t1.micro" 184 } 185 186 resource "aws_autoscaling_group" "foobar" { 187 availability_zones = ["us-west-2a"] 188 name = "terraform-test-%s" 189 max_size = 5 190 min_size = 1 191 health_check_grace_period = 300 192 health_check_type = "ELB" 193 force_delete = true 194 termination_policies = ["OldestInstance"] 195 launch_configuration = "${aws_launch_configuration.foobar.name}" 196 197 tag { 198 key = "Foo" 199 value = "foo-bar" 200 propagate_at_launch = true 201 } 202 } 203 204 resource "aws_autoscaling_policy" "foobar_simple" { 205 name = "foobar_simple_%s" 206 adjustment_type = "PercentChangeInCapacity" 207 cooldown = 300 208 policy_type = "SimpleScaling" 209 scaling_adjustment = 2 210 min_adjustment_step = 1 211 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 212 } 213 `, name, name, name) 214 } 215 216 func testAccAWSAutoscalingPolicyConfig_upgrade_615(name string) string { 217 return fmt.Sprintf(` 218 resource "aws_launch_configuration" "foobar" { 219 name = "tf-test-%s" 220 image_id = "ami-21f78e11" 221 instance_type = "t1.micro" 222 } 223 224 resource "aws_autoscaling_group" "foobar" { 225 availability_zones = ["us-west-2a"] 226 name = "terraform-test-%s" 227 max_size = 5 228 min_size = 1 229 health_check_grace_period = 300 230 health_check_type = "ELB" 231 force_delete = true 232 termination_policies = ["OldestInstance"] 233 launch_configuration = "${aws_launch_configuration.foobar.name}" 234 235 tag { 236 key = "Foo" 237 value = "foo-bar" 238 propagate_at_launch = true 239 } 240 } 241 242 resource "aws_autoscaling_policy" "foobar_simple" { 243 name = "foobar_simple_%s" 244 adjustment_type = "PercentChangeInCapacity" 245 cooldown = 300 246 policy_type = "SimpleScaling" 247 scaling_adjustment = 2 248 min_adjustment_magnitude = 1 249 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 250 } 251 `, name, name, name) 252 }