github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_appautoscaling_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/applicationautoscaling" 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSAppautoScalingPolicy_basic(t *testing.T) { 15 var policy applicationautoscaling.ScalingPolicy 16 17 randClusterName := fmt.Sprintf("cluster%s", acctest.RandString(10)) 18 randPolicyName := fmt.Sprintf("terraform-test-foobar-%s", acctest.RandString(5)) 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testAccCheckAWSAppautoscalingPolicyDestroy, 24 Steps: []resource.TestStep{ 25 resource.TestStep{ 26 Config: testAccAWSAppautoscalingPolicyConfig(randClusterName, randPolicyName), 27 Check: resource.ComposeTestCheckFunc( 28 testAccCheckAWSAppautoscalingPolicyExists("aws_appautoscaling_policy.foobar_simple", &policy), 29 resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "adjustment_type", "ChangeInCapacity"), 30 resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "policy_type", "StepScaling"), 31 resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "cooldown", "60"), 32 resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "name", randPolicyName), 33 resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "resource_id", fmt.Sprintf("service/%s/foobar", randClusterName)), 34 resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "service_namespace", "ecs"), 35 resource.TestCheckResourceAttr("aws_appautoscaling_policy.foobar_simple", "scalable_dimension", "ecs:service:DesiredCount"), 36 ), 37 }, 38 }, 39 }) 40 } 41 42 func TestAccAWSAppautoScalingPolicy_spotFleetRequest(t *testing.T) { 43 var policy applicationautoscaling.ScalingPolicy 44 45 randPolicyName := fmt.Sprintf("test-appautoscaling-policy-%s", acctest.RandString(5)) 46 47 resource.Test(t, resource.TestCase{ 48 PreCheck: func() { testAccPreCheck(t) }, 49 Providers: testAccProviders, 50 CheckDestroy: testAccCheckAWSAppautoscalingPolicyDestroy, 51 Steps: []resource.TestStep{ 52 resource.TestStep{ 53 Config: testAccAWSAppautoscalingPolicySpotFleetRequestConfig(randPolicyName), 54 Check: resource.ComposeTestCheckFunc( 55 testAccCheckAWSAppautoscalingPolicyExists("aws_appautoscaling_policy.test", &policy), 56 resource.TestCheckResourceAttr("aws_appautoscaling_policy.test", "name", randPolicyName), 57 resource.TestCheckResourceAttr("aws_appautoscaling_policy.test", "service_namespace", "ec2"), 58 resource.TestCheckResourceAttr("aws_appautoscaling_policy.test", "scalable_dimension", "ec2:spot-fleet-request:TargetCapacity"), 59 ), 60 }, 61 }, 62 }) 63 } 64 65 func testAccCheckAWSAppautoscalingPolicyExists(n string, policy *applicationautoscaling.ScalingPolicy) resource.TestCheckFunc { 66 return func(s *terraform.State) error { 67 rs, ok := s.RootModule().Resources[n] 68 if !ok { 69 return fmt.Errorf("Not found: %s", n) 70 } 71 72 conn := testAccProvider.Meta().(*AWSClient).appautoscalingconn 73 params := &applicationautoscaling.DescribeScalingPoliciesInput{ 74 ServiceNamespace: aws.String(rs.Primary.Attributes["service_namespace"]), 75 PolicyNames: []*string{aws.String(rs.Primary.ID)}, 76 } 77 resp, err := conn.DescribeScalingPolicies(params) 78 if err != nil { 79 return err 80 } 81 if len(resp.ScalingPolicies) == 0 { 82 return fmt.Errorf("ScalingPolicy %s not found", rs.Primary.ID) 83 } 84 85 return nil 86 } 87 } 88 89 func testAccCheckAWSAppautoscalingPolicyDestroy(s *terraform.State) error { 90 conn := testAccProvider.Meta().(*AWSClient).appautoscalingconn 91 92 for _, rs := range s.RootModule().Resources { 93 params := applicationautoscaling.DescribeScalingPoliciesInput{ 94 ServiceNamespace: aws.String(rs.Primary.Attributes["service_namespace"]), 95 PolicyNames: []*string{aws.String(rs.Primary.ID)}, 96 } 97 98 resp, err := conn.DescribeScalingPolicies(¶ms) 99 100 if err == nil { 101 if len(resp.ScalingPolicies) != 0 && 102 *resp.ScalingPolicies[0].PolicyName == rs.Primary.ID { 103 return fmt.Errorf("Application autoscaling policy still exists: %s", rs.Primary.ID) 104 } 105 } 106 } 107 108 return nil 109 } 110 111 func testAccAWSAppautoscalingPolicyConfig( 112 randClusterName string, 113 randPolicyName string) string { 114 return fmt.Sprintf(` 115 resource "aws_iam_role" "autoscale_role" { 116 name = "%s" 117 path = "/" 118 119 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":[\"sts:AssumeRole\"]}]}" 120 } 121 122 resource "aws_iam_role_policy" "autoscale_role_policy" { 123 name = "%s" 124 role = "${aws_iam_role.autoscale_role.id}" 125 126 policy = <<EOF 127 { 128 "Version": "2012-10-17", 129 "Statement": [ 130 { 131 "Effect": "Allow", 132 "Action": [ 133 "ecs:DescribeServices", 134 "ecs:UpdateService", 135 "cloudwatch:DescribeAlarms" 136 ], 137 "Resource": ["*"] 138 } 139 ] 140 } 141 EOF 142 } 143 144 resource "aws_ecs_cluster" "foo" { 145 name = "%s" 146 } 147 148 resource "aws_ecs_task_definition" "task" { 149 family = "foobar" 150 container_definitions = <<EOF 151 [ 152 { 153 "name": "busybox", 154 "image": "busybox:latest", 155 "cpu": 10, 156 "memory": 128, 157 "essential": true 158 } 159 ] 160 EOF 161 } 162 163 resource "aws_ecs_service" "service" { 164 name = "foobar" 165 cluster = "${aws_ecs_cluster.foo.id}" 166 task_definition = "${aws_ecs_task_definition.task.arn}" 167 desired_count = 1 168 deployment_maximum_percent = 200 169 deployment_minimum_healthy_percent = 50 170 } 171 172 resource "aws_appautoscaling_target" "tgt" { 173 service_namespace = "ecs" 174 resource_id = "service/${aws_ecs_cluster.foo.name}/${aws_ecs_service.service.name}" 175 scalable_dimension = "ecs:service:DesiredCount" 176 role_arn = "${aws_iam_role.autoscale_role.arn}" 177 min_capacity = 1 178 max_capacity = 4 179 } 180 181 resource "aws_appautoscaling_policy" "foobar_simple" { 182 name = "%s" 183 service_namespace = "ecs" 184 resource_id = "service/${aws_ecs_cluster.foo.name}/${aws_ecs_service.service.name}" 185 scalable_dimension = "ecs:service:DesiredCount" 186 adjustment_type = "ChangeInCapacity" 187 cooldown = 60 188 metric_aggregation_type = "Average" 189 step_adjustment { 190 metric_interval_lower_bound = 0 191 scaling_adjustment = 1 192 } 193 depends_on = ["aws_appautoscaling_target.tgt"] 194 } 195 `, randClusterName, randClusterName, randClusterName, randPolicyName) 196 } 197 198 func testAccAWSAppautoscalingPolicySpotFleetRequestConfig( 199 randPolicyName string) string { 200 return fmt.Sprintf(` 201 resource "aws_iam_role" "fleet_role" { 202 assume_role_policy = <<EOF 203 { 204 "Version": "2012-10-17", 205 "Statement": [ 206 { 207 "Effect": "Allow", 208 "Principal": { 209 "Service": [ 210 "spotfleet.amazonaws.com", 211 "ec2.amazonaws.com" 212 ] 213 }, 214 "Action": "sts:AssumeRole" 215 } 216 ] 217 } 218 EOF 219 } 220 221 resource "aws_iam_role_policy_attachment" "fleet_role_policy" { 222 role = "${aws_iam_role.fleet_role.name}" 223 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole" 224 } 225 226 resource "aws_spot_fleet_request" "test" { 227 iam_fleet_role = "${aws_iam_role.fleet_role.arn}" 228 spot_price = "0.005" 229 target_capacity = 2 230 valid_until = "2019-11-04T20:44:20Z" 231 terminate_instances_with_expiration = true 232 233 launch_specification { 234 instance_type = "m3.medium" 235 ami = "ami-d06a90b0" 236 } 237 } 238 239 resource "aws_iam_role" "autoscale_role" { 240 assume_role_policy = <<EOF 241 { 242 "Version": "2012-10-17", 243 "Statement": [ 244 { 245 "Effect": "Allow", 246 "Principal": { 247 "Service": "application-autoscaling.amazonaws.com" 248 }, 249 "Action": "sts:AssumeRole" 250 } 251 ] 252 } 253 EOF 254 } 255 256 resource "aws_iam_role_policy_attachment" "autoscale_role_policy_a" { 257 role = "${aws_iam_role.autoscale_role.name}" 258 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole" 259 } 260 261 resource "aws_iam_role_policy_attachment" "autoscale_role_policy_b" { 262 role = "${aws_iam_role.autoscale_role.name}" 263 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetAutoscaleRole" 264 } 265 266 resource "aws_appautoscaling_target" "test" { 267 service_namespace = "ec2" 268 resource_id = "spot-fleet-request/${aws_spot_fleet_request.test.id}" 269 scalable_dimension = "ec2:spot-fleet-request:TargetCapacity" 270 role_arn = "${aws_iam_role.autoscale_role.arn}" 271 min_capacity = 1 272 max_capacity = 3 273 } 274 275 resource "aws_appautoscaling_policy" "test" { 276 name = "%s" 277 service_namespace = "ec2" 278 resource_id = "spot-fleet-request/${aws_spot_fleet_request.test.id}" 279 scalable_dimension = "ec2:spot-fleet-request:TargetCapacity" 280 adjustment_type = "ChangeInCapacity" 281 cooldown = 60 282 metric_aggregation_type = "Average" 283 284 step_adjustment { 285 metric_interval_lower_bound = 0 286 scaling_adjustment = 1 287 } 288 289 depends_on = ["aws_appautoscaling_target.test"] 290 } 291 `, randPolicyName) 292 }