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/resource/aws_route53_query_log: Remove hardcoded environment variable handling #16044

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
23 changes: 9 additions & 14 deletions aws/resource_aws_route53_query_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package aws
import (
"fmt"
"log"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -69,22 +68,16 @@ func testSweepRoute53QueryLogs(region string) error {
}

func TestAccAWSRoute53QueryLog_basic(t *testing.T) {
// The underlying resources are sensitive to where they are located
// Use us-east-1 for testing
oldRegion := os.Getenv("AWS_DEFAULT_REGION")
os.Setenv("AWS_DEFAULT_REGION", "us-east-1")
defer os.Setenv("AWS_DEFAULT_REGION", oldRegion)

cloudwatchLogGroupResourceName := "aws_cloudwatch_log_group.test"
resourceName := "aws_route53_query_log.test"
route53ZoneResourceName := "aws_route53_zone.test"
rName := strings.ToLower(fmt.Sprintf("%s-%s", t.Name(), acctest.RandString(5)))

var queryLoggingConfig route53.QueryLoggingConfig
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRoute53QueryLogDestroy,
PreCheck: func() { testAccPreCheck(t); testAccPreCheckRoute53QueryLog(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckRoute53QueryLogDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckAWSRoute53QueryLogResourceConfigBasic1(rName),
Expand All @@ -105,7 +98,7 @@ func TestAccAWSRoute53QueryLog_basic(t *testing.T) {

func testAccCheckRoute53QueryLogExists(pr string, queryLoggingConfig *route53.QueryLoggingConfig) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).r53conn
conn := testAccProviderRoute53QueryLog.Meta().(*AWSClient).r53conn
rs, ok := s.RootModule().Resources[pr]
if !ok {
return fmt.Errorf("Not found: %s", pr)
Expand All @@ -132,7 +125,7 @@ func testAccCheckRoute53QueryLogExists(pr string, queryLoggingConfig *route53.Qu
}

func testAccCheckRoute53QueryLogDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).r53conn
conn := testAccProviderRoute53QueryLog.Meta().(*AWSClient).r53conn

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_route53_query_log" {
Expand All @@ -155,7 +148,9 @@ func testAccCheckRoute53QueryLogDestroy(s *terraform.State) error {
}

func testAccCheckAWSRoute53QueryLogResourceConfigBasic1(rName string) string {
return fmt.Sprintf(`
return composeConfig(
testAccRoute53QueryLogRegionProviderConfig(),
fmt.Sprintf(`
resource "aws_cloudwatch_log_group" "test" {
name = "/aws/route53/${aws_route53_zone.test.name}"
retention_in_days = 1
Expand Down Expand Up @@ -194,5 +189,5 @@ resource "aws_route53_query_log" "test" {
cloudwatch_log_group_arn = aws_cloudwatch_log_group.test.arn
zone_id = aws_route53_zone.test.zone_id
}
`, rName)
`, rName))
}
87 changes: 87 additions & 0 deletions aws/route53_query_log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package aws

import (
"context"
"sync"
"testing"

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

// Route 53 Query Logging can only be enabled with CloudWatch Log Groups in specific regions,

// testAccRoute53QueryLogRegion is the chosen Route 53 Query Logging testing region
//
// Cached to prevent issues should multiple regions become available.
var testAccRoute53QueryLogRegion string

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

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

// testAccPreCheckRoute53QueryLog verifies AWS credentials and that Route 53 Query Logging is supported
func testAccPreCheckRoute53QueryLog(t *testing.T) {
testAccPartitionHasServicePreCheck(route53.EndpointsID, t)

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

region := testAccGetRoute53QueryLogRegion()

if region == "" {
t.Skip("Route 53 Query Log not available in this AWS Partition")
}

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

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

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

// testAccRoute53QueryLogRegionProviderConfig is the Terraform provider configuration for Route 53 Query Logging region testing
//
// Testing Route 53 Query Logging assumes no other provider configurations
// are necessary and overwrites the "aws" provider configuration.
func testAccRoute53QueryLogRegionProviderConfig() string {
return testAccRegionalProviderConfig(testAccGetRoute53QueryLogRegion())
}

// testAccGetRoute53QueryLogRegion returns the Route 53 Query Logging region for testing
func testAccGetRoute53QueryLogRegion() string {
if testAccRoute53QueryLogRegion != "" {
return testAccRoute53QueryLogRegion
}

// AWS Commercial: https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/query-logs.html
// AWS GovCloud (US) - only private DNS: https://docs.aws.amazon.com/govcloud-us/latest/UserGuide/govcloud-r53.html
// AWS China - not available yet: https://docs.amazonaws.cn/en_us/aws/latest/userguide/route53.html
switch testAccGetPartition() {
case endpoints.AwsPartitionID:
testAccRoute53QueryLogRegion = endpoints.UsEast1RegionID
}

return testAccRoute53QueryLogRegion
}