github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/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  		},
    26  	})
    27  }
    28  
    29  func testAccCheckAwsAvailabilityZonesMeta(n string) resource.TestCheckFunc {
    30  	return func(s *terraform.State) error {
    31  		rs, ok := s.RootModule().Resources[n]
    32  		if !ok {
    33  			return fmt.Errorf("Can't find AZ resource: %s", n)
    34  		}
    35  
    36  		if rs.Primary.ID == "" {
    37  			return fmt.Errorf("AZ resource ID not set")
    38  		}
    39  
    40  		actual, err := testAccCheckAwsAvailabilityZonesBuildAvailable(rs.Primary.Attributes)
    41  		if err != nil {
    42  			return err
    43  		}
    44  
    45  		expected := actual
    46  		sort.Strings(expected)
    47  		if reflect.DeepEqual(expected, actual) != true {
    48  			return fmt.Errorf("AZs not sorted - expected %v, got %v", expected, actual)
    49  		}
    50  		return nil
    51  	}
    52  }
    53  
    54  func testAccCheckAwsAvailabilityZonesBuildAvailable(attrs map[string]string) ([]string, error) {
    55  	v, ok := attrs["names.#"]
    56  	if !ok {
    57  		return nil, fmt.Errorf("Available AZ list is missing")
    58  	}
    59  	qty, err := strconv.Atoi(v)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	if qty < 1 {
    64  		return nil, fmt.Errorf("No AZs found in region, this is probably a bug.")
    65  	}
    66  	zones := make([]string, qty)
    67  	for n := range zones {
    68  		zone, ok := attrs["names."+strconv.Itoa(n)]
    69  		if !ok {
    70  			return nil, fmt.Errorf("AZ list corrupt, this is definitely a bug")
    71  		}
    72  		zones[n] = zone
    73  	}
    74  	return zones, nil
    75  }
    76  
    77  const testAccCheckAwsAvailabilityZonesConfig = `
    78  data "aws_availability_zones" "availability_zones" {
    79  }
    80  `