Skip to content

Commit cde5001

Browse files
committed
Fix 'No changes. Your infrastructure matches the configuration.' false negatives
1 parent e472964 commit cde5001

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

helper/resource/importstate/import_block_with_id_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestImportBlock_WithID_ExpectError(t *testing.T) {
8383
ResourceName: "examplecloud_container.test",
8484
ImportState: true,
8585
ImportStateKind: r.ImportBlockWithID,
86-
ExpectError: regexp.MustCompile(`importing resource examplecloud_container.test: expected a no-op resource action, got \["update"\] action with plan(.?)`),
86+
ExpectError: regexp.MustCompile(`importing resource examplecloud_container.test: expected a no-op import operation, got.*\["update"\] action with plan(.?)`),
8787
},
8888
},
8989
})

helper/resource/testing_new_import_state.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,19 +219,16 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
219219
return fmt.Errorf("importing resource %s: expected a resource change, got no changes", resourceName)
220220
}
221221

222-
actions := resourceChangeUnderTest.Change.Actions
223-
if !actions.NoOp() {
224-
var stdout string
225-
err = runProviderCommand(ctx, t, func() error {
226-
var err error
227-
stdout, err = importWd.SavedPlanRawStdout(ctx)
228-
return err
229-
}, importWd, providers)
230-
if err != nil {
231-
return fmt.Errorf("retrieving formatted plan output: %w", err)
232-
}
222+
change := resourceChangeUnderTest.Change
223+
actions := change.Actions
224+
importing := change.Importing
233225

234-
return fmt.Errorf("importing resource %s: expected a no-op resource action, got %q action with plan \nstdout:\n\n%s", resourceChangeUnderTest.Address, actions, stdout)
226+
switch {
227+
case importing == nil:
228+
return fmt.Errorf("importing resource %s: expected an import operation, got %q action with plan \nstdout:\n\n%s", resourceChangeUnderTest.Address, actions, savedPlanRawStdout(ctx, t, importWd, providers))
229+
230+
case !actions.NoOp():
231+
return fmt.Errorf("importing resource %s: expected a no-op import operation, got %q action with plan \nstdout:\n\n%s", resourceChangeUnderTest.Address, actions, savedPlanRawStdout(ctx, t, importWd, providers))
235232
}
236233

237234
if err := runPlanChecks(ctx, t, plan, step.ImportPlanChecks.PreApply); err != nil {
@@ -469,3 +466,20 @@ func runImportStateCheckFunction(ctx context.Context, t testing.T, importState *
469466

470467
logging.HelperResourceTrace(ctx, "Called TestStep ImportStateCheck")
471468
}
469+
470+
func savedPlanRawStdout(ctx context.Context, t testing.T, wd *plugintest.WorkingDir, providers *providerFactories) string {
471+
t.Helper()
472+
473+
var stdout string
474+
475+
err := runProviderCommand(ctx, t, func() error {
476+
var err error
477+
stdout, err = wd.SavedPlanRawStdout(ctx)
478+
return err
479+
}, wd, providers)
480+
481+
if err != nil {
482+
return fmt.Sprintf("error retrieving formatted plan output: %w", err)
483+
}
484+
return stdout
485+
}

0 commit comments

Comments
 (0)