Skip to content

Commit 1bc96e1

Browse files
authored
Merge pull request hashicorp#25733 from hashicorp/td-acctest.ProviderFactories-to-acctest.ProtoV5ProviderFactories
Replace use of `acctest.ProviderFactories` with `acctest.ProtoV5ProviderFactories`
2 parents 34af55a + 530ed64 commit 1bc96e1

File tree

1,421 files changed

+31305
-31614
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,421 files changed

+31305
-31614
lines changed

.ci/.semgrep.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -770,13 +770,15 @@ rules:
770770
- pattern: tfiam.PropagationTimeout
771771
severity: ERROR
772772

773-
- id: use-acctest-provider-factories
773+
- id: use-acctest-protov5-provider-factories
774774
languages: [go]
775-
message: Use acctest.ProviderFactories, not acctest.Providers
775+
message: Use acctest.ProtoV5ProviderFactories, not acctest.Providers or acctest.ProviderFactories
776776
paths:
777777
include:
778778
- "internal/**/*_test.go"
779-
pattern-regex: Providers:\s+(acctest\.)?Providers,
779+
pattern-either:
780+
- pattern-regex: Providers:\s+(acctest\.)?Providers,
781+
- pattern-regex: ProviderFactories:\s+(acctest\.)?ProviderFactories,
780782
severity: WARNING
781783

782784
- id: prefer-aws-go-sdk-pointer-conversion-int-conversion-int64-pointer

