Skip to content

Commit

Permalink
tests/provider: Bulk update aws_availability_zones data sources in te…
Browse files Browse the repository at this point in the history
…st configurations to exclude Local Zones (#12517)

* tests/provider: Bulk update aws_availability_zones data sources in test configurations to exclude Local Zones

Reference: #12412

This is the least risky update for all testing to exclude Local Zones from the data source, since us-west-2-lax-1 generally is missing functionality and also to prevent Availability Zone and Local Zone mixing when multiple Availability Zones are required, which is not allowed in many services.

In the future, this type of configuration churn can be reduced with one of the following solutions:

- Removing aws_availability_zones data source usage where it has now been superceded by the creation of the aws_ec2_instance_type_offering data source
- Standardizing base configurations per data source/resource
- Standardizing shared configurations into helper test configurations and composing them

Output from acceptance testing (as a smoke test):

```
--- PASS: TestAccAWSLB_ALB_basic (197.43s)
```

* docs/provider: Include Local Zones filtering in Contributing Guide

Reference: #12517 (comment)

* data-source/aws_vpc_endpoint_service: Keep non-filtered aws_availability_zones data source configuration

Since the expected return value of availability_zones applies to all Availability Zones and Local Zones:

```
--- FAIL: TestAccDataSourceAwsVpcEndpointService_gateway (6.13s)
    testing.go:654: Step 0 error: Check failed: Check 3/10 error: data.aws_vpc_endpoint_service.test: Attribute 'availability_zones.#' expected "4", got "5"
```

Output from acceptance testing:

```
--- PASS: TestAccDataSourceAwsVpcEndpointService_gateway (15.03s)
```

* tests/data-source/aws_availability_zone: Exclude Local Zones from aws_availability_zones results in test configurations

Previously:

```
--- FAIL: TestAccDataSourceAwsAvailabilityZone_Filter (9.96s)
    testing.go:654: Step 0 error: Check failed: Check 1/7 error: data.aws_availability_zone.test: Attribute 'group_name' expected "us-west-2", got "us-west-2-lax-1"
```

Output from acceptance testing:

```
--- PASS: TestAccDataSourceAwsAvailabilityZone_Filter (16.70s)
--- PASS: TestAccDataSourceAwsAvailabilityZone_AllAvailabilityZones (16.97s)
--- PASS: TestAccDataSourceAwsAvailabilityZone_Name (17.17s)
--- PASS: TestAccDataSourceAwsAvailabilityZone_ZoneId (17.28s)
```
  • Loading branch information
bflad authored Mar 25, 2020
1 parent f3eb815 commit f004cde
Show file tree
Hide file tree
Showing 110 changed files with 2,055 additions and 181 deletions.
14 changes: 13 additions & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,19 @@ The below are location-based items that _may_ be noted during review and are rec
}
```

- [ ] __Uses aws_availability_zones Data Source__: Any hardcoded AWS Availability Zone configuration, e.g. `us-west-2a`, should be replaced with the [`aws_availability_zones` data source](https://www.terraform.io/docs/providers/aws/d/availability_zones.html). A common pattern is declaring `data "aws_availability_zones" "current" {}` and referencing it via `data.aws_availability_zones.current.names[0]` or `data.aws_availability_zones.current.names[count.index]` in resources utilizing `count`.
- [ ] __Uses aws_availability_zones Data Source__: Any hardcoded AWS Availability Zone configuration, e.g. `us-west-2a`, should be replaced with the [`aws_availability_zones` data source](https://www.terraform.io/docs/providers/aws/d/availability_zones.html). A common pattern is declaring `data "aws_availability_zones" "available" {...}` and referencing it via `data.aws_availability_zones.available.names[0]` or `data.aws_availability_zones.available.names[count.index]` in resources utilizing `count`.

```hcl
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
```

- [ ] __Uses aws_region Data Source__: Any hardcoded AWS Region configuration, e.g. `us-west-2`, should be replaced with the [`aws_region` data source](https://www.terraform.io/docs/providers/aws/d/region.html). A common pattern is declaring `data "aws_region" "current" {}` and referencing it via `data.aws_region.current.name`
- [ ] __Uses aws_partition Data Source__: Any hardcoded AWS Partition configuration, e.g. the `aws` in a `arn:aws:SERVICE:REGION:ACCOUNT:RESOURCE` ARN, should be replaced with the [`aws_partition` data source](https://www.terraform.io/docs/providers/aws/d/partition.html). A common pattern is declaring `data "aws_partition" "current" {}` and referencing it via `data.aws_partition.current.partition`
- [ ] __Uses Builtin ARN Check Functions__: Tests should utilize available ARN check functions, e.g. `testAccMatchResourceAttrRegionalARN()`, to validate ARN attribute values in the Terraform state over `resource.TestCheckResourceAttrSet()` and `resource.TestMatchResourceAttr()`
Expand Down
5 changes: 5 additions & 0 deletions aws/data_source_aws_autoscaling_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ data "aws_ami" "ubuntu" {
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_launch_configuration" "data_source_aws_autoscaling_group_test" {
Expand Down
18 changes: 16 additions & 2 deletions aws/data_source_aws_autoscaling_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@ data "aws_ami" "test_ami" {
}
}
data "aws_availability_zones" "available" {}
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_launch_configuration" "foobar" {
image_id = "${data.aws_ami.test_ami.id}"
Expand Down Expand Up @@ -167,7 +174,14 @@ data "aws_ami" "test_ami" {
}
}
data "aws_availability_zones" "available" {}
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_launch_configuration" "foobar" {
image_id = "${data.aws_ami.test_ami.id}"
Expand Down
36 changes: 32 additions & 4 deletions aws/data_source_aws_availability_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,14 @@ func TestAccDataSourceAwsAvailabilityZone_ZoneId(t *testing.T) {

func testAccDataSourceAwsAvailabilityZoneConfigAllAvailabilityZones() string {
return fmt.Sprintf(`
data "aws_availability_zones" "test" {}
data "aws_availability_zones" "test" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
data "aws_availability_zone" "test" {
all_availability_zones = true
Expand All @@ -117,7 +124,14 @@ data "aws_availability_zone" "test" {

func testAccDataSourceAwsAvailabilityZoneConfigFilter() string {
return fmt.Sprintf(`
data "aws_availability_zones" "test" {}
data "aws_availability_zones" "test" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
data "aws_availability_zone" "test" {
filter {
Expand All @@ -130,7 +144,14 @@ data "aws_availability_zone" "test" {

func testAccDataSourceAwsAvailabilityZoneConfigName() string {
return fmt.Sprintf(`
data "aws_availability_zones" "test" {}
data "aws_availability_zones" "test" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
data "aws_availability_zone" "test" {
name = data.aws_availability_zones.test.names[0]
Expand All @@ -140,7 +161,14 @@ data "aws_availability_zone" "test" {

func testAccDataSourceAwsAvailabilityZoneConfigZoneId() string {
return fmt.Sprintf(`
data "aws_availability_zones" "test" {}
data "aws_availability_zones" "test" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
data "aws_availability_zone" "test" {
zone_id = data.aws_availability_zones.test.zone_ids[0]
Expand Down
9 changes: 8 additions & 1 deletion aws/data_source_aws_cloudhsm2_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ variable "subnets" {
type = "list"
}
data "aws_availability_zones" "available" {}
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_vpc" "cloudhsm_v2_test_vpc" {
cidr_block = "10.0.0.0/16"
Expand Down
27 changes: 24 additions & 3 deletions aws/data_source_aws_db_cluster_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@ func testAccCheckAwsDbClusterSnapshotDataSourceExists(dataSourceName string) res

func testAccCheckAwsDbClusterSnapshotDataSourceConfig_DbClusterSnapshotIdentifier(rName string) string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {}
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_vpc" "test" {
cidr_block = "192.168.0.0/16"
Expand Down Expand Up @@ -169,7 +176,14 @@ data "aws_db_cluster_snapshot" "test" {

func testAccCheckAwsDbClusterSnapshotDataSourceConfig_DbClusterIdentifier(rName string) string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {}
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_vpc" "test" {
cidr_block = "192.168.0.0/16"
Expand Down Expand Up @@ -220,7 +234,14 @@ data "aws_db_cluster_snapshot" "test" {

func testAccCheckAwsDbClusterSnapshotDataSourceConfig_MostRecent(rName string) string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {}
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_vpc" "test" {
cidr_block = "192.168.0.0/16"
Expand Down
8 changes: 7 additions & 1 deletion aws/data_source_aws_directory_service_directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package aws

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

Expand Down Expand Up @@ -74,6 +75,11 @@ func testAccDataSourceAwsDirectoryServiceDirectoryConfig_Prerequisites(adType st
return fmt.Sprintf(`
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_vpc" "main" {
Expand Down
27 changes: 24 additions & 3 deletions aws/data_source_aws_ebs_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,14 @@ func testAccCheckAwsEbsSnapshotDataSourceID(n string) resource.TestCheckFunc {
}

const testAccCheckAwsEbsSnapshotDataSourceConfig = `
data "aws_availability_zones" "available" {}
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_ebs_volume" "test" {
availability_zone = "${data.aws_availability_zones.available.names[0]}"
Expand All @@ -106,7 +113,14 @@ data "aws_ebs_snapshot" "test" {
`

const testAccCheckAwsEbsSnapshotDataSourceConfigFilter = `
data "aws_availability_zones" "available" {}
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_ebs_volume" "test" {
availability_zone = "${data.aws_availability_zones.available.names[0]}"
Expand All @@ -127,7 +141,14 @@ data "aws_ebs_snapshot" "test" {
`

const testAccCheckAwsEbsSnapshotDataSourceConfigMostRecent = `
data "aws_availability_zones" "available" {}
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_ebs_volume" "test" {
availability_zone = "${data.aws_availability_zones.available.names[0]}"
Expand Down
18 changes: 16 additions & 2 deletions aws/data_source_aws_ebs_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,14 @@ func testAccCheckAwsEbsVolumeDataSourceID(n string) resource.TestCheckFunc {
}

const testAccCheckAwsEbsVolumeDataSourceConfig = `
data "aws_availability_zones" "available" {}
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_ebs_volume" "example" {
availability_zone = "${data.aws_availability_zones.available.names[0]}"
Expand All @@ -86,7 +93,14 @@ data "aws_ebs_volume" "ebs_volume" {
`

const testAccCheckAwsEbsVolumeDataSourceConfigWithMultipleFilters = `
data "aws_availability_zones" "available" {}
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_ebs_volume" "external1" {
availability_zone = "${data.aws_availability_zones.available.names[0]}"
Expand Down
5 changes: 5 additions & 0 deletions aws/data_source_aws_ec2_instance_type_offering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ func testAccAWSEc2InstanceTypeOfferingDataSourceConfigLocationType() string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
# Rather than hardcode an instance type in the testing,
Expand Down
5 changes: 5 additions & 0 deletions aws/data_source_aws_ec2_instance_type_offerings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ func testAccAWSEc2InstanceTypeOfferingsDataSourceConfigLocationType() string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
data "aws_ec2_instance_type_offerings" "test" {
Expand Down
10 changes: 10 additions & 0 deletions aws/data_source_aws_ec2_transit_gateway_vpc_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ data "aws_availability_zones" "available" {
# IncorrectState: Transit Gateway is not available in availability zone us-west-2d
blacklisted_zone_ids = ["usw2-az4"]
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_vpc" "test" {
Expand Down Expand Up @@ -106,6 +111,11 @@ data "aws_availability_zones" "available" {
# IncorrectState: Transit Gateway is not available in availability zone us-west-2d
blacklisted_zone_ids = ["usw2-az4"]
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_vpc" "test" {
Expand Down
5 changes: 5 additions & 0 deletions aws/data_source_aws_eip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ data "aws_availability_zones" "available" {
# Error launching source instance: Unsupported: Your requested instance type (t2.micro) is not supported in your requested Availability Zone (us-west-2d).
blacklisted_zone_ids = ["usw2-az4"]
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_vpc" "test" {
Expand Down
9 changes: 8 additions & 1 deletion aws/data_source_aws_elasticache_replication_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ func TestAccDataSourceAwsElasticacheReplicationGroup_ClusterMode(t *testing.T) {

func testAccDataSourceAwsElasticacheReplicationGroupConfig_basic(rName string) string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {}
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_elasticache_replication_group" "test" {
replication_group_id = %[1]q
Expand Down
5 changes: 5 additions & 0 deletions aws/data_source_aws_elasticsearch_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ func testAccAWSElasticsearchDomainConfigAdvancedWithDataSource(rInt int) string
return fmt.Sprintf(`
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
data "aws_partition" "current" {}
Expand Down
Loading

0 comments on commit f004cde

Please sign in to comment.