github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_autoscaling_attachment_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 TestAccAwsAutoscalingAttachment_elb(t *testing.T) { 15 16 rInt := acctest.RandInt() 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 Steps: []resource.TestStep{ 22 { 23 Config: testAccAWSAutoscalingAttachment_elb(rInt), 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckAWSAutocalingElbAttachmentExists("aws_autoscaling_group.asg", 0), 26 ), 27 }, 28 { 29 Config: testAccAWSAutoscalingAttachment_elb_associated(rInt), 30 Check: resource.ComposeTestCheckFunc( 31 testAccCheckAWSAutocalingElbAttachmentExists("aws_autoscaling_group.asg", 1), 32 ), 33 }, 34 { 35 Config: testAccAWSAutoscalingAttachment_elb_double_associated(rInt), 36 Check: resource.ComposeTestCheckFunc( 37 testAccCheckAWSAutocalingElbAttachmentExists("aws_autoscaling_group.asg", 2), 38 ), 39 }, 40 { 41 Config: testAccAWSAutoscalingAttachment_elb_associated(rInt), 42 Check: resource.ComposeTestCheckFunc( 43 testAccCheckAWSAutocalingElbAttachmentExists("aws_autoscaling_group.asg", 1), 44 ), 45 }, 46 { 47 Config: testAccAWSAutoscalingAttachment_elb(rInt), 48 Check: resource.ComposeTestCheckFunc( 49 testAccCheckAWSAutocalingElbAttachmentExists("aws_autoscaling_group.asg", 0), 50 ), 51 }, 52 }, 53 }) 54 } 55 56 func TestAccAwsAutoscalingAttachment_albTargetGroup(t *testing.T) { 57 58 rInt := acctest.RandInt() 59 60 resource.Test(t, resource.TestCase{ 61 PreCheck: func() { testAccPreCheck(t) }, 62 Providers: testAccProviders, 63 Steps: []resource.TestStep{ 64 { 65 Config: testAccAWSAutoscalingAttachment_alb(rInt), 66 Check: resource.ComposeTestCheckFunc( 67 testAccCheckAWSAutocalingAlbAttachmentExists("aws_autoscaling_group.asg", 0), 68 ), 69 }, 70 { 71 Config: testAccAWSAutoscalingAttachment_alb_associated(rInt), 72 Check: resource.ComposeTestCheckFunc( 73 testAccCheckAWSAutocalingAlbAttachmentExists("aws_autoscaling_group.asg", 1), 74 ), 75 }, 76 { 77 Config: testAccAWSAutoscalingAttachment_alb_double_associated(rInt), 78 Check: resource.ComposeTestCheckFunc( 79 testAccCheckAWSAutocalingAlbAttachmentExists("aws_autoscaling_group.asg", 2), 80 ), 81 }, 82 { 83 Config: testAccAWSAutoscalingAttachment_alb_associated(rInt), 84 Check: resource.ComposeTestCheckFunc( 85 testAccCheckAWSAutocalingAlbAttachmentExists("aws_autoscaling_group.asg", 1), 86 ), 87 }, 88 { 89 Config: testAccAWSAutoscalingAttachment_alb(rInt), 90 Check: resource.ComposeTestCheckFunc( 91 testAccCheckAWSAutocalingAlbAttachmentExists("aws_autoscaling_group.asg", 0), 92 ), 93 }, 94 }, 95 }) 96 } 97 98 func testAccCheckAWSAutocalingElbAttachmentExists(asgname string, loadBalancerCount int) resource.TestCheckFunc { 99 return func(s *terraform.State) error { 100 rs, ok := s.RootModule().Resources[asgname] 101 if !ok { 102 return fmt.Errorf("Not found: %s", asgname) 103 } 104 105 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 106 asg := rs.Primary.ID 107 108 actual, err := conn.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{ 109 AutoScalingGroupNames: []*string{aws.String(asg)}, 110 }) 111 112 if err != nil { 113 return fmt.Errorf("Received an error when attempting to load %s: %s", asg, err) 114 } 115 116 if loadBalancerCount != len(actual.AutoScalingGroups[0].LoadBalancerNames) { 117 return fmt.Errorf("Error: ASG has the wrong number of load balacners associated. Expected [%d] but got [%d]", loadBalancerCount, len(actual.AutoScalingGroups[0].LoadBalancerNames)) 118 } 119 120 return nil 121 } 122 } 123 124 func testAccCheckAWSAutocalingAlbAttachmentExists(asgname string, targetGroupCount int) resource.TestCheckFunc { 125 return func(s *terraform.State) error { 126 rs, ok := s.RootModule().Resources[asgname] 127 if !ok { 128 return fmt.Errorf("Not found: %s", asgname) 129 } 130 131 conn := testAccProvider.Meta().(*AWSClient).autoscalingconn 132 asg := rs.Primary.ID 133 134 actual, err := conn.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{ 135 AutoScalingGroupNames: []*string{aws.String(asg)}, 136 }) 137 138 if err != nil { 139 return fmt.Errorf("Recieved an error when attempting to load %s: %s", asg, err) 140 } 141 142 if targetGroupCount != len(actual.AutoScalingGroups[0].TargetGroupARNs) { 143 return fmt.Errorf("Error: ASG has the wrong number of Target Groups associated. Expected [%d] but got [%d]", targetGroupCount, len(actual.AutoScalingGroups[0].TargetGroupARNs)) 144 } 145 146 return nil 147 } 148 } 149 150 func testAccAWSAutoscalingAttachment_alb(rInt int) string { 151 return fmt.Sprintf(` 152 resource "aws_alb_target_group" "test" { 153 name = "test-alb-%d" 154 port = 443 155 protocol = "HTTPS" 156 vpc_id = "${aws_vpc.test.id}" 157 158 deregistration_delay = 200 159 160 stickiness { 161 type = "lb_cookie" 162 cookie_duration = 10000 163 } 164 165 health_check { 166 path = "/health" 167 interval = 60 168 port = 8081 169 protocol = "HTTP" 170 timeout = 3 171 healthy_threshold = 3 172 unhealthy_threshold = 3 173 matcher = "200-299" 174 } 175 176 tags { 177 TestName = "TestAccAWSALBTargetGroup_basic" 178 } 179 } 180 181 resource "aws_alb_target_group" "another_test" { 182 name = "atest-alb-%d" 183 port = 443 184 protocol = "HTTPS" 185 vpc_id = "${aws_vpc.test.id}" 186 187 deregistration_delay = 200 188 189 stickiness { 190 type = "lb_cookie" 191 cookie_duration = 10000 192 } 193 194 health_check { 195 path = "/health" 196 interval = 60 197 port = 8081 198 protocol = "HTTP" 199 timeout = 3 200 healthy_threshold = 3 201 unhealthy_threshold = 3 202 matcher = "200-299" 203 } 204 205 tags { 206 TestName = "TestAccAWSALBTargetGroup_basic" 207 } 208 } 209 210 resource "aws_autoscaling_group" "asg" { 211 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 212 name = "asg-lb-assoc-terraform-test_%d" 213 max_size = 1 214 min_size = 0 215 desired_capacity = 0 216 health_check_grace_period = 300 217 force_delete = true 218 launch_configuration = "${aws_launch_configuration.as_conf.name}" 219 220 tag { 221 key = "Name" 222 value = "terraform-asg-lg-assoc-test" 223 propagate_at_launch = true 224 } 225 } 226 227 resource "aws_launch_configuration" "as_conf" { 228 name = "test_config_%d" 229 image_id = "ami-f34032c3" 230 instance_type = "t1.micro" 231 } 232 233 resource "aws_vpc" "test" { 234 cidr_block = "10.0.0.0/16" 235 236 tags { 237 TestName = "TestAccAWSALBTargetGroup_basic" 238 } 239 } 240 `, rInt, rInt, rInt, rInt) 241 } 242 243 func testAccAWSAutoscalingAttachment_elb(rInt int) string { 244 return fmt.Sprintf(` 245 resource "aws_elb" "foo" { 246 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 247 248 listener { 249 instance_port = 8000 250 instance_protocol = "http" 251 lb_port = 80 252 lb_protocol = "http" 253 } 254 } 255 256 resource "aws_elb" "bar" { 257 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 258 259 listener { 260 instance_port = 8000 261 instance_protocol = "http" 262 lb_port = 80 263 lb_protocol = "http" 264 } 265 } 266 267 resource "aws_launch_configuration" "as_conf" { 268 name = "test_config_%d" 269 image_id = "ami-f34032c3" 270 instance_type = "t1.micro" 271 } 272 273 resource "aws_autoscaling_group" "asg" { 274 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 275 name = "asg-lb-assoc-terraform-test_%d" 276 max_size = 1 277 min_size = 0 278 desired_capacity = 0 279 health_check_grace_period = 300 280 force_delete = true 281 launch_configuration = "${aws_launch_configuration.as_conf.name}" 282 283 tag { 284 key = "Name" 285 value = "terraform-asg-lg-assoc-test" 286 propagate_at_launch = true 287 } 288 }`, rInt, rInt) 289 } 290 291 func testAccAWSAutoscalingAttachment_elb_associated(rInt int) string { 292 return testAccAWSAutoscalingAttachment_elb(rInt) + ` 293 resource "aws_autoscaling_attachment" "asg_attachment_foo" { 294 autoscaling_group_name = "${aws_autoscaling_group.asg.id}" 295 elb = "${aws_elb.foo.id}" 296 }` 297 } 298 299 func testAccAWSAutoscalingAttachment_alb_associated(rInt int) string { 300 return testAccAWSAutoscalingAttachment_alb(rInt) + ` 301 resource "aws_autoscaling_attachment" "asg_attachment_foo" { 302 autoscaling_group_name = "${aws_autoscaling_group.asg.id}" 303 alb_target_group_arn = "${aws_alb_target_group.test.arn}" 304 }` 305 } 306 307 func testAccAWSAutoscalingAttachment_elb_double_associated(rInt int) string { 308 return testAccAWSAutoscalingAttachment_elb_associated(rInt) + ` 309 resource "aws_autoscaling_attachment" "asg_attachment_bar" { 310 autoscaling_group_name = "${aws_autoscaling_group.asg.id}" 311 elb = "${aws_elb.bar.id}" 312 }` 313 } 314 315 func testAccAWSAutoscalingAttachment_alb_double_associated(rInt int) string { 316 return testAccAWSAutoscalingAttachment_alb_associated(rInt) + ` 317 resource "aws_autoscaling_attachment" "asg_attachment_bar" { 318 autoscaling_group_name = "${aws_autoscaling_group.asg.id}" 319 alb_target_group_arn = "${aws_alb_target_group.another_test.arn}" 320 }` 321 }