Skip to content

Commit f9c8a54

Browse files
committed
Replace t.Fatalf with an error return
1 parent a13eded commit f9c8a54

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

helper/resource/testing_new_import_state.go

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,25 @@ type testOutcome struct {
3131
err error
3232
}
3333

34-
var testOK = testOutcome{ok: true}
35-
var testFail = func(format string, a ...any) testOutcome {
34+
var testOK = func() (testOutcome, error) {
35+
return testOutcome{ok: true}, nil
36+
}
37+
38+
var testFail = func(format string, a ...any) (testOutcome, error) {
3639
return testOutcome{
3740
ok: false,
3841
message: fmt.Sprintf(format, a...),
39-
}
42+
}, nil
4043
}
41-
var testFailErr = func(err error) testOutcome {
44+
var testErr = func(err error) (testOutcome, error) {
4245
return testOutcome{
4346
ok: false,
4447
err: err,
45-
}
48+
}, nil
49+
}
50+
51+
var testFatal = func(fatalErr error) (testOutcome, error) {
52+
return testOutcome{ok: false}, fatalErr
4653
}
4754

4855
func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest.Helper, wd *plugintest.WorkingDir, step TestStep, cfgRaw string, providers *providerFactories, stepNumber int) error {
@@ -72,7 +79,7 @@ func runImportTestStep(ctx context.Context, t testing.T, helper *plugintest.Help
7279
// An alternative, [plugintest.TestExpectTFatal], does not have access to logged error messages, so it is open to false positives on this
7380
// complex code path.
7481
if err := requirePlannableImport(t, *helper.TerraformVersion()); err != nil {
75-
return testFailErr(err), nil
82+
return testErr(err)
7683
}
7784
}
7885

@@ -90,7 +97,7 @@ func runImportTestStep(ctx context.Context, t testing.T, helper *plugintest.Help
9097

9198
resourceName := step.ResourceName
9299
if resourceName == "" {
93-
t.Fatal("ResourceName is required for an import state test")
100+
return testFatal(fmt.Errorf("ResourceName is required for an import state test"))
94101
}
95102

96103
// get state from check sequence
@@ -105,7 +112,7 @@ func runImportTestStep(ctx context.Context, t testing.T, helper *plugintest.Help
105112
return nil
106113
}, wd, providers)
107114
if err != nil {
108-
t.Fatalf("Error getting state: %s", err)
115+
return testFatal(fmt.Errorf("Error getting state: %s", err))
109116
}
110117

111118
// Determine the ID to import
@@ -121,7 +128,7 @@ func runImportTestStep(ctx context.Context, t testing.T, helper *plugintest.Help
121128
importId, err = step.ImportStateIdFunc(state)
122129

123130
if err != nil {
124-
t.Fatal(err)
131+
return testFatal(err)
125132
}
126133

127134
logging.HelperResourceDebug(ctx, "Called TestStep ImportStateIdFunc")
@@ -134,7 +141,7 @@ func runImportTestStep(ctx context.Context, t testing.T, helper *plugintest.Help
134141

135142
resource, err := testResource(resourceName, state)
136143
if err != nil {
137-
t.Fatal(err)
144+
return testFatal(err)
138145
}
139146
importId = resource.Primary.ID
140147
}
@@ -187,7 +194,7 @@ func runImportTestStep(ctx context.Context, t testing.T, helper *plugintest.Help
187194

188195
err = importWd.SetConfig(ctx, testStepConfig, step.ConfigVariables)
189196
if err != nil {
190-
t.Fatalf("Error setting test config: %s", err)
197+
return testFatal(fmt.Errorf("Error setting test config: %s", err))
191198
}
192199

193200
if !step.ImportStatePersist {
@@ -196,7 +203,7 @@ func runImportTestStep(ctx context.Context, t testing.T, helper *plugintest.Help
196203
return importWd.Init(ctx)
197204
}, importWd, providers)
198205
if err != nil {
199-
t.Fatalf("Error running init: %s", err)
206+
return testFatal(fmt.Errorf("Error running init: %s", err))
200207
}
201208
}
202209

@@ -209,7 +216,7 @@ func runImportTestStep(ctx context.Context, t testing.T, helper *plugintest.Help
209216
return importWd.CreatePlan(ctx, opts...)
210217
}, importWd, providers)
211218
if err != nil {
212-
return testFailErr(err), nil
219+
return testErr(err)
213220
}
214221

215222
err = runProviderCommand(ctx, t, func() error {
@@ -219,7 +226,7 @@ func runImportTestStep(ctx context.Context, t testing.T, helper *plugintest.Help
219226
return err
220227
}, importWd, providers)
221228
if err != nil {
222-
return testFailErr(err), nil
229+
return testErr(err)
223230
}
224231

225232
if plan.ResourceChanges != nil {
@@ -241,10 +248,10 @@ func runImportTestStep(ctx context.Context, t testing.T, helper *plugintest.Help
241248
return err
242249
}, importWd, providers)
243250
if err != nil {
244-
return testFail("retrieving formatted plan output: %w", err), nil
251+
return testFail("retrieving formatted plan output: %w", err)
245252
}
246253

247-
return testFail("importing resource %s: expected a no-op resource action, got %q action with plan \nstdout:\n\n%s", rc.Address, action, stdout), nil
254+
return testFail("importing resource %s: expected a no-op resource action, got %q action with plan \nstdout:\n\n%s", rc.Address, action, stdout)
248255
}
249256
}
250257
}
@@ -254,14 +261,14 @@ func runImportTestStep(ctx context.Context, t testing.T, helper *plugintest.Help
254261
// TODO compare plan to state from previous step
255262

256263
if err := runPlanChecks(ctx, t, plan, step.ImportPlanChecks.PreApply); err != nil {
257-
return testFailErr(err), nil
264+
return testErr(err)
258265
}
259266
} else {
260267
err = runProviderCommand(ctx, t, func() error {
261268
return importWd.Import(ctx, resourceName, importId)
262269
}, importWd, providers)
263270
if err != nil {
264-
return testFailErr(err), nil
271+
return testErr(err)
265272
}
266273
}
267274

