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