-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/aws: Add tests for aws_availability_zones data source
Closing off my other AWS availability zone branch, I'm adding tests for the existing aws_availability_zones data source. This closes #4848.
- Loading branch information
1 parent
fcc3736
commit 2d65e9d
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
builtin/providers/aws/data_source_availability_zones_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
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"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
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 == "" { | ||
return fmt.Errorf("AZ resource ID not set") | ||
} | ||
|
||
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 | ||
} | ||
} | ||
|
||
func testAccCheckAwsAvailabilityZonesBuildAvailable(attrs map[string]string) ([]string, error) { | ||
v, ok := attrs["instance.#"] | ||
if !ok { | ||
return nil, fmt.Errorf("Available AZ list is missing") | ||
} | ||
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 { | ||
zone, ok := attrs["instance."+strconv.Itoa(n)] | ||
if !ok { | ||
return nil, fmt.Errorf("AZ list corrupt, this is definitely a bug") | ||
} | ||
zones[n] = zone | ||
} | ||
return zones, nil | ||
} | ||
|
||
const testAccCheckAwsAvailabilityZonesConfig = ` | ||
data "aws_availability_zones" "availability_zones" { | ||
} | ||
` |