Skip to content

Commit ccff991

Browse files
committed
Simplify no-op action check
1 parent 2456d71 commit ccff991

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
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 resource action, got \["update"\] action with plan(.?)`),
8787
},
8888
},
8989
})

helper/resource/testing_new_import_state.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -202,36 +202,37 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
202202

203203
logging.HelperResourceDebug(ctx, fmt.Sprintf("ImportBlockWithId: %d resource changes", len(plan.ResourceChanges)))
204204

205-
for _, rc := range plan.ResourceChanges {
206-
if rc.Address != resourceName {
207-
// we're only interested in the changes for the resource being imported
208-
continue
209-
}
210-
if rc.Change != nil && rc.Change.Actions != nil {
211-
// should this be length checked and used as a condition, if it's a no-op then there shouldn't be any other changes here
212-
for _, action := range rc.Change.Actions {
213-
if action != "no-op" {
214-
var stdout string
215-
err = runProviderCommand(ctx, t, func() error {
216-
var err error
217-
stdout, err = importWd.SavedPlanRawStdout(ctx)
218-
return err
219-
}, importWd, providers)
220-
if err != nil {
221-
return fmt.Errorf("retrieving formatted plan output: %w", err)
222-
}
223-
224-
return fmt.Errorf("importing resource %s: expected a no-op resource action, got %q action with plan \nstdout:\n\n%s", rc.Address, action, stdout)
225-
}
226-
}
205+
// Verify reasonable things about the plan
206+
var resourceChangeUnderTest *tfjson.ResourceChange
207+
208+
if len(plan.ResourceChanges) == 0 {
209+
return fmt.Errorf("importing resource %s: expected a resource change, got no changes", resourceName)
210+
}
211+
212+
for _, change := range plan.ResourceChanges {
213+
if change.Address == resourceName {
214+
resourceChangeUnderTest = change
227215
}
228216
}
229217

230-
if len(plan.ResourceChanges) == 0 {
218+
if resourceChangeUnderTest == nil || resourceChangeUnderTest.Change == nil || resourceChangeUnderTest.Change.Actions == nil {
231219
return fmt.Errorf("importing resource %s: expected a resource change, got no changes", resourceName)
232220
}
233221

234-
// TODO compare plan to state from previous step
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+
}
233+
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)
235+
}
235236

236237
if err := runPlanChecks(ctx, t, plan, step.ImportPlanChecks.PreApply); err != nil {
237238
return err

0 commit comments

Comments
 (0)