github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/data_source_aws_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 TestResourceCheckAwsAvailabilityZones_validateStateType(t *testing.T) {
    36  	_, errors := validateStateType("incorrect", "state")
    37  	if len(errors) == 0 {
    38  		t.Fatalf("Expected to trigger a validation error")
    39  	}
    40  
    41  	var testCases = []struct {
    42  		Value    string
    43  		ErrCount int
    44  	}{
    45  		{
    46  			Value:    "available",
    47  			ErrCount: 0,
    48  		},
    49  		{
    50  			Value:    "unavailable",
    51  			ErrCount: 0,
    52  		},
    53  	}
    54  
    55  	for _, tc := range testCases {
    56  		_, errors := validateStateType(tc.Value, "state")
    57  		if len(errors) != tc.ErrCount {
    58  			t.Fatalf("Expected %q not to trigger a validation error.", tc.Value)
    59  		}
    60  	}
    61  }
    62  
    63  func testAccCheckAwsAvailabilityZonesMeta(n string) resource.TestCheckFunc {
    64  	return func(s *terraform.State) error {
    65  		rs, ok := s.RootModule().Resources[n]
    66  		if !ok {
    67  			return fmt.Errorf("Can't find AZ resource: %s", n)
    68  		}
    69  
    70  		if rs.Primary.ID == "" {
    71  			return fmt.Errorf("AZ resource ID not set.")
    72  		}
    73  
    74  		actual, err := testAccCheckAwsAvailabilityZonesBuildAvailable(rs.Primary.Attributes)
    75  		if err != nil {
    76  			return err
    77  		}
    78  
    79  		expected := actual
    80  		sort.Strings(expected)
    81  		if reflect.DeepEqual(expected, actual) != true {
    82  			return fmt.Errorf("AZs not sorted - expected %v, got %v", expected, actual)
    83  		}
    84  		return nil
    85  	}
    86  }
    87  
    88  func testAccCheckAwsAvailabilityZoneState(n string) resource.TestCheckFunc {
    89  	return func(s *terraform.State) error {
    90  		rs, ok := s.RootModule().Resources[n]
    91  		if !ok {
    92  			return fmt.Errorf("Can't find AZ resource: %s", n)
    93  		}
    94  
    95  		if rs.Primary.ID == "" {
    96  			return fmt.Errorf("AZ resource ID not set.")
    97  		}
    98  
    99  		if _, ok := rs.Primary.Attributes["state"]; !ok {
   100  			return fmt.Errorf("AZs state filter is missing, should be set.")
   101  		}
   102  
   103  		_, err := testAccCheckAwsAvailabilityZonesBuildAvailable(rs.Primary.Attributes)
   104  		if err != nil {
   105  			return err
   106  		}
   107  		return nil
   108  	}
   109  }
   110  
   111  func testAccCheckAwsAvailabilityZonesBuildAvailable(attrs map[string]string) ([]string, error) {
   112  	v, ok := attrs["names.#"]
   113  	if !ok {
   114  		return nil, fmt.Errorf("Available AZ list is missing.")
   115  	}
   116  	qty, err := strconv.Atoi(v)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  	if qty < 1 {
   121  		return nil, fmt.Errorf("No AZs found in region, this is probably a bug.")
   122  	}
   123  	zones := make([]string, qty)
   124  	for n := range zones {
   125  		zone, ok := attrs["names."+strconv.Itoa(n)]
   126  		if !ok {
   127  			return nil, fmt.Errorf("AZ list corrupt, this is definitely a bug.")
   128  		}
   129  		zones[n] = zone
   130  	}
   131  	return zones, nil
   132  }
   133  
   134  const testAccCheckAwsAvailabilityZonesConfig = `
   135  data "aws_availability_zones" "availability_zones" { }
   136  `
   137  
   138  const testAccCheckAwsAvailabilityZonesStateConfig = `
   139  data "aws_availability_zones" "state_filter" {
   140  	state = "available"
   141  }
   142  `