github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_autoscaling_lifecycle_hook_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 TestAccAWSAutoscalingLifecycleHook_basic(t *testing.T) { 15 resourceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckAWSAutoscalingLifecycleHookDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccAWSAutoscalingLifecycleHookConfig(resourceName), 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckLifecycleHookExists("aws_autoscaling_lifecycle_hook.foobar"), 26 resource.TestCheckResourceAttr("aws_autoscaling_lifecycle_hook.foobar", "autoscaling_group_name", resourceName), 27 resource.TestCheckResourceAttr("aws_autoscaling_lifecycle_hook.foobar", "default_result", "CONTINUE"), 28 resource.TestCheckResourceAttr("aws_autoscaling_lifecycle_hook.foobar", "heartbeat_timeout", "2000"), 29 resource.TestCheckResourceAttr("aws_autoscaling_lifecycle_hook.foobar", "lifecycle_transition", "autoscaling:EC2_INSTANCE_LAUNCHING"), 30 ), 31 }, 32 }, 33 }) 34 } 35 36 func TestAccAWSAutoscalingLifecycleHook_omitDefaultResult(t *testing.T) { 37 resource.Test(t, resource.TestCase{ 38 PreCheck: func() { testAccPreCheck(t) }, 39 Providers: testAccProviders, 40 CheckDestroy: testAccCheckAWSAutoscalingLifecycleHookDestroy, 41 Steps: []resource.TestStep{ 42 resource.TestStep{ 43 Config: testAccAWSAutoscalingLifecycleHookConfig_omitDefaultResult(acctest.RandString(10)), 44 Check: resource.ComposeTestCheckFunc( 45 testAccCheckLifecycleHookExists("aws_autoscaling_lifecycle_hook.foobar"), 46 resource.TestCheckResourceAttr("aws_autoscaling_lifecycle_hook.foobar", "default_result", "ABANDON"), 47 ), 48 }, 49 }, 50 }) 51 } 52 53 func testAccCheckLifecycleHookExists(n string) resource.TestCheckFunc { 54 return func(s *terraform.State) error { 55 rs, ok := s.RootModule().Resources[n] 56 if !ok { 57 return fmt.Errorf("Not found: %s", n) 58 } 59 60 return checkLifecycleHookExistsByName( 61 rs.Primary.Attributes["autoscaling_group_name"], rs.Primary.ID) 62 } 63 } 64 65 func checkLifecycleHookExistsByName(asgName, hookName string) error { 66 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 67 params := &autoscaling.DescribeLifecycleHooksInput{ 68 AutoScalingGroupName: aws.String(asgName), 69 LifecycleHookNames: []*string{aws.String(hookName)}, 70 } 71 resp, err := conn.DescribeLifecycleHooks(params) 72 if err != nil { 73 return err 74 } 75 if len(resp.LifecycleHooks) == 0 { 76 return fmt.Errorf("LifecycleHook not found") 77 } 78 79 return nil 80 } 81 82 func testAccCheckAWSAutoscalingLifecycleHookDestroy(s *terraform.State) error { 83 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 84 85 for _, rs := range s.RootModule().Resources { 86 if rs.Type != "aws_autoscaling_group" { 87 continue 88 } 89 90 params := autoscaling.DescribeLifecycleHooksInput{ 91 AutoScalingGroupName: aws.String(rs.Primary.Attributes["autoscaling_group_name"]), 92 LifecycleHookNames: []*string{aws.String(rs.Primary.ID)}, 93 } 94 95 resp, err := conn.DescribeLifecycleHooks(¶ms) 96 97 if err == nil { 98 if len(resp.LifecycleHooks) != 0 && 99 *resp.LifecycleHooks[0].LifecycleHookName == rs.Primary.ID { 100 return fmt.Errorf("Lifecycle Hook Still Exists: %s", rs.Primary.ID) 101 } 102 } 103 } 104 105 return nil 106 } 107 108 func testAccAWSAutoscalingLifecycleHookConfig(name string) string { 109 return fmt.Sprintf(` 110 resource "aws_launch_configuration" "foobar" { 111 name = "%s" 112 image_id = "ami-21f78e11" 113 instance_type = "t1.micro" 114 } 115 116 resource "aws_sqs_queue" "foobar" { 117 name = "foobar" 118 delay_seconds = 90 119 max_message_size = 2048 120 message_retention_seconds = 86400 121 receive_wait_time_seconds = 10 122 } 123 124 resource "aws_iam_role" "foobar" { 125 name = "foobar" 126 assume_role_policy = <<EOF 127 { 128 "Version" : "2012-10-17", 129 "Statement": [ { 130 "Effect": "Allow", 131 "Principal": {"AWS": "*"}, 132 "Action": [ "sts:AssumeRole" ] 133 } ] 134 } 135 EOF 136 } 137 138 resource "aws_iam_role_policy" "foobar" { 139 name = "foobar" 140 role = "${aws_iam_role.foobar.id}" 141 policy = <<EOF 142 { 143 "Version" : "2012-10-17", 144 "Statement": [ { 145 "Effect": "Allow", 146 "Action": [ 147 "sqs:SendMessage", 148 "sqs:GetQueueUrl", 149 "sns:Publish" 150 ], 151 "Resource": [ 152 "${aws_sqs_queue.foobar.arn}" 153 ] 154 } ] 155 } 156 EOF 157 } 158 159 160 resource "aws_autoscaling_group" "foobar" { 161 availability_zones = ["us-west-2a"] 162 name = "%s" 163 max_size = 5 164 min_size = 2 165 health_check_grace_period = 300 166 health_check_type = "ELB" 167 force_delete = true 168 termination_policies = ["OldestInstance"] 169 launch_configuration = "${aws_launch_configuration.foobar.name}" 170 tag { 171 key = "Foo" 172 value = "foo-bar" 173 propagate_at_launch = true 174 } 175 } 176 177 resource "aws_autoscaling_lifecycle_hook" "foobar" { 178 name = "foobar" 179 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 180 default_result = "CONTINUE" 181 heartbeat_timeout = 2000 182 lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING" 183 notification_metadata = <<EOF 184 { 185 "foo": "bar" 186 } 187 EOF 188 notification_target_arn = "${aws_sqs_queue.foobar.arn}" 189 role_arn = "${aws_iam_role.foobar.arn}" 190 } 191 `, name, name) 192 } 193 194 func testAccAWSAutoscalingLifecycleHookConfig_omitDefaultResult(name string) string { 195 return fmt.Sprintf(` 196 resource "aws_launch_configuration" "foobar" { 197 name = "%s" 198 image_id = "ami-21f78e11" 199 instance_type = "t1.micro" 200 } 201 202 resource "aws_sqs_queue" "foobar" { 203 name = "foobar" 204 delay_seconds = 90 205 max_message_size = 2048 206 message_retention_seconds = 86400 207 receive_wait_time_seconds = 10 208 } 209 210 resource "aws_iam_role" "foobar" { 211 name = "foobar" 212 213 assume_role_policy = <<EOF 214 { 215 "Version" : "2012-10-17", 216 "Statement": [ { 217 "Effect": "Allow", 218 "Principal": {"AWS": "*"}, 219 "Action": [ "sts:AssumeRole" ] 220 } ] 221 } 222 EOF 223 } 224 225 resource "aws_iam_role_policy" "foobar" { 226 name = "foobar" 227 role = "${aws_iam_role.foobar.id}" 228 229 policy = <<EOF 230 { 231 "Version" : "2012-10-17", 232 "Statement": [ { 233 "Effect": "Allow", 234 "Action": [ 235 "sqs:SendMessage", 236 "sqs:GetQueueUrl", 237 "sns:Publish" 238 ], 239 "Resource": [ 240 "${aws_sqs_queue.foobar.arn}" 241 ] 242 } ] 243 } 244 EOF 245 } 246 247 resource "aws_autoscaling_group" "foobar" { 248 availability_zones = ["us-west-2a"] 249 name = "%s" 250 max_size = 5 251 min_size = 2 252 health_check_grace_period = 300 253 health_check_type = "ELB" 254 force_delete = true 255 termination_policies = ["OldestInstance"] 256 launch_configuration = "${aws_launch_configuration.foobar.name}" 257 258 tag { 259 key = "Foo" 260 value = "foo-bar" 261 propagate_at_launch = true 262 } 263 } 264 265 resource "aws_autoscaling_lifecycle_hook" "foobar" { 266 name = "foobar" 267 autoscaling_group_name = "${aws_autoscaling_group.foobar.name}" 268 heartbeat_timeout = 2000 269 lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING" 270 271 notification_metadata = <<EOF 272 { 273 "foo": "bar" 274 } 275 EOF 276 277 notification_target_arn = "${aws_sqs_queue.foobar.arn}" 278 role_arn = "${aws_iam_role.foobar.arn}" 279 }`, name, name) 280 }