diff --git a/helper/resource/importstate/import_block_with_id_test.go b/helper/resource/importstate/import_block_with_id_test.go index 150c84d3..da552fef 100644 --- a/helper/resource/importstate/import_block_with_id_test.go +++ b/helper/resource/importstate/import_block_with_id_test.go @@ -8,7 +8,10 @@ import ( "regexp" "testing" + "github.com/hashicorp/terraform-plugin-testing/knownvalue" + "github.com/hashicorp/terraform-plugin-testing/plancheck" "github.com/hashicorp/terraform-plugin-testing/terraform" + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" "github.com/hashicorp/terraform-plugin-go/tfprotov6" "github.com/hashicorp/terraform-plugin-go/tftypes" @@ -376,3 +379,50 @@ func TestImportBlock_WithID_WithBlankComputedAttribute_GeneratesCorrectPlan(t *t }, }) } + +func TestImportBlock_WithID_WithExternalProvider(t *testing.T) { + t.Parallel() + + config := ` +resource "random_string" "mystery_message" { + length = 31 +} +` + + configWithImportBlock := config + ` +import { + to = random_string.mystery_message + id = "It was a dark and stormy night." +} +` + + r.UnitTest(t, r.TestCase{ + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later + }, + ExternalProviders: map[string]r.ExternalProvider{ + "random": { + Source: "hashicorp/random", + }, + }, + Steps: []r.TestStep{ + { + Config: config, + }, + { + ImportState: true, + ImportStateKind: r.ImportBlockWithID, + Config: configWithImportBlock, + ResourceName: "random_string.mystery_message", + ImportPlanChecks: r.ImportPlanChecks{ + PreApply: []plancheck.PlanCheck{ + plancheck.ExpectKnownValue( + "random_string.mystery_message", + tfjsonpath.New("result"), + knownvalue.StringExact("It was a dark and stormy night.")), + }, + }, + }, + }, + }) +} diff --git a/helper/resource/testing_new_import_state.go b/helper/resource/testing_new_import_state.go index dc3b06ed..4d315fab 100644 --- a/helper/resource/testing_new_import_state.go +++ b/helper/resource/testing_new_import_state.go @@ -109,13 +109,10 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest logging.HelperResourceTrace(ctx, fmt.Sprintf("Using import identifier: %s", importId)) - // Append to previous step config unless using ConfigFile or ConfigDirectory - if testStepConfig == nil || step.Config != "" { - importConfig := step.Config - if importConfig == "" { - logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import") - importConfig = cfgRaw - } + // Append to previous step config unless using explicit inline Config, or ConfigFile, or ConfigDirectory + if testStepConfig == nil && step.ConfigFile == nil && step.ConfigDirectory == nil { + logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import") + importConfig := cfgRaw if kind.plannable() { importConfig = appendImportBlock(importConfig, resourceName, importId)