github.com/rmenn/terraform@v0.3.8-0.20150225065417-fc84b3a78802/builtin/providers/aws/resource_aws_elb_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "os" 6 "reflect" 7 "sort" 8 "testing" 9 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 "github.com/mitchellh/goamz/elb" 13 ) 14 15 func TestAccAWSELB_basic(t *testing.T) { 16 var conf elb.LoadBalancer 17 ssl_certificate_id := os.Getenv("AWS_SSL_CERTIFICATE_ID") 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckAWSELBDestroy, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: testAccAWSELBConfig, 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckAWSELBExists("aws_elb.bar", &conf), 28 testAccCheckAWSELBAttributes(&conf), 29 resource.TestCheckResourceAttr( 30 "aws_elb.bar", "name", "foobar-terraform-test"), 31 resource.TestCheckResourceAttr( 32 "aws_elb.bar", "availability_zones.2487133097", "us-west-2a"), 33 resource.TestCheckResourceAttr( 34 "aws_elb.bar", "availability_zones.221770259", "us-west-2b"), 35 resource.TestCheckResourceAttr( 36 "aws_elb.bar", "availability_zones.2050015877", "us-west-2c"), 37 resource.TestCheckResourceAttr( 38 "aws_elb.bar", "listener.206423021.instance_port", "8000"), 39 resource.TestCheckResourceAttr( 40 "aws_elb.bar", "listener.206423021.instance_protocol", "http"), 41 resource.TestCheckResourceAttr( 42 "aws_elb.bar", "listener.206423021.ssl_certificate_id", ssl_certificate_id), 43 resource.TestCheckResourceAttr( 44 "aws_elb.bar", "listener.206423021.lb_port", "80"), 45 resource.TestCheckResourceAttr( 46 "aws_elb.bar", "listener.206423021.lb_protocol", "http"), 47 resource.TestCheckResourceAttr( 48 "aws_elb.bar", "cross_zone_load_balancing", "true"), 49 ), 50 }, 51 }, 52 }) 53 } 54 55 func TestAccAWSELB_InstanceAttaching(t *testing.T) { 56 var conf elb.LoadBalancer 57 58 testCheckInstanceAttached := func(count int) resource.TestCheckFunc { 59 return func(*terraform.State) error { 60 if len(conf.Instances) != count { 61 return fmt.Errorf("instance count does not match") 62 } 63 return nil 64 } 65 } 66 67 resource.Test(t, resource.TestCase{ 68 PreCheck: func() { testAccPreCheck(t) }, 69 Providers: testAccProviders, 70 CheckDestroy: testAccCheckAWSELBDestroy, 71 Steps: []resource.TestStep{ 72 resource.TestStep{ 73 Config: testAccAWSELBConfig, 74 Check: resource.ComposeTestCheckFunc( 75 testAccCheckAWSELBExists("aws_elb.bar", &conf), 76 testAccCheckAWSELBAttributes(&conf), 77 ), 78 }, 79 80 resource.TestStep{ 81 Config: testAccAWSELBConfigNewInstance, 82 Check: resource.ComposeTestCheckFunc( 83 testAccCheckAWSELBExists("aws_elb.bar", &conf), 84 testCheckInstanceAttached(1), 85 ), 86 }, 87 }, 88 }) 89 } 90 91 func TestAccAWSELB_HealthCheck(t *testing.T) { 92 var conf elb.LoadBalancer 93 94 resource.Test(t, resource.TestCase{ 95 PreCheck: func() { testAccPreCheck(t) }, 96 Providers: testAccProviders, 97 CheckDestroy: testAccCheckAWSELBDestroy, 98 Steps: []resource.TestStep{ 99 resource.TestStep{ 100 Config: testAccAWSELBConfigHealthCheck, 101 Check: resource.ComposeTestCheckFunc( 102 testAccCheckAWSELBExists("aws_elb.bar", &conf), 103 testAccCheckAWSELBAttributesHealthCheck(&conf), 104 resource.TestCheckResourceAttr( 105 "aws_elb.bar", "health_check.3484319807.healthy_threshold", "5"), 106 resource.TestCheckResourceAttr( 107 "aws_elb.bar", "health_check.3484319807.unhealthy_threshold", "5"), 108 resource.TestCheckResourceAttr( 109 "aws_elb.bar", "health_check.3484319807.target", "HTTP:8000/"), 110 resource.TestCheckResourceAttr( 111 "aws_elb.bar", "health_check.3484319807.timeout", "30"), 112 resource.TestCheckResourceAttr( 113 "aws_elb.bar", "health_check.3484319807.interval", "60"), 114 ), 115 }, 116 }, 117 }) 118 } 119 120 func TestAccAWSELBUpdate_HealthCheck(t *testing.T) { 121 resource.Test(t, resource.TestCase{ 122 PreCheck: func() { testAccPreCheck(t) }, 123 Providers: testAccProviders, 124 CheckDestroy: testAccCheckAWSELBDestroy, 125 Steps: []resource.TestStep{ 126 resource.TestStep{ 127 Config: testAccAWSELBConfigHealthCheck, 128 Check: resource.ComposeTestCheckFunc( 129 resource.TestCheckResourceAttr( 130 "aws_elb.bar", "health_check.3484319807.healthy_threshold", "5"), 131 ), 132 }, 133 resource.TestStep{ 134 Config: testAccAWSELBConfigHealthCheck_update, 135 Check: resource.ComposeTestCheckFunc( 136 resource.TestCheckResourceAttr( 137 "aws_elb.bar", "health_check.2648756019.healthy_threshold", "10"), 138 ), 139 }, 140 }, 141 }) 142 } 143 144 func testAccCheckAWSELBDestroy(s *terraform.State) error { 145 conn := testAccProvider.Meta().(*AWSClient).elbconn 146 147 for _, rs := range s.RootModule().Resources { 148 if rs.Type != "aws_elb" { 149 continue 150 } 151 152 describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancer{ 153 Names: []string{rs.Primary.ID}, 154 }) 155 156 if err == nil { 157 if len(describe.LoadBalancers) != 0 && 158 describe.LoadBalancers[0].LoadBalancerName == rs.Primary.ID { 159 return fmt.Errorf("ELB still exists") 160 } 161 } 162 163 // Verify the error 164 providerErr, ok := err.(*elb.Error) 165 if !ok { 166 return err 167 } 168 169 if providerErr.Code != "InvalidLoadBalancerName.NotFound" { 170 return fmt.Errorf("Unexpected error: %s", err) 171 } 172 } 173 174 return nil 175 } 176 177 func testAccCheckAWSELBAttributes(conf *elb.LoadBalancer) resource.TestCheckFunc { 178 return func(s *terraform.State) error { 179 zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"} 180 sort.StringSlice(conf.AvailabilityZones).Sort() 181 if !reflect.DeepEqual(conf.AvailabilityZones, zones) { 182 return fmt.Errorf("bad availability_zones") 183 } 184 185 if conf.LoadBalancerName != "foobar-terraform-test" { 186 return fmt.Errorf("bad name") 187 } 188 189 l := elb.Listener{ 190 InstancePort: 8000, 191 InstanceProtocol: "HTTP", 192 LoadBalancerPort: 80, 193 Protocol: "HTTP", 194 } 195 196 if !reflect.DeepEqual(conf.Listeners[0], l) { 197 return fmt.Errorf( 198 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 199 conf.Listeners[0], 200 l) 201 } 202 203 if conf.DNSName == "" { 204 return fmt.Errorf("empty dns_name") 205 } 206 207 return nil 208 } 209 } 210 211 func testAccCheckAWSELBAttributesHealthCheck(conf *elb.LoadBalancer) resource.TestCheckFunc { 212 return func(s *terraform.State) error { 213 zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"} 214 sort.StringSlice(conf.AvailabilityZones).Sort() 215 if !reflect.DeepEqual(conf.AvailabilityZones, zones) { 216 return fmt.Errorf("bad availability_zones") 217 } 218 219 if conf.LoadBalancerName != "foobar-terraform-test" { 220 return fmt.Errorf("bad name") 221 } 222 223 check := elb.HealthCheck{ 224 Timeout: 30, 225 UnhealthyThreshold: 5, 226 HealthyThreshold: 5, 227 Interval: 60, 228 Target: "HTTP:8000/", 229 } 230 231 if !reflect.DeepEqual(conf.HealthCheck, check) { 232 return fmt.Errorf( 233 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 234 conf.HealthCheck, 235 check) 236 } 237 238 if conf.DNSName == "" { 239 return fmt.Errorf("empty dns_name") 240 } 241 242 return nil 243 } 244 } 245 246 func testAccCheckAWSELBExists(n string, res *elb.LoadBalancer) resource.TestCheckFunc { 247 return func(s *terraform.State) error { 248 rs, ok := s.RootModule().Resources[n] 249 if !ok { 250 return fmt.Errorf("Not found: %s", n) 251 } 252 253 if rs.Primary.ID == "" { 254 return fmt.Errorf("No ELB ID is set") 255 } 256 257 conn := testAccProvider.Meta().(*AWSClient).elbconn 258 259 describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancer{ 260 Names: []string{rs.Primary.ID}, 261 }) 262 263 if err != nil { 264 return err 265 } 266 267 if len(describe.LoadBalancers) != 1 || 268 describe.LoadBalancers[0].LoadBalancerName != rs.Primary.ID { 269 return fmt.Errorf("ELB not found") 270 } 271 272 *res = describe.LoadBalancers[0] 273 274 return nil 275 } 276 } 277 278 const testAccAWSELBConfig = ` 279 resource "aws_elb" "bar" { 280 name = "foobar-terraform-test" 281 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 282 283 listener { 284 instance_port = 8000 285 instance_protocol = "http" 286 lb_port = 80 287 lb_protocol = "http" 288 } 289 290 cross_zone_load_balancing = true 291 } 292 ` 293 294 const testAccAWSELBConfigNewInstance = ` 295 resource "aws_elb" "bar" { 296 name = "foobar-terraform-test" 297 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 298 299 listener { 300 instance_port = 8000 301 instance_protocol = "http" 302 lb_port = 80 303 lb_protocol = "http" 304 } 305 306 instances = ["${aws_instance.foo.id}"] 307 } 308 309 resource "aws_instance" "foo" { 310 # us-west-2 311 ami = "ami-043a5034" 312 instance_type = "t1.micro" 313 } 314 ` 315 316 const testAccAWSELBConfigListenerSSLCertificateId = ` 317 resource "aws_elb" "bar" { 318 name = "foobar-terraform-test" 319 availability_zones = ["us-west-2a"] 320 321 listener { 322 instance_port = 8000 323 instance_protocol = "http" 324 ssl_certificate_id = "%s" 325 lb_port = 443 326 lb_protocol = "https" 327 } 328 } 329 ` 330 331 const testAccAWSELBConfigHealthCheck = ` 332 resource "aws_elb" "bar" { 333 name = "foobar-terraform-test" 334 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 335 336 listener { 337 instance_port = 8000 338 instance_protocol = "http" 339 lb_port = 80 340 lb_protocol = "http" 341 } 342 343 health_check { 344 healthy_threshold = 5 345 unhealthy_threshold = 5 346 target = "HTTP:8000/" 347 interval = 60 348 timeout = 30 349 } 350 } 351 ` 352 353 const testAccAWSELBConfigHealthCheck_update = ` 354 resource "aws_elb" "bar" { 355 name = "foobar-terraform-test" 356 availability_zones = ["us-west-2a"] 357 358 listener { 359 instance_port = 8000 360 instance_protocol = "http" 361 lb_port = 80 362 lb_protocol = "http" 363 } 364 365 health_check { 366 healthy_threshold = 10 367 unhealthy_threshold = 5 368 target = "HTTP:8000/" 369 interval = 60 370 timeout = 30 371 } 372 } 373 `