Skip to content

Commit

Permalink
tests/service/costandusagereporting: Migrate to automatic region lookup
Browse files Browse the repository at this point in the history
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)
```
  • Loading branch information
bflad committed Oct 23, 2020
1 parent fd841b4 commit 8508556
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 125 deletions.
83 changes: 83 additions & 0 deletions aws/cur_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package aws

import (
"context"
"sync"
"testing"

"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/costandusagereportservice"
"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(),
}

err := testAccProviderCur.Configure(context.Background(), terraform.NewResourceConfigRaw(config))

if err != nil {
t.Fatal(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))
}
Loading

0 comments on commit 8508556

Please sign in to comment.