Skip to content

Commit 2fd4c09

Browse files
authored
helper/resource: refine config-building for plannable import tests (#484)
* 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. * fixup! helper/resource: refine config-building for plannable import tests
1 parent bfd5be7 commit 2fd4c09

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

helper/resource/importstate/import_block_with_id_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import (
88
"regexp"
99
"testing"
1010

11+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
12+
"github.com/hashicorp/terraform-plugin-testing/plancheck"
1113
"github.com/hashicorp/terraform-plugin-testing/terraform"
14+
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
1215

1316
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
1417
"github.com/hashicorp/terraform-plugin-go/tftypes"
@@ -376,3 +379,50 @@ func TestImportBlock_WithID_WithBlankComputedAttribute_GeneratesCorrectPlan(t *t
376379
},
377380
})
378381
}
382+
383+
func TestImportBlock_WithID_WithExternalProvider(t *testing.T) {
384+
t.Parallel()
385+
386+
config := `
387+
resource "random_string" "mystery_message" {
388+
length = 31
389+
}
390+
`
391+
392+
configWithImportBlock := config + `
393+
import {
394+
to = random_string.mystery_message
395+
id = "It was a dark and stormy night."
396+
}
397+
`
398+
399+
r.UnitTest(t, r.TestCase{
400+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
401+
tfversion.SkipBelow(tfversion.Version1_5_0), // ImportBlockWithID requires Terraform 1.5.0 or later
402+
},
403+
ExternalProviders: map[string]r.ExternalProvider{
404+
"random": {
405+
Source: "hashicorp/random",
406+
},
407+
},
408+
Steps: []r.TestStep{
409+
{
410+
Config: config,
411+
},
412+
{
413+
ImportState: true,
414+
ImportStateKind: r.ImportBlockWithID,
415+
Config: configWithImportBlock,
416+
ResourceName: "random_string.mystery_message",
417+
ImportPlanChecks: r.ImportPlanChecks{
418+
PreApply: []plancheck.PlanCheck{
419+
plancheck.ExpectKnownValue(
420+
"random_string.mystery_message",
421+
tfjsonpath.New("result"),
422+
knownvalue.StringExact("It was a dark and stormy night.")),
423+
},
424+
},
425+
},
426+
},
427+
})
428+
}

helper/resource/testing_new_import_state.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,10 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
109109

110110
logging.HelperResourceTrace(ctx, fmt.Sprintf("Using import identifier: %s", importId))
111111

112-
// Append to previous step config unless using ConfigFile or ConfigDirectory
113-
if testStepConfig == nil || step.Config != "" {
114-
importConfig := step.Config
115-
if importConfig == "" {
116-
logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import")
117-
importConfig = cfgRaw
118-
}
112+
// Append to previous step config unless using explicit inline Config, or ConfigFile, or ConfigDirectory
113+
if testStepConfig == nil && step.ConfigFile == nil && step.ConfigDirectory == nil {
114+
logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import")
115+
importConfig := cfgRaw
119116

120117
if kind.plannable() {
121118
importConfig = appendImportBlock(importConfig, resourceName, importId)

0 commit comments

Comments
 (0)