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