diff --git a/helper/resource/testing_new.go b/helper/resource/testing_new.go index ea66b67b..284ebf49 100644 --- a/helper/resource/testing_new.go +++ b/helper/resource/testing_new.go @@ -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 { @@ -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) @@ -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 { @@ -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 } diff --git a/helper/resource/testing_new_import_state.go b/helper/resource/testing_new_import_state.go index f2e265d5..6d820962 100644 --- a/helper/resource/testing_new_import_state.go +++ b/helper/resource/testing_new_import_state.go @@ -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 @@ -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())