providers/aws: add test for health check, save health check state

cc/ @vertis

I added an acceptance test here, as well as making it so we
saved the health check state on update_state.
This commit is contained in:
Jack Pearkes 2014-07-30 10:15:22 -04:00
parent c2f718d33f
commit 7adc6162d8
4 changed files with 148 additions and 8 deletions

View File

@ -273,10 +273,17 @@ func resource_aws_elb_update_state(
if len(balancer.Instances) > 0 && balancer.Instances[0].InstanceId != "" { if len(balancer.Instances) > 0 && balancer.Instances[0].InstanceId != "" {
toFlatten["instances"] = flattenInstances(balancer.Instances) toFlatten["instances"] = flattenInstances(balancer.Instances)
}
// There's only one health check, so save that to state as we
// currently can
if balancer.HealthCheck.Target != "" {
toFlatten["health_check"] = flattenHealthCheck(balancer.HealthCheck)
}
for k, v := range flatmap.Flatten(toFlatten) { for k, v := range flatmap.Flatten(toFlatten) {
s.Attributes[k] = v s.Attributes[k] = v
} }
}
return s, nil return s, nil
} }

View File

@ -81,6 +81,34 @@ func TestAccAWSELB_InstanceAttaching(t *testing.T) {
}) })
} }
func TestAccAWSELB_HealthCheck(t *testing.T) {
var conf elb.LoadBalancer
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSELBDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSELBConfigHealthCheck,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSELBExists("aws_elb.bar", &conf),
testAccCheckAWSELBAttributesHealthCheck(&conf),
resource.TestCheckResourceAttr(
"aws_elb.bar", "health_check.0.healthy_threshold", "5"),
resource.TestCheckResourceAttr(
"aws_elb.bar", "health_check.0.unhealthy_threshold", "5"),
resource.TestCheckResourceAttr(
"aws_elb.bar", "health_check.0.target", "HTTP:8000/"),
resource.TestCheckResourceAttr(
"aws_elb.bar", "health_check.0.timeout", "30"),
resource.TestCheckResourceAttr(
"aws_elb.bar", "health_check.0.interval", "60"),
),
},
},
})
}
func testAccCheckAWSELBDestroy(s *terraform.State) error { func testAccCheckAWSELBDestroy(s *terraform.State) error {
conn := testAccProvider.elbconn conn := testAccProvider.elbconn
@ -146,6 +174,39 @@ func testAccCheckAWSELBAttributes(conf *elb.LoadBalancer) resource.TestCheckFunc
} }
} }
func testAccCheckAWSELBAttributesHealthCheck(conf *elb.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error {
if conf.AvailabilityZones[0].AvailabilityZone != "us-west-2a" {
return fmt.Errorf("bad availability_zones")
}
if conf.LoadBalancerName != "foobar-terraform-test" {
return fmt.Errorf("bad name")
}
check := elb.HealthCheck{
Timeout: 30,
UnhealthyThreshold: 5,
HealthyThreshold: 5,
Interval: 60,
Target: "HTTP:8000/",
}
if !reflect.DeepEqual(conf.HealthCheck, check) {
return fmt.Errorf(
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
conf.HealthCheck,
check)
}
if conf.DNSName == "" {
return fmt.Errorf("empty dns_name")
}
return nil
}
}
func testAccCheckAWSELBExists(n string, res *elb.LoadBalancer) resource.TestCheckFunc { func testAccCheckAWSELBExists(n string, res *elb.LoadBalancer) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
rs, ok := s.Resources[n] rs, ok := s.Resources[n]
@ -215,3 +276,25 @@ resource "aws_instance" "foo" {
instance_type = "t1.micro" instance_type = "t1.micro"
} }
` `
const testAccAWSELBConfigHealthCheck = `
resource "aws_elb" "bar" {
name = "foobar-terraform-test"
availability_zones = ["us-west-2a"]
listener {
instance_port = 8000
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
health_check {
healthy_threshold = 5
unhealthy_threshold = 5
target = "HTTP:8000/"
interval = 60
timeout = 30
}
}
`

View File

@ -126,6 +126,23 @@ func flattenIPPerms(list []ec2.IPPerm) []map[string]interface{} {
return result return result
} }
// Flattens a health check into something that flatmap.Flatten()
// can handle
func flattenHealthCheck(check elb.HealthCheck) []map[string]interface{} {
result := make([]map[string]interface{}, 0, 1)
chk := make(map[string]interface{})
chk["unhealthy_threshold"] = int(check.UnhealthyThreshold)
chk["healthy_threshold"] = int(check.HealthyThreshold)
chk["target"] = check.Target
chk["timeout"] = int(check.Timeout)
chk["interval"] = int(check.Interval)
result = append(result, chk)
return result
}
// Flattens an array of UserSecurityGroups into a []string // Flattens an array of UserSecurityGroups into a []string
func flattenSecurityGroups(list []ec2.UserSecurityGroup) []string { func flattenSecurityGroups(list []ec2.UserSecurityGroup) []string {
result := make([]string, 0, len(list)) result := make([]string, 0, len(list))

View File

@ -206,6 +206,39 @@ func Test_expandListeners(t *testing.T) {
} }
func Test_flattenHealthCheck(t *testing.T) {
cases := []struct {
Input elb.HealthCheck
Output []map[string]interface{}
}{
{
Input: elb.HealthCheck{
UnhealthyThreshold: 10,
HealthyThreshold: 10,
Target: "HTTP:80/",
Timeout: 30,
Interval: 30,
},
Output: []map[string]interface{}{
map[string]interface{}{
"unhealthy_threshold": 10,
"healthy_threshold": 10,
"target": "HTTP:80/",
"timeout": 30,
"interval": 30,
},
},
},
}
for _, tc := range cases {
output := flattenHealthCheck(tc.Input)
if !reflect.DeepEqual(output, tc.Output) {
t.Fatalf("Got:\n\n%#v\n\nExpected:\n\n%#v", output, tc.Output)
}
}
}
func Test_expandStringList(t *testing.T) { func Test_expandStringList(t *testing.T) {
expanded := flatmap.Expand(testConf(), "availability_zones").([]interface{}) expanded := flatmap.Expand(testConf(), "availability_zones").([]interface{})
stringList := expandStringList(expanded) stringList := expandStringList(expanded)