Skip to content

Commit 87c1654

Browse files
committed
add validation to prevent new import kinds to be used with config files or directories
1 parent 218cb61 commit 87c1654

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

helper/resource/testing_new_import_block_id_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func TestTest_TestStep_ImportBlockId_SkipDataSourceState(t *testing.T) {
214214
func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore_Real_Example(t *testing.T) {
215215
/*
216216
This test tries to imitate a real world example of behaviour we often see in the AzureRM provider which requires
217-
the use of `ImportStateVerifyIgnore` when testing the import of a resource.
217+
the use of `ImportStateVerifyIgnore` when testing the import of a resource using the import command.
218218
219219
A sensitive field e.g. a password can be supplied on create but isn't returned in the API response on a subsequent
220220
read, resulting in a different value for password in the two states.
@@ -297,7 +297,7 @@ func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore_Real_Example(t *tes
297297
state.
298298
299299
Modifying the plan to set the id to a known value appears to be the only way to
300-
circumvent this behaviour, the cause of which I don't fully understand
300+
circumvent this behaviour, the cause of which I don't fully understand.
301301
302302
This doesn't seem great, because this gets applied to all Plans that happen in this
303303
test - so we're modifying plans in steps that we might not want to.
@@ -351,18 +351,13 @@ func TestTest_TestStep_ImportBlockId_ImportStateVerifyIgnore_Real_Example(t *tes
351351
required_providers {
352352
examplecloud = {
353353
source = "registry.terraform.io/hashicorp/examplecloud"
354-
}
354+
}
355355
}
356356
}
357357
358358
resource "examplecloud_container" "test" {
359359
name = "somename"
360-
}
361-
362-
import {
363-
to = examplecloud_container.test
364-
id = "sometestid"
365-
}`,
360+
}`,
366361
ResourceName: "examplecloud_container.test",
367362
ImportState: true,
368363
ImportStateKind: ImportBlockWithId,

helper/resource/testing_new_import_state.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ import (
2323
func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest.Helper, wd *plugintest.WorkingDir, step TestStep, cfgRaw string, providers *providerFactories, stepNumber int) error {
2424
t.Helper()
2525

26+
// Currently import modes `ImportBlockWithId` and `ImportBlockWithResourceIdentity` cannot support config file or directory
27+
// since these modes append the import block to the configuration automatically
28+
if step.ImportStateKind != ImportCommandWithId {
29+
if step.ConfigFile != nil || step.ConfigDirectory != nil {
30+
t.Fatalf("ImportStateKind %q is not supported for config file or directory", step.ImportStateKind)
31+
}
32+
}
33+
2634
configRequest := teststep.PrepareConfigurationRequest{
2735
Directory: step.ConfigDirectory,
2836
File: step.ConfigFile,
@@ -93,15 +101,19 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
93101

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

96-
// Prepare the test config dependent on the kind of import test being performed
97-
if testStepConfig == nil {
98-
logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import")
104+
if testStepConfig == nil || step.Config != "" {
105+
importConfig := step.Config
106+
if importConfig == "" {
107+
logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import")
108+
importConfig = cfgRaw
109+
}
99110

111+
// Update the test config dependent on the kind of import test being performed
100112
switch step.ImportStateKind {
101113
case ImportBlockWithResourceIdentity:
102114
t.Fatalf("TODO implement me")
103115
case ImportBlockWithId:
104-
cfgRaw += fmt.Sprintf(`
116+
importConfig += fmt.Sprintf(`
105117
import {
106118
to = %s
107119
id = %q

0 commit comments

Comments
 (0)