Skip to content

Commit

Permalink
tests/provider: Initial migration from hardcoded regions and shared e…
Browse files Browse the repository at this point in the history
…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
bflad authored Oct 28, 2020
1 parent 20fa9f8 commit ee329f6
Show file tree
Hide file tree
Showing 8 changed files with 384 additions and 238 deletions.
106 changes: 106 additions & 0 deletions aws/cur_test.go
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})
}
48 changes: 14 additions & 34 deletions aws/data_source_aws_cur_report_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package aws

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
Expand All @@ -11,22 +10,16 @@ import (
)

func TestAccDataSourceAwsCurReportDefinition_basic(t *testing.T) {
testAccPartitionHasServicePreCheck("cur", t) // This check must come before os.Setenv() or creds fail on GovCloud

oldvar := os.Getenv("AWS_DEFAULT_REGION")
os.Setenv("AWS_DEFAULT_REGION", "us-east-1") // lintignore:AWSAT003
defer os.Setenv("AWS_DEFAULT_REGION", oldvar)

resourceName := "aws_cur_report_definition.test"
datasourceName := "data.aws_cur_report_definition.test"

reportName := acctest.RandomWithPrefix("tf_acc_test")
bucketName := fmt.Sprintf("tf-test-bucket-%d", acctest.RandInt())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSCur(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsCurReportDefinitionDestroy,
PreCheck: func() { testAccPreCheck(t); testAccPreCheckCur(t) },
ProviderFactories: testAccProviderFactoriesCur(),
CheckDestroy: testAccCheckAwsCurReportDefinitionDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsCurReportDefinitionConfig_basic(reportName, bucketName),
Expand All @@ -47,22 +40,16 @@ func TestAccDataSourceAwsCurReportDefinition_basic(t *testing.T) {
}

func TestAccDataSourceAwsCurReportDefinition_additional(t *testing.T) {
testAccPartitionHasServicePreCheck("cur", t) // This check must come before os.Setenv() or creds fail on GovCloud

oldvar := os.Getenv("AWS_DEFAULT_REGION")
os.Setenv("AWS_DEFAULT_REGION", "us-east-1") //lintignore:AWSAT003
defer os.Setenv("AWS_DEFAULT_REGION", oldvar)

resourceName := "aws_cur_report_definition.test"
datasourceName := "data.aws_cur_report_definition.test"

reportName := acctest.RandomWithPrefix("tf_acc_test")
bucketName := fmt.Sprintf("tf-test-bucket-%d", acctest.RandInt())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSCur(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsCurReportDefinitionDestroy,
PreCheck: func() { testAccPreCheck(t); testAccPreCheckCur(t) },
ProviderFactories: testAccProviderFactoriesCur(),
CheckDestroy: testAccCheckAwsCurReportDefinitionDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsCurReportDefinitionConfig_additional(reportName, bucketName),
Expand Down Expand Up @@ -98,14 +85,10 @@ func testAccDataSourceAwsCurReportDefinitionCheckExists(datasourceName, resource
}
}

// note: cur report definitions are currently only supported in us-east-1
func testAccDataSourceAwsCurReportDefinitionConfig_basic(reportName string, bucketName string) string {
//lintignore:AWSAT003,AWSAT005
return fmt.Sprintf(`
provider "aws" {
region = "us-east-1"
}
return composeConfig(
testAccCurRegionProviderConfig(),
fmt.Sprintf(`
data "aws_billing_service_account" "test" {}
resource "aws_s3_bucket" "test" {
Expand Down Expand Up @@ -165,16 +148,13 @@ resource "aws_cur_report_definition" "test" {
data "aws_cur_report_definition" "test" {
report_name = aws_cur_report_definition.test.report_name
}
`, reportName, bucketName)
`, reportName, bucketName))
}

func testAccDataSourceAwsCurReportDefinitionConfig_additional(reportName string, bucketName string) string {
//lintignore:AWSAT003,AWSAT005
return fmt.Sprintf(`
provider "aws" {
region = "us-east-1"
}
return composeConfig(
testAccCurRegionProviderConfig(),
fmt.Sprintf(`
data "aws_billing_service_account" "test" {}
resource "aws_s3_bucket" "test" {
Expand Down Expand Up @@ -236,5 +216,5 @@ resource "aws_cur_report_definition" "test" {
data "aws_cur_report_definition" "test" {
report_name = aws_cur_report_definition.test.report_name
}
`, reportName, bucketName)
`, reportName, bucketName))
}
84 changes: 84 additions & 0 deletions aws/ec2_classic_test.go
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})
}
Loading

0 comments on commit ee329f6

Please sign in to comment.