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/data-source/aws_pricing_product: Remove hardcoded us-east-1 handling #16040

Merged
merged 1 commit into from
Nov 11, 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
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
}