Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions helper/resource/testing_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
// use this to track last step successfully applied
// acts as default for import tests
var appliedCfg string
var appliedDirCfg string
var appliedFileCfg string
var stepNumber int

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

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

currentTestStepConfigRequest := config.TestStepConfigRequest{
StepNumber: stepNumber,
TestName: t.Name(),
}

configRequest := teststep.PrepareConfigurationRequest{
Directory: step.ConfigDirectory,
File: step.ConfigFile,
Raw: step.Config,
TestStepConfigRequest: config.TestStepConfigRequest{
StepNumber: stepNumber,
TestName: t.Name(),
},
Directory: step.ConfigDirectory,
File: step.ConfigFile,
Raw: step.Config,
TestStepConfigRequest: currentTestStepConfigRequest,
}.Exec()

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

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

logging.HelperResourceDebug(ctx, "Finished TestStep")

// Store the ConfigDirectory and ConfigFile from the current step
// so they can be used by subsequent ImportState steps if not explicitly set.
appliedDirCfg = step.ConfigDirectory.Exec(currentTestStepConfigRequest)
appliedFileCfg = step.ConfigFile.Exec(currentTestStepConfigRequest)

continue
}

Expand Down
16 changes: 12 additions & 4 deletions helper/resource/testing_new_import_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest.Helper, testCaseWorkingDir *plugintest.WorkingDir, step TestStep, cfgRaw string, providers *providerFactories, stepNumber int) error {
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 {
t.Helper()

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

var inlineConfig string
if step.Config != "" {
var directoryConfig config.TestStepConfigFunc
var fileConfig config.TestStepConfigFunc
if step.ConfigDirectory != nil || step.ConfigFile != nil || step.Config != "" {
directoryConfig = step.ConfigDirectory
fileConfig = step.ConfigFile
inlineConfig = step.Config
} else {
// use config from previous step
directoryConfig = config.TestStepConfigFunc(func(_ config.TestStepConfigRequest) string { return cfgDir })
fileConfig = config.TestStepConfigFunc(func(_ config.TestStepConfigRequest) string { return cfgFile })
inlineConfig = cfgRaw
}

testStepConfigRequest := config.TestStepConfigRequest{
StepNumber: stepNumber,
TestName: t.Name(),
}
testStepConfig := teststep.Configuration(teststep.PrepareConfigurationRequest{
Directory: step.ConfigDirectory,
File: step.ConfigFile,
Directory: directoryConfig,
File: fileConfig,
Raw: inlineConfig,
TestStepConfigRequest: testStepConfigRequest,
}.Exec())
Expand Down
Loading