Skip to content

Commit db02baa

Browse files
authored
helper/resource: Only call final Terraform CLI show command each TestStep when TestCase.IDRefreshName is set (#892)
Reference: #730 Also clarifies the Go documentation for TestCase type IDRefreshName and IDRefreshIgnore fields.
1 parent fe75bb1 commit db02baa

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

.changelog/892.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
helper/resource: Removed extraneous Terraform CLI `show` command each `TestStep` unless using `TestCase.IDRefreshName`
3+
```

helper/resource/testing.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -376,16 +376,18 @@ type TestCase struct {
376376
// same state. Each step can have its own check to verify correctness.
377377
Steps []TestStep
378378

379-
// The settings below control the "ID-only refresh test." This is
380-
// an enabled-by-default test that tests that a refresh can be
381-
// refreshed with only an ID to result in the same attributes.
382-
// This validates completeness of Refresh.
379+
// IDRefreshName is the name of the resource to check during ID-only
380+
// refresh testing, which ensures that a resource can be refreshed solely
381+
// by its identifier. This will default to the first non-nil primary
382+
// resource in the state. It runs every TestStep.
383383
//
384-
// IDRefreshName is the name of the resource to check. This will
385-
// default to the first non-nil primary resource in the state.
386-
//
387-
// IDRefreshIgnore is a list of configuration keys that will be ignored.
388-
IDRefreshName string
384+
// While not deprecated, most resource tests should instead prefer using
385+
// TestStep.ImportState based testing as it works with multiple attribute
386+
// identifiers and also verifies resource import functionality.
387+
IDRefreshName string
388+
389+
// IDRefreshIgnore is a list of configuration keys that will be ignored
390+
// during ID-only refresh testing.
389391
IDRefreshIgnore []string
390392
}
391393

helper/resource/testing_new_config.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import (
1616
func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugintest.WorkingDir, step TestStep) error {
1717
t.Helper()
1818

19-
var idRefreshCheck *terraform.ResourceState
20-
idRefresh := c.IDRefreshName != ""
21-
2219
if !step.Destroy {
2320
var state *terraform.State
2421
var err error
@@ -249,22 +246,31 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint
249246
// ID-ONLY REFRESH
250247
// If we've never checked an id-only refresh and our state isn't
251248
// empty, find the first resource and test it.
252-
var state *terraform.State
253-
err = runProviderCommand(ctx, t, func() error {
254-
state, err = getState(ctx, t, wd)
249+
if c.IDRefreshName != "" {
250+
logging.HelperResourceTrace(ctx, "Using TestCase IDRefreshName")
251+
252+
var state *terraform.State
253+
254+
err = runProviderCommand(ctx, t, func() error {
255+
state, err = getState(ctx, t, wd)
256+
if err != nil {
257+
return err
258+
}
259+
return nil
260+
}, wd, providerFactories{
261+
legacy: c.ProviderFactories,
262+
protov5: c.ProtoV5ProviderFactories,
263+
protov6: c.ProtoV6ProviderFactories})
264+
255265
if err != nil {
256266
return err
257267
}
258-
return nil
259-
}, wd, providerFactories{
260-
legacy: c.ProviderFactories,
261-
protov5: c.ProtoV5ProviderFactories,
262-
protov6: c.ProtoV6ProviderFactories})
263-
if err != nil {
264-
return err
265-
}
266-
if idRefresh && idRefreshCheck == nil && !state.Empty() {
267-
logging.HelperResourceTrace(ctx, "Using TestCase IDRefreshName")
268+
269+
if state.Empty() {
270+
return nil
271+
}
272+
273+
var idRefreshCheck *terraform.ResourceState
268274

269275
// Find the first non-nil resource in the state
270276
for _, m := range state.Modules {

0 commit comments

Comments
 (0)