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