github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/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 func testAccCheckAWSELBDestroy(s *terraform.State) error { 120 conn := testAccProvider.Meta().(*AWSClient).elbconn 121 122 for _, rs := range s.RootModule().Resources { 123 if rs.Type != "aws_elb" { 124 continue 125 } 126 127 describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancer{ 128 Names: []string{rs.Primary.ID}, 129 }) 130 131 if err == nil { 132 if len(describe.LoadBalancers) != 0 && 133 describe.LoadBalancers[0].LoadBalancerName == rs.Primary.ID { 134 return fmt.Errorf("ELB still exists") 135 } 136 } 137 138 // Verify the error 139 providerErr, ok := err.(*elb.Error) 140 if !ok { 141 return err 142 } 143 144 if providerErr.Code != "InvalidLoadBalancerName.NotFound" { 145 return fmt.Errorf("Unexpected error: %s", err) 146 } 147 } 148 149 return nil 150 } 151 152 func testAccCheckAWSELBAttributes(conf *elb.LoadBalancer) resource.TestCheckFunc { 153 return func(s *terraform.State) error { 154 zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"} 155 sort.StringSlice(conf.AvailabilityZones).Sort() 156 if !reflect.DeepEqual(conf.AvailabilityZones, zones) { 157 return fmt.Errorf("bad availability_zones") 158 } 159 160 if conf.LoadBalancerName != "foobar-terraform-test" { 161 return fmt.Errorf("bad name") 162 } 163 164 l := elb.Listener{ 165 InstancePort: 8000, 166 InstanceProtocol: "HTTP", 167 LoadBalancerPort: 80, 168 Protocol: "HTTP", 169 } 170 171 if !reflect.DeepEqual(conf.Listeners[0], l) { 172 return fmt.Errorf( 173 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 174 conf.Listeners[0], 175 l) 176 } 177 178 if conf.DNSName == "" { 179 return fmt.Errorf("empty dns_name") 180 } 181 182 return nil 183 } 184 } 185 186 func testAccCheckAWSELBAttributesHealthCheck(conf *elb.LoadBalancer) resource.TestCheckFunc { 187 return func(s *terraform.State) error { 188 zones := []string{"us-west-2a", "us-west-2b", "us-west-2c"} 189 sort.StringSlice(conf.AvailabilityZones).Sort() 190 if !reflect.DeepEqual(conf.AvailabilityZones, zones) { 191 return fmt.Errorf("bad availability_zones") 192 } 193 194 if conf.LoadBalancerName != "foobar-terraform-test" { 195 return fmt.Errorf("bad name") 196 } 197 198 check := elb.HealthCheck{ 199 Timeout: 30, 200 UnhealthyThreshold: 5, 201 HealthyThreshold: 5, 202 Interval: 60, 203 Target: "HTTP:8000/", 204 } 205 206 if !reflect.DeepEqual(conf.HealthCheck, check) { 207 return fmt.Errorf( 208 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 209 conf.HealthCheck, 210 check) 211 } 212 213 if conf.DNSName == "" { 214 return fmt.Errorf("empty dns_name") 215 } 216 217 return nil 218 } 219 } 220 221 func testAccCheckAWSELBExists(n string, res *elb.LoadBalancer) resource.TestCheckFunc { 222 return func(s *terraform.State) error { 223 rs, ok := s.RootModule().Resources[n] 224 if !ok { 225 return fmt.Errorf("Not found: %s", n) 226 } 227 228 if rs.Primary.ID == "" { 229 return fmt.Errorf("No ELB ID is set") 230 } 231 232 conn := testAccProvider.Meta().(*AWSClient).elbconn 233 234 describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancer{ 235 Names: []string{rs.Primary.ID}, 236 }) 237 238 if err != nil { 239 return err 240 } 241 242 if len(describe.LoadBalancers) != 1 || 243 describe.LoadBalancers[0].LoadBalancerName != rs.Primary.ID { 244 return fmt.Errorf("ELB not found") 245 } 246 247 *res = describe.LoadBalancers[0] 248 249 return nil 250 } 251 } 252 253 const testAccAWSELBConfig = ` 254 resource "aws_elb" "bar" { 255 name = "foobar-terraform-test" 256 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 257 258 listener { 259 instance_port = 8000 260 instance_protocol = "http" 261 lb_port = 80 262 lb_protocol = "http" 263 } 264 265 cross_zone_load_balancing = true 266 } 267 ` 268 269 const testAccAWSELBConfigNewInstance = ` 270 resource "aws_elb" "bar" { 271 name = "foobar-terraform-test" 272 availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"] 273 274 listener { 275 instance_port = 8000 276 instance_protocol = "http" 277 lb_port = 80 278 lb_protocol = "http" 279 } 280 281 instances = ["${aws_instance.foo.id}"] 282 } 283 284 resource "aws_instance" "foo" { 285 # us-west-2 286 ami = "ami-043a5034" 287 instance_type = "t1.micro" 288 } 289 ` 290 291 const testAccAWSELBConfigListenerSSLCertificateId = ` 292 resource "aws_elb" "bar" { 293 name = "foobar-terraform-test" 294 availability_zones = ["us-west-2a"] 295 296 listener { 297 instance_port = 8000 298 instance_protocol = "http" 299 ssl_certificate_id = "%s" 300 lb_port = 443 301 lb_protocol = "https" 302 } 303 } 304 ` 305 306 const testAccAWSELBConfigHealthCheck = ` 307 resource "aws_elb" "bar" { 308 name = "foobar-terraform-test" 309 availability_zones = ["us-west-2a"] 310 311 listener { 312 instance_port = 8000 313 instance_protocol = "http" 314 lb_port = 80 315 lb_protocol = "http" 316 } 317 318 health_check { 319 healthy_threshold = 5 320 unhealthy_threshold = 5 321 target = "HTTP:8000/" 322 interval = 60 323 timeout = 30 324 } 325 } 326 `