Skip to content

Commit 5ec1482

Browse files
authored
tests/resource/aws_route53_query_log: Remove hardcoded environment variable handling (#16044)
Reference: #8316 Reference: #15737 Previously in AWS GovCloud (US): ``` === CONT TestAccAWSRoute53QueryLog_basic TestAccAWSRoute53QueryLog_basic: 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: cb8c48d8-2335-4b2f-b5f0-97cd5a4ec4d7 []}] --- FAIL: TestAccAWSRoute53QueryLog_basic (0.40s) ``` Output from acceptance testing in AWS Commercial: ``` --- PASS: TestAccAWSRoute53QueryLog_basic (46.16s) ``` Output from acceptance testing in AWS GovCloud (US): ``` --- SKIP: TestAccAWSRoute53QueryLog_basic (1.56s) ```
1 parent feee86f commit 5ec1482

File tree

2 files changed

+96
-14
lines changed

2 files changed

+96
-14
lines changed

aws/resource_aws_route53_query_log_test.go

+9-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package aws
33
import (
44
"fmt"
55
"log"
6-
"os"
76
"strings"
87
"testing"
98

@@ -69,22 +68,16 @@ func testSweepRoute53QueryLogs(region string) error {
6968
}
7069

7170
func TestAccAWSRoute53QueryLog_basic(t *testing.T) {
72-
// The underlying resources are sensitive to where they are located
73-
// Use us-east-1 for testing
74-
oldRegion := os.Getenv("AWS_DEFAULT_REGION")
75-
os.Setenv("AWS_DEFAULT_REGION", "us-east-1")
76-
defer os.Setenv("AWS_DEFAULT_REGION", oldRegion)
77-
7871
cloudwatchLogGroupResourceName := "aws_cloudwatch_log_group.test"
7972
resourceName := "aws_route53_query_log.test"
8073
route53ZoneResourceName := "aws_route53_zone.test"
8174
rName := strings.ToLower(fmt.Sprintf("%s-%s", t.Name(), acctest.RandString(5)))
8275

8376
var queryLoggingConfig route53.QueryLoggingConfig
8477
resource.ParallelTest(t, resource.TestCase{
85-
PreCheck: func() { testAccPreCheck(t) },
86-
Providers: testAccProviders,
87-
CheckDestroy: testAccCheckRoute53QueryLogDestroy,
78+
PreCheck: func() { testAccPreCheck(t); testAccPreCheckRoute53QueryLog(t) },
79+
ProviderFactories: testAccProviderFactories,
80+
CheckDestroy: testAccCheckRoute53QueryLogDestroy,
8881
Steps: []resource.TestStep{
8982
{
9083
Config: testAccCheckAWSRoute53QueryLogResourceConfigBasic1(rName),
@@ -105,7 +98,7 @@ func TestAccAWSRoute53QueryLog_basic(t *testing.T) {
10598

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

134127
func testAccCheckRoute53QueryLogDestroy(s *terraform.State) error {
135-
conn := testAccProvider.Meta().(*AWSClient).r53conn
128+
conn := testAccProviderRoute53QueryLog.Meta().(*AWSClient).r53conn
136129

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

157150
func testAccCheckAWSRoute53QueryLogResourceConfigBasic1(rName string) string {
158-
return fmt.Sprintf(`
151+
return composeConfig(
152+
testAccRoute53QueryLogRegionProviderConfig(),
153+
fmt.Sprintf(`
159154
resource "aws_cloudwatch_log_group" "test" {
160155
name = "/aws/route53/${aws_route53_zone.test.name}"
161156
retention_in_days = 1
@@ -194,5 +189,5 @@ resource "aws_route53_query_log" "test" {
194189
cloudwatch_log_group_arn = aws_cloudwatch_log_group.test.arn
195190
zone_id = aws_route53_zone.test.zone_id
196191
}
197-
`, rName)
192+
`, rName))
198193
}

aws/route53_query_log_test.go

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package aws
2+
3+
import (
4+
"context"
5+
"sync"
6+
"testing"
7+
8+
"github.com/aws/aws-sdk-go/aws/endpoints"
9+
"github.com/aws/aws-sdk-go/service/route53"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
13+
)
14+
15+
// Route 53 Query Logging can only be enabled with CloudWatch Log Groups in specific regions,
16+
17+
// testAccRoute53QueryLogRegion is the chosen Route 53 Query Logging testing region
18+
//
19+
// Cached to prevent issues should multiple regions become available.
20+
var testAccRoute53QueryLogRegion string
21+
22+
// testAccProviderRoute53QueryLog is the Route 53 Query Logging provider instance
23+
//
24+
// This Provider can be used in testing code for API calls without requiring
25+
// the use of saving and referencing specific ProviderFactories instances.
26+
//
27+
// testAccPreCheckRoute53QueryLog(t) must be called before using this provider instance.
28+
var testAccProviderRoute53QueryLog *schema.Provider
29+
30+
// testAccProviderRoute53QueryLogConfigure ensures the provider is only configured once
31+
var testAccProviderRoute53QueryLogConfigure sync.Once
32+
33+
// testAccPreCheckRoute53QueryLog verifies AWS credentials and that Route 53 Query Logging is supported
34+
func testAccPreCheckRoute53QueryLog(t *testing.T) {
35+
testAccPartitionHasServicePreCheck(route53.EndpointsID, t)
36+
37+
// Since we are outside the scope of the Terraform configuration we must
38+
// call Configure() to properly initialize the provider configuration.
39+
testAccProviderRoute53QueryLogConfigure.Do(func() {
40+
testAccProviderRoute53QueryLog = Provider()
41+
42+
region := testAccGetRoute53QueryLogRegion()
43+
44+
if region == "" {
45+
t.Skip("Route 53 Query Log not available in this AWS Partition")
46+
}
47+
48+
config := map[string]interface{}{
49+
"region": region,
50+
}
51+
52+
diags := testAccProviderRoute53QueryLog.Configure(context.Background(), terraform.NewResourceConfigRaw(config))
53+
54+
if diags != nil && diags.HasError() {
55+
for _, d := range diags {
56+
if d.Severity == diag.Error {
57+
t.Fatalf("error configuring Route 53 Query Logging provider: %s", d.Summary)
58+
}
59+
}
60+
}
61+
})
62+
}
63+
64+
// testAccRoute53QueryLogRegionProviderConfig is the Terraform provider configuration for Route 53 Query Logging region testing
65+
//
66+
// Testing Route 53 Query Logging assumes no other provider configurations
67+
// are necessary and overwrites the "aws" provider configuration.
68+
func testAccRoute53QueryLogRegionProviderConfig() string {
69+
return testAccRegionalProviderConfig(testAccGetRoute53QueryLogRegion())
70+
}
71+
72+
// testAccGetRoute53QueryLogRegion returns the Route 53 Query Logging region for testing
73+
func testAccGetRoute53QueryLogRegion() string {
74+
if testAccRoute53QueryLogRegion != "" {
75+
return testAccRoute53QueryLogRegion
76+
}
77+
78+
// AWS Commercial: https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/query-logs.html
79+
// AWS GovCloud (US) - only private DNS: https://docs.aws.amazon.com/govcloud-us/latest/UserGuide/govcloud-r53.html
80+
// AWS China - not available yet: https://docs.amazonaws.cn/en_us/aws/latest/userguide/route53.html
81+
switch testAccGetPartition() {
82+
case endpoints.AwsPartitionID:
83+
testAccRoute53QueryLogRegion = endpoints.UsEast1RegionID
84+
}
85+
86+
return testAccRoute53QueryLogRegion
87+
}

0 commit comments

Comments
 (0)