github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 testAccCheckAWSAppautoscalingPolicyExists(n string, policy *applicationautoscaling.ScalingPolicy) resource.TestCheckFunc { 43 return func(s *terraform.State) error { 44 rs, ok := s.RootModule().Resources[n] 45 if !ok { 46 return fmt.Errorf("Not found: %s", n) 47 } 48 49 conn := testAccProvider.Meta().(*AWSClient).appautoscalingconn 50 params := &applicationautoscaling.DescribeScalingPoliciesInput{ 51 ServiceNamespace: aws.String(rs.Primary.Attributes["service_namespace"]), 52 PolicyNames: []*string{aws.String(rs.Primary.ID)}, 53 } 54 resp, err := conn.DescribeScalingPolicies(params) 55 if err != nil { 56 return err 57 } 58 if len(resp.ScalingPolicies) == 0 { 59 return fmt.Errorf("ScalingPolicy %s not found", rs.Primary.ID) 60 } 61 62 return nil 63 } 64 } 65 66 func testAccCheckAWSAppautoscalingPolicyDestroy(s *terraform.State) error { 67 conn := testAccProvider.Meta().(*AWSClient).appautoscalingconn 68 69 for _, rs := range s.RootModule().Resources { 70 params := applicationautoscaling.DescribeScalingPoliciesInput{ 71 ServiceNamespace: aws.String(rs.Primary.Attributes["service_namespace"]), 72 PolicyNames: []*string{aws.String(rs.Primary.ID)}, 73 } 74 75 resp, err := conn.DescribeScalingPolicies(¶ms) 76 77 if err == nil { 78 if len(resp.ScalingPolicies) != 0 && 79 *resp.ScalingPolicies[0].PolicyName == rs.Primary.ID { 80 return fmt.Errorf("Application autoscaling policy still exists: %s", rs.Primary.ID) 81 } 82 } 83 } 84 85 return nil 86 } 87 88 func testAccAWSAppautoscalingPolicyConfig( 89 randClusterName string, 90 randPolicyName string) string { 91 return fmt.Sprintf(` 92 resource "aws_iam_role" "autoscale_role" { 93 name = "%s" 94 path = "/" 95 96 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":[\"sts:AssumeRole\"]}]}" 97 } 98 99 resource "aws_iam_role_policy" "autoscale_role_policy" { 100 name = "%s" 101 role = "${aws_iam_role.autoscale_role.id}" 102 103 policy = <<EOF 104 { 105 "Version": "2012-10-17", 106 "Statement": [ 107 { 108 "Effect": "Allow", 109 "Action": [ 110 "ecs:DescribeServices", 111 "ecs:UpdateService", 112 "cloudwatch:DescribeAlarms" 113 ], 114 "Resource": ["*"] 115 } 116 ] 117 } 118 EOF 119 } 120 121 resource "aws_ecs_cluster" "foo" { 122 name = "%s" 123 } 124 125 resource "aws_ecs_task_definition" "task" { 126 family = "foobar" 127 container_definitions = <<EOF 128 [ 129 { 130 "name": "busybox", 131 "image": "busybox:latest", 132 "cpu": 10, 133 "memory": 128, 134 "essential": true 135 } 136 ] 137 EOF 138 } 139 140 resource "aws_ecs_service" "service" { 141 name = "foobar" 142 cluster = "${aws_ecs_cluster.foo.id}" 143 task_definition = "${aws_ecs_task_definition.task.arn}" 144 desired_count = 1 145 deployment_maximum_percent = 200 146 deployment_minimum_healthy_percent = 50 147 } 148 149 resource "aws_appautoscaling_target" "tgt" { 150 service_namespace = "ecs" 151 resource_id = "service/${aws_ecs_cluster.foo.name}/${aws_ecs_service.service.name}" 152 scalable_dimension = "ecs:service:DesiredCount" 153 role_arn = "${aws_iam_role.autoscale_role.arn}" 154 min_capacity = 1 155 max_capacity = 4 156 } 157 158 resource "aws_appautoscaling_policy" "foobar_simple" { 159 name = "%s" 160 service_namespace = "ecs" 161 resource_id = "service/${aws_ecs_cluster.foo.name}/${aws_ecs_service.service.name}" 162 scalable_dimension = "ecs:service:DesiredCount" 163 adjustment_type = "ChangeInCapacity" 164 cooldown = 60 165 metric_aggregation_type = "Average" 166 step_adjustment { 167 metric_interval_lower_bound = 0 168 scaling_adjustment = 1 169 } 170 depends_on = ["aws_appautoscaling_target.tgt"] 171 } 172 `, randClusterName, randClusterName, randClusterName, randPolicyName) 173 }