2016-05-30 03:23:42 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAWSAvailabilityZones_basic(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckAwsAvailabilityZonesConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAwsAvailabilityZonesMeta("data.aws_availability_zones.availability_zones"),
|
|
|
|
),
|
|
|
|
},
|
2016-08-05 02:14:05 +02:00
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckAwsAvailabilityZonesStateConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAwsAvailabilityZoneState("data.aws_availability_zones.state_filter"),
|
|
|
|
),
|
|
|
|
},
|
2016-05-30 03:23:42 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-03 15:04:35 +02:00
|
|
|
func TestResourceCheckAwsAvailabilityZones_validateStateType(t *testing.T) {
|
|
|
|
_, errors := validateStateType("incorrect", "state")
|
|
|
|
if len(errors) == 0 {
|
|
|
|
t.Fatalf("Expected to trigger a validation error")
|
|
|
|
}
|
|
|
|
|
|
|
|
var testCases = []struct {
|
|
|
|
Value string
|
|
|
|
ErrCount int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Value: "available",
|
|
|
|
ErrCount: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "unavailable",
|
|
|
|
ErrCount: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
_, errors := validateStateType(tc.Value, "state")
|
|
|
|
if len(errors) != tc.ErrCount {
|
|
|
|
t.Fatalf("Expected %q not to trigger a validation error.", tc.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-30 03:23:42 +02:00
|
|
|
func testAccCheckAwsAvailabilityZonesMeta(n string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Can't find AZ resource: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
2016-08-05 02:14:05 +02:00
|
|
|
return fmt.Errorf("AZ resource ID not set.")
|
2016-05-30 03:23:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := testAccCheckAwsAvailabilityZonesBuildAvailable(rs.Primary.Attributes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := actual
|
|
|
|
sort.Strings(expected)
|
|
|
|
if reflect.DeepEqual(expected, actual) != true {
|
|
|
|
return fmt.Errorf("AZs not sorted - expected %v, got %v", expected, actual)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:14:05 +02:00
|
|
|
func testAccCheckAwsAvailabilityZoneState(n string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Can't find AZ resource: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("AZ resource ID not set.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := rs.Primary.Attributes["state"]; !ok {
|
|
|
|
return fmt.Errorf("AZs state filter is missing, should be set.")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := testAccCheckAwsAvailabilityZonesBuildAvailable(rs.Primary.Attributes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-30 03:23:42 +02:00
|
|
|
func testAccCheckAwsAvailabilityZonesBuildAvailable(attrs map[string]string) ([]string, error) {
|
2016-06-15 15:17:12 +02:00
|
|
|
v, ok := attrs["names.#"]
|
2016-05-30 03:23:42 +02:00
|
|
|
if !ok {
|
2016-08-05 02:14:05 +02:00
|
|
|
return nil, fmt.Errorf("Available AZ list is missing.")
|
2016-05-30 03:23:42 +02:00
|
|
|
}
|
|
|
|
qty, err := strconv.Atoi(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if qty < 1 {
|
|
|
|
return nil, fmt.Errorf("No AZs found in region, this is probably a bug.")
|
|
|
|
}
|
|
|
|
zones := make([]string, qty)
|
|
|
|
for n := range zones {
|
2016-06-15 15:17:12 +02:00
|
|
|
zone, ok := attrs["names."+strconv.Itoa(n)]
|
2016-05-30 03:23:42 +02:00
|
|
|
if !ok {
|
2016-08-05 02:14:05 +02:00
|
|
|
return nil, fmt.Errorf("AZ list corrupt, this is definitely a bug.")
|
2016-05-30 03:23:42 +02:00
|
|
|
}
|
|
|
|
zones[n] = zone
|
|
|
|
}
|
|
|
|
return zones, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccCheckAwsAvailabilityZonesConfig = `
|
2016-08-05 02:14:05 +02:00
|
|
|
data "aws_availability_zones" "availability_zones" { }
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccCheckAwsAvailabilityZonesStateConfig = `
|
|
|
|
data "aws_availability_zones" "state_filter" {
|
|
|
|
state = "available"
|
2016-05-30 03:23:42 +02:00
|
|
|
}
|
|
|
|
`
|