From ff261307ba7797a5d46394ff81a23d10d4c583bf Mon Sep 17 00:00:00 2001 From: Baraa Basata Date: Tue, 15 Apr 2025 15:59:21 -0400 Subject: [PATCH 1/2] helper/resource: refine config-building for plannable import tests This change allows explict inline Terraform configuration in an `ImportState` test step. In a newly-added test, we have a scenario where it's useful to the test author to write their own import block, without resorting to `ConfigFile` or `ConfigDirectory`. This change also adds a test to cover the use of external providers with plannable import tests. --- .../importstate/import_block_with_id_test.go | 50 +++++++++++++++++++ helper/resource/testing_new_import_state.go | 9 ++-- 2 files changed, 53 insertions(+), 6 deletions(-) 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..7e106116 100644 --- a/helper/resource/testing_new_import_state.go +++ b/helper/resource/testing_new_import_state.go @@ -110,12 +110,9 @@ 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 - } + 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) From 5fe6cf7e25ebea9b095aa43cf2ecad583ec6435c Mon Sep 17 00:00:00 2001 From: Baraa Basata Date: Tue, 15 Apr 2025 16:04:59 -0400 Subject: [PATCH 2/2] fixup! helper/resource: refine config-building for plannable import tests --- helper/resource/testing_new_import_state.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helper/resource/testing_new_import_state.go b/helper/resource/testing_new_import_state.go index 7e106116..4d315fab 100644 --- a/helper/resource/testing_new_import_state.go +++ b/helper/resource/testing_new_import_state.go @@ -109,7 +109,7 @@ 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 + // 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