Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests/provider: Migrate additional tests to newer ProviderFactories setup and PreCheck functions #15877

Merged
merged 1 commit into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions aws/cloudfront_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
27 changes: 12 additions & 15 deletions aws/data_source_aws_ec2_transit_gateway_peering_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand All @@ -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{
{
Expand All @@ -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{
{
Expand All @@ -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{
{
Expand All @@ -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{
{
Expand Down
96 changes: 62 additions & 34 deletions aws/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]))$`
Expand Down Expand Up @@ -99,11 +94,6 @@ func init() {
return testAccProviderFactoriesInit(providers, []string{
ProviderNameAws,
ProviderNameAwsAlternate,
ProviderNameAwsAlternateAccountAlternateRegion,
ProviderNameAwsAlternateAccountSameRegion,
ProviderNameAwsSameAccountAlternateRegion,
ProviderNameAwsThird,
ProviderNameAwsUsEast1,
})
}
testAccProviderFunc = func() *schema.Provider { return testAccProvider }
Expand All @@ -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)
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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{
{
Expand All @@ -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{
{
Expand All @@ -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{
{
Expand All @@ -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{
{
Expand All @@ -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{
{
Expand All @@ -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{
{
Expand All @@ -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{
{
Expand All @@ -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{
{
Expand All @@ -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{
{
Expand All @@ -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{
{
Expand All @@ -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{
{
Expand All @@ -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{
{
Expand Down
5 changes: 2 additions & 3 deletions aws/resource_aws_backup_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down
4 changes: 1 addition & 3 deletions aws/resource_aws_cloudfront_distribution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
19 changes: 8 additions & 11 deletions aws/resource_aws_db_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down Expand Up @@ -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{
{
Expand All @@ -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{
{
Expand All @@ -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{
{
Expand Down Expand Up @@ -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{
{
Expand Down
4 changes: 2 additions & 2 deletions aws/resource_aws_dynamodb_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down Expand Up @@ -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{
{
Expand Down
Loading