@@ -274,7 +281,7 @@ func runImportTestStep(ctx context.Context, t testing.T, helper *plugintest.Help
274281
return nil
275282
}, importWd, providers)
276283
if err != nil {
277-
t.Fatalf("Error getting state: %s", err)
284+
return testFatal(fmt.Errorf("Error getting state: %s", err))
278285
}
279286

280287
logging.HelperResourceDebug(ctx, fmt.Sprintf("State after import: %d resources in the root module", len(importState.RootModule().Resources)))
@@ -316,7 +323,7 @@ func runImportTestStep(ctx context.Context, t testing.T, helper *plugintest.Help
316323
rIdentifier, ok := r.Primary.Attributes[identifierAttribute]
317324

318325
if !ok {
319-
t.Fatalf("ImportStateVerify: New resource missing identifier attribute %q, ensure attribute value is properly set or use ImportStateVerifyIdentifierAttribute to choose different attribute", identifierAttribute)
326+
return testFatal(fmt.Errorf("ImportStateVerify: New resource missing identifier attribute %q, ensure attribute value is properly set or use ImportStateVerifyIdentifierAttribute to choose different attribute", identifierAttribute))
320327
}
321328

322329
// Find the existing resource
@@ -329,7 +336,7 @@ func runImportTestStep(ctx context.Context, t testing.T, helper *plugintest.Help
329336
r2Identifier, ok := r2.Primary.Attributes[identifierAttribute]
330337

331338
if !ok {
332-
t.Fatalf("ImportStateVerify: Old resource missing identifier attribute %q, ensure attribute value is properly set or use ImportStateVerifyIdentifierAttribute to choose different attribute", identifierAttribute)
339+
return testFatal(fmt.Errorf("ImportStateVerify: Old resource missing identifier attribute %q, ensure attribute value is properly set or use ImportStateVerifyIdentifierAttribute to choose different attribute", identifierAttribute))
333340
}
334341

335342
if r2Identifier == rIdentifier {
@@ -338,9 +345,7 @@ func runImportTestStep(ctx context.Context, t testing.T, helper *plugintest.Help
338345
}
339346
}
340347
if oldR == nil || oldR.Primary == nil {
341-
t.Fatalf(
342-
"Failed state verification, resource with ID %s not found",
343-
rIdentifier)
348+
return testFatal(fmt.Errorf("Failed state verification, resource with ID %s not found", rIdentifier))
344349
}
345350

346351
// don't add empty flatmapped containers, so we can more easily
@@ -417,13 +422,13 @@ func runImportTestStep(ctx context.Context, t testing.T, helper *plugintest.Help
417422
}
418423

419424
if diff := cmp.Diff(expected, actual); diff != "" {
420-
return testFail("ImportStateVerify attributes not equivalent. Difference is shown below. The - symbol indicates attributes missing after import.\n\n%s", diff), nil
425+
return testFail("ImportStateVerify attributes not equivalent. Difference is shown below. The - symbol indicates attributes missing after import.\n\n%s", diff)
421426
}
422427
}
423428
}
424429
}
425430

426-
return testOK, nil
431+
return testOK()
427432
}
428433

429434
func appendImportBlock(config string, resourceName string, importID string) string {

0 commit comments

Comments
 (0)