Skip to content

Commit b0acd90

Browse files
committed
importstate: fix reading configuration from previous step when previous step use file or directory
Fix #516
1 parent f93511e commit b0acd90

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

helper/resource/testing_new.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
132132
// use this to track last step successfully applied
133133
// acts as default for import tests
134134
var appliedCfg string
135+
var appliedDirCfg string
136+
var appliedFileCfg string
135137
var stepNumber int
136138

137139
for stepIndex, step := range c.Steps {
@@ -141,14 +143,16 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
141143

142144
stepNumber = stepIndex + 1 // 1-based indexing for humans
143145

146+
currentTestStepConfigRequest := config.TestStepConfigRequest{
147+
StepNumber: stepNumber,
148+
TestName: t.Name(),
149+
}
150+
144151
configRequest := teststep.PrepareConfigurationRequest{
145-
Directory: step.ConfigDirectory,
146-
File: step.ConfigFile,
147-
Raw: step.Config,
148-
TestStepConfigRequest: config.TestStepConfigRequest{
149-
StepNumber: stepNumber,
150-
TestName: t.Name(),
151-
},
152+
Directory: step.ConfigDirectory,
153+
File: step.ConfigFile,
154+
Raw: step.Config,
155+
TestStepConfigRequest: currentTestStepConfigRequest,
152156
}.Exec()
153157

154158
cfg := teststep.Configuration(configRequest)
@@ -281,7 +285,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
281285
if step.ImportState {
282286
logging.HelperResourceTrace(ctx, "TestStep is ImportState mode")
283287

284-
err := testStepNewImportState(ctx, t, helper, wd, step, appliedCfg, providers, stepNumber)
288+
err := testStepNewImportState(ctx, t, helper, wd, step, appliedCfg, appliedDirCfg, appliedFileCfg, providers, stepNumber)
285289
if step.ExpectError != nil {
286290
logging.HelperResourceDebug(ctx, "Checking TestStep ExpectError")
287291
if err == nil {
@@ -430,6 +434,11 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
430434

431435
logging.HelperResourceDebug(ctx, "Finished TestStep")
432436

437+
// Store the ConfigDirectory and ConfigFile from the current step
438+
// so they can be used by subsequent ImportState steps if not explicitly set.
439+
appliedDirCfg = step.ConfigDirectory.Exec(currentTestStepConfigRequest)
440+
appliedFileCfg = step.ConfigFile.Exec(currentTestStepConfigRequest)
441+
433442
continue
434443
}
435444

helper/resource/testing_new_import_state.go

Lines changed: 23 additions & 6 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, cfgRaw string, cfgDir string, cfgFile string, providers *providerFactories, stepNumber int) error {
2929
t.Helper()
3030

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

108108
var inlineConfig string
109+
var configDirectory string
110+
var configFile string
111+
112+
testStepConfigRequest := config.TestStepConfigRequest{
113+
StepNumber: stepNumber,
114+
TestName: t.Name(),
115+
}
116+
109117
if step.Config != "" {
110118
inlineConfig = step.Config
111119
} else {
112120
inlineConfig = cfgRaw
113121
}
114-
testStepConfigRequest := config.TestStepConfigRequest{
115-
StepNumber: stepNumber,
116-
TestName: t.Name(),
122+
123+
if step.ConfigDirectory != nil {
124+
configDirectory = step.ConfigDirectory.Exec(testStepConfigRequest)
125+
} else {
126+
configDirectory = cfgDir
117127
}
128+
129+
if step.ConfigFile != nil {
130+
configFile = step.ConfigFile.Exec(testStepConfigRequest)
131+
} else {
132+
configFile = cfgFile
133+
}
134+
118135
testStepConfig := teststep.Configuration(teststep.PrepareConfigurationRequest{
119-
Directory: step.ConfigDirectory,
120-
File: step.ConfigFile,
136+
Directory: config.TestStepConfigFunc(func(_ config.TestStepConfigRequest) string { return configDirectory }),
137+
File: config.TestStepConfigFunc(func(_ config.TestStepConfigRequest) string { return configFile }),
121138
Raw: inlineConfig,
122139
TestStepConfigRequest: testStepConfigRequest,
123140
}.Exec())

0 commit comments

Comments
 (0)