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
13 changes: 13 additions & 0 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,17 @@ type ExternalProvider struct {
Source string // the provider source
}

type ImportStateKind byte

const (
// ImportCommandWithId imports the state using the import command
ImportCommandWithId ImportStateKind = iota
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed a nice side-effect here. ImportCommandWithId equals zero. So, all existing tests will default to ImportCommandWithId, as TestStep.ImportStateKind will get the zero value.

// ImportBlockWithId imports the state using an import block with an ID
ImportBlockWithId
// ImportBlockWithResourceIdentity imports the state using an import block with a resource identity
ImportBlockWithResourceIdentity
)

// TestStep is a single apply sequence of a test, done within the
// context of a state.
//
Expand Down Expand Up @@ -633,6 +644,8 @@ type TestStep struct {
// ID of that resource.
ImportState bool

ImportStateKind ImportStateKind

// ImportStateId is the ID to perform an ImportState operation with.
// This is optional. If it isn't set, then the resource ID is automatically
// determined by inspecting the state for ResourceName's ID.
Expand Down
20 changes: 4 additions & 16 deletions helper/resource/testing_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,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 teststep.Config
var appliedCfg string
var stepNumber int

for stepIndex, step := range c.Steps {
Expand Down Expand Up @@ -249,7 +249,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
File: step.ConfigFile,
Raw: rawCfg,
TestStepConfigRequest: config.TestStepConfigRequest{
StepNumber: stepIndex + 1,
StepNumber: stepNumber,
TestName: t.Name(),
},
}.Exec()
Expand Down Expand Up @@ -289,7 +289,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, stepIndex)
err := testStepNewImportState(ctx, t, helper, wd, step, appliedCfg, providers, stepNumber)
if step.ExpectError != nil {
logging.HelperResourceDebug(ctx, "Checking TestStep ExpectError")
if err == nil {
Expand Down Expand Up @@ -426,7 +426,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
}
}

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

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

confRequest := teststep.PrepareConfigurationRequest{
Directory: step.ConfigDirectory,
File: step.ConfigFile,
Raw: mergedConfig,
TestStepConfigRequest: config.TestStepConfigRequest{
StepNumber: stepIndex + 1,
TestName: t.Name(),
},
}.Exec()

appliedCfg = teststep.Configuration(confRequest)

Comment on lines -439 to -450
Copy link
Member Author

@stephybun stephybun Mar 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this was only ever needed and done for the import tests, so I've opted to move this into testing_new_import_state.go and pass the appliedConfig through as a string so it can be easily appended/modified based on the import mode/kind we're using in the test.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

logging.HelperResourceDebug(ctx, "Finished TestStep")

continue
Expand Down
Loading
Loading