Skip to content

Commit

Permalink
Refactoring testStepNewImportState() to use type implementing testste…
Browse files Browse the repository at this point in the history
…p.Config interface for applied configuration (#150)
  • Loading branch information
bendbennett committed Jul 17, 2023
1 parent 5634313 commit 18a3602
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
16 changes: 14 additions & 2 deletions helper/resource/testing_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,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 @@ -353,7 +353,19 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
}
}

appliedCfg = step.mergedConfig(ctx, c)
appliedCfg, err = teststep.Configuration(
teststep.ConfigurationRequest{
Raw: step.mergedConfig(ctx, c),
},
)

if err != nil {
logging.HelperResourceError(ctx,
"Error creating applied configuration",
map[string]interface{}{logging.KeyError: err},
)
t.Fatalf("Step %d/%d error: %s", stepNumber, len(c.Steps), err)
}

logging.HelperResourceDebug(ctx, "Finished TestStep")

Expand Down
34 changes: 17 additions & 17 deletions helper/resource/testing_new_import_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,28 @@ import (
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
)

func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest.Helper, wd *plugintest.WorkingDir, step TestStep, cfg string, providers *providerFactories) error {
func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest.Helper, wd *plugintest.WorkingDir, step TestStep, cfg teststep.Config, providers *providerFactories) error {
t.Helper()

var config teststep.Config

config, err := teststep.Configuration(
teststep.ConfigurationRequest{
Directory: step.ConfigDirectory,
Raw: step.Config,
},
)

if err != nil {
t.Fatalf("Error creating configuration: %s", err)
}

if step.ResourceName == "" {
t.Fatal("ResourceName is required for an import state test")
}

// get state from check sequence
var state *terraform.State
var err error
err = runProviderCommand(ctx, t, func() error {
state, err = getState(ctx, t, wd)
if err != nil {
Expand Down Expand Up @@ -79,14 +91,12 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest

logging.HelperResourceTrace(ctx, fmt.Sprintf("Using import identifier: %s", importId))

// TODO: Refactor to inspect type implementing teststep.Config to determine if configuration
// is set.
// Create working directory for import tests
if step.Config == "" {
if !config.HasConfiguration() {
logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import")

step.Config = cfg
if step.Config == "" {
config = cfg
if !config.HasConfiguration() {
t.Fatal("Cannot import state with no specified config")
}
}
Expand All @@ -101,16 +111,6 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
defer importWd.Close()
}

config, err := teststep.Configuration(
teststep.ConfigurationRequest{
Raw: step.Config,
},
)

if err != nil {
t.Fatalf("Error creating test config: %s", err)
}

err = importWd.SetConfig(ctx, config)
if err != nil {
t.Fatalf("Error setting test config: %s", err)
Expand Down

0 comments on commit 18a3602

Please sign in to comment.