github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_autoscaling_policy_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "testing" 7 "time" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/aws/awserr" 11 "github.com/aws/aws-sdk-go/service/autoscaling" 12 "github.com/davecgh/go-spew/spew" 13 "github.com/hashicorp/terraform/helper/acctest" 14 "github.com/hashicorp/terraform/helper/resource" 15 "github.com/hashicorp/terraform/terraform" 16 ) 17 18 func TestAccAWSAutoscalingPolicy_basic(t *testing.T) { 19 var policy autoscaling.ScalingPolicy 20 21 name := fmt.Sprintf("terraform-test-foobar-%s", acctest.RandString(5)) 22 23 resource.Test(t, resource.TestCase{ 24 PreCheck: func() { testAccPreCheck(t) }, 25 Providers: testAccProviders, 26 CheckDestroy: testAccCheckAWSAutoscalingPolicyDestroy, 27 Steps: []resource.TestStep{ 28 resource.TestStep{ 29 Config: testAccAWSAutoscalingPolicyConfig(name), 30 Check: resource.ComposeTestCheckFunc( 31 testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_simple", &policy), 32 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "adjustment_type", "ChangeInCapacity"), 33 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "policy_type", "SimpleScaling"), 34 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "cooldown", "300"), 35 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "name", "foobar_simple"), 36 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "scaling_adjustment", "2"), 37 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "autoscaling_group_name", name), 38 testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_step", &policy), 39 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "adjustment_type", "ChangeInCapacity"), 40 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "policy_type", "StepScaling"), 41 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "name", "foobar_step"), 42 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "metric_aggregation_type", "Minimum"), 43 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "estimated_instance_warmup", "200"), 44 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_step", "autoscaling_group_name", name), 45 ), 46 }, 47 }, 48 }) 49 } 50 51 func TestAccAWSAutoscalingPolicy_disappears(t *testing.T) { 52 var policy autoscaling.ScalingPolicy 53 54 name := fmt.Sprintf("terraform-test-foobar-%s", acctest.RandString(5)) 55 56 resource.Test(t, resource.TestCase{ 57 PreCheck: func() { testAccPreCheck(t) }, 58 Providers: testAccProviders, 59 CheckDestroy: testAccCheckAWSAutoscalingPolicyDestroy, 60 Steps: []resource.TestStep{ 61 resource.TestStep{ 62 Config: testAccAWSAutoscalingPolicyConfig(name), 63 Check: resource.ComposeTestCheckFunc( 64 testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_simple", &policy), 65 testAccCheckScalingPolicyDisappears(&policy), 66 ), 67 ExpectNonEmptyPlan: true, 68 }, 69 }, 70 }) 71 } 72 73 func testAccCheckScalingPolicyDisappears(conf *autoscaling.ScalingPolicy) resource.TestCheckFunc { 74 return func(s *terraform.State) error { 75 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 76 77 params := &autoscaling.DeletePolicyInput{ 78 AutoScalingGroupName: conf.AutoScalingGroupName, 79 PolicyName: conf.PolicyName, 80 } 81 82 log.Printf("TEST %s", spew.Sdump(params)) 83 _, err := conn.DeletePolicy(params) 84 if err != nil { 85 return err 86 } 87 88 return resource.Retry(10*time.Minute, func() *resource.RetryError { 89 params := &autoscaling.DescribePoliciesInput{ 90 AutoScalingGroupName: conf.AutoScalingGroupName, 91 PolicyNames: []*string{conf.PolicyName}, 92 } 93 resp, err := conn.DescribePolicies(params) 94 if err != nil { 95 cgw, ok := err.(awserr.Error) 96 if ok && cgw.Code() == "ValidationError" { 97 return nil 98 } 99 return resource.NonRetryableError( 100 fmt.Errorf("Error retrieving Autoscaling Policy: %s", err)) 101 } 102 if resp.ScalingPolicies == nil || len(resp.ScalingPolicies) == 0 { 103 return nil 104 } 105 return resource.RetryableError(fmt.Errorf( 106 "Waiting for Autoscaling Policy: %v", conf.PolicyName)) 107 }) 108 } 109 } 110 111 func TestAccAWSAutoscalingPolicy_upgrade(t *testing.T) { 112 var policy autoscaling.ScalingPolicy 113 114 name := acctest.RandString(5) 115 116 resource.Test(t, resource.TestCase{ 117 PreCheck: func() { testAccPreCheck(t) }, 118 Providers: testAccProviders, 119 CheckDestroy: testAccCheckAWSAutoscalingPolicyDestroy, 120 Steps: []resource.TestStep{ 121 resource.TestStep{ 122 Config: testAccAWSAutoscalingPolicyConfig_upgrade_614(name), 123 Check: resource.ComposeTestCheckFunc( 124 testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_simple", &policy), 125 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "min_adjustment_step", "0"), 126 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "min_adjustment_magnitude", "1"), 127 ), 128 ExpectNonEmptyPlan: true, 129 }, 130 131 resource.TestStep{ 132 Config: testAccAWSAutoscalingPolicyConfig_upgrade_615(name), 133 Check: resource.ComposeTestCheckFunc( 134 testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_simple", &policy), 135 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "min_adjustment_step", "0"), 136 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "min_adjustment_magnitude", "1"), 137 ), 138 }, 139 }, 140 }) 141 } 142 143 func testAccCheckScalingPolicyExists(n string, policy *autoscaling.ScalingPolicy) resource.TestCheckFunc { 144 return func(s *terraform.State) error { 145 rs, ok := s.RootModule().Resources[n] 146 if !ok { 147 return fmt.Errorf("Not found: %s", n) 148 } 149 150 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 151 params := &autoscaling.DescribePoliciesInput{ 152 AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]), 153 PolicyNames: []*string{aws.String(rs.Primary.ID)}, 154 } 155 resp, err := conn.DescribePolicies(params) 156 if err != nil { 157 return err 158 } 159 if len(resp.ScalingPolicies) == 0 { 160 return fmt.Errorf("ScalingPolicy not found") 161 } 162 163 *policy = *resp.ScalingPolicies[0] 164 165 return nil 166 } 167 } 168 169 func testAccCheckAWSAutoscalingPolicyDestroy(s *terraform.State) error { 170 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 171 172 for _, rs := range s.RootModule().Resources { 173 if rs.Type != "aws_autoscaling_group" { 174 continue 175 } 176 177 params := autoscaling.DescribePoliciesInput{ 178 AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]), 179 PolicyNames: []*string{aws.String(rs.Primary.ID)}, 180 } 181 182 resp, err := conn.DescribePolicies(¶ms) 183 184 if err == nil { 185 if len(resp.ScalingPolicies) != 0 && 186 *resp.ScalingPolicies[0].PolicyName == rs.Primary.ID { 187 return fmt.Errorf("Scaling Policy Still Exists: %s", rs.Primary.ID) 188 } 189 } 190 } 191 192 return nil 193 } 194 195 func testAccAWSAutoscalingPolicyConfig(name string) string { 196 return fmt.Sprintf(` 197 resource "aws_launch_configuration" "foobar" { 198 name = "%s" 199 image_id = "ami-21f78e11" 200 instance_type = "t1.micro" 201 } 202 203 resource "aws_autoscaling_group" "foobar" { 204 availability_zones = ["us-west-2a"] 205 name = "%s" 206 max_size = 5 207 min_size = 2 208 health_check_grace_period = 300 209 health_check_type = "ELB" 210 force_delete = true 211 termination_policies = ["OldestInstance"] 212 launch_configuration = "${aws_launch_configuration.foobar.name}" 213 tag { 214 key = "Foo" 215 value = "foo-bar" 216 propagate_at_launch = true 217 } 218 } 219 220 resource "aws_autoscaling_policy" "foobar_simple" { 221 name = "foobar_simple" 222 adjustment_type = "ChangeInCapacity" 223 cooldown = 300 224 policy_type = "SimpleScaling" 225 scaling_adjustment = 2 226 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 227 } 228 229 resource "aws_autoscaling_policy" "foobar_step" { 230 name = "foobar_step" 231 adjustment_type = "ChangeInCapacity" 232 policy_type = "StepScaling" 233 estimated_instance_warmup = 200 234 metric_aggregation_type = "Minimum" 235 step_adjustment { 236 scaling_adjustment = 1 237 metric_interval_lower_bound = 2.0 238 } 239 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 240 } 241 `, name, name) 242 } 243 244 func testAccAWSAutoscalingPolicyConfig_upgrade_614(name string) string { 245 return fmt.Sprintf(` 246 resource "aws_launch_configuration" "foobar" { 247 name = "tf-test-%s" 248 image_id = "ami-21f78e11" 249 instance_type = "t1.micro" 250 } 251 252 resource "aws_autoscaling_group" "foobar" { 253 availability_zones = ["us-west-2a"] 254 name = "terraform-test-%s" 255 max_size = 5 256 min_size = 1 257 health_check_grace_period = 300 258 health_check_type = "ELB" 259 force_delete = true 260 termination_policies = ["OldestInstance"] 261 launch_configuration = "${aws_launch_configuration.foobar.name}" 262 263 tag { 264 key = "Foo" 265 value = "foo-bar" 266 propagate_at_launch = true 267 } 268 } 269 270 resource "aws_autoscaling_policy" "foobar_simple" { 271 name = "foobar_simple_%s" 272 adjustment_type = "PercentChangeInCapacity" 273 cooldown = 300 274 policy_type = "SimpleScaling" 275 scaling_adjustment = 2 276 min_adjustment_step = 1 277 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 278 } 279 `, name, name, name) 280 } 281 282 func testAccAWSAutoscalingPolicyConfig_upgrade_615(name string) string { 283 return fmt.Sprintf(` 284 resource "aws_launch_configuration" "foobar" { 285 name = "tf-test-%s" 286 image_id = "ami-21f78e11" 287 instance_type = "t1.micro" 288 } 289 290 resource "aws_autoscaling_group" "foobar" { 291 availability_zones = ["us-west-2a"] 292 name = "terraform-test-%s" 293 max_size = 5 294 min_size = 1 295 health_check_grace_period = 300 296 health_check_type = "ELB" 297 force_delete = true 298 termination_policies = ["OldestInstance"] 299 launch_configuration = "${aws_launch_configuration.foobar.name}" 300 301 tag { 302 key = "Foo" 303 value = "foo-bar" 304 propagate_at_launch = true 305 } 306 } 307 308 resource "aws_autoscaling_policy" "foobar_simple" { 309 name = "foobar_simple_%s" 310 adjustment_type = "PercentChangeInCapacity" 311 cooldown = 300 312 policy_type = "SimpleScaling" 313 scaling_adjustment = 2 314 min_adjustment_magnitude = 1 315 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 316 } 317 `, name, name, name) 318 } 319 320 func TestAccAWSAutoscalingPolicy_SimpleScalingStepAdjustment(t *testing.T) { 321 var policy autoscaling.ScalingPolicy 322 323 name := acctest.RandString(5) 324 325 resource.Test(t, resource.TestCase{ 326 PreCheck: func() { testAccPreCheck(t) }, 327 Providers: testAccProviders, 328 CheckDestroy: testAccCheckAWSAutoscalingPolicyDestroy, 329 Steps: []resource.TestStep{ 330 resource.TestStep{ 331 Config: testAccAWSAutoscalingPolicyConfig_SimpleScalingStepAdjustment(name), 332 Check: resource.ComposeTestCheckFunc( 333 testAccCheckScalingPolicyExists("aws_autoscaling_policy.foobar_simple", &policy), 334 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "adjustment_type", "ExactCapacity"), 335 resource.TestCheckResourceAttr("aws_autoscaling_policy.foobar_simple", "scaling_adjustment", "0"), 336 ), 337 }, 338 }, 339 }) 340 } 341 342 func testAccAWSAutoscalingPolicyConfig_SimpleScalingStepAdjustment(name string) string { 343 return fmt.Sprintf(` 344 resource "aws_launch_configuration" "foobar" { 345 name = "tf-test-%s" 346 image_id = "ami-21f78e11" 347 instance_type = "t1.micro" 348 } 349 350 resource "aws_autoscaling_group" "foobar" { 351 availability_zones = ["us-west-2a"] 352 name = "terraform-test-%s" 353 max_size = 5 354 min_size = 0 355 health_check_grace_period = 300 356 health_check_type = "ELB" 357 force_delete = true 358 termination_policies = ["OldestInstance"] 359 launch_configuration = "${aws_launch_configuration.foobar.name}" 360 361 tag { 362 key = "Foo" 363 value = "foo-bar" 364 propagate_at_launch = true 365 } 366 } 367 368 resource "aws_autoscaling_policy" "foobar_simple" { 369 name = "foobar_simple_%s" 370 adjustment_type = "ExactCapacity" 371 cooldown = 300 372 policy_type = "SimpleScaling" 373 scaling_adjustment = 0 374 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 375 } 376 `, name, name, name) 377 }