Skip to content

Commit bc7b1c9

Browse files
authored
Merge pull request #357 from hashicorp/auto-configure-providers-acctest-v2
v2: binary acctesting - auto-Configure providers
2 parents d5f3a05 + aa6793d commit bc7b1c9

File tree

7 files changed

+55
-39
lines changed

7 files changed

+55
-39
lines changed

.go-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.14.0
1+
1.14.2

helper/resource/testing.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package resource
22

33
import (
4+
"context"
45
"errors"
56
"flag"
67
"fmt"
@@ -20,6 +21,7 @@ import (
2021
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
2122
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2223
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/addrs"
24+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/diagutils"
2325
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
2426
)
2527

@@ -539,11 +541,6 @@ func Test(t TestT, c TestCase) {
539541
t.Fatal("Acceptance tests must be run with the -v flag on tests")
540542
}
541543

542-
// Run the PreCheck if we have it
543-
if c.PreCheck != nil {
544-
c.PreCheck()
545-
}
546-
547544
// get instances of all providers, so we can use the individual
548545
// resources to shim the state during the tests.
549546
providers := make(map[string]*schema.Provider)
@@ -558,6 +555,21 @@ func Test(t TestT, c TestCase) {
558555
providers[name] = p
559556
}
560557

558+
// Auto-configure all providers.
559+
for _, p := range providers {
560+
diags := p.Configure(context.Background(), terraform.NewResourceConfigRaw(nil))
561+
if diags.HasError() {
562+
t.Fatal("error configuring provider: %s", diagutils.ErrorDiags(diags))
563+
}
564+
}
565+
566+
// Run the PreCheck if we have it.
567+
// This is done after the auto-configure to allow providers
568+
// to override the default auto-configure parameters.
569+
if c.PreCheck != nil {
570+
c.PreCheck()
571+
}
572+
561573
if acctest.TestHelper == nil {
562574
t.Fatal("Please configure the acctest binary driver")
563575
}

helper/schema/provider_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1515
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/configschema"
16+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/diagutils"
1617
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1718
)
1819

@@ -180,7 +181,7 @@ func TestProviderConfigure(t *testing.T) {
180181
c := terraform.NewResourceConfigRaw(tc.Config)
181182
diags := tc.P.Configure(context.Background(), c)
182183
if diags.HasError() != tc.Err {
183-
t.Fatalf("%d: %s", i, errorDiags(diags))
184+
t.Fatalf("%d: %s", i, diagutils.ErrorDiags(diags))
184185
}
185186
}
186187
}

helper/schema/resource_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1515
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/hcl2shim"
16+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/diagutils"
1617
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1718
)
1819

