diff --git a/helper/resource/testing_new.go b/helper/resource/testing_new.go index 11d8821ba..6ae8c2840 100644 --- a/helper/resource/testing_new.go +++ b/helper/resource/testing_new.go @@ -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 { @@ -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") diff --git a/helper/resource/testing_new_import_state.go b/helper/resource/testing_new_import_state.go index f2d98da92..ad443e239 100644 --- a/helper/resource/testing_new_import_state.go +++ b/helper/resource/testing_new_import_state.go @@ -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 { @@ -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") } } @@ -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)