2014-07-15 00:48:22 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/goamz/elb"
|
|
|
|
)
|
|
|
|
|
2014-07-16 23:02:47 +02:00
|
|
|
func TestAccAWSELB_basic(t *testing.T) {
|
2014-07-15 00:48:22 +02:00
|
|
|
var conf elb.LoadBalancer
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSELBDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSELBConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSELBExists("aws_elb.bar", &conf),
|
|
|
|
testAccCheckAWSELBAttributes(&conf),
|
2014-07-16 23:02:47 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_elb.bar", "name", "foobar-terraform-test"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_elb.bar", "availability_zones.0", "us-west-2a"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_elb.bar", "availability_zones.1", "us-west-2b"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_elb.bar", "availability_zones.2", "us-west-2c"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_elb.bar", "listener.0.instance_port", "8000"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_elb.bar", "listener.0.instance_protocol", "http"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_elb.bar", "listener.0.lb_port", "80"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_elb.bar", "listener.0.lb_protocol", "http"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSELB_InstanceAttaching(t *testing.T) {
|
|
|
|
var conf elb.LoadBalancer
|
|
|
|
|
|
|
|
testCheckInstanceAttached := func(count int) resource.TestCheckFunc {
|
|
|
|
return func(*terraform.State) error {
|
|
|
|
if len(conf.Instances) != count {
|
|
|
|
return fmt.Errorf("instance count does not match")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSELBDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSELBConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSELBExists("aws_elb.bar", &conf),
|
|
|
|
testAccCheckAWSELBAttributes(&conf),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSELBConfigNewInstance,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSELBExists("aws_elb.bar", &conf),
|
|
|
|
testCheckInstanceAttached(1),
|
2014-07-15 00:48:22 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSELBDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.elbconn
|
|
|
|
|
|
|
|
for _, rs := range s.Resources {
|
|
|
|
if rs.Type != "aws_elb" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancer{
|
|
|
|
Names: []string{rs.ID},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
if len(describe.LoadBalancers) != 0 &&
|
|
|
|
describe.LoadBalancers[0].LoadBalancerName == rs.ID {
|
|
|
|
return fmt.Errorf("ELB still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the error
|
|
|
|
providerErr, ok := err.(*elb.Error)
|
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if providerErr.Code != "InvalidLoadBalancerName.NotFound" {
|
|
|
|
return fmt.Errorf("Unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSELBAttributes(conf *elb.LoadBalancer) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-07-16 23:02:47 +02:00
|
|
|
if conf.AvailabilityZones[0].AvailabilityZone != "us-west-2a" {
|
2014-07-15 00:48:22 +02:00
|
|
|
return fmt.Errorf("bad availability_zones")
|
|
|
|
}
|
|
|
|
|
2014-07-15 00:57:04 +02:00
|
|
|
if conf.LoadBalancerName != "foobar-terraform-test" {
|
2014-07-15 00:48:22 +02:00
|
|
|
return fmt.Errorf("bad name")
|
|
|
|
}
|
|
|
|
|
|
|
|
l := elb.Listener{
|
|
|
|
InstancePort: 8000,
|
2014-07-15 14:49:40 +02:00
|
|
|
InstanceProtocol: "HTTP",
|
2014-07-15 00:48:22 +02:00
|
|
|
LoadBalancerPort: 80,
|
2014-07-15 14:49:40 +02:00
|
|
|
Protocol: "HTTP",
|
2014-07-15 00:48:22 +02:00
|
|
|
}
|
2014-07-15 00:57:04 +02:00
|
|
|
|
2014-07-15 00:48:22 +02:00
|
|
|
if !reflect.DeepEqual(conf.Listeners[0], l) {
|
2014-07-15 14:49:40 +02:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
|
|
|
conf.Listeners[0],
|
|
|
|
l)
|
2014-07-15 00:48:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if conf.DNSName == "" {
|
|
|
|
return fmt.Errorf("empty dns_name")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSELBExists(n string, res *elb.LoadBalancer) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.ID == "" {
|
|
|
|
return fmt.Errorf("No ELB ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.elbconn
|
|
|
|
|
|
|
|
describe, err := conn.DescribeLoadBalancers(&elb.DescribeLoadBalancer{
|
|
|
|
Names: []string{rs.ID},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(describe.LoadBalancers) != 1 ||
|
|
|
|
describe.LoadBalancers[0].LoadBalancerName != rs.ID {
|
|
|
|
return fmt.Errorf("ELB not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*res = describe.LoadBalancers[0]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccAWSELBConfig = `
|
|
|
|
resource "aws_elb" "bar" {
|
|
|
|
name = "foobar-terraform-test"
|
2014-07-16 23:02:47 +02:00
|
|
|
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
|
2014-07-15 00:48:22 +02:00
|
|
|
|
2014-07-15 00:57:04 +02:00
|
|
|
listener {
|
2014-07-15 00:48:22 +02:00
|
|
|
instance_port = 8000
|
|
|
|
instance_protocol = "http"
|
|
|
|
lb_port = 80
|
|
|
|
lb_protocol = "http"
|
|
|
|
}
|
2014-07-15 00:57:04 +02:00
|
|
|
|
|
|
|
instances = []
|
2014-07-15 00:48:22 +02:00
|
|
|
}
|
|
|
|
`
|
2014-07-16 23:02:47 +02:00
|
|
|
|
|
|
|
const testAccAWSELBConfigNewInstance = `
|
|
|
|
resource "aws_elb" "bar" {
|
|
|
|
name = "foobar-terraform-test"
|
|
|
|
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
|
|
|
|
|
|
|
|
listener {
|
|
|
|
instance_port = 8000
|
|
|
|
instance_protocol = "http"
|
|
|
|
lb_port = 80
|
|
|
|
lb_protocol = "http"
|
|
|
|
}
|
|
|
|
|
|
|
|
instances = ["${aws_instance.foo.id}"]
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "foo" {
|
|
|
|
# us-west-2
|
|
|
|
ami = "ami-043a5034"
|
|
|
|
instance_type = "t1.micro"
|
|
|
|
}
|
|
|
|
`
|