github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_appautoscaling_target_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/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/applicationautoscaling" 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAWSAppautoScalingTarget_basic(t *testing.T) { 16 var target applicationautoscaling.ScalableTarget 17 18 randClusterName := fmt.Sprintf("cluster-%s", acctest.RandString(10)) 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 IDRefreshName: "aws_appautoscaling_target.bar", 23 Providers: testAccProviders, 24 CheckDestroy: testAccCheckAWSAppautoscalingTargetDestroy, 25 Steps: []resource.TestStep{ 26 resource.TestStep{ 27 Config: testAccAWSAppautoscalingTargetConfig(randClusterName), 28 Check: resource.ComposeTestCheckFunc( 29 testAccCheckAWSAppautoscalingTargetExists("aws_appautoscaling_target.bar", &target), 30 resource.TestCheckResourceAttr("aws_appautoscaling_target.bar", "service_namespace", "ecs"), 31 resource.TestCheckResourceAttr("aws_appautoscaling_target.bar", "scalable_dimension", "ecs:service:DesiredCount"), 32 resource.TestCheckResourceAttr("aws_appautoscaling_target.bar", "min_capacity", "1"), 33 resource.TestCheckResourceAttr("aws_appautoscaling_target.bar", "max_capacity", "3"), 34 ), 35 }, 36 37 resource.TestStep{ 38 Config: testAccAWSAppautoscalingTargetConfigUpdate(randClusterName), 39 Check: resource.ComposeTestCheckFunc( 40 testAccCheckAWSAppautoscalingTargetExists("aws_appautoscaling_target.bar", &target), 41 resource.TestCheckResourceAttr("aws_appautoscaling_target.bar", "min_capacity", "2"), 42 resource.TestCheckResourceAttr("aws_appautoscaling_target.bar", "max_capacity", "8"), 43 ), 44 }, 45 }, 46 }) 47 } 48 49 func TestAccAWSAppautoScalingTarget_spotFleetRequest(t *testing.T) { 50 var target applicationautoscaling.ScalableTarget 51 52 resource.Test(t, resource.TestCase{ 53 PreCheck: func() { testAccPreCheck(t) }, 54 IDRefreshName: "aws_appautoscaling_target.test", 55 Providers: testAccProviders, 56 CheckDestroy: testAccCheckAWSAppautoscalingTargetDestroy, 57 Steps: []resource.TestStep{ 58 resource.TestStep{ 59 Config: testAccAWSAppautoscalingTargetSpotFleetRequestConfig, 60 Check: resource.ComposeTestCheckFunc( 61 testAccCheckAWSAppautoscalingTargetExists("aws_appautoscaling_target.test", &target), 62 resource.TestCheckResourceAttr("aws_appautoscaling_target.test", "service_namespace", "ec2"), 63 resource.TestCheckResourceAttr("aws_appautoscaling_target.test", "scalable_dimension", "ec2:spot-fleet-request:TargetCapacity"), 64 ), 65 }, 66 }, 67 }) 68 } 69 70 func testAccCheckAWSAppautoscalingTargetDestroy(s *terraform.State) error { 71 conn := testAccProvider.Meta().(*AWSClient).appautoscalingconn 72 73 for _, rs := range s.RootModule().Resources { 74 if rs.Type != "aws_appautoscaling_target" { 75 continue 76 } 77 78 // Try to find the target 79 describeTargets, err := conn.DescribeScalableTargets( 80 &applicationautoscaling.DescribeScalableTargetsInput{ 81 ResourceIds: []*string{aws.String(rs.Primary.ID)}, 82 ServiceNamespace: aws.String(rs.Primary.Attributes["service_namespace"]), 83 }, 84 ) 85 86 if err == nil { 87 if len(describeTargets.ScalableTargets) != 0 && 88 *describeTargets.ScalableTargets[0].ResourceId == rs.Primary.ID { 89 return fmt.Errorf("Application AutoScaling Target still exists") 90 } 91 } 92 93 // Verify error 94 e, ok := err.(awserr.Error) 95 if !ok { 96 return err 97 } 98 if e.Code() != "" { 99 return e 100 } 101 } 102 103 return nil 104 } 105 106 func testAccCheckAWSAppautoscalingTargetExists(n string, target *applicationautoscaling.ScalableTarget) resource.TestCheckFunc { 107 return func(s *terraform.State) error { 108 rs, ok := s.RootModule().Resources[n] 109 if !ok { 110 return fmt.Errorf("Not found: %s", n) 111 } 112 113 if rs.Primary.ID == "" { 114 return fmt.Errorf("No Application AutoScaling Target ID is set") 115 } 116 117 conn := testAccProvider.Meta().(*AWSClient).appautoscalingconn 118 119 describeTargets, err := conn.DescribeScalableTargets( 120 &applicationautoscaling.DescribeScalableTargetsInput{ 121 ResourceIds: []*string{aws.String(rs.Primary.ID)}, 122 ServiceNamespace: aws.String(rs.Primary.Attributes["service_namespace"]), 123 }, 124 ) 125 126 if err != nil { 127 return err 128 } 129 130 if len(describeTargets.ScalableTargets) != 1 || *describeTargets.ScalableTargets[0].ResourceId != rs.Primary.ID { 131 return fmt.Errorf("Application AutoScaling ResourceId not found") 132 } 133 134 target = describeTargets.ScalableTargets[0] 135 136 return nil 137 } 138 } 139 140 func testAccAWSAppautoscalingTargetConfig( 141 randClusterName string) string { 142 return fmt.Sprintf(` 143 resource "aws_iam_role" "autoscale_role" { 144 name = "autoscalerole%s" 145 path = "/" 146 147 assume_role_policy = <<EOF 148 { 149 "Version": "2012-10-17", 150 "Statement": [ 151 { 152 "Effect": "Allow", 153 "Principal": { 154 "Service": "application-autoscaling.amazonaws.com" 155 }, 156 "Action": "sts:AssumeRole" 157 } 158 ] 159 } 160 EOF 161 } 162 163 resource "aws_iam_role_policy" "autoscale_role_policy" { 164 name = "autoscalepolicy%s" 165 role = "${aws_iam_role.autoscale_role.id}" 166 167 policy = <<EOF 168 { 169 "Version": "2012-10-17", 170 "Statement": [ 171 { 172 "Effect": "Allow", 173 "Action": [ 174 "ecs:DescribeServices", 175 "ecs:UpdateService" 176 ], 177 "Resource": [ 178 "*" 179 ] 180 }, 181 { 182 "Effect": "Allow", 183 "Action": [ 184 "cloudwatch:DescribeAlarms" 185 ], 186 "Resource": [ 187 "*" 188 ] 189 } 190 ] 191 } 192 EOF 193 } 194 195 resource "aws_ecs_cluster" "foo" { 196 name = "%s" 197 } 198 199 resource "aws_ecs_task_definition" "task" { 200 family = "foobar" 201 container_definitions = <<EOF 202 [ 203 { 204 "name": "busybox", 205 "image": "busybox:latest", 206 "cpu": 10, 207 "memory": 128, 208 "essential": true 209 } 210 ] 211 EOF 212 } 213 214 resource "aws_ecs_service" "service" { 215 name = "foobar" 216 cluster = "${aws_ecs_cluster.foo.id}" 217 task_definition = "${aws_ecs_task_definition.task.arn}" 218 desired_count = 1 219 220 deployment_maximum_percent = 200 221 deployment_minimum_healthy_percent = 50 222 } 223 224 resource "aws_appautoscaling_target" "bar" { 225 service_namespace = "ecs" 226 resource_id = "service/${aws_ecs_cluster.foo.name}/${aws_ecs_service.service.name}" 227 scalable_dimension = "ecs:service:DesiredCount" 228 role_arn = "${aws_iam_role.autoscale_role.arn}" 229 min_capacity = 1 230 max_capacity = 3 231 } 232 `, randClusterName, randClusterName, randClusterName) 233 } 234 235 func testAccAWSAppautoscalingTargetConfigUpdate( 236 randClusterName string) string { 237 return fmt.Sprintf(` 238 resource "aws_iam_role" "autoscale_role" { 239 name = "autoscalerole%s" 240 path = "/" 241 242 assume_role_policy = <<EOF 243 { 244 "Version": "2012-10-17", 245 "Statement": [ 246 { 247 "Effect": "Allow", 248 "Principal": { 249 "Service": "application-autoscaling.amazonaws.com" 250 }, 251 "Action": "sts:AssumeRole" 252 } 253 ] 254 } 255 EOF 256 } 257 258 resource "aws_iam_role_policy" "autoscale_role_policy" { 259 name = "autoscalepolicy%s" 260 role = "${aws_iam_role.autoscale_role.id}" 261 262 policy = <<EOF 263 { 264 "Version": "2012-10-17", 265 "Statement": [ 266 { 267 "Effect": "Allow", 268 "Action": [ 269 "ecs:DescribeServices", 270 "ecs:UpdateService" 271 ], 272 "Resource": [ 273 "*" 274 ] 275 }, 276 { 277 "Effect": "Allow", 278 "Action": [ 279 "cloudwatch:DescribeAlarms" 280 ], 281 "Resource": [ 282 "*" 283 ] 284 } 285 ] 286 } 287 EOF 288 } 289 290 resource "aws_ecs_cluster" "foo" { 291 name = "%s" 292 } 293 294 resource "aws_ecs_task_definition" "task" { 295 family = "foobar" 296 container_definitions = <<EOF 297 [ 298 { 299 "name": "busybox", 300 "image": "busybox:latest", 301 "cpu": 10, 302 "memory": 128, 303 "essential": true 304 } 305 ] 306 EOF 307 } 308 309 resource "aws_ecs_service" "service" { 310 name = "foobar" 311 cluster = "${aws_ecs_cluster.foo.id}" 312 task_definition = "${aws_ecs_task_definition.task.arn}" 313 desired_count = 2 314 315 deployment_maximum_percent = 200 316 deployment_minimum_healthy_percent = 50 317 } 318 319 resource "aws_appautoscaling_target" "bar" { 320 service_namespace = "ecs" 321 resource_id = "service/${aws_ecs_cluster.foo.name}/${aws_ecs_service.service.name}" 322 scalable_dimension = "ecs:service:DesiredCount" 323 role_arn = "${aws_iam_role.autoscale_role.arn}" 324 min_capacity = 2 325 max_capacity = 8 326 } 327 `, randClusterName, randClusterName, randClusterName) 328 } 329 330 var testAccAWSAppautoscalingTargetSpotFleetRequestConfig = fmt.Sprintf(` 331 resource "aws_iam_role" "fleet_role" { 332 assume_role_policy = <<EOF 333 { 334 "Version": "2012-10-17", 335 "Statement": [ 336 { 337 "Effect": "Allow", 338 "Principal": { 339 "Service": [ 340 "spotfleet.amazonaws.com", 341 "ec2.amazonaws.com" 342 ] 343 }, 344 "Action": "sts:AssumeRole" 345 } 346 ] 347 } 348 EOF 349 } 350 351 resource "aws_iam_role_policy_attachment" "fleet_role_policy" { 352 role = "${aws_iam_role.fleet_role.name}" 353 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole" 354 } 355 356 resource "aws_spot_fleet_request" "test" { 357 iam_fleet_role = "${aws_iam_role.fleet_role.arn}" 358 spot_price = "0.005" 359 target_capacity = 2 360 valid_until = "2019-11-04T20:44:20Z" 361 terminate_instances_with_expiration = true 362 363 launch_specification { 364 instance_type = "m3.medium" 365 ami = "ami-d06a90b0" 366 } 367 } 368 369 resource "aws_iam_role" "autoscale_role" { 370 assume_role_policy = <<EOF 371 { 372 "Version": "2012-10-17", 373 "Statement": [ 374 { 375 "Effect": "Allow", 376 "Principal": { 377 "Service": "application-autoscaling.amazonaws.com" 378 }, 379 "Action": "sts:AssumeRole" 380 } 381 ] 382 } 383 EOF 384 } 385 386 resource "aws_iam_role_policy_attachment" "autoscale_role_policy_a" { 387 role = "${aws_iam_role.autoscale_role.name}" 388 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole" 389 } 390 391 resource "aws_iam_role_policy_attachment" "autoscale_role_policy_b" { 392 role = "${aws_iam_role.autoscale_role.name}" 393 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetAutoscaleRole" 394 } 395 396 resource "aws_appautoscaling_target" "test" { 397 service_namespace = "ec2" 398 resource_id = "spot-fleet-request/${aws_spot_fleet_request.test.id}" 399 scalable_dimension = "ec2:spot-fleet-request:TargetCapacity" 400 role_arn = "${aws_iam_role.autoscale_role.arn}" 401 min_capacity = 1 402 max_capacity = 3 403 } 404 `)