From c3a6290e25d01f36cabf5905322f059f93e389a1 Mon Sep 17 00:00:00 2001 From: Baraa Basata Date: Mon, 31 Mar 2025 12:42:17 -0400 Subject: [PATCH] helper/resource: easy access to state in tfjson.State form --- helper/resource/testing_new.go | 10 +++++----- helper/resource/testing_new_config.go | 6 +++--- helper/resource/testing_new_import_state.go | 8 ++++++-- helper/resource/testing_new_refresh_state.go | 4 ++-- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/helper/resource/testing_new.go b/helper/resource/testing_new.go index 1a84188b8..8d8d5ffd6 100644 --- a/helper/resource/testing_new.go +++ b/helper/resource/testing_new.go @@ -68,7 +68,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest var statePreDestroy *terraform.State var err error err = runProviderCommand(ctx, t, func() error { - statePreDestroy, err = getState(ctx, t, wd) + _, statePreDestroy, err = getState(ctx, t, wd) if err != nil { return err } @@ -447,18 +447,18 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest } } -func getState(ctx context.Context, t testing.T, wd *plugintest.WorkingDir) (*terraform.State, error) { +func getState(ctx context.Context, t testing.T, wd *plugintest.WorkingDir) (*tfjson.State, *terraform.State, error) { t.Helper() jsonState, err := wd.State(ctx) if err != nil { - return nil, err + return nil, nil, err } state, err := shimStateFromJson(jsonState) if err != nil { t.Fatal(err) } - return state, nil + return jsonState, state, nil } func stateIsEmpty(state *terraform.State) bool { @@ -573,7 +573,7 @@ func testIDRefresh(ctx context.Context, t testing.T, c TestCase, wd *plugintest. if err != nil { t.Fatalf("Error running terraform refresh: %s", err) } - state, err = getState(ctx, t, wd) + _, state, err = getState(ctx, t, wd) if err != nil { return err } diff --git a/helper/resource/testing_new_config.go b/helper/resource/testing_new_config.go index 1456f7fba..04f8882d9 100644 --- a/helper/resource/testing_new_config.go +++ b/helper/resource/testing_new_config.go @@ -159,7 +159,7 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint } err = runProviderCommand(ctx, t, func() error { - stateBeforeApplication, err = getState(ctx, t, wd) + _, stateBeforeApplication, err = getState(ctx, t, wd) if err != nil { return err } @@ -200,7 +200,7 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint var state *terraform.State err := runProviderCommand(ctx, t, func() error { - state, err = getState(ctx, t, wd) + _, state, err = getState(ctx, t, wd) if err != nil { return err } @@ -384,7 +384,7 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint var state *terraform.State err = runProviderCommand(ctx, t, func() error { - state, err = getState(ctx, t, wd) + _, state, err = getState(ctx, t, wd) if err != nil { return err } diff --git a/helper/resource/testing_new_import_state.go b/helper/resource/testing_new_import_state.go index 7581892dc..3f74838d3 100644 --- a/helper/resource/testing_new_import_state.go +++ b/helper/resource/testing_new_import_state.go @@ -58,10 +58,11 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest // get state from check sequence var state *terraform.State + var stateJSON *tfjson.State var err error err = runProviderCommand(ctx, t, func() error { - state, err = getState(ctx, t, wd) + stateJSON, state, err = getState(ctx, t, wd) if err != nil { return err } @@ -71,6 +72,9 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest t.Fatalf("Error getting state: %s", err) } + // TODO: this statement is a placeholder -- it simply prevents stateJSON from being unused + logging.HelperResourceTrace(ctx, fmt.Sprintf("State before import: values %v", stateJSON.Values != nil)) + // Determine the ID to import var importId string switch { @@ -230,7 +234,7 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest var importState *terraform.State err = runProviderCommand(ctx, t, func() error { - importState, err = getState(ctx, t, importWd) + _, importState, err = getState(ctx, t, importWd) if err != nil { return err } diff --git a/helper/resource/testing_new_refresh_state.go b/helper/resource/testing_new_refresh_state.go index 5c0e38758..4a3b362c1 100644 --- a/helper/resource/testing_new_refresh_state.go +++ b/helper/resource/testing_new_refresh_state.go @@ -22,7 +22,7 @@ func testStepNewRefreshState(ctx context.Context, t testing.T, wd *plugintest.Wo var err error // Explicitly ensure prior state exists before refresh. err = runProviderCommand(ctx, t, func() error { - _, err = getState(ctx, t, wd) + _, _, err = getState(ctx, t, wd) if err != nil { return err } @@ -41,7 +41,7 @@ func testStepNewRefreshState(ctx context.Context, t testing.T, wd *plugintest.Wo var refreshState *terraform.State err = runProviderCommand(ctx, t, func() error { - refreshState, err = getState(ctx, t, wd) + _, refreshState, err = getState(ctx, t, wd) if err != nil { return err }