Skip to content

Commit 3134be2

Browse files
committed
Extract functions
1 parent 3b822d6 commit 3134be2

File tree

1 file changed

+52
-23
lines changed

1 file changed

+52
-23
lines changed

helper/resource/testing_new_import_state.go

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"reflect"
1010
"strings"
1111

12+
"github.com/hashicorp/go-version"
13+
1214
tfjson "github.com/hashicorp/terraform-json"
1315

1416
"github.com/google/go-cmp/cmp"
@@ -20,6 +22,7 @@ import (
2022
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
2123
"github.com/hashicorp/terraform-plugin-testing/internal/teststep"
2224
"github.com/hashicorp/terraform-plugin-testing/terraform"
25+
"github.com/hashicorp/terraform-plugin-testing/tfversion"
2326
)
2427

2528
func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest.Helper, wd *plugintest.WorkingDir, step TestStep, cfgRaw string, providers *providerFactories, stepNumber int) error {
@@ -242,29 +245,7 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
242245
// Go through the imported state and verify
243246
if step.ImportStateCheck != nil {
244247
logging.HelperResourceTrace(ctx, "Using TestStep ImportStateCheck")
245-
246-
var states []*terraform.InstanceState
247-
for address, r := range importState.RootModule().Resources {
248-
if strings.HasPrefix(address, "data.") {
249-
continue
250-
}
251-
252-
if r.Primary == nil {
253-
continue
254-
}
255-
256-
is := r.Primary.DeepCopy() //nolint:staticcheck // legacy usage
257-
is.Ephemeral.Type = r.Type // otherwise the check function cannot see the type
258-
states = append(states, is)
259-
}
260-
261-
logging.HelperResourceDebug(ctx, "Calling TestStep ImportStateCheck")
262-
263-
if err := step.ImportStateCheck(states); err != nil {
264-
t.Fatal(err)
265-
}
266-
267-
logging.HelperResourceDebug(ctx, "Called TestStep ImportStateCheck")
248+
runImportStateCheckFunction(ctx, t, importState, step)
268249
}
269250

270251
// Verify that all the states match
@@ -407,3 +388,51 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
407388

408389
return nil
409390
}
391+
392+
func appendImportBlock(config string, resourceName string, importID string) string {
393+
return config + fmt.Sprintf(``+"\n"+
394+
`import {`+"\n"+
395+
` to = %s`+"\n"+
396+
` id = %q`+"\n"+
397+
`}`,
398+
resourceName, importID)
399+
}
400+
401+
func requirePlannableImport(t testing.T, versionUnderTest version.Version) error {
402+
t.Helper()
403+
404+
if versionUnderTest.LessThan(tfversion.Version1_5_0) {
405+
return fmt.Errorf(
406+
`ImportState steps using plannable import blocks require Terraform 1.5.0 or later. Either ` +
407+
`upgrade the Terraform version running the test or add a ` + "`TerraformVersionChecks`" + ` to ` +
408+
`the test case to skip this test.` + "\n\n" +
409+
`https://developer.hashicorp.com/terraform/plugin/testing/acceptance-tests/tfversion-checks#skip-version-checks`)
410+
}
411+
412+
return nil
413+
}
414+
415+
func runImportStateCheckFunction(ctx context.Context, t testing.T, importState *terraform.State, step TestStep) {
416+
var states []*terraform.InstanceState
417+
for address, r := range importState.RootModule().Resources {
418+
if strings.HasPrefix(address, "data.") {
419+
continue
420+
}
421+
422+
if r.Primary == nil {
423+
continue
424+
}
425+
426+
is := r.Primary.DeepCopy() //nolint:staticcheck // legacy usage
427+
is.Ephemeral.Type = r.Type // otherwise the check function cannot see the type
428+
states = append(states, is)
429+
}
430+
431+
logging.HelperResourceTrace(ctx, "Calling TestStep ImportStateCheck")
432+
433+
if err := step.ImportStateCheck(states); err != nil {
434+
t.Fatal(err)
435+
}
436+
437+
logging.HelperResourceTrace(ctx, "Called TestStep ImportStateCheck")
438+
}

0 commit comments

Comments
 (0)