github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_alb_target_group_test.go (about) 1 package aws 2 3 import ( 4 "errors" 5 "fmt" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/service/elbv2" 10 "github.com/hashicorp/errwrap" 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSALBTargetGroup_basic(t *testing.T) { 17 var conf elbv2.TargetGroup 18 targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 IDRefreshName: "aws_alb_target_group.test", 23 Providers: testAccProviders, 24 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 25 Steps: []resource.TestStep{ 26 { 27 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), 28 Check: resource.ComposeAggregateTestCheckFunc( 29 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 30 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), 31 resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), 32 resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), 33 resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), 34 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), 35 resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), 36 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"), 37 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"), 38 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"), 39 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), 40 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health"), 41 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "60"), 42 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8081"), 43 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTP"), 44 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "3"), 45 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "3"), 46 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "3"), 47 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200-299"), 48 ), 49 }, 50 }, 51 }) 52 } 53 54 func TestAccAWSALBTargetGroup_tags(t *testing.T) { 55 var conf elbv2.TargetGroup 56 targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 57 58 resource.Test(t, resource.TestCase{ 59 PreCheck: func() { testAccPreCheck(t) }, 60 IDRefreshName: "aws_alb_target_group.test", 61 Providers: testAccProviders, 62 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 63 Steps: []resource.TestStep{ 64 { 65 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), 66 Check: resource.ComposeAggregateTestCheckFunc( 67 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 68 resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.%", "1"), 69 resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.TestName", "TestAccAWSALBTargetGroup_basic"), 70 ), 71 }, 72 { 73 Config: testAccAWSALBTargetGroupConfig_updateTags(targetGroupName), 74 Check: resource.ComposeAggregateTestCheckFunc( 75 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 76 resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.%", "2"), 77 resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.Environment", "Production"), 78 resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.Type", "ALB Target Group"), 79 ), 80 }, 81 }, 82 }) 83 } 84 85 func TestAccAWSALBTargetGroup_updateHealthCheck(t *testing.T) { 86 var conf elbv2.TargetGroup 87 targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 88 89 resource.Test(t, resource.TestCase{ 90 PreCheck: func() { testAccPreCheck(t) }, 91 IDRefreshName: "aws_alb_target_group.test", 92 Providers: testAccProviders, 93 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 94 Steps: []resource.TestStep{ 95 { 96 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), 97 Check: resource.ComposeAggregateTestCheckFunc( 98 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 99 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), 100 resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), 101 resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), 102 resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), 103 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), 104 resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), 105 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"), 106 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"), 107 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"), 108 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), 109 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health"), 110 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "60"), 111 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8081"), 112 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTP"), 113 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "3"), 114 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "3"), 115 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "3"), 116 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200-299"), 117 ), 118 }, 119 { 120 Config: testAccAWSALBTargetGroupConfig_updateHealthCheck(targetGroupName), 121 Check: resource.ComposeAggregateTestCheckFunc( 122 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 123 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), 124 resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), 125 resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), 126 resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), 127 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), 128 resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), 129 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"), 130 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"), 131 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"), 132 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), 133 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health2"), 134 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "30"), 135 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8082"), 136 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTPS"), 137 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "4"), 138 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "4"), 139 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "4"), 140 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200"), 141 ), 142 }, 143 }, 144 }) 145 } 146 147 func testAccCheckAWSALBTargetGroupExists(n string, res *elbv2.TargetGroup) resource.TestCheckFunc { 148 return func(s *terraform.State) error { 149 rs, ok := s.RootModule().Resources[n] 150 if !ok { 151 return fmt.Errorf("Not found: %s", n) 152 } 153 154 if rs.Primary.ID == "" { 155 return errors.New("No Target Group ID is set") 156 } 157 158 conn := testAccProvider.Meta().(*AWSClient).elbv2conn 159 160 describe, err := conn.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{ 161 TargetGroupArns: []*string{aws.String(rs.Primary.ID)}, 162 }) 163 164 if err != nil { 165 return err 166 } 167 168 if len(describe.TargetGroups) != 1 || 169 *describe.TargetGroups[0].TargetGroupArn != rs.Primary.ID { 170 return errors.New("Target Group not found") 171 } 172 173 *res = *describe.TargetGroups[0] 174 return nil 175 } 176 } 177 178 func testAccCheckAWSALBTargetGroupDestroy(s *terraform.State) error { 179 conn := testAccProvider.Meta().(*AWSClient).elbv2conn 180 181 for _, rs := range s.RootModule().Resources { 182 if rs.Type != "aws_alb_target_group" { 183 continue 184 } 185 186 describe, err := conn.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{ 187 TargetGroupArns: []*string{aws.String(rs.Primary.ID)}, 188 }) 189 190 if err == nil { 191 if len(describe.TargetGroups) != 0 && 192 *describe.TargetGroups[0].TargetGroupArn == rs.Primary.ID { 193 return fmt.Errorf("Target Group %q still exists", rs.Primary.ID) 194 } 195 } 196 197 // Verify the error 198 if isTargetGroupNotFound(err) { 199 return nil 200 } else { 201 return errwrap.Wrapf("Unexpected error checking ALB destroyed: {{err}}", err) 202 } 203 } 204 205 return nil 206 } 207 208 func testAccAWSALBTargetGroupConfig_basic(targetGroupName string) string { 209 return fmt.Sprintf(`resource "aws_alb_target_group" "test" { 210 name = "%s" 211 port = 443 212 protocol = "HTTPS" 213 vpc_id = "${aws_vpc.test.id}" 214 215 deregistration_delay = 200 216 217 stickiness { 218 type = "lb_cookie" 219 cookie_duration = 10000 220 } 221 222 health_check { 223 path = "/health" 224 interval = 60 225 port = 8081 226 protocol = "HTTP" 227 timeout = 3 228 healthy_threshold = 3 229 unhealthy_threshold = 3 230 matcher = "200-299" 231 } 232 233 tags { 234 TestName = "TestAccAWSALBTargetGroup_basic" 235 } 236 } 237 238 resource "aws_vpc" "test" { 239 cidr_block = "10.0.0.0/16" 240 241 tags { 242 TestName = "TestAccAWSALBTargetGroup_basic" 243 } 244 }`, targetGroupName) 245 } 246 247 func testAccAWSALBTargetGroupConfig_updateTags(targetGroupName string) string { 248 return fmt.Sprintf(`resource "aws_alb_target_group" "test" { 249 name = "%s" 250 port = 443 251 protocol = "HTTPS" 252 vpc_id = "${aws_vpc.test.id}" 253 254 deregistration_delay = 200 255 256 stickiness { 257 type = "lb_cookie" 258 cookie_duration = 10000 259 } 260 261 health_check { 262 path = "/health" 263 interval = 60 264 port = 8081 265 protocol = "HTTP" 266 timeout = 3 267 healthy_threshold = 3 268 unhealthy_threshold = 3 269 matcher = "200-299" 270 } 271 272 tags { 273 Environment = "Production" 274 Type = "ALB Target Group" 275 } 276 } 277 278 resource "aws_vpc" "test" { 279 cidr_block = "10.0.0.0/16" 280 281 tags { 282 TestName = "TestAccAWSALBTargetGroup_basic" 283 } 284 }`, targetGroupName) 285 } 286 287 func testAccAWSALBTargetGroupConfig_updateHealthCheck(targetGroupName string) string { 288 return fmt.Sprintf(`resource "aws_alb_target_group" "test" { 289 name = "%s" 290 port = 443 291 protocol = "HTTPS" 292 vpc_id = "${aws_vpc.test.id}" 293 294 deregistration_delay = 200 295 296 stickiness { 297 type = "lb_cookie" 298 cookie_duration = 10000 299 } 300 301 health_check { 302 path = "/health2" 303 interval = 30 304 port = 8082 305 protocol = "HTTPS" 306 timeout = 4 307 healthy_threshold = 4 308 unhealthy_threshold = 4 309 matcher = "200" 310 } 311 } 312 313 resource "aws_vpc" "test" { 314 cidr_block = "10.0.0.0/16" 315 316 tags { 317 TestName = "TestAccAWSALBTargetGroup_basic" 318 } 319 }`, targetGroupName) 320 }