-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests/provider: Initial migration from hardcoded regions and shared e…
…nvironment variable overwrite (CUR and EC2-Classic) (#15803) * tests/provider: Prepare EC2-Classic handling to remove reseting default region environment variable and better document existing provider initialization Reference: #8316 Reference: #15737 Reference: #15791 Output from acceptance testing in AWS Commercial (DynamoDB to verify existing testAccProviderFactories handling): ``` --- PASS: TestAccAWSDynamoDbTable_Replica_Single (274.15s) --- PASS: TestAccAWSRedshiftSecurityGroup_basic (10.87s) --- PASS: TestAccAWSRedshiftSecurityGroup_ingressCidr (10.88s) --- PASS: TestAccAWSRedshiftSecurityGroup_ingressSecurityGroup (12.45s) --- PASS: TestAccAWSRedshiftSecurityGroup_updateIngressCidr (24.07s) --- PASS: TestAccAWSRedshiftSecurityGroup_updateIngressSecurityGroup (27.14s) ``` Output from acceptance testing in AWS GovCloud (US): ``` --- SKIP: TestAccAWSRedshiftSecurityGroup_updateIngressCidr (2.50s) --- SKIP: TestAccAWSRedshiftSecurityGroup_basic (2.50s) --- SKIP: TestAccAWSRedshiftSecurityGroup_ingressCidr (2.50s) --- SKIP: TestAccAWSRedshiftSecurityGroup_ingressSecurityGroup (2.50s) --- SKIP: TestAccAWSRedshiftSecurityGroup_updateIngressSecurityGroup (2.50s) ``` * tests/service/costandusagereporting: Migrate to automatic region lookup Reference: #8316 Reference: #15737 Output from acceptance testing in AWS Commercial: ``` --- PASS: TestAccAwsCurReportDefinition_refresh (13.35s) --- PASS: TestAccAwsCurReportDefinition_overwrite (13.53s) --- PASS: TestAccAwsCurReportDefinition_athena (13.55s) --- PASS: TestAccAwsCurReportDefinition_basic (13.56s) --- PASS: TestAccAwsCurReportDefinition_textOrCsv (13.77s) --- PASS: TestAccAwsCurReportDefinition_parquet (13.83s) --- PASS: TestAccDataSourceAwsCurReportDefinition_additional (15.71s) --- PASS: TestAccDataSourceAwsCurReportDefinition_basic (16.05s) ``` Output from acceptance testing in AWS GovCloud (US): ``` --- SKIP: TestAccAwsCurReportDefinition_refresh (1.22s) --- SKIP: TestAccAwsCurReportDefinition_athena (1.22s) --- SKIP: TestAccAwsCurReportDefinition_basic (1.23s) --- SKIP: TestAccAwsCurReportDefinition_textOrCsv (1.23s) --- SKIP: TestAccAwsCurReportDefinition_overwrite (1.24s) --- SKIP: TestAccAwsCurReportDefinition_parquet (1.25s) ``` * tests/service/cur: Ensure DescribeReportDefinitions call is in PreCheck Output from acceptance testing in AWS Commercial ("main" testing account -- Organizations member account): ``` === CONT TestAccAwsCurReportDefinition_basic cur_test.go:66: skipping acceptance testing: AccessDeniedException: accountId: --OMITTED-- is not authorized to callDescribeReportDefinitions. Note: linked account is not allowed to modify report preference. status code: 400, request id: 6e513d8a-4ff8-4af2-a4d3-7523dacb2dbb --- SKIP: TestAccAwsCurReportDefinition_basic (2.42s) ``` Output from acceptance testing in AWS Commercial (standalone account): ``` --- PASS: TestAccAwsCurReportDefinition_basic (13.76s) ``` Output from acceptance testing in AWS GovCloud (US): ``` === CONT TestAccAwsCurReportDefinition_basic provider_test.go:602: skipping tests; partition aws-us-gov does not support cur service --- SKIP: TestAccAwsCurReportDefinition_basic (1.37s) ```
- Loading branch information
Showing
8 changed files
with
384 additions
and
238 deletions.
There are no files selected for viewing
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,106 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/endpoints" | ||
"github.com/aws/aws-sdk-go/service/costandusagereportservice" | ||
"github.com/hashicorp/aws-sdk-go-base/tfawserr" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
// testAccCurRegion is the chosen Cost and Usage Reporting testing region | ||
// | ||
// Cached to prevent issues should multiple regions become available. | ||
var testAccCurRegion string | ||
|
||
// testAccProviderCur is the Cost and Usage Reporting provider instance | ||
// | ||
// This Provider can be used in testing code for API calls without requiring | ||
// the use of saving and referencing specific ProviderFactories instances. | ||
// | ||
// testAccPreCheckCur(t) must be called before using this provider instance. | ||
var testAccProviderCur *schema.Provider | ||
|
||
// testAccProviderCurConfigure ensures the provider is only configured once | ||
var testAccProviderCurConfigure sync.Once | ||
|
||
// testAccPreCheckCur verifies AWS credentials and that Cost and Usage Reporting is supported | ||
func testAccPreCheckCur(t *testing.T) { | ||
testAccPartitionHasServicePreCheck(costandusagereportservice.ServiceName, t) | ||
|
||
// Since we are outside the scope of the Terraform configuration we must | ||
// call Configure() to properly initialize the provider configuration. | ||
testAccProviderCurConfigure.Do(func() { | ||
testAccProviderCur = Provider() | ||
|
||
config := map[string]interface{}{ | ||
"region": testAccGetCurRegion(), | ||
} | ||
|
||
diags := testAccProviderCur.Configure(context.Background(), terraform.NewResourceConfigRaw(config)) | ||
|
||
if diags != nil && diags.HasError() { | ||
for _, d := range diags { | ||
if d.Severity == diag.Error { | ||
t.Fatalf("error configuring CUR provider: %s", d.Summary) | ||
} | ||
} | ||
} | ||
}) | ||
|
||
conn := testAccProviderCur.Meta().(*AWSClient).costandusagereportconn | ||
|
||
input := &costandusagereportservice.DescribeReportDefinitionsInput{ | ||
MaxResults: aws.Int64(5), | ||
} | ||
|
||
_, err := conn.DescribeReportDefinitions(input) | ||
|
||
if testAccPreCheckSkipError(err) || tfawserr.ErrMessageContains(err, "AccessDeniedException", "linked account is not allowed to modify report preference") { | ||
t.Skipf("skipping acceptance testing: %s", err) | ||
} | ||
|
||
if err != nil { | ||
t.Fatalf("unexpected PreCheck error: %s", err) | ||
} | ||
} | ||
|
||
// testAccCurRegionProviderConfig is the Terraform provider configuration for Cost and Usage Reporting region testing | ||
// | ||
// Testing Cost and Usage Reporting assumes no other provider configurations | ||
// are necessary and overwrites the "aws" provider configuration. | ||
func testAccCurRegionProviderConfig() string { | ||
return testAccRegionalProviderConfig(testAccGetCurRegion()) | ||
} | ||
|
||
// testAccGetCurRegion returns the Cost and Usage Reporting region for testing | ||
func testAccGetCurRegion() string { | ||
if testAccCurRegion != "" { | ||
return testAccCurRegion | ||
} | ||
|
||
if rs, ok := endpoints.RegionsForService(endpoints.DefaultPartitions(), testAccGetPartition(), costandusagereportservice.ServiceName); ok { | ||
// return available region (random if multiple) | ||
for regionID := range rs { | ||
testAccCurRegion = regionID | ||
return testAccCurRegion | ||
} | ||
} | ||
|
||
testAccCurRegion = testAccGetRegion() | ||
|
||
return testAccCurRegion | ||
} | ||
|
||
// testAccProviderFactoriesCur initializes providers for Cost and Usage Reporting testing. | ||
// | ||
// Deprecated: This will be replaced with testAccProviderFactories when it only returns the "aws" provider | ||
func testAccProviderFactoriesCur() map[string]func() (*schema.Provider, error) { | ||
return testAccProviderFactoriesInit(nil, []string{ProviderNameAws}) | ||
} |
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
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,84 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws/endpoints" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
const ( | ||
// EC2-Classic region testing environment variable name | ||
Ec2ClassicRegionEnvVar = "AWS_EC2_CLASSIC_REGION" | ||
) | ||
|
||
// testAccProviderEc2Classic is the EC2-Classic provider instance | ||
// | ||
// This Provider can be used in testing code for API calls without requiring | ||
// the use of saving and referencing specific ProviderFactories instances. | ||
// | ||
// testAccEC2ClassicPreCheck(t) must be called before using this provider instance. | ||
var testAccProviderEc2Classic *schema.Provider | ||
|
||
// testAccProviderEc2ClassicConfigure ensures the provider is only configured once | ||
var testAccProviderEc2ClassicConfigure sync.Once | ||
|
||
// testAccEC2ClassicPreCheck verifies AWS credentials and that EC2-Classic is supported | ||
func testAccEC2ClassicPreCheck(t *testing.T) { | ||
// Since we are outside the scope of the Terraform configuration we must | ||
// call Configure() to properly initialize the provider configuration. | ||
testAccProviderEc2ClassicConfigure.Do(func() { | ||
testAccProviderEc2Classic = Provider() | ||
|
||
config := map[string]interface{}{ | ||
"region": testAccGetEc2ClassicRegion(), | ||
} | ||
|
||
err := testAccProviderEc2Classic.Configure(context.Background(), terraform.NewResourceConfigRaw(config)) | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
|
||
client := testAccProviderEc2Classic.Meta().(*AWSClient) | ||
platforms := client.supportedplatforms | ||
region := client.region | ||
if !hasEc2Classic(platforms) { | ||
t.Skipf("this test can only run in EC2-Classic, platforms available in %s: %q", region, platforms) | ||
} | ||
} | ||
|
||
// testAccEc2ClassicRegionProviderConfig is the Terraform provider configuration for EC2-Classic region testing | ||
// | ||
// Testing EC2-Classic assumes no other provider configurations are necessary | ||
// and overwrites the "aws" provider configuration. | ||
func testAccEc2ClassicRegionProviderConfig() string { | ||
return testAccRegionalProviderConfig(testAccGetEc2ClassicRegion()) | ||
} | ||
|
||
// testAccGetEc2ClassicRegion returns the EC2-Classic region for testing | ||
func testAccGetEc2ClassicRegion() string { | ||
v := os.Getenv(Ec2ClassicRegionEnvVar) | ||
|
||
if v != "" { | ||
return v | ||
} | ||
|
||
if testAccGetPartition() == endpoints.AwsPartitionID { | ||
return endpoints.UsEast1RegionID | ||
} | ||
|
||
return testAccGetRegion() | ||
} | ||
|
||
// testAccProviderFactoriesEc2Classic initializes providers for EC2-Classic testing. | ||
// | ||
// Deprecated: This will be replaced with testAccProviderFactories when it only returns the "aws" provider | ||
func testAccProviderFactoriesEc2Classic() map[string]func() (*schema.Provider, error) { | ||
return testAccProviderFactoriesInit(nil, []string{ProviderNameAws}) | ||
} |
Oops, something went wrong.