From bfc7d1eb0cb2e9420827c2892aaae9eb3764db1c Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Tue, 27 Oct 2020 23:52:20 -0400 Subject: [PATCH] tests/provider: Migrate additional tests to newer ProviderFactories setup and PreCheck functions Reference: https://github.com/terraform-providers/terraform-provider-aws/issues/8316 Reference: https://github.com/terraform-providers/terraform-provider-aws/issues/15737 Reference: https://github.com/terraform-providers/terraform-provider-aws/issues/15791 This creates the remaining special `ProviderFactories` functions (as they need to exist currently), updates the Contributing Guide documentation, and migrates a portion of the tests over to them. Further changesets will continue these efforts by: * Migrating the rest of the `awsalternate` provider test configurations to `testAccProviderFactoriesAlternate()` * Migrating the rest of the `testAccMultipleRegionsPreCheck()` and `testAccAlternateRegionPreCheck()` usage to `testAccMultipleRegionPreCheck()` (bundling with the above as its fairly common together) * Flipping `testAccProviderFactories` to a variable with only the `aws` provider and replacing `testAccProviderFactoriesEc2Classic` and `testAccProviderFactoriesCur()` * Continuing efforts to remove environment variable handling in test functions Output from acceptance testing in AWS Commercial: ``` --- PASS: TestAccAwsBackupPlan_Rule_CopyAction_CrossRegion (20.85s) --- PASS: TestAccAWSCloudFrontDistribution_ViewerCertificate_AcmCertificateArn (157.90s) --- PASS: TestAccAWSCloudFrontDistribution_ViewerCertificate_AcmCertificateArn_ConflictsWithCloudFrontDefaultCertificate (157.61s) --- FAIL: TestAccAWSDBInstance_DbSubnetGroupName_RamShared (18.95s) resource_aws_db_instance_test.go:352: Step 1/1 error: Error running apply: 2020/10/27 23:13:26 [DEBUG] Using modified User-Agent: Terraform/0.12.29 HashiCorp-terraform-exec/0.10.0 Error: Error associating principal with RAM resource share: UnknownResourceException: Organization o-upyv668dz5 could not be found. Error: error associating RAM Resource Share: InvalidParameterException: The resource you are attempting to share can only be shared within your AWS Organization. This error may also occur if you have not enabled sharing with your AWS organization, or that onboarding process is still in progress. Error: error associating RAM Resource Share: InvalidParameterException: The resource you are attempting to share can only be shared within your AWS Organization. This error may also occur if you have not enabled sharing with your AWS organization, or that onboarding process is still in progress. --- FAIL: TestAccAWSDBInstance_ReplicateSourceDb_DbSubnetGroupName_RamShared (604.80s) resource_aws_db_instance_test.go:808: Step 1/1 error: Error running apply: 2020/10/27 21:59:55 [DEBUG] Using modified User-Agent: Terraform/0.12.29 HashiCorp-terraform-exec/0.10.0 Error: Error associating principal with RAM resource share: UnknownResourceException: Organization o-upyv668dz5 could not be found. Error: error associating RAM Resource Share: InvalidParameterException: The resource you are attempting to share can only be shared within your AWS Organization. This error may also occur if you have not enabled sharing with your AWS organization, or that onboarding process is still in progress. Error: error associating RAM Resource Share: InvalidParameterException: The resource you are attempting to share can only be shared within your AWS Organization. This error may also occur if you have not enabled sharing with your AWS organization, or that onboarding process is still in progress. --- FAIL: TestAccAWSDBInstance_SnapshotIdentifier_DbSubnetGroupName_RamShared (615.89s) resource_aws_db_instance_test.go:1437: Step 1/1 error: Error running apply: 2020/10/27 23:13:26 [DEBUG] Using modified User-Agent: Terraform/0.12.29 HashiCorp-terraform-exec/0.10.0 Error: Error associating principal with RAM resource share: UnknownResourceException: Organization o-upyv668dz5 could not be found. Error: error associating RAM Resource Share: InvalidParameterException: The resource you are attempting to share can only be shared within your AWS Organization. This error may also occur if you have not enabled sharing with your AWS organization, or that onboarding process is still in progress. Error: error associating RAM Resource Share: InvalidParameterException: The resource you are attempting to share can only be shared within your AWS Organization. This error may also occur if you have not enabled sharing with your AWS organization, or that onboarding process is still in progress. --- PASS: TestAccAWSDBInstance_ReplicateSourceDb_DbSubnetGroupName (2252.27s) --- PASS: TestAccAWSDBInstance_ReplicateSourceDb_DbSubnetGroupName_VpcSecurityGroupIds (1908.33s) --- PASS: TestAccAWSDynamoDbTable_Replica_Multiple (766.22s) --- PASS: TestAccAWSDynamoDbTable_Replica_Single (412.01s) --- PASS: TestAccAWSEc2TransitGatewayPeeringAttachment_basic (376.21s) --- PASS: TestAccAWSEc2TransitGatewayPeeringAttachment_differentAccount (360.55s) --- PASS: TestAccAWSEc2TransitGatewayPeeringAttachment_disappears (363.16s) --- PASS: TestAccAWSEc2TransitGatewayPeeringAttachment_Tags_sameAccount (365.29s) --- PASS: TestAccAWSEc2TransitGatewayPeeringAttachmentAccepter_basic_differentAccount (780.52s) --- PASS: TestAccAWSEc2TransitGatewayPeeringAttachmentAccepter_basic_sameAccount (773.52s) --- PASS: TestAccAWSEc2TransitGatewayPeeringAttachmentAccepter_Tags_sameAccount (815.32s) --- PASS: TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_Filter_differentAccount (389.30s) --- PASS: TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_Filter_sameAccount (397.06s) --- PASS: TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_ID_differentAccount (333.33s) --- PASS: TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_ID_sameAccount (333.84s) --- PASS: TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_Tags (332.99s) --- PASS: TestAccAWSProvider_AssumeRole_Empty (16.67s) --- PASS: TestAccAWSProvider_Endpoints (14.28s) --- PASS: TestAccAWSProvider_IgnoreTags_EmptyConfigurationBlock (14.21s) --- PASS: TestAccAWSProvider_IgnoreTags_KeyPrefixes_Multiple (14.15s) --- PASS: TestAccAWSProvider_IgnoreTags_KeyPrefixes_None (14.28s) --- PASS: TestAccAWSProvider_IgnoreTags_KeyPrefixes_One (14.05s) --- PASS: TestAccAWSProvider_IgnoreTags_Keys_Multiple (14.11s) --- PASS: TestAccAWSProvider_IgnoreTags_Keys_None (14.19s) --- PASS: TestAccAWSProvider_IgnoreTags_Keys_One (14.31s) --- PASS: TestAccAWSProvider_Region_AwsChina (11.20s) --- PASS: TestAccAWSProvider_Region_AwsCommercial (11.54s) --- PASS: TestAccAWSProvider_Region_AwsGovCloudUs (11.22s) ``` Output from acceptance testing in AWS GovCloud (US): ``` --- PASS: TestAccAwsBackupPlan_Rule_CopyAction_CrossRegion (24.77s) --- SKIP: TestAccAWSCloudFrontDistribution_ViewerCertificate_AcmCertificateArn (1.52s) --- SKIP: TestAccAWSCloudFrontDistribution_ViewerCertificate_AcmCertificateArn_ConflictsWithCloudFrontDefaultCertificate (1.55s) ``` --- aws/cloudfront_test.go | 18 ++++ ...transit_gateway_peering_attachment_test.go | 27 +++--- aws/provider_test.go | 96 ++++++++++++------- aws/resource_aws_backup_plan_test.go | 5 +- ...source_aws_cloudfront_distribution_test.go | 4 +- aws/resource_aws_db_instance_test.go | 19 ++-- aws/resource_aws_dynamodb_table_test.go | 4 +- ...ateway_peering_attachment_accepter_test.go | 12 +-- ...transit_gateway_peering_attachment_test.go | 21 ++-- .../running-and-writing-acceptance-tests.md | 16 ++-- 10 files changed, 127 insertions(+), 95 deletions(-) create mode 100644 aws/cloudfront_test.go diff --git a/aws/cloudfront_test.go b/aws/cloudfront_test.go new file mode 100644 index 00000000000..a98e83e3fe3 --- /dev/null +++ b/aws/cloudfront_test.go @@ -0,0 +1,18 @@ +package aws + +import "github.com/aws/aws-sdk-go/aws/endpoints" + +// testAccCloudfrontRegionProviderConfig is the Terraform provider configuration for CloudFront region testing +// +// Testing CloudFront assumes no other provider configurations +// are necessary and overwrites the "aws" provider configuration. +func testAccCloudfrontRegionProviderConfig() string { + switch testAccGetPartition() { + case endpoints.AwsPartitionID: + return testAccRegionalProviderConfig(endpoints.UsEast1RegionID) + case endpoints.AwsCnPartitionID: + return testAccRegionalProviderConfig(endpoints.CnNorthwest1RegionID) + default: + return testAccRegionalProviderConfig(testAccGetRegion()) + } +} diff --git a/aws/data_source_aws_ec2_transit_gateway_peering_attachment_test.go b/aws/data_source_aws_ec2_transit_gateway_peering_attachment_test.go index 4f3cb0e1dda..ab6a6f52a4a 100644 --- a/aws/data_source_aws_ec2_transit_gateway_peering_attachment_test.go +++ b/aws/data_source_aws_ec2_transit_gateway_peering_attachment_test.go @@ -17,10 +17,9 @@ func TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_Filter_sameAccount(t PreCheck: func() { testAccPreCheck(t) testAccPreCheckAWSEc2TransitGateway(t) - testAccMultipleRegionsPreCheck(t) - testAccAlternateRegionPreCheck(t) + testAccMultipleRegionPreCheck(t, 2) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy, Steps: []resource.TestStep{ { @@ -46,10 +45,10 @@ func TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_Filter_differentAcco PreCheck: func() { testAccPreCheck(t) testAccPreCheckAWSEc2TransitGateway(t) - testAccMultipleRegionsPreCheck(t) - testAccAlternateRegionPreCheck(t) + testAccMultipleRegionPreCheck(t, 2) + testAccAlternateAccountPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy, Steps: []resource.TestStep{ { @@ -73,10 +72,9 @@ func TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_ID_sameAccount(t *te PreCheck: func() { testAccPreCheck(t) testAccPreCheckAWSEc2TransitGateway(t) - testAccMultipleRegionsPreCheck(t) - testAccAlternateRegionPreCheck(t) + testAccMultipleRegionPreCheck(t, 2) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy, Steps: []resource.TestStep{ { @@ -102,10 +100,10 @@ func TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_ID_differentAccount( PreCheck: func() { testAccPreCheck(t) testAccPreCheckAWSEc2TransitGateway(t) - testAccMultipleRegionsPreCheck(t) - testAccAlternateRegionPreCheck(t) + testAccMultipleRegionPreCheck(t, 2) + testAccAlternateAccountPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy, Steps: []resource.TestStep{ { @@ -129,10 +127,9 @@ func TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_Tags(t *testing.T) { PreCheck: func() { testAccPreCheck(t) testAccPreCheckAWSEc2TransitGateway(t) - testAccMultipleRegionsPreCheck(t) - testAccAlternateRegionPreCheck(t) + testAccMultipleRegionPreCheck(t, 2) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy, Steps: []resource.TestStep{ { diff --git a/aws/provider_test.go b/aws/provider_test.go index 07d8acef670..26675729095 100644 --- a/aws/provider_test.go +++ b/aws/provider_test.go @@ -42,11 +42,6 @@ const ( // Provider name for third configuration testing ProviderNameAwsThird = "awsthird" - - // Provider name for hardcoded us-east-1 configuration testing - // - // Deprecated: This will be replaced with service specific providers - ProviderNameAwsUsEast1 = "awsus-east-1" ) const rfc3339RegexPattern = `^[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?([Zz]|([+-]([01][0-9]|2[0-3]):[0-5][0-9]))$` @@ -99,11 +94,6 @@ func init() { return testAccProviderFactoriesInit(providers, []string{ ProviderNameAws, ProviderNameAwsAlternate, - ProviderNameAwsAlternateAccountAlternateRegion, - ProviderNameAwsAlternateAccountSameRegion, - ProviderNameAwsSameAccountAlternateRegion, - ProviderNameAwsThird, - ProviderNameAwsUsEast1, }) } testAccProviderFunc = func() *schema.Provider { return testAccProvider } @@ -128,6 +118,56 @@ func testAccProviderFactoriesInit(providers *[]*schema.Provider, providerNames [ return factories } +// testAccProviderFactoriesInternal creates ProviderFactories for provider configuration testing +// +// This should only be used for TestAccAWSProvider_ tests which need to +// reference the provider instance itself. Other testing should use +// testAccProviderFactories or other related functions. +func testAccProviderFactoriesInternal(providers *[]*schema.Provider) map[string]func() (*schema.Provider, error) { + return testAccProviderFactoriesInit(providers, []string{ProviderNameAws}) +} + +// testAccProviderFactoriesAlternate creates ProviderFactories for cross-account and cross-region configurations +// +// For cross-region testing: Typically paired with testAccMultipleRegionPreCheck and testAccAlternateRegionProviderConfig. +// +// For cross-account testing: Typically paired with testAccAlternateAccountPreCheck and testAccAlternateAccountProviderConfig. +func testAccProviderFactoriesAlternate(providers *[]*schema.Provider) map[string]func() (*schema.Provider, error) { + return testAccProviderFactoriesInit(providers, []string{ + ProviderNameAws, + ProviderNameAwsAlternate, + }) +} + +// testAccProviderFactoriesAlternateAccountAndAlternateRegion creates ProviderFactories for cross-account and cross-region configurations +// +// Usage typically paired with testAccMultipleRegionPreCheck, testAccAlternateAccountPreCheck, +// and testAccAlternateAccountAndAlternateRegionProviderConfig. +func testAccProviderFactoriesAlternateAccountAndAlternateRegion(providers *[]*schema.Provider) map[string]func() (*schema.Provider, error) { + return testAccProviderFactoriesInit(providers, []string{ + ProviderNameAws, + ProviderNameAwsAlternateAccountAlternateRegion, + ProviderNameAwsAlternateAccountSameRegion, + ProviderNameAwsSameAccountAlternateRegion, + }) +} + +// testAccProviderFactoriesMultipleRegion creates ProviderFactories for the number of region configurations +// +// Usage typically paired with testAccMultipleRegionPreCheck and testAccMultipleRegionProviderConfig. +func testAccProviderFactoriesMultipleRegion(providers *[]*schema.Provider, regions int) map[string]func() (*schema.Provider, error) { + providerNames := []string{ + ProviderNameAws, + ProviderNameAwsAlternate, + } + + if regions >= 3 { + providerNames = append(providerNames, ProviderNameAwsThird) + } + + return testAccProviderFactoriesInit(providers, providerNames) +} + func TestProvider(t *testing.T) { if err := Provider().InternalValidate(); err != nil { t.Fatalf("err: %s", err) @@ -816,18 +856,6 @@ provider "aws" { `, region) } -// Provider configuration hardcoded for us-east-1. -// This should only be necessary for testing ACM Certificates with CloudFront -// related infrastucture such as API Gateway Domain Names for EDGE endpoints, -// CloudFront Distribution Viewer Certificates, and Cognito User Pool Domains. -// Other valid usage is for services only available in us-east-1 such as the -// Cost and Usage Reporting and Pricing services. -// -// Deprecated: This will be replaced with service specific provider configurations. -func testAccUsEast1RegionProviderConfig() string { - return testAccNamedRegionalProviderConfig(ProviderNameAwsUsEast1, endpoints.UsEast1RegionID) -} - func testAccAwsRegionProviderFunc(region string, providers *[]*schema.Provider) func() *schema.Provider { return func() *schema.Provider { if region == "" { @@ -991,7 +1019,7 @@ func TestAccAWSProvider_Endpoints(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesInternal(&providers), CheckDestroy: nil, Steps: []resource.TestStep{ { @@ -1009,7 +1037,7 @@ func TestAccAWSProvider_IgnoreTags_EmptyConfigurationBlock(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesInternal(&providers), CheckDestroy: nil, Steps: []resource.TestStep{ { @@ -1028,7 +1056,7 @@ func TestAccAWSProvider_IgnoreTags_KeyPrefixes_None(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesInternal(&providers), CheckDestroy: nil, Steps: []resource.TestStep{ { @@ -1046,7 +1074,7 @@ func TestAccAWSProvider_IgnoreTags_KeyPrefixes_One(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesInternal(&providers), CheckDestroy: nil, Steps: []resource.TestStep{ { @@ -1064,7 +1092,7 @@ func TestAccAWSProvider_IgnoreTags_KeyPrefixes_Multiple(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesInternal(&providers), CheckDestroy: nil, Steps: []resource.TestStep{ { @@ -1082,7 +1110,7 @@ func TestAccAWSProvider_IgnoreTags_Keys_None(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesInternal(&providers), CheckDestroy: nil, Steps: []resource.TestStep{ { @@ -1100,7 +1128,7 @@ func TestAccAWSProvider_IgnoreTags_Keys_One(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesInternal(&providers), CheckDestroy: nil, Steps: []resource.TestStep{ { @@ -1118,7 +1146,7 @@ func TestAccAWSProvider_IgnoreTags_Keys_Multiple(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesInternal(&providers), CheckDestroy: nil, Steps: []resource.TestStep{ { @@ -1136,7 +1164,7 @@ func TestAccAWSProvider_Region_AwsChina(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesInternal(&providers), CheckDestroy: nil, Steps: []resource.TestStep{ { @@ -1156,7 +1184,7 @@ func TestAccAWSProvider_Region_AwsCommercial(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesInternal(&providers), CheckDestroy: nil, Steps: []resource.TestStep{ { @@ -1176,7 +1204,7 @@ func TestAccAWSProvider_Region_AwsGovCloudUs(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesInternal(&providers), CheckDestroy: nil, Steps: []resource.TestStep{ { @@ -1196,7 +1224,7 @@ func TestAccAWSProvider_AssumeRole_Empty(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesInternal(&providers), CheckDestroy: nil, Steps: []resource.TestStep{ { diff --git a/aws/resource_aws_backup_plan_test.go b/aws/resource_aws_backup_plan_test.go index d647508ec46..0f44e0d6456 100644 --- a/aws/resource_aws_backup_plan_test.go +++ b/aws/resource_aws_backup_plan_test.go @@ -436,10 +436,9 @@ func TestAccAwsBackupPlan_Rule_CopyAction_CrossRegion(t *testing.T) { PreCheck: func() { testAccPreCheck(t) testAccPreCheckAWSBackup(t) - testAccMultipleRegionsPreCheck(t) - testAccAlternateRegionPreCheck(t) + testAccMultipleRegionPreCheck(t, 2) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAwsBackupPlanDestroy, Steps: []resource.TestStep{ { diff --git a/aws/resource_aws_cloudfront_distribution_test.go b/aws/resource_aws_cloudfront_distribution_test.go index 5c0ba1139cd..ec5e35393a7 100644 --- a/aws/resource_aws_cloudfront_distribution_test.go +++ b/aws/resource_aws_cloudfront_distribution_test.go @@ -2538,10 +2538,8 @@ func testAccAWSCloudFrontDistributionConfigViewerCertificateAcmCertificateArnBas key := tlsRsaPrivateKeyPem(2048) certificate := tlsRsaX509SelfSignedCertificatePem(key, commonName) - return testAccUsEast1RegionProviderConfig() + fmt.Sprintf(` + return testAccCloudfrontRegionProviderConfig() + fmt.Sprintf(` resource "aws_acm_certificate" "test" { - provider = "awsus-east-1" - certificate_body = "%[1]s" private_key = "%[2]s" } diff --git a/aws/resource_aws_db_instance_test.go b/aws/resource_aws_db_instance_test.go index 8e1e0fb4730..4b671ef39f0 100644 --- a/aws/resource_aws_db_instance_test.go +++ b/aws/resource_aws_db_instance_test.go @@ -355,7 +355,7 @@ func TestAccAWSDBInstance_DbSubnetGroupName_RamShared(t *testing.T) { testAccAlternateAccountPreCheck(t) testAccOrganizationsEnabledPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAWSDBInstanceDestroy, Steps: []resource.TestStep{ { @@ -778,10 +778,9 @@ func TestAccAWSDBInstance_ReplicateSourceDb_DbSubnetGroupName(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) - testAccMultipleRegionsPreCheck(t) - testAccAlternateRegionPreCheck(t) + testAccMultipleRegionPreCheck(t, 2) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAWSDBInstanceDestroy, Steps: []resource.TestStep{ { @@ -808,12 +807,11 @@ func TestAccAWSDBInstance_ReplicateSourceDb_DbSubnetGroupName_RamShared(t *testi resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) - testAccMultipleRegionsPreCheck(t) - testAccAlternateRegionPreCheck(t) + testAccMultipleRegionPreCheck(t, 2) testAccAlternateAccountPreCheck(t) testAccOrganizationsEnabledPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternateAccountAndAlternateRegion(&providers), CheckDestroy: testAccCheckAWSDBInstanceDestroy, Steps: []resource.TestStep{ { @@ -840,10 +838,9 @@ func TestAccAWSDBInstance_ReplicateSourceDb_DbSubnetGroupName_VpcSecurityGroupId resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) - testAccMultipleRegionsPreCheck(t) - testAccAlternateRegionPreCheck(t) + testAccMultipleRegionPreCheck(t, 2) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAWSDBInstanceDestroy, Steps: []resource.TestStep{ { @@ -1443,7 +1440,7 @@ func TestAccAWSDBInstance_SnapshotIdentifier_DbSubnetGroupName_RamShared(t *test testAccAlternateAccountPreCheck(t) testAccOrganizationsEnabledPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAWSDBInstanceDestroy, Steps: []resource.TestStep{ { diff --git a/aws/resource_aws_dynamodb_table_test.go b/aws/resource_aws_dynamodb_table_test.go index 5d62548aecb..e7b6503dd6f 100644 --- a/aws/resource_aws_dynamodb_table_test.go +++ b/aws/resource_aws_dynamodb_table_test.go @@ -1402,7 +1402,7 @@ func TestAccAWSDynamoDbTable_Replica_Multiple(t *testing.T) { testAccPreCheck(t) testAccMultipleRegionPreCheck(t, 3) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesMultipleRegion(&providers, 3), CheckDestroy: testAccCheckAWSDynamoDbTableDestroy, Steps: []resource.TestStep{ { @@ -1447,7 +1447,7 @@ func TestAccAWSDynamoDbTable_Replica_Single(t *testing.T) { testAccPreCheck(t) testAccMultipleRegionPreCheck(t, 2) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesMultipleRegion(&providers, 3), // 3 due to shared test configuration CheckDestroy: testAccCheckAWSDynamoDbTableDestroy, Steps: []resource.TestStep{ { diff --git a/aws/resource_aws_ec2_transit_gateway_peering_attachment_accepter_test.go b/aws/resource_aws_ec2_transit_gateway_peering_attachment_accepter_test.go index d75ba843f96..d1a2cee7aac 100644 --- a/aws/resource_aws_ec2_transit_gateway_peering_attachment_accepter_test.go +++ b/aws/resource_aws_ec2_transit_gateway_peering_attachment_accepter_test.go @@ -22,11 +22,10 @@ func TestAccAWSEc2TransitGatewayPeeringAttachmentAccepter_basic_sameAccount(t *t resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) - testAccMultipleRegionsPreCheck(t) - testAccAlternateRegionPreCheck(t) + testAccMultipleRegionPreCheck(t, 2) testAccPreCheckAWSEc2TransitGateway(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAWSEc2TransitGatewayPeeringAttachmentDestroy, Steps: []resource.TestStep{ { @@ -60,11 +59,10 @@ func TestAccAWSEc2TransitGatewayPeeringAttachmentAccepter_Tags_sameAccount(t *te resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) - testAccMultipleRegionsPreCheck(t) - testAccAlternateRegionPreCheck(t) + testAccMultipleRegionPreCheck(t, 2) testAccPreCheckAWSEc2TransitGateway(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAWSEc2TransitGatewayPeeringAttachmentDestroy, Steps: []resource.TestStep{ { @@ -114,7 +112,7 @@ func TestAccAWSEc2TransitGatewayPeeringAttachmentAccepter_basic_differentAccount testAccAlternateAccountPreCheck(t) testAccPreCheckAWSEc2TransitGateway(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAWSEc2TransitGatewayPeeringAttachmentDestroy, Steps: []resource.TestStep{ { diff --git a/aws/resource_aws_ec2_transit_gateway_peering_attachment_test.go b/aws/resource_aws_ec2_transit_gateway_peering_attachment_test.go index a17692766d8..8003764130d 100644 --- a/aws/resource_aws_ec2_transit_gateway_peering_attachment_test.go +++ b/aws/resource_aws_ec2_transit_gateway_peering_attachment_test.go @@ -91,10 +91,9 @@ func TestAccAWSEc2TransitGatewayPeeringAttachment_basic(t *testing.T) { PreCheck: func() { testAccPreCheck(t) testAccPreCheckAWSEc2TransitGateway(t) - testAccMultipleRegionsPreCheck(t) - testAccAlternateRegionPreCheck(t) + testAccMultipleRegionPreCheck(t, 2) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAWSEc2TransitGatewayPeeringAttachmentDestroy, Steps: []resource.TestStep{ { @@ -128,10 +127,9 @@ func TestAccAWSEc2TransitGatewayPeeringAttachment_disappears(t *testing.T) { PreCheck: func() { testAccPreCheck(t) testAccPreCheckAWSEc2TransitGateway(t) - testAccMultipleRegionsPreCheck(t) - testAccAlternateRegionPreCheck(t) + testAccMultipleRegionPreCheck(t, 2) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAWSEc2TransitGatewayPeeringAttachmentDestroy, Steps: []resource.TestStep{ { @@ -156,10 +154,9 @@ func TestAccAWSEc2TransitGatewayPeeringAttachment_Tags_sameAccount(t *testing.T) PreCheck: func() { testAccPreCheck(t) testAccPreCheckAWSEc2TransitGateway(t) - testAccMultipleRegionsPreCheck(t) - testAccAlternateRegionPreCheck(t) + testAccMultipleRegionPreCheck(t, 2) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAWSEc2TransitGatewayPeeringAttachmentDestroy, Steps: []resource.TestStep{ { @@ -210,10 +207,10 @@ func TestAccAWSEc2TransitGatewayPeeringAttachment_differentAccount(t *testing.T) PreCheck: func() { testAccPreCheck(t) testAccPreCheckAWSEc2TransitGateway(t) - testAccMultipleRegionsPreCheck(t) - testAccAlternateRegionPreCheck(t) + testAccMultipleRegionPreCheck(t, 2) + testAccAlternateAccountPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAWSEc2TransitGatewayPeeringAttachmentDestroy, Steps: []resource.TestStep{ { diff --git a/docs/contributing/running-and-writing-acceptance-tests.md b/docs/contributing/running-and-writing-acceptance-tests.md index f6eb9f19533..ae43b8ceeac 100644 --- a/docs/contributing/running-and-writing-acceptance-tests.md +++ b/docs/contributing/running-and-writing-acceptance-tests.md @@ -659,8 +659,8 @@ When testing requires AWS infrastructure in a second AWS account, the below chan - In the `PreCheck` function, include `testAccAlternateAccountPreCheck(t)` to ensure a standardized set of information is required for cross-account testing credentials - Declare a `providers` variable at the top of the test function: `var providers []*schema.Provider` -- Switch usage of `Providers: testAccProviders` to `ProviderFactories: testAccProviderFactories(&providers)` -- Add `testAccAlternateAccountProviderConfig()` to the test configuration and use `provider = "awsalternate"` for cross-account resources. The resource that is the focus of the acceptance test should _not_ use the alternate provider identification to simplify the testing setup. +- Switch usage of `Providers: testAccProviders` to `ProviderFactories: testAccProviderFactoriesAlternate(&providers)` +- Add `testAccAlternateAccountProviderConfig()` to the test configuration and use `provider = awsalternate` for cross-account resources. The resource that is the focus of the acceptance test should _not_ use the alternate provider identification to simplify the testing setup. - For any `TestStep` that includes `ImportState: true`, add the `Config` that matches the previous `TestStep` `Config` An example acceptance test implementation can be seen below: @@ -675,7 +675,7 @@ func TestAccAwsExample_basic(t *testing.T) { testAccPreCheck(t) testAccAlternateAccountPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesAlternate(&providers), CheckDestroy: testAccCheckAwsExampleDestroy, Steps: []resource.TestStep{ { @@ -700,7 +700,7 @@ func testAccAwsExampleConfig() string { # Cross account resources should be handled by the cross account provider. # The standardized provider block to use is awsalternate as seen below. resource "aws_cross_account_example" "test" { - provider = "awsalternate" + provider = awsalternate # ... configuration ... } @@ -722,8 +722,8 @@ When testing requires AWS infrastructure in a second or third AWS region, the be - In the `PreCheck` function, include `testAccMultipleRegionPreCheck(t, ###)` to ensure a standardized set of information is required for cross-region testing configuration. If the infrastructure in the second AWS region is also in a second AWS account also include `testAccAlternateAccountPreCheck(t)` - Declare a `providers` variable at the top of the test function: `var providers []*schema.Provider` -- Switch usage of `Providers: testAccProviders` to `ProviderFactories: testAccProviderFactories(&providers)` -- Add `testAccMultipleRegionProviderConfig(###)` to the test configuration and use `provider = "awsalternate"` (and/or `provider = "awsthird"`) for cross-region resources. The resource that is the focus of the acceptance test should _not_ use the alternative providers to simplify the testing setup. If the infrastructure in the second AWS region is also in a second AWS account use `testAccAlternateAccountAlternateRegionProviderConfig()` instead +- Switch usage of `Providers: testAccProviders` to `ProviderFactories: testAccProviderFactoriesMultipleRegion(&providers, 2)` (where the last parameter is number of regions) +- Add `testAccMultipleRegionProviderConfig(###)` to the test configuration and use `provider = awsalternate` (and potentially `provider = awsthird`) for cross-region resources. The resource that is the focus of the acceptance test should _not_ use the alternative providers to simplify the testing setup. If the infrastructure in the second AWS region is also in a second AWS account use `testAccAlternateAccountAlternateRegionProviderConfig()` instead - For any `TestStep` that includes `ImportState: true`, add the `Config` that matches the previous `TestStep` `Config` An example acceptance test implementation can be seen below: @@ -738,7 +738,7 @@ func TestAccAwsExample_basic(t *testing.T) { testAccPreCheck(t) testAccMultipleRegionPreCheck(t, 2) }, - ProviderFactories: testAccProviderFactories(&providers), + ProviderFactories: testAccProviderFactoriesMultipleRegion(&providers, 2), CheckDestroy: testAccCheckAwsExampleDestroy, Steps: []resource.TestStep{ { @@ -763,7 +763,7 @@ func testAccAwsExampleConfig() string { # Cross region resources should be handled by the cross region provider. # The standardized provider is awsalternate as seen below. resource "aws_cross_region_example" "test" { - provider = "awsalternate" + provider = awsalternate # ... configuration ... }