docs/contributing/contribution-checklists.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ func TestAccServiceThing_nameGenerated(t *testing.T) {
138138
resourceName := "aws_service_thing.test"
139139

140140
resource.ParallelTest(t, resource.TestCase{
141-
PreCheck: func() { acctest.PreCheck(t) },
142-
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
143-
ProviderFactories: acctest.ProviderFactories,
144-
CheckDestroy: testAccCheckThingDestroy,
141+
PreCheck: func() { acctest.PreCheck(t) },
142+
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
143+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
144+
CheckDestroy: testAccCheckThingDestroy,
145145
Steps: []resource.TestStep{
146146
{
147147
Config: testAccThingNameGeneratedConfig(),
@@ -166,10 +166,10 @@ func TestAccServiceThing_namePrefix(t *testing.T) {
166166
resourceName := "aws_service_thing.test"
167167

168168
resource.ParallelTest(t, resource.TestCase{
169-
PreCheck: func() { acctest.PreCheck(t) },
170-
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
171-
ProviderFactories: acctest.ProviderFactories,
172-
CheckDestroy: testAccCheckThingDestroy,
169+
PreCheck: func() { acctest.PreCheck(t) },
170+
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
171+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
172+
CheckDestroy: testAccCheckThingDestroy,
173173
Steps: []resource.TestStep{
174174
{
175175
Config: testAccThingNamePrefixConfig("tf-acc-test-prefix-"),
@@ -465,10 +465,10 @@ More details about this code generation, including fixes for potential error mes
465465
resourceName := "aws_eks_cluster.test"
466466

467467
resource.ParallelTest(t, resource.TestCase{
468-
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
469-
ErrorCheck: acctest.ErrorCheck(t, eks.EndpointsID),
470-
ProviderFactories: acctest.ProviderFactories,
471-
CheckDestroy: testAccCheckClusterDestroy,
468+
PreCheck: func() { acctest.PreCheck(t); testAccPreCheck(t) },
469+
ErrorCheck: acctest.ErrorCheck(t, eks.EndpointsID),
470+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
471+
CheckDestroy: testAccCheckClusterDestroy,
472472
Steps: []resource.TestStep{
473473
{
474474
Config: testAccClusterConfigTags1(rName, "key1", "value1"),
@@ -740,11 +740,11 @@ func TestAcc{Service}Tag_Value(t *testing.T) {
740740
resourceName := "aws_{service}_tag.test"
741741

742742
resource.ParallelTest(t, resource.TestCase{
743-
PreCheck: func() { acctest.PreCheck(t) },
744-
ErrorCheck: acctest.ErrorCheck(t, {Service}.EndpointsID),
745-
ProviderFactories: acctest.ProviderFactories,
746-
CheckDestroy: testAccCheck{Service}TagDestroy,
747-
Steps: []resource.TestStep{
743+
PreCheck: func() { acctest.PreCheck(t) },
744+
ErrorCheck: acctest.ErrorCheck(t, {Service}.EndpointsID),
745+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
746+
CheckDestroy: testAccCheck{Service}TagDestroy,
747+
Steps: []resource.TestStep{
748748
{
749749
Config: testAcc{Service}TagConfig(rName, "key1", "value1"),
750750
Check: resource.ComposeTestCheckFunc(

docs/contributing/running-and-writing-acceptance-tests.md

+33-37
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,10 @@ func TestAccCloudWatchDashboard_basic(t *testing.T) {
243243
var dashboard cloudwatch.GetDashboardOutput
244244
rInt := acctest.RandInt()
245245
resource.ParallelTest(t, resource.TestCase{
246-
PreCheck: func() { acctest.PreCheck(t) },
247-
ErrorCheck: acctest.ErrorCheck(t, cloudwatch.EndpointsID),
248-
ProviderFactories: acctest.ProviderFactories,
249-
CheckDestroy: testAccCheckDashboardDestroy,
246+
PreCheck: func() { acctest.PreCheck(t) },
247+
ErrorCheck: acctest.ErrorCheck(t, cloudwatch.EndpointsID),
248+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
249+
CheckDestroy: testAccCheckDashboardDestroy,
250250
Steps: []resource.TestStep{
251251
{
252252
Config: testAccDashboardConfig(rInt),
@@ -556,10 +556,10 @@ func TestAccExampleThing_basic(t *testing.T) {
556556
resourceName := "aws_example_thing.test"
557557

558558
resource.ParallelTest(t, resource.TestCase{
559-
PreCheck: func() { acctest.PreCheck(t) },
560-
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
561-
ProviderFactories: acctest.ProviderFactories,
562-
CheckDestroy: testAccCheckExampleThingDestroy,
559+
PreCheck: func() { acctest.PreCheck(t) },
560+
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
561+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
562+
CheckDestroy: testAccCheckExampleThingDestroy,
563563
Steps: []resource.TestStep{
564564
{
565565
Config: testAccExampleThingConfigName(rName),
@@ -754,10 +754,10 @@ func TestAccExampleThing_disappears(t *testing.T) {
754754
resourceName := "aws_example_thing.test"
755755

756756
resource.ParallelTest(t, resource.TestCase{
757-
PreCheck: func() { acctest.PreCheck(t) },
758-
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
759-
ProviderFactories: acctest.ProviderFactories,
760-
CheckDestroy: testAccCheckExampleThingDestroy,
757+
PreCheck: func() { acctest.PreCheck(t) },
758+
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
759+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
760+
CheckDestroy: testAccCheckExampleThingDestroy,
761761
Steps: []resource.TestStep{
762762
{
763763
Config: testAccExampleThingConfigName(rName),
@@ -797,10 +797,10 @@ func TestAccExampleChildThing_disappears_ParentThing(t *testing.T) {
797797
resourceName := "aws_example_child_thing.test"
798798

799799
resource.ParallelTest(t, resource.TestCase{
800-
PreCheck: func() { acctest.PreCheck(t) },
801-
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
802-
ProviderFactories: acctest.ProviderFactories,
803-
CheckDestroy: testAccCheckExampleChildThingDestroy,
800+
PreCheck: func() { acctest.PreCheck(t) },
801+
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
802+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
803+
CheckDestroy: testAccCheckExampleChildThingDestroy,
804804
Steps: []resource.TestStep{
805805
{
806806
Config: testAccExampleThingConfigName(rName),
@@ -827,10 +827,10 @@ func TestAccExampleThing_Description(t *testing.T) {
827827
resourceName := "aws_example_thing.test"
828828

829829
resource.ParallelTest(t, resource.TestCase{
830-
PreCheck: func() { acctest.PreCheck(t) },
831-
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
832-
ProviderFactories: acctest.ProviderFactories,
833-
CheckDestroy: testAccCheckExampleThingDestroy,
830+
PreCheck: func() { acctest.PreCheck(t) },
831+
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
832+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
833+
CheckDestroy: testAccCheckExampleThingDestroy,
834834
Steps: []resource.TestStep{
835835
{
836836
Config: testAccExampleThingConfigDescription(rName, "description1"),
@@ -872,26 +872,24 @@ resource "aws_example_thing" "test" {
872872
When testing requires AWS infrastructure in a second AWS account, the below changes to the normal setup will allow the management or reference of resources and data sources across accounts:
873873

874874
- In the `PreCheck` function, include `acctest.PreCheckOrganizationsAccount(t)` to ensure a standardized set of information is required for cross-account testing credentials
875-
- Declare a `providers` variable at the top of the test function: `var providers []*schema.Provider`
876-
- Switch usage of `ProviderFactories: acctest.ProviderFactories` to `ProviderFactories: acctest.FactoriesAlternate(&providers)`
875+
- Switch usage of `ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories` to `ProtoV5ProviderFactories: acctest.ProtoV5FactoriesAlternate(t)`
877876
- Add `acctest.ConfigAlternateAccountProvider()` to the test configuration and use `provider = awsalternate` for cross-account resources. The resource that is the focus of the acceptance test should _not_ use the alternate provider identification to simplify the testing setup.
878877
- For any `TestStep` that includes `ImportState: true`, add the `Config` that matches the previous `TestStep` `Config`
879878

880879
An example acceptance test implementation can be seen below:
881880

882881
```go
883882
func TestAccExample_basic(t *testing.T) {
884-
var providers []*schema.Provider
885883
resourceName := "aws_example.test"
886884

887885
resource.ParallelTest(t, resource.TestCase{
888886
PreCheck: func() {
889887
acctest.PreCheck(t)
890888
acctest.PreCheckOrganizationsAccount(t)
891889
},
892-
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
893-
ProviderFactories: acctest.FactoriesAlternate(&providers),
894-
CheckDestroy: testAccCheckExampleDestroy,
890+
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
891+
ProtoV5ProviderFactories: acctest.ProtoV5FactoriesAlternate(t),
892+
CheckDestroy: testAccCheckExampleDestroy,
895893
Steps: []resource.TestStep{
896894
{
897895
Config: testAccExampleConfig(),
@@ -936,26 +934,24 @@ Searching for usage of `acctest.PreCheckOrganizationsAccount` in the codebase wi
936934
When testing requires AWS infrastructure in a second or third AWS region, the below changes to the normal setup will allow the management or reference of resources and data sources across regions:
937935

938936
- In the `PreCheck` function, include `acctest.PreCheckMultipleRegion(t, ###)` to ensure a standardized set of information is required for cross-region testing configuration. If the infrastructure in the second AWS region is also in a second AWS account also include `acctest.PreCheckOrganizationsAccount(t)`
939-
- Declare a `providers` variable at the top of the test function: `var providers []*schema.Provider`
940-
- Switch usage of `ProviderFactories: acctest.ProviderFactories` to `ProviderFactories: acctest.FactoriesMultipleRegion(&providers, 2)` (where the last parameter is number of regions)
937+
- Switch usage of `ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories` to `ProtoV5ProviderFactories: acctest.ProtoV5FactoriesMultipleRegions(t, 2)` (where the last parameter is number of regions)
941938
- Add `acctest.ConfigMultipleRegionProvider(###)` to the test configuration and use `provider = awsalternate` (and potentially `provider = awsthird`) for cross-region resources. The resource that is the focus of the acceptance test should _not_ use the alternative providers to simplify the testing setup. If the infrastructure in the second AWS region is also in a second AWS account use `testAccAlternateAccountAlternateRegionProviderConfig()` (EC2) instead
942939
- For any `TestStep` that includes `ImportState: true`, add the `Config` that matches the previous `TestStep` `Config`
943940

944941
An example acceptance test implementation can be seen below:
945942

946943
```go
947944
func TestAccExample_basic(t *testing.T) {
948-
var providers []*schema.Provider
949945
resourceName := "aws_example.test"
950946

951947
resource.ParallelTest(t, resource.TestCase{
952948
PreCheck: func() {
953949
acctest.PreCheck(t)
954950
acctest.PreCheckMultipleRegion(t, 2)
955951
},
956-
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
957-
ProviderFactories: acctest.FactoriesMultipleRegion(&providers, 2),
958-
CheckDestroy: testAccCheckExampleDestroy,
952+
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
953+
ProtoV5ProviderFactories: acctest.ProtoV5FactoriesMultipleRegions(t, 2),
954+
CheckDestroy: testAccCheckExampleDestroy,
959955
Steps: []resource.TestStep{
960956
{
961957
Config: testAccExampleConfig(),
@@ -1089,7 +1085,7 @@ func testAccGetPricingRegion() string {
10891085

10901086
For the resource or data source acceptance tests, the key items to adjust are:
10911087

1092-
* Ensure `TestCase` uses `ProviderFactories: acctest.ProviderFactories` instead of `Providers: acctest.Providers`
1088+
* Ensure `TestCase` uses `ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories` instead of `ProviderFactories: acctest.ProviderFactories` or `Providers: acctest.Providers`
10931089
* Add the call for the new `PreCheck` function (keeping `acctest.PreCheck(t)`), e.g. `PreCheck: func() { acctest.PreCheck(t); testAccPreCheckPricing(t) },`
10941090
* If the testing is for a managed resource with a `CheckDestroy` function, ensure it uses the new provider instance, e.g. `testAccProviderPricing`, instead of `acctest.Provider`.
10951091
* If the testing is for a managed resource with a `Check...Exists` function, ensure it uses the new provider instance, e.g. `testAccProviderPricing`, instead of `acctest.Provider`.
@@ -1169,10 +1165,10 @@ func TestAccExampleThingDataSource_Name(t *testing.T) {
11691165
resourceName := "aws_example_thing.test"
11701166

11711167
resource.ParallelTest(t, resource.TestCase{
1172-
PreCheck: func() { acctest.PreCheck(t) },
1173-
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
1174-
ProviderFactories: acctest.ProviderFactories,
1175-
CheckDestroy: testAccCheckExampleThingDestroy,
1168+
PreCheck: func() { acctest.PreCheck(t) },
1169+
ErrorCheck: acctest.ErrorCheck(t, service.EndpointsID),
1170+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
1171+
CheckDestroy: testAccCheckExampleThingDestroy,
11761172
Steps: []resource.TestStep{
11771173
{
11781174
Config: testAccExampleThingDataSourceConfigName(rName),

internal/acctest/acctest.go

+29-54
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,10 @@ func Skip(t *testing.T, message string) {
6969
t.Skip(message)
7070
}
7171

72-
// Providers is a static map containing only the main provider instance.
73-
//
74-
// Deprecated: Terraform Plugin SDK version 2 uses TestCase.ProviderFactories
75-
// but supports this value in TestCase.Providers for backwards compatibility.
76-
// In the future Providers: Providers will be changed to
77-
// ProviderFactories: ProviderFactories
78-
var Providers map[string]*schema.Provider
79-
80-
// ProviderFactories is a static map containing only the main provider instance
72+
// ProtoV5ProviderFactories is a static map containing only the main provider instance
8173
//
8274
// Use other ProviderFactories functions, such as FactoriesAlternate,
8375
// for tests requiring special provider configurations.
84-
var ProviderFactories map[string]func() (*schema.Provider, error)
85-
8676
var ProtoV5ProviderFactories map[string]func() (tfprotov5.ProviderServer, error)
8777

8878
// Provider is the "main" provider instance
@@ -104,30 +94,29 @@ var testAccProviderConfigure sync.Once
10494
func init() {
10595
Provider = provider.Provider()
10696

107-
Providers = map[string]*schema.Provider{
108-
ProviderName: Provider,
109-
}
110-
11197
// Always allocate a new provider instance each invocation, otherwise gRPC
11298
// ProviderConfigure() can overwrite configuration during concurrent testing.
113-
ProviderFactories = map[string]func() (*schema.Provider, error){
114-
ProviderName: func() (*schema.Provider, error) { return provider.Provider(), nil }, //nolint:unparam
115-
}
99+
ProtoV5ProviderFactories = protoV5ProviderFactoriesInit(ProviderName)
100+
}
101+
102+
func protoV5ProviderFactoriesInit(providerNames ...string) map[string]func() (tfprotov5.ProviderServer, error) {
103+
factories := make(map[string]func() (tfprotov5.ProviderServer, error), len(providerNames))
116104

117-
ProtoV5ProviderFactories = map[string]func() (tfprotov5.ProviderServer, error){
118-
ProviderName: func() (tfprotov5.ProviderServer, error) {
105+
for _, name := range providerNames {
106+
factories[name] = func() (tfprotov5.ProviderServer, error) {
119107
providerServerFactory, err := provider.ProtoV5ProviderServerFactory(context.Background())
120108

121109
if err != nil {
122110
return nil, err
123111
}
124112

125113
return providerServerFactory(), nil
126-
},
114+
}
127115
}
116+
117+
return factories
128118
}
129119

130-
// factoriesInit creates ProviderFactories for the provider under testing.
131120
func factoriesInit(providers *[]*schema.Provider, providerNames []string) map[string]func() (*schema.Provider, error) {
132121
var factories = make(map[string]func() (*schema.Provider, error), len(providerNames))
133122

@@ -146,15 +135,6 @@ func factoriesInit(providers *[]*schema.Provider, providerNames []string) map[st
146135
return factories
147136
}
148137

149-
// FactoriesInternal creates ProviderFactories for provider configuration testing
150-
//
151-
// This should only be used for TestAccProvider_ tests which need to
152-
// reference the provider instance itself. Other testing should use
153-
// ProviderFactories or other related functions.
154-
func FactoriesInternal(providers *[]*schema.Provider) map[string]func() (*schema.Provider, error) {
155-
return factoriesInit(providers, []string{ProviderName})
156-
}
157-
158138
// FactoriesAlternate creates ProviderFactories for cross-account and cross-region configurations
159139
//
160140
// For cross-region testing: Typically paired with PreCheckMultipleRegion and ConfigAlternateRegionProvider.
@@ -167,33 +147,37 @@ func FactoriesAlternate(providers *[]*schema.Provider) map[string]func() (*schem
167147
})
168148
}
169149

170-
// FactoriesAlternateAccountAndAlternateRegion creates ProviderFactories for cross-account and cross-region configurations
150+
func ProtoV5FactoriesAlternate(t *testing.T) map[string]func() (tfprotov5.ProviderServer, error) {
151+
return protoV5ProviderFactoriesInit(ProviderName, ProviderNameAlternate)
152+
}
153+
154+
// ProtoV5FactoriesAlternateAccountAndAlternateRegion creates ProtoV5ProviderFactories for cross-account and cross-region configurations
171155
//
172156
// Usage typically paired with PreCheckMultipleRegion, PreCheckAlternateAccount,
173157
// and ConfigAlternateAccountAndAlternateRegionProvider.
174-
func FactoriesAlternateAccountAndAlternateRegion(providers *[]*schema.Provider) map[string]func() (*schema.Provider, error) {
175-
return factoriesInit(providers, []string{
158+
func ProtoV5FactoriesAlternateAccountAndAlternateRegion(t *testing.T) map[string]func() (tfprotov5.ProviderServer, error) {
159+
return protoV5ProviderFactoriesInit(
176160
ProviderName,
177161
ProviderNameAlternateAccountAlternateRegion,
178162
ProviderNameAlternateAccountSameRegion,
179163
ProviderNameSameAccountAlternateRegion,
180-
})
164+
)
181165
}
182166

183-
// FactoriesMultipleRegion creates ProviderFactories for the number of region configurations
167+
// ProtoV5FactoriesMultipleRegions creates ProtoV5ProviderFactories for the specified number of region configurations
184168
//
185169
// Usage typically paired with PreCheckMultipleRegion and ConfigMultipleRegionProvider.
186-
func FactoriesMultipleRegion(providers *[]*schema.Provider, regions int) map[string]func() (*schema.Provider, error) {
187-
providerNames := []string{
188-
ProviderName,
189-
ProviderNameAlternate,
190-
}
191-
192-
if regions >= 3 {
193-
providerNames = append(providerNames, ProviderNameThird)
170+
func ProtoV5FactoriesMultipleRegions(t *testing.T, n int) map[string]func() (tfprotov5.ProviderServer, error) {
171+
switch n {
172+
case 2:
173+
return protoV5ProviderFactoriesInit(ProviderName, ProviderNameAlternate)
174+
case 3:
175+
return protoV5ProviderFactoriesInit(ProviderName, ProviderNameAlternate, ProviderNameThird)
176+
default:
177+
t.Fatalf("invalid number of Region configurations: %d", n)
194178
}
195179

196-
return factoriesInit(providers, providerNames)
180+
return nil
197181
}
198182

199183
// PreCheck verifies and sets required provider testing configuration
@@ -1214,15 +1198,6 @@ provider "aws" {
12141198
`, os.Getenv(conns.EnvVarAccAssumeRoleARN), policy)
12151199
}
12161200

1217-
const testAccProviderConfig_assumeRoleEmpty = `
1218-
provider "aws" {
1219-
assume_role {
1220-
}
1221-
}
1222-
1223-
data "aws_caller_identity" "current" {}
1224-
` //lintignore:AT004
1225-
12261201
const testAccProviderConfigBase = `
12271202
data "aws_partition" "provider_test" {}
12281203

0 commit comments

Comments
 (0)