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