github.com/daveadams/terraform@v0.6.4-0.20160830094355-13ce74975936/builtin/providers/aws/data_source_availability_zones_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "reflect" 6 "sort" 7 "strconv" 8 "testing" 9 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSAvailabilityZones_basic(t *testing.T) { 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 Steps: []resource.TestStep{ 19 resource.TestStep{ 20 Config: testAccCheckAwsAvailabilityZonesConfig, 21 Check: resource.ComposeTestCheckFunc( 22 testAccCheckAwsAvailabilityZonesMeta("data.aws_availability_zones.availability_zones"), 23 ), 24 }, 25 resource.TestStep{ 26 Config: testAccCheckAwsAvailabilityZonesStateConfig, 27 Check: resource.ComposeTestCheckFunc( 28 testAccCheckAwsAvailabilityZoneState("data.aws_availability_zones.state_filter"), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func testAccCheckAwsAvailabilityZonesMeta(n string) resource.TestCheckFunc { 36 return func(s *terraform.State) error { 37 rs, ok := s.RootModule().Resources[n] 38 if !ok { 39 return fmt.Errorf("Can't find AZ resource: %s", n) 40 } 41 42 if rs.Primary.ID == "" { 43 return fmt.Errorf("AZ resource ID not set.") 44 } 45 46 actual, err := testAccCheckAwsAvailabilityZonesBuildAvailable(rs.Primary.Attributes) 47 if err != nil { 48 return err 49 } 50 51 expected := actual 52 sort.Strings(expected) 53 if reflect.DeepEqual(expected, actual) != true { 54 return fmt.Errorf("AZs not sorted - expected %v, got %v", expected, actual) 55 } 56 return nil 57 } 58 } 59 60 func testAccCheckAwsAvailabilityZoneState(n string) resource.TestCheckFunc { 61 return func(s *terraform.State) error { 62 rs, ok := s.RootModule().Resources[n] 63 if !ok { 64 return fmt.Errorf("Can't find AZ resource: %s", n) 65 } 66 67 if rs.Primary.ID == "" { 68 return fmt.Errorf("AZ resource ID not set.") 69 } 70 71 if _, ok := rs.Primary.Attributes["state"]; !ok { 72 return fmt.Errorf("AZs state filter is missing, should be set.") 73 } 74 75 _, err := testAccCheckAwsAvailabilityZonesBuildAvailable(rs.Primary.Attributes) 76 if err != nil { 77 return err 78 } 79 return nil 80 } 81 } 82 83 func testAccCheckAwsAvailabilityZonesBuildAvailable(attrs map[string]string) ([]string, error) { 84 v, ok := attrs["names.#"] 85 if !ok { 86 return nil, fmt.Errorf("Available AZ list is missing.") 87 } 88 qty, err := strconv.Atoi(v) 89 if err != nil { 90 return nil, err 91 } 92 if qty < 1 { 93 return nil, fmt.Errorf("No AZs found in region, this is probably a bug.") 94 } 95 zones := make([]string, qty) 96 for n := range zones { 97 zone, ok := attrs["names."+strconv.Itoa(n)] 98 if !ok { 99 return nil, fmt.Errorf("AZ list corrupt, this is definitely a bug.") 100 } 101 zones[n] = zone 102 } 103 return zones, nil 104 } 105 106 const testAccCheckAwsAvailabilityZonesConfig = ` 107 data "aws_availability_zones" "availability_zones" { } 108 ` 109 110 const testAccCheckAwsAvailabilityZonesStateConfig = ` 111 data "aws_availability_zones" "state_filter" { 112 state = "available" 113 } 114 `