Skip to content

Commit

Permalink
tests/data-source/aws_pricing_product: Remove hardcoded us-east-1 han…
Browse files Browse the repository at this point in the history
…dling (#16040)

Reference: #8316
Reference: #15737

Previously in AWS GovCloud (US):

```
=== CONT  TestAccDataSourceAwsPricingProduct_ec2
TestAccDataSourceAwsPricingProduct_ec2: provider_test.go:184: [{0 error configuring Terraform AWS Provider: error validating provider credentials: error calling sts:GetCallerIdentity: InvalidClientTokenId: The security token included in the request is invalid.
  status code: 403, request id: bc4d07ae-04d0-4fad-b695-5ef067be32ee  []}]
--- FAIL: TestAccDataSourceAwsPricingProduct_ec2 (0.41s)

=== CONT  TestAccDataSourceAwsPricingProduct_redshift
TestAccDataSourceAwsPricingProduct_redshift: provider_test.go:184: [{0 error configuring Terraform AWS Provider: error validating provider credentials: error calling sts:GetCallerIdentity: InvalidClientTokenId: The security token included in the request is invalid.
  status code: 403, request id: 0871bc77-35ba-4dbe-aa49-7a54304986ce  []}]
--- FAIL: TestAccDataSourceAwsPricingProduct_redshift (0.38s)
```

Output from acceptance testing in AWS Commercial:

```
--- PASS: TestAccDataSourceAwsPricingProduct_ec2 (18.23s)
--- PASS: TestAccDataSourceAwsPricingProduct_redshift (18.42s)
```

Output from acceptance testing in AWS GovCloud (US):

```
--- SKIP: TestAccDataSourceAwsPricingProduct_ec2 (1.54s)
--- SKIP: TestAccDataSourceAwsPricingProduct_redshift (1.54s)
```
  • Loading branch information
bflad authored Nov 11, 2020
1 parent ba26435 commit 1bc1592
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 22 deletions.
53 changes: 31 additions & 22 deletions aws/data_source_aws_pricing_product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@ package aws
import (
"encoding/json"
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccDataSourceAwsPricingProduct_ec2(t *testing.T) {
oldRegion := os.Getenv("AWS_DEFAULT_REGION")
os.Setenv("AWS_DEFAULT_REGION", "us-east-1")
defer os.Setenv("AWS_DEFAULT_REGION", oldRegion)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t); testAccPreCheckPricing(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsPricingProductConfigEc2("test", "c5.large"),
Config: testAccDataSourceAwsPricingProductConfigEc2(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.aws_pricing_product.test", "result"),
testAccPricingCheckValueIsJSON("data.aws_pricing_product.test"),
Expand All @@ -30,12 +26,9 @@ func TestAccDataSourceAwsPricingProduct_ec2(t *testing.T) {
}

func TestAccDataSourceAwsPricingProduct_redshift(t *testing.T) {
oldRegion := os.Getenv("AWS_DEFAULT_REGION")
os.Setenv("AWS_DEFAULT_REGION", "us-east-1")
defer os.Setenv("AWS_DEFAULT_REGION", oldRegion)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t); testAccPreCheckPricing(t) },
ProviderFactories: testAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsPricingProductConfigRedshift(),
Expand All @@ -48,14 +41,22 @@ func TestAccDataSourceAwsPricingProduct_redshift(t *testing.T) {
})
}

func testAccDataSourceAwsPricingProductConfigEc2(dataName string, instanceType string) string {
return fmt.Sprintf(`
data "aws_pricing_product" "%s" {
func testAccDataSourceAwsPricingProductConfigEc2() string {
return composeConfig(
testAccPricingRegionProviderConfig(),
`
data "aws_ec2_instance_type_offering" "available" {
preferred_instance_types = ["c5.large", "c4.large"]
}
data "aws_region" "current" {}
data "aws_pricing_product" "test" {
service_code = "AmazonEC2"
filters {
field = "instanceType"
value = "%s"
value = data.aws_ec2_instance_type_offering.available.instance_type
}
filters {
Expand All @@ -65,7 +66,7 @@ data "aws_pricing_product" "%s" {
filters {
field = "location"
value = "US East (N. Virginia)"
value = data.aws_region.current.description
}
filters {
Expand All @@ -88,25 +89,33 @@ data "aws_pricing_product" "%s" {
value = "Used"
}
}
`, dataName, instanceType)
`)
}

func testAccDataSourceAwsPricingProductConfigRedshift() string {
return `
return composeConfig(
testAccPricingRegionProviderConfig(),
`
data "aws_redshift_orderable_cluster" "test" {
preferred_node_types = ["dc2.8xlarge", "ds2.8xlarge"]
}
data "aws_region" "current" {}
data "aws_pricing_product" "test" {
service_code = "AmazonRedshift"
filters {
field = "instanceType"
value = "ds1.xlarge"
value = data.aws_redshift_orderable_cluster.test.node_type
}
filters {
field = "location"
value = "US East (N. Virginia)"
value = data.aws_region.current.description
}
}
`
`)
}

func testAccPricingCheckValueIsJSON(data string) resource.TestCheckFunc {
Expand Down
81 changes: 81 additions & 0 deletions aws/pricing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package aws

import (
"context"
"sync"
"testing"

"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/pricing"
"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"
)

// testAccPricingRegion is the chosen Pricing testing region
//
// Cached to prevent issues should multiple regions become available.
var testAccPricingRegion string

// testAccProviderPricing is the Pricing provider instance
//
// This Provider can be used in testing code for API calls without requiring
// the use of saving and referencing specific ProviderFactories instances.
//
// testAccPreCheckPricing(t) must be called before using this provider instance.
var testAccProviderPricing *schema.Provider

// testAccProviderPricingConfigure ensures the provider is only configured once
var testAccProviderPricingConfigure sync.Once

// testAccPreCheckPricing verifies AWS credentials and that Pricing is supported
func testAccPreCheckPricing(t *testing.T) {
testAccPartitionHasServicePreCheck(pricing.EndpointsID, t)

// Since we are outside the scope of the Terraform configuration we must
// call Configure() to properly initialize the provider configuration.
testAccProviderPricingConfigure.Do(func() {
testAccProviderPricing = Provider()

config := map[string]interface{}{
"region": testAccGetPricingRegion(),
}

diags := testAccProviderPricing.Configure(context.Background(), terraform.NewResourceConfigRaw(config))

if diags != nil && diags.HasError() {
for _, d := range diags {
if d.Severity == diag.Error {
t.Fatalf("error configuring Pricing provider: %s", d.Summary)
}
}
}
})
}

// testAccPricingRegionProviderConfig is the Terraform provider configuration for Pricing region testing
//
// Testing Pricing assumes no other provider configurations
// are necessary and overwrites the "aws" provider configuration.
func testAccPricingRegionProviderConfig() string {
return testAccRegionalProviderConfig(testAccGetPricingRegion())
}

// testAccGetPricingRegion returns the Pricing region for testing
func testAccGetPricingRegion() string {
if testAccPricingRegion != "" {
return testAccPricingRegion
}

if rs, ok := endpoints.RegionsForService(endpoints.DefaultPartitions(), testAccGetPartition(), pricing.ServiceName); ok {
// return available region (random if multiple)
for regionID := range rs {
testAccPricingRegion = regionID
return testAccPricingRegion
}
}

testAccPricingRegion = testAccGetRegion()

return testAccPricingRegion
}

0 comments on commit 1bc1592

Please sign in to comment.