Skip to content

Commit

Permalink
tests/provider: Remove extraneously hardcoded provider configurations…
Browse files Browse the repository at this point in the history
… in test configurations (#12277)

Reference: #8983
Reference: https://github.com/terraform-providers/terraform-provider-aws/blob/master/.github/CONTRIBUTING.md#writing-and-running-cross-region-acceptance-tests

This is the first round of tfproviderlint AT004 fixes. Remaining reports will require some additional design work, e.g. potentially creating a separate EC2 Classic provider in the acceptance testing framework.

Previously:

```
aws/data_source_aws_elastic_beanstalk_hosted_zone_test.go:42:75: AT004: provider declaration should be omitted
aws/provider_test.go:425:21: AT004: provider declaration should be omitted
...
aws/provider_test.go:1259:21: AT004: provider declaration should be omitted
aws/resource_aws_ami_from_instance_test.go:131:21: AT004: provider declaration should be omitted
aws/resource_aws_codecommit_trigger_test.go:84:40: AT004: provider declaration should be omitted
aws/resource_aws_dynamodb_global_table_test.go:186:21: AT004: provider declaration should be omitted
aws/resource_aws_eip_test.go:999:21: AT004: provider declaration should be
aws/resource_aws_pinpoint_adm_channel_test.go:115:21: AT004: provider declaration should be omitted
aws/resource_aws_pinpoint_apns_channel_test.go:197:21: AT004: provider declaration should be omitted
aws/resource_aws_pinpoint_apns_channel_test.go:215:21: AT004: provider declaration should be omitted
aws/resource_aws_pinpoint_apns_sandbox_channel_test.go:197:21: AT004: provider declaration should be omitted
aws/resource_aws_pinpoint_apns_sandbox_channel_test.go:215:21: AT004: provider declaration should be omitted
aws/resource_aws_pinpoint_apns_voip_channel_test.go:197:21: AT004: provider declaration should be omitted
aws/resource_aws_pinpoint_apns_voip_channel_test.go:215:21: AT004: provider declaration should be omitted
aws/resource_aws_pinpoint_apns_voip_sandbox_channel_test.go:197:21: AT004: provider declaration should be omitted
aws/resource_aws_pinpoint_apns_voip_sandbox_channel_test.go:215:21: AT004: provider declaration should be omitted
aws/resource_aws_pinpoint_baidu_channel_test.go:109:21: AT004: provider declaration should be omitted
aws/resource_aws_pinpoint_baidu_channel_test.go:91:21: AT004: provider declaration should be omitted
aws/resource_aws_pinpoint_email_channel_test.go:141:53: AT004: provider declaration should be omitted
aws/resource_aws_pinpoint_email_channel_test.go:83:52: AT004: provider declaration should be omitted
aws/resource_aws_pinpoint_event_stream_test.go:136:52: AT004: provider declaration should be omitted
aws/resource_aws_pinpoint_event_stream_test.go:79:51: AT004: provider declaration should be omitted
aws/resource_aws_pinpoint_gcm_channel_test.go:95:21: AT004: provider declaration should be omitted
aws/resource_aws_pinpoint_sms_channel_test.go:146:50: AT004: provider declaration should be omitted
aws/resource_aws_pinpoint_sms_channel_test.go:158:21: AT004: provider declaration should be omitted
aws/resource_aws_rds_cluster_test.go:2796:21: AT004: provider declaration should be omitted
aws/resource_aws_route53_zone_association_test.go:267:51: AT004: provider declaration should be omitted
aws/resource_aws_s3_bucket_test.go:2772:21: AT004: provider declaration should be omitted
aws/resource_aws_s3_bucket_test.go:3268:50: AT004: provider declaration should be omitted
```

Output from acceptance testing:

```
--- PASS: TestAccAWSDataSourceElasticBeanstalkHostedZone_basic (15.30s)
--- PASS: TestAccAWSDataSourceElasticBeanstalkHostedZone_Region (19.40s)

--- PASS: TestAccAWSAMIFromInstance_basic (458.33s)
--- PASS: TestAccAWSAMIFromInstance_tags (459.26s)

--- PASS: TestAccAWSCodeCommitTrigger_basic (16.98s)

--- PASS: TestAccAWSDynamoDbGlobalTable_multipleRegions (107.60s)

--- PASS: TestAccAWSEIP_Instance_Reassociate (120.78s)

--- SKIP: TestAccAWSPinpointADMChannel_basic (0.00s)
    resource_aws_pinpoint_adm_channel_test.go:30: ADM_CLIENT_ID ENV is missing

--- SKIP: TestAccAWSPinpointAPNSChannel_basicCertificate (0.00s)
    resource_aws_pinpoint_apns_channel_test.go:58: Pinpoint certificate credentials envs are missing, skipping test
--- SKIP: TestAccAWSPinpointAPNSChannel_basicToken (0.00s)
    resource_aws_pinpoint_apns_channel_test.go:66: APNS_BUNDLE_ID env is missing, skipping test

--- SKIP: TestAccAWSPinpointAPNSSandboxChannel_basicCertificate (0.00s)
    resource_aws_pinpoint_apns_sandbox_channel_test.go:58: Pinpoint certificate credentials envs are missing, skipping test
--- SKIP: TestAccAWSPinpointAPNSSandboxChannel_basicToken (0.00s)
    resource_aws_pinpoint_apns_sandbox_channel_test.go:66: APNS_SANDBOX_BUNDLE_ID env is missing, skipping test

--- SKIP: TestAccAWSPinpointAPNSVoipChannel_basicCertificate (0.00s)
    resource_aws_pinpoint_apns_voip_channel_test.go:58: Pinpoint certificate credentials envs are missing, skipping test
--- SKIP: TestAccAWSPinpointAPNSVoipChannel_basicToken (0.00s)
    resource_aws_pinpoint_apns_voip_channel_test.go:66: APNS_VOIP_BUNDLE_ID env is missing, skipping test

--- SKIP: TestAccAWSPinpointAPNSVoipSandboxChannel_basicCertificate (0.00s)
    resource_aws_pinpoint_apns_voip_sandbox_channel_test.go:58: Pinpoint certificate credentials envs are missing, skipping test
--- SKIP: TestAccAWSPinpointAPNSVoipSandboxChannel_basicToken (0.00s)
    resource_aws_pinpoint_apns_voip_sandbox_channel_test.go:66: APNS_VOIP_BUNDLE_ID env is missing, skipping test

--- SKIP: TestAccAWSPinpointGCMChannel_basic (0.00s)
    resource_aws_pinpoint_gcm_channel_test.go:26: GCM_API_KEY env missing, skip test

--- PASS: TestAccAWSPinpointApp_basic (18.30s)
--- PASS: TestAccAWSPinpointApp_CampaignHookLambda (51.62s)
--- PASS: TestAccAWSPinpointApp_Limits (16.81s)
--- PASS: TestAccAWSPinpointApp_QuietTime (16.39s)
--- PASS: TestAccAWSPinpointApp_Tags (39.82s)

--- PASS: TestAccAWSPinpointBaiduChannel_basic (27.75s)

--- PASS: TestAccAWSPinpointEmailChannel_basic (31.77s)

--- PASS: TestAccAWSPinpointEventStream_basic (192.97s)

--- PASS: TestAccAWSPinpointSMSChannel_basic (27.71s)
--- PASS: TestAccAWSPinpointSMSChannel_full (29.13s)

--- PASS: TestAccAWSRDSCluster_EncryptedCrossRegionReplication (1593.29s)

--- PASS: TestAccAWSRoute53ZoneAssociation_region (161.54s)

--- PASS: TestAccAWSS3Bucket_region (38.19s)
--- PASS: TestAccAWSS3Bucket_ReplicationExpectVersioningValidationError (28.48s)
--- PASS: TestAccAWSS3Bucket_ReplicationWithoutPrefix (55.65s)
--- PASS: TestAccAWSS3Bucket_ReplicationWithoutStorageClass (56.38s)
--- PASS: TestAccAWSS3Bucket_ReplicationConfiguration_Rule_Destination_AccessControlTranslation (108.97s)
--- PASS: TestAccAWSS3Bucket_ReplicationSchemaV2 (161.13s)
--- PASS: TestAccAWSS3Bucket_Replication (172.15s)
```

In a region where Pinpoint is not supported, all tests continue to skip:

```
--- SKIP: TestAccAWSPinpointApp_basic (27.81s)
    resource_aws_pinpoint_app_test.go:228: skipping acceptance testing: RequestError: send request failed
        caused by: Get https://pinpoint.us-east-2.amazonaws.com/v1/apps: dial tcp: lookup pinpoint.us-east-2.amazonaws.com: no such host
```
  • Loading branch information
bflad committed Mar 10, 2020
1 parent 65d96db commit 4c275f4
Show file tree
Hide file tree
Showing 20 changed files with 425 additions and 825 deletions.
39 changes: 31 additions & 8 deletions aws/data_source_aws_elastic_beanstalk_hosted_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,43 @@ import (
"testing"

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

func TestAccAWSDataSourceElasticBeanstalkHostedZone(t *testing.T) {
func TestAccAWSDataSourceElasticBeanstalkHostedZone_basic(t *testing.T) {
dataSourceName := "data.aws_elastic_beanstalk_hosted_zone.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsElasticBeanstalkHostedZoneDataSource_currentRegion,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_elastic_beanstalk_hosted_zone.current", "id", "Z2PCDNR3VC2G1N"),
testAccCheckAwsElasticBeanstalkHostedZone(dataSourceName, testAccGetRegion()),
),
},
},
})
}

