Skip to content

Commit 2d87e3d

Browse files
authored
importstate: Fixed bug where prior test config is not used for ConfigFile or ConfigDirectory (#522)
* test config fix * add changelog * swap the statements around * adjusted changelog
1 parent baee5c6 commit 2d87e3d

File tree

6 files changed

+88
-16
lines changed

6 files changed

+88
-16
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: BUG FIXES
2+
body: 'helper/resource: Fixed bug with import state mode where prior test config is not used for `ConfigFile` or `ConfigDirectory`'
3+
time: 2025-06-06T11:04:44.925152-04:00
4+
custom:
5+
Issue: "516"

helper/resource/importstate/import_block_in_config_directory_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ func TestImportBlock_InConfigDirectory(t *testing.T) {
3838
ResourceName: "examplecloud_container.test",
3939
ImportState: true,
4040
ImportStateKind: r.ImportBlockWithID,
41-
ConfigDirectory: config.StaticDirectory(`testdata/2`),
4241
},
4342
},
4443
})

helper/resource/importstate/import_block_in_config_file_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ func TestImportBlock_InConfigFile(t *testing.T) {
3838
ResourceName: "examplecloud_container.test",
3939
ImportState: true,
4040
ImportStateKind: r.ImportBlockWithID,
41-
ConfigFile: config.StaticFile(`testdata/2/examplecloud_container.tf`),
4241
},
4342
},
4443
})

helper/resource/importstate/import_command_with_id_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
1212
"github.com/hashicorp/terraform-plugin-go/tftypes"
1313

14+
"github.com/hashicorp/terraform-plugin-testing/config"
1415
r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
1516
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
1617
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/datasource"
@@ -197,6 +198,60 @@ func TestImportCommand_ImportStateVerify(t *testing.T) {
197198
})
198199
}
199200

201+
func TestImportCommand_InConfigFile(t *testing.T) {
202+
t.Parallel()
203+
204+
r.UnitTest(t, r.TestCase{
205+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
206+
tfversion.SkipBelow(tfversion.Version1_0_0), // ProtoV6ProviderFactories
207+
},
208+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
209+
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
210+
Resources: map[string]testprovider.Resource{
211+
"examplecloud_container": examplecloudResource(),
212+
},
213+
}),
214+
},
215+
Steps: []r.TestStep{
216+
{
217+
ConfigFile: config.StaticFile(`testdata/1/examplecloud_container.tf`),
218+
},
219+
{
220+
ResourceName: "examplecloud_container.test",
221+
ImportState: true,
222+
ImportStateVerify: true,
223+
},
224+
},
225+
})
226+
}
227+
228+
func TestImportCommand_InConfigDirectory(t *testing.T) {
229+
t.Parallel()
230+
231+
r.UnitTest(t, r.TestCase{
232+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
233+
tfversion.SkipBelow(tfversion.Version1_0_0), // ProtoV6ProviderFactories
234+
},
235+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
236+
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
237+
Resources: map[string]testprovider.Resource{
238+
"examplecloud_container": examplecloudResource(),
239+
},
240+
}),
241+
},
242+
Steps: []r.TestStep{
243+
{
244+
ConfigDirectory: config.StaticDirectory(`testdata/1`),
245+
},
246+
{
247+
ResourceName: "examplecloud_container.test",
248+
ImportState: true,
249+
ImportStateVerify: true,
250+
},
251+
},
252+
})
253+
}
254+
200255
func TestImportCommand_ImportStateVerify_Ignore(t *testing.T) {
201256
t.Parallel()
202257

helper/resource/testing_new.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
131131

132132
// use this to track last step successfully applied
133133
// acts as default for import tests
134-
var appliedCfg string
134+
var appliedCfg teststep.Config
135135
var stepNumber int
136136

137137
for stepIndex, step := range c.Steps {
@@ -418,7 +418,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
418418
}
419419
}
420420

421-
appliedCfg, err = step.mergedConfig(ctx, c, hasTerraformBlock, hasProviderBlock, helper.TerraformVersion())
421+
mergedConfig, err := step.mergedConfig(ctx, c, hasTerraformBlock, hasProviderBlock, helper.TerraformVersion())
422422

423423
if err != nil {
424424
logging.HelperResourceError(ctx,
@@ -428,6 +428,19 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
428428
t.Fatalf("Error generating merged configuration: %s", err)
429429
}
430430

431+
// Preserve the step config for future test steps to use (import state)
432+
confRequest := teststep.PrepareConfigurationRequest{
433+
Directory: step.ConfigDirectory,
434+
File: step.ConfigFile,
435+
Raw: mergedConfig,
436+
TestStepConfigRequest: config.TestStepConfigRequest{
437+
StepNumber: stepNumber,
438+
TestName: t.Name(),
439+
},
440+
}.Exec()
441+
442+
appliedCfg = teststep.Configuration(confRequest)
443+
431444
logging.HelperResourceDebug(ctx, "Finished TestStep")
432445

433446
continue

helper/resource/testing_new_import_state.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"github.com/hashicorp/terraform-plugin-testing/tfversion"
2626
)
2727

28-
func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest.Helper, testCaseWorkingDir *plugintest.WorkingDir, step TestStep, cfgRaw string, providers *providerFactories, stepNumber int) error {
28+
func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest.Helper, testCaseWorkingDir *plugintest.WorkingDir, step TestStep, priorStepCfg teststep.Config, providers *providerFactories, stepNumber int) error {
2929
t.Helper()
3030

3131
// step.ImportStateKind implicitly defaults to the zero-value (ImportCommandWithID) for backward compatibility
@@ -105,23 +105,28 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
105105
}
106106
}
107107

108-
var inlineConfig string
109-
if step.Config != "" {
110-
inlineConfig = step.Config
111-
} else {
112-
inlineConfig = cfgRaw
113-
}
114108
testStepConfigRequest := config.TestStepConfigRequest{
115109
StepNumber: stepNumber,
116110
TestName: t.Name(),
117111
}
118112
testStepConfig := teststep.Configuration(teststep.PrepareConfigurationRequest{
119113
Directory: step.ConfigDirectory,
120114
File: step.ConfigFile,
121-
Raw: inlineConfig,
115+
Raw: step.Config,
122116
TestStepConfigRequest: testStepConfigRequest,
123117
}.Exec())
124118

119+
// If the current import state test step doesn't have configuration, use the prior test step config
120+
if testStepConfig == nil {
121+
if priorStepCfg == nil {
122+
t.Fatal("Cannot import state with no specified config")
123+
}
124+
125+
logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import")
126+
127+
testStepConfig = priorStepCfg
128+
}
129+
125130
switch {
126131
case step.ImportStateConfigExact:
127132
break
@@ -133,10 +138,6 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
133138
testStepConfig = appendImportBlock(testStepConfig, resourceName, importId)
134139
}
135140

136-
if testStepConfig == nil {
137-
t.Fatal("Cannot import state with no specified config")
138-
}
139-
140141
var workingDir *plugintest.WorkingDir
141142
if importStatePersist {
142143
workingDir = testCaseWorkingDir

0 commit comments

Comments
 (0)