@@ -46,7 +47,7 @@ func TestResourceApply_create(t *testing.T) {
4647

4748
actual, diags := r.Apply(context.Background(), s, d, nil)
4849
if diags.HasError() {
49-
t.Fatalf("err: %s", errorDiags(diags))
50+
t.Fatalf("err: %s", diagutils.ErrorDiags(diags))
5051
}
5152

5253
if !called {
@@ -114,7 +115,7 @@ func TestResourceApply_Timeout_state(t *testing.T) {
114115

115116
actual, diags := r.Apply(context.Background(), s, d, nil)
116117
if diags.HasError() {
117-
t.Fatalf("err: %s", errorDiags(diags))
118+
t.Fatalf("err: %s", diagutils.ErrorDiags(diags))
118119
}
119120

120121
if !called {
@@ -180,7 +181,7 @@ func TestResourceApply_Timeout_destroy(t *testing.T) {
180181

181182
actual, diags := r.Apply(context.Background(), s, d, nil)
182183
if diags.HasError() {
183-
t.Fatalf("err: %s", errorDiags(diags))
184+
t.Fatalf("err: %s", diagutils.ErrorDiags(diags))
184185
}
185186

186187
if !called {
@@ -315,7 +316,7 @@ func TestResourceApply_destroy(t *testing.T) {
315316

316317
actual, diags := r.Apply(context.Background(), s, d, nil)
317318
if diags.HasError() {
318-
t.Fatalf("err: %s", errorDiags(diags))
319+
t.Fatalf("err: %s", diagutils.ErrorDiags(diags))
319320
}
320321

321322
if !called {
@@ -377,7 +378,7 @@ func TestResourceApply_destroyCreate(t *testing.T) {
377378

378379
actual, diags := r.Apply(context.Background(), s, d, nil)
379380
if diags.HasError() {
380-
t.Fatalf("err: %s", errorDiags(diags))
381+
t.Fatalf("err: %s", diagutils.ErrorDiags(diags))
381382
}
382383

383384
if !change {
@@ -479,7 +480,7 @@ func TestResourceApply_update(t *testing.T) {
479480

480481
actual, diags := r.Apply(context.Background(), s, d, nil)
481482
if diags.HasError() {
482-
t.Fatalf("err: %s", errorDiags(diags))
483+
t.Fatalf("err: %s", diagutils.ErrorDiags(diags))
483484
}
484485

485486
expected := &terraform.InstanceState{
@@ -576,7 +577,7 @@ func TestResourceApply_isNewResource(t *testing.T) {
576577

577578
actual, diags := r.Apply(context.Background(), s, d, nil)
578579
if diags.HasError() {
579-
t.Fatalf("err: %s", errorDiags(diags))
580+
t.Fatalf("err: %s", diagutils.ErrorDiags(diags))
580581
}
581582

582583
expected := &terraform.InstanceState{
@@ -603,7 +604,7 @@ func TestResourceApply_isNewResource(t *testing.T) {
603604

604605
actual, diags = r.Apply(context.Background(), s, d, nil)
605606
if diags.HasError() {
606-
t.Fatalf("err: %s", errorDiags(diags))
607+
t.Fatalf("err: %s", diagutils.ErrorDiags(diags))
607608
}
608609

609610
expected = &terraform.InstanceState{
@@ -987,7 +988,7 @@ func TestResourceRefresh(t *testing.T) {
987988

988989
actual, diags := r.RefreshWithoutUpgrade(context.Background(), s, 42)
989990
if diags.HasError() {
990-
t.Fatalf("err: %s", errorDiags(diags))
991+
t.Fatalf("err: %s", diagutils.ErrorDiags(diags))
991992
}
992993

993994
if !reflect.DeepEqual(actual, expected) {
@@ -1017,7 +1018,7 @@ func TestResourceRefresh_blankId(t *testing.T) {
10171018

10181019
actual, diags := r.RefreshWithoutUpgrade(context.Background(), s, 42)
10191020
if diags.HasError() {
1020-
t.Fatalf("err: %s", errorDiags(diags))
1021+
t.Fatalf("err: %s", diagutils.ErrorDiags(diags))
10211022
}
10221023
if actual != nil {
10231024
t.Fatalf("bad: %#v", actual)
@@ -1048,7 +1049,7 @@ func TestResourceRefresh_delete(t *testing.T) {
10481049

10491050
actual, diags := r.RefreshWithoutUpgrade(context.Background(), s, 42)
10501051
if diags.HasError() {
1051-
t.Fatalf("err: %s", errorDiags(diags))
1052+
t.Fatalf("err: %s", diagutils.ErrorDiags(diags))
10521053
}
10531054

10541055
if actual != nil {
@@ -1117,7 +1118,7 @@ func TestResourceRefresh_noExists(t *testing.T) {
11171118

11181119
actual, diags := r.RefreshWithoutUpgrade(context.Background(), s, 42)
11191120
if diags.HasError() {
1120-
t.Fatalf("err: %s", errorDiags(diags))
1121+
t.Fatalf("err: %s", diagutils.ErrorDiags(diags))
11211122
}
11221123
if actual != nil {
11231124
t.Fatalf("should have no state")
@@ -1416,7 +1417,7 @@ func TestResource_ContextTimeout(t *testing.T) {
14161417
}
14171418

14181419
if _, diags := r.Apply(context.Background(), s, d, nil); diags.HasError() {
1419-
t.Fatalf("err: %s", errorDiags(diags))
1420+
t.Fatalf("err: %s", diagutils.ErrorDiags(diags))
14201421
}
14211422

14221423
if !deadlineSet {

helper/schema/schema_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1717
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/hashcode"
1818
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/hcl2shim"
19+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/diagutils"
1920
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
2021
)
2122

@@ -5197,19 +5198,19 @@ func TestSchemaMap_Validate(t *testing.T) {
51975198
t.Errorf("%q: no errors", tn)
51985199
}
51995200

5200-
for _, e := range errorDiags(diags).Errors() {
5201+
for _, e := range diagutils.ErrorDiags(diags).Errors() {
52015202
t.Errorf("%q: err: %s", tn, e)
52025203
}
52035204

52045205
t.FailNow()
52055206
}
52065207

5207-
ws := warningDiags(diags).Warnings()
5208+
ws := diagutils.WarningDiags(diags).Warnings()
52085209
if !reflect.DeepEqual(ws, tc.Warnings) {
52095210
t.Fatalf("%q: warnings:\n\nexpected: %#v\ngot:%#v", tn, tc.Warnings, ws)
52105211
}
52115212

5212-
es := errorDiags(diags).Errors()
5213+
es := diagutils.ErrorDiags(diags).Errors()
52135214
if tc.Errors != nil {
52145215
sort.Sort(errorSort(es))
52155216
sort.Sort(errorSort(tc.Errors))
@@ -5308,14 +5309,14 @@ func TestSchemaSet_ValidateMaxItems(t *testing.T) {
53085309
t.Errorf("%q: no errors", tn)
53095310
}
53105311

5311-
for _, e := range errorDiags(diags).Errors() {
5312+
for _, e := range diagutils.ErrorDiags(diags).Errors() {
53125313
t.Errorf("%q: err: %s", tn, e)
53135314
}
53145315

53155316
t.FailNow()
53165317
}
53175318

5318-
es := errorDiags(diags).Errors()
5319+
es := diagutils.ErrorDiags(diags).Errors()
53195320
if tc.Errors != nil {
53205321
if !errorEquals(es, tc.Errors) {
53215322
t.Fatalf("%q: expected: %q\ngot: %q", tn, tc.Errors, es)
@@ -5397,14 +5398,14 @@ func TestSchemaSet_ValidateMinItems(t *testing.T) {
53975398
t.Errorf("%q: no errors", tn)
53985399
}
53995400

5400-
for _, e := range errorDiags(diags).Errors() {
5401+
for _, e := range diagutils.ErrorDiags(diags).Errors() {
54015402
t.Errorf("%q: err: %s", tn, e)
54025403
}
54035404

54045405
t.FailNow()
54055406
}
54065407

5407-
es := errorDiags(diags).Errors()
5408+
es := diagutils.ErrorDiags(diags).Errors()
54085409
if tc.Errors != nil {
54095410
if !errorEquals(es, tc.Errors) {
54105411
t.Fatalf("%q: expected: %q\ngot: %q", tn, tc.Errors, es)
@@ -6482,7 +6483,7 @@ func TestValidateAtLeastOneOfAttributes(t *testing.T) {
64826483
t.Fatalf("expected error")
64836484
}
64846485

6485-
for _, e := range errorDiags(diags).Errors() {
6486+
for _, e := range diagutils.ErrorDiags(diags).Errors() {
64866487
t.Fatalf("didn't expect error, got error: %+v", e)
64876488
}
64886489

@@ -6792,7 +6793,7 @@ func TestValidateRequiredWithAttributes(t *testing.T) {
67926793
t.Run(tn, func(t *testing.T) {
67936794
c := terraform.NewResourceConfigRaw(tc.Config)
67946795
diags := schemaMap(tc.Schema).Validate(c)
6795-
es := errorDiags(diags).Errors()
6796+
es := diagutils.ErrorDiags(diags).Errors()
67966797
if len(es) > 0 != tc.Err {
67976798
if len(es) == 0 {
67986799
t.Fatalf("expected error")

helper/schema/shims_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/hashcode"
1818
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/configschema"
1919
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/hcl2shim"
20+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/diagutils"
2021
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/providers"
2122
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/tfdiags"
2223
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
@@ -151,7 +152,7 @@ func TestShimResourceApply_create(t *testing.T) {
151152

152153
actual, diags := r.Apply(context.Background(), s, d, nil)
153154
if diags.HasError() {
154-
t.Fatalf("err: %s", errorDiags(diags))
155+
t.Fatalf("err: %s", diagutils.ErrorDiags(diags))
155156
}
156157

157158
if !called {
@@ -230,7 +231,7 @@ func TestShimResourceApply_Timeout_state(t *testing.T) {
230231

231232
actual, diags := r.Apply(context.Background(), s, d, nil)
232233
if diags.HasError() {
233-
t.Fatalf("err: %s", errorDiags(diags))
234+
t.Fatalf("err: %s", diagutils.ErrorDiags(diags))
234235
}
235236

236237
if !called {
@@ -381,7 +382,7 @@ func TestShimResourceApply_destroy(t *testing.T) {
381382

382383
actual, diags := r.Apply(context.Background(), s, d, nil)
383384
if diags.HasError() {
384-
t.Fatalf("err: %s", errorDiags(diags))
385+
t.Fatalf("err: %s", diagutils.ErrorDiags(diags))
385386
}
386387

387388
if !called {
@@ -453,7 +454,7 @@ func TestShimResourceApply_destroyCreate(t *testing.T) {
453454

454455
actual, diags := r.Apply(context.Background(), s, d, nil)
455456
if diags.HasError() {
456-
t.Fatalf("err: %s", errorDiags(diags))
457+
t.Fatalf("err: %s", diagutils.ErrorDiags(diags))
457458
}
458459

459460
if !change {

helper/schema/diagnostic_test.go renamed to internal/diagutils/diagutils.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package schema
1+
package diagutils
22

33
import (
44
"errors"
@@ -8,9 +8,9 @@ import (
88
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
99
)
1010

11-
type errorDiags diag.Diagnostics
11+
type ErrorDiags diag.Diagnostics
1212

13-
func (diags errorDiags) Errors() []error {
13+
func (diags ErrorDiags) Errors() []error {
1414
var es []error
1515
for i := range diags {
1616
if diags[i].Severity == diag.Error {
@@ -24,13 +24,13 @@ func (diags errorDiags) Errors() []error {
2424
return es
2525
}
2626

27-
func (diags errorDiags) Error() string {
27+
func (diags ErrorDiags) Error() string {
2828
return multierror.ListFormatFunc(diags.Errors())
2929
}
3030

31-
type warningDiags diag.Diagnostics
31+
type WarningDiags diag.Diagnostics
3232

33-
func (diags warningDiags) Warnings() []string {
33+
func (diags WarningDiags) Warnings() []string {
3434
var ws []string
3535
for i := range diags {
3636
if diags[i].Severity == diag.Warning {

0 commit comments

Comments
 (0)