Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changes/unreleased/BUG FIXES-20250606-110444.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: 'helper/resource: Fixed bug with import state mode where prior test config is not used for `ConfigFile` or `ConfigDirectory`'
time: 2025-06-06T11:04:44.925152-04:00
custom:
Issue: "516"
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func TestImportBlock_InConfigDirectory(t *testing.T) {
ResourceName: "examplecloud_container.test",
ImportState: true,
ImportStateKind: r.ImportBlockWithID,
ConfigDirectory: config.StaticDirectory(`testdata/2`),
},
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func TestImportBlock_InConfigFile(t *testing.T) {
ResourceName: "examplecloud_container.test",
ImportState: true,
ImportStateKind: r.ImportBlockWithID,
ConfigFile: config.StaticFile(`testdata/2/examplecloud_container.tf`),
},
},
})
Expand Down
55 changes: 55 additions & 0 deletions helper/resource/importstate/import_command_with_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-testing/config"
r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/datasource"
Expand Down Expand Up @@ -197,6 +198,60 @@ func TestImportCommand_ImportStateVerify(t *testing.T) {
})
}

func TestImportCommand_InConfigFile(t *testing.T) {
t.Parallel()

r.UnitTest(t, r.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_0_0), // ProtoV6ProviderFactories
},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
Resources: map[string]testprovider.Resource{
"examplecloud_container": examplecloudResource(),
},
}),
},
Steps: []r.TestStep{
{
ConfigFile: config.StaticFile(`testdata/1/examplecloud_container.tf`),
},
{
ResourceName: "examplecloud_container.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestImportCommand_InConfigDirectory(t *testing.T) {
t.Parallel()

r.UnitTest(t, r.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_0_0), // ProtoV6ProviderFactories
},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{
Resources: map[string]testprovider.Resource{
"examplecloud_container": examplecloudResource(),
},
}),
},
Steps: []r.TestStep{
{
ConfigDirectory: config.StaticDirectory(`testdata/1`),
},
{
ResourceName: "examplecloud_container.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestImportCommand_ImportStateVerify_Ignore(t *testing.T) {
t.Parallel()

Expand Down
17 changes: 15 additions & 2 deletions helper/resource/testing_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ 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 appliedCfg teststep.Config
var stepNumber int

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

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

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

// Preserve the step config for future test steps to use (import state)
confRequest := teststep.PrepareConfigurationRequest{
Directory: step.ConfigDirectory,
File: step.ConfigFile,
Raw: mergedConfig,
TestStepConfigRequest: config.TestStepConfigRequest{
StepNumber: stepNumber,
TestName: t.Name(),
},
}.Exec()

appliedCfg = teststep.Configuration(confRequest)

logging.HelperResourceDebug(ctx, "Finished TestStep")

continue
Expand Down
25 changes: 13 additions & 12 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, priorStepCfg teststep.Config, providers *providerFactories, stepNumber int) error {
t.Helper()

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

var inlineConfig string
if step.Config != "" {
inlineConfig = step.Config
} else {
inlineConfig = cfgRaw
}
testStepConfigRequest := config.TestStepConfigRequest{
StepNumber: stepNumber,
TestName: t.Name(),
}
testStepConfig := teststep.Configuration(teststep.PrepareConfigurationRequest{
Directory: step.ConfigDirectory,
File: step.ConfigFile,
Raw: inlineConfig,
Raw: step.Config,
TestStepConfigRequest: testStepConfigRequest,
}.Exec())

// If the current import state test step doesn't have configuration, use the prior test step config
if testStepConfig == nil {
if priorStepCfg == nil {
t.Fatal("Cannot import state with no specified config")
}

logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import")

testStepConfig = priorStepCfg
}

switch {
case step.ImportStateConfigExact:
break
Expand All @@ -133,10 +138,6 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
testStepConfig = appendImportBlock(testStepConfig, resourceName, importId)
}

if testStepConfig == nil {
t.Fatal("Cannot import state with no specified config")
}

var workingDir *plugintest.WorkingDir
if importStatePersist {
workingDir = testCaseWorkingDir
Expand Down