func TestAccAWSDataSourceElasticBeanstalkHostedZone_Region(t *testing.T) {
dataSourceName := "data.aws_elastic_beanstalk_hosted_zone.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsElasticBeanstalkHostedZoneDataSource_byRegion("ap-southeast-2"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_elastic_beanstalk_hosted_zone.test", "id", "Z2PCDNR3VC2G1N"),
testAccCheckAwsElasticBeanstalkHostedZone(dataSourceName, "ap-southeast-2"),
),
},
{
Config: testAccCheckAwsElasticBeanstalkHostedZoneDataSource_byRegion("eu-west-1"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_elastic_beanstalk_hosted_zone.test", "id", "Z2NYPWQ7DFZAZH"),
testAccCheckAwsElasticBeanstalkHostedZone(dataSourceName, "eu-west-1"),
),
},
{
Expand All @@ -39,11 +53,20 @@ func TestAccAWSDataSourceElasticBeanstalkHostedZone(t *testing.T) {
})
}

const testAccCheckAwsElasticBeanstalkHostedZoneDataSource_currentRegion = `
provider "aws" {
region = "ap-southeast-2"
func testAccCheckAwsElasticBeanstalkHostedZone(resourceName string, region string) resource.TestCheckFunc {
return func(s *terraform.State) error {
expectedValue, ok := elasticBeanstalkHostedZoneIds[region]

if !ok {
return fmt.Errorf("Unsupported region: %s", region)
}

return resource.TestCheckResourceAttr(resourceName, "id", expectedValue)(s)
}
}
data "aws_elastic_beanstalk_hosted_zone" "current" {}

const testAccCheckAwsElasticBeanstalkHostedZoneDataSource_currentRegion = `
data "aws_elastic_beanstalk_hosted_zone" "test" {}
`

func testAccCheckAwsElasticBeanstalkHostedZoneDataSource_byRegion(r string) string {
Expand Down
34 changes: 15 additions & 19 deletions aws/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,25 +381,6 @@ func testAccPartitionHasServicePreCheck(serviceId string, t *testing.T) {
}
}

// testAccRegionHasServicePreCheck skips a test if the AWS Go SDK endpoint value in a region is missing
// NOTE: Most acceptance testing should prefer behavioral checks against an API (e.g. making an API call and
// using response errors) to determine if a test should be skipped since AWS Go SDK endpoint information
// can be incorrect, especially for newer endpoints or for private feature testing. This functionality
// is provided for cases where the API behavior may be completely unacceptable, such as permanent
// retries by the AWS Go SDK.
func testAccRegionHasServicePreCheck(serviceId string, t *testing.T) {
regionId := testAccGetRegion()
if partition, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), regionId); ok {
service, ok := partition.Services()[serviceId]
if !ok {
t.Skip(fmt.Sprintf("skipping tests; partition %s does not support %s service", partition.ID(), serviceId))
}
if _, ok := service.Regions()[regionId]; !ok {
t.Skip(fmt.Sprintf("skipping tests; region %s does not support %s service", regionId, serviceId))
}
}
}

func testAccMultipleRegionsPreCheck(t *testing.T) {
if partition, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), testAccGetRegion()); ok {
if len(partition.Regions()) < 2 {
Expand All @@ -422,6 +403,7 @@ func testAccOrganizationsAccountPreCheck(t *testing.T) {
}

func testAccAlternateAccountProviderConfig() string {
//lintignore:AT004
return fmt.Sprintf(`
provider "aws" {
access_key = %[1]q
Expand All @@ -433,6 +415,7 @@ provider "aws" {
}

func testAccAlternateAccountAlternateRegionProviderConfig() string {
//lintignore:AT004
return fmt.Sprintf(`
provider "aws" {
access_key = %[1]q
Expand All @@ -445,6 +428,7 @@ provider "aws" {
}

func testAccAlternateRegionProviderConfig() string {
//lintignore:AT004
return fmt.Sprintf(`
provider "aws" {
alias = "alternate"
Expand All @@ -454,6 +438,7 @@ provider "aws" {
}

func testAccProviderConfigIgnoreTagPrefixes1(keyPrefix1 string) string {
//lintignore:AT004
return fmt.Sprintf(`
provider "aws" {
ignore_tag_prefixes = [%[1]q]
Expand All @@ -462,6 +447,7 @@ provider "aws" {
}

func testAccProviderConfigIgnoreTags1(key1 string) string {
//lintignore:AT004
return fmt.Sprintf(`
provider "aws" {
ignore_tags = [%[1]q]
Expand All @@ -476,6 +462,7 @@ provider "aws" {
// Other valid usage is for services only available in us-east-1 such as the
// Cost and Usage Reporting and Pricing services.
func testAccUsEast1RegionProviderConfig() string {
//lintignore:AT004
return fmt.Sprintf(`
provider "aws" {
alias = "us-east-1"
Expand Down Expand Up @@ -1112,6 +1099,7 @@ func testAccCheckAWSProviderPartition(providers *[]*schema.Provider, expectedPar
}

func testAccAWSProviderConfigEndpoints(endpoints string) string {
//lintignore:AT004
return fmt.Sprintf(`
provider "aws" {
skip_credentials_validation = true
Expand All @@ -1132,6 +1120,7 @@ data "aws_arn" "test" {
}

func testAccAWSProviderConfigIgnoreTagPrefixes0() string {
//lintignore:AT004
return fmt.Sprintf(`
provider "aws" {
skip_credentials_validation = true
Expand All @@ -1148,6 +1137,7 @@ data "aws_arn" "test" {
}

func testAccAWSProviderConfigIgnoreTagPrefixes1(tagPrefix1 string) string {
//lintignore:AT004
return fmt.Sprintf(`
provider "aws" {
ignore_tag_prefixes = [%[1]q]
Expand All @@ -1165,6 +1155,7 @@ data "aws_arn" "test" {
}

func testAccAWSProviderConfigIgnoreTagPrefixes2(tagPrefix1, tagPrefix2 string) string {
//lintignore:AT004
return fmt.Sprintf(`
provider "aws" {
ignore_tag_prefixes = [%[1]q, %[2]q]
Expand All @@ -1182,6 +1173,7 @@ data "aws_arn" "test" {
}

func testAccAWSProviderConfigIgnoreTags0() string {
//lintignore:AT004
return fmt.Sprintf(`
provider "aws" {
skip_credentials_validation = true
Expand All @@ -1198,6 +1190,7 @@ data "aws_arn" "test" {
}

func testAccAWSProviderConfigIgnoreTags1(tag1 string) string {
//lintignore:AT004
return fmt.Sprintf(`
provider "aws" {
ignore_tags = [%[1]q]
Expand All @@ -1215,6 +1208,7 @@ data "aws_arn" "test" {
}

func testAccAWSProviderConfigIgnoreTags2(tag1, tag2 string) string {
//lintignore:AT004
return fmt.Sprintf(`
provider "aws" {
ignore_tags = [%[1]q, %[2]q]
Expand All @@ -1232,6 +1226,7 @@ data "aws_arn" "test" {
}

func testAccAWSProviderConfigRegion(region string) string {
//lintignore:AT004
return fmt.Sprintf(`
provider "aws" {
region = %[1]q
Expand All @@ -1256,6 +1251,7 @@ func testAccAssumeRoleARNPreCheck(t *testing.T) {
}

func testAccProviderConfigAssumeRolePolicy(policy string) string {
//lintignore:AT004
return fmt.Sprintf(`
provider "aws" {
assume_role {
Expand Down
31 changes: 24 additions & 7 deletions aws/resource_aws_ami_from_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,33 @@ func testAccCheckAWSAMIFromInstanceDestroy(s *terraform.State) error {

func testAccAWSAMIFromInstanceConfigBase() string {
return fmt.Sprintf(`
provider "aws" {
region = "us-east-1"
data "aws_ec2_instance_type_offering" "available" {
filter {
name = "instance-type"
values = ["t3.micro", "t2.micro"]
}
preferred_instance_types = ["t3.micro", "t2.micro"]
}
resource "aws_instance" "test" {
// This AMI has one block device mapping, so we expect to have
// one snapshot in our created AMI.
ami = "ami-408c7f28"
data "aws_ami" "amzn-ami-minimal-hvm-ebs" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn-ami-minimal-hvm-*"]
}
instance_type = "t1.micro"
filter {
name = "root-device-type"
values = ["ebs"]
}
}
resource "aws_instance" "test" {
ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
instance_type = data.aws_ec2_instance_type_offering.available.instance_type
tags = {
Name = "testAccAWSAMIFromInstanceConfig_TestAMI"
Expand Down
39 changes: 21 additions & 18 deletions aws/resource_aws_codecommit_trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/codecommit"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccAWSCodeCommitTrigger_basic(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_codecommit_trigger.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCodeCommitTriggerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCodeCommitTrigger_basic,
Config: testAccCodeCommitTrigger_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCodeCommitTriggerExists("aws_codecommit_trigger.test"),
resource.TestCheckResourceAttr(
"aws_codecommit_trigger.test", "trigger.#", "1"),
testAccCheckCodeCommitTriggerExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "trigger.#", "1"),
),
},
},
Expand Down Expand Up @@ -81,24 +84,24 @@ func testAccCheckCodeCommitTriggerExists(name string) resource.TestCheckFunc {
}
}

const testAccCodeCommitTrigger_basic = `
provider "aws" {
region = "us-east-1"
}
func testAccCodeCommitTrigger_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_sns_topic" "test" {
name = "tf-test-topic"
name = %[1]q
}
resource "aws_codecommit_repository" "test" {
repository_name = "tf_test_repository"
description = "This is a test description"
repository_name = %[1]q
}
resource "aws_codecommit_trigger" "test" {
depends_on = ["aws_codecommit_repository.test"]
repository_name = "tf_test_repository"
trigger {
name = "tf-test-trigger"
events = ["all"]
destination_arn = "${aws_sns_topic.test.arn}"
repository_name = aws_codecommit_repository.test.id
trigger {
name = %[1]q
events = ["all"]
destination_arn = aws_sns_topic.test.arn
}
}
`
`, rName)
}
Loading

0 comments on commit 4c275f4

Please sign in to comment.