Skip to content

Commit 8bd8f63

Browse files
committed
tests/resource/aws_fms_admin_account: Remove hardcoded environment variable handling
Reference: #8316 Reference: #15737 Previously in AWS GovCloud (US): ``` === CONT TestAccAwsFmsAdminAccount_basic TestAccAwsFmsAdminAccount_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: b96069f2-b851-4a14-814c-e04ebd3a1e7e []}] --- FAIL: TestAccAwsFmsAdminAccount_basic (0.33s) ``` Output from acceptance testing in AWS Commercial (standalone account): ``` --- PASS: TestAccAwsFmsAdminAccount_basic (97.32s) ``` Output from acceptance testing in AWS GovCloud (US): ``` --- SKIP: TestAccAwsFmsAdminAccount_basic (1.51s) ```
1 parent 75026f0 commit 8bd8f63

File tree

2 files changed

+94
-13
lines changed

2 files changed

+94
-13
lines changed

aws/fms_admin_test.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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/fms"
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+
// Firewall Management Service admin APIs are only enabled in specific regions, otherwise:
16+
// InvalidOperationException: This operation is not supported in the 'us-west-2' region.
17+
18+
// testAccFmsAdminRegion is the chosen Firewall Management Service testing region
19+
//
20+
// Cached to prevent issues should multiple regions become available.
21+
var testAccFmsAdminRegion string
22+
23+
// testAccProviderFmsAdmin is the Firewall Management Service provider instance
24+
//
25+
// This Provider can be used in testing code for API calls without requiring
26+
// the use of saving and referencing specific ProviderFactories instances.
27+
//
28+
// testAccPreCheckFmsAdmin(t) must be called before using this provider instance.
29+
var testAccProviderFmsAdmin *schema.Provider
30+
31+
// testAccProviderFmsAdminConfigure ensures the provider is only configured once
32+
var testAccProviderFmsAdminConfigure sync.Once
33+
34+
// testAccPreCheckFmsAdmin verifies AWS credentials and that Firewall Management Service is supported
35+
func testAccPreCheckFmsAdmin(t *testing.T) {
36+
testAccPartitionHasServicePreCheck(fms.EndpointsID, t)
37+
38+
// Since we are outside the scope of the Terraform configuration we must
39+
// call Configure() to properly initialize the provider configuration.
40+
testAccProviderFmsAdminConfigure.Do(func() {
41+
testAccProviderFmsAdmin = Provider()
42+
43+
config := map[string]interface{}{
44+
"region": testAccGetFmsAdminRegion(),
45+
}
46+
47+
diags := testAccProviderFmsAdmin.Configure(context.Background(), terraform.NewResourceConfigRaw(config))
48+
49+
if diags != nil && diags.HasError() {
50+
for _, d := range diags {
51+
if d.Severity == diag.Error {
52+
t.Fatalf("error configuring Firewall Management Service provider: %s", d.Summary)
53+
}
54+
}
55+
}
56+
})
57+
}
58+
59+
// testAccFmsAdminRegionProviderConfig is the Terraform provider configuration for Firewall Management Service region testing
60+
//
61+
// Testing Firewall Management Service assumes no other provider configurations
62+
// are necessary and overwrites the "aws" provider configuration.
63+
func testAccFmsAdminRegionProviderConfig() string {
64+
return testAccRegionalProviderConfig(testAccGetFmsAdminRegion())
65+
}
66+
67+
// testAccGetFmsAdminRegion returns the Firewall Management Service region for testing
68+
func testAccGetFmsAdminRegion() string {
69+
if testAccFmsAdminRegion != "" {
70+
return testAccFmsAdminRegion
71+
}
72+
73+
testAccFmsAdminRegion = endpoints.UsEast1RegionID
74+
75+
return testAccFmsAdminRegion
76+
}

aws/resource_aws_fms_admin_account_test.go

+18-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package aws
22

33
import (
44
"fmt"
5-
"os"
65
"testing"
76

87
"github.com/aws/aws-sdk-go/aws"
@@ -12,19 +11,19 @@ import (
1211
)
1312

1413
func TestAccAwsFmsAdminAccount_basic(t *testing.T) {
15-
oldDefaultRegion := os.Getenv("AWS_DEFAULT_REGION")
16-
os.Setenv("AWS_DEFAULT_REGION", "us-east-1")
17-
defer os.Setenv("AWS_DEFAULT_REGION", oldDefaultRegion)
18-
1914
resourceName := "aws_fms_admin_account.test"
2015

2116
resource.ParallelTest(t, resource.TestCase{
22-
PreCheck: func() { testAccPreCheck(t); testAccOrganizationsAccountPreCheck(t) },
23-
Providers: testAccProviders,
24-
CheckDestroy: testAccCheckFmsAdminAccountDestroy,
17+
PreCheck: func() {
18+
testAccPreCheck(t)
19+
testAccPreCheckFmsAdmin(t)
20+
testAccOrganizationsAccountPreCheck(t)
21+
},
22+
ProviderFactories: testAccProviderFactories,
23+
CheckDestroy: testAccCheckFmsAdminAccountDestroy,
2524
Steps: []resource.TestStep{
2625
{
27-
Config: testAccFmsAdminAccountConfig_basic,
26+
Config: testAccFmsAdminAccountConfig_basic(),
2827
Check: resource.ComposeTestCheckFunc(
2928
testAccCheckResourceAttrAccountID(resourceName, "account_id"),
3029
),
@@ -34,7 +33,7 @@ func TestAccAwsFmsAdminAccount_basic(t *testing.T) {
3433
}
3534

3635
func testAccCheckFmsAdminAccountDestroy(s *terraform.State) error {
37-
conn := testAccProvider.Meta().(*AWSClient).fmsconn
36+
conn := testAccProviderFmsAdmin.Meta().(*AWSClient).fmsconn
3837

3938
for _, rs := range s.RootModule().Resources {
4039
if rs.Type != "aws_fms_admin_account" {
@@ -61,13 +60,19 @@ func testAccCheckFmsAdminAccountDestroy(s *terraform.State) error {
6160
return nil
6261
}
6362

64-
const testAccFmsAdminAccountConfig_basic = `
63+
func testAccFmsAdminAccountConfig_basic() string {
64+
return composeConfig(
65+
testAccFmsAdminRegionProviderConfig(),
66+
`
67+
data "aws_partition" "current" {}
68+
6569
resource "aws_organizations_organization" "test" {
66-
aws_service_access_principals = ["fms.amazonaws.com"]
70+
aws_service_access_principals = ["fms.${data.aws_partition.current.dns_suffix}"]
6771
feature_set = "ALL"
6872
}
6973
7074
resource "aws_fms_admin_account" "test" {
7175
account_id = aws_organizations_organization.test.master_account_id
7276
}
73-
`
77+
`)
78+
}

0 commit comments

Comments
 (0)