From a6e79046ab29f47fe3701cb73e93f14fb5b6394e Mon Sep 17 00:00:00 2001 From: Paddy Carver Date: Thu, 3 Sep 2020 14:22:00 -0700 Subject: [PATCH] Backport #550 Fixed import state testing to be able to return an error that TestStep.ExpectError can handle. --- helper/resource/testing_new.go | 13 +++++++++++-- helper/resource/testing_new_import_state.go | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/helper/resource/testing_new.go b/helper/resource/testing_new.go index 4426c02774..6dcb240681 100644 --- a/helper/resource/testing_new.go +++ b/helper/resource/testing_new.go @@ -107,8 +107,17 @@ func RunNewTest(t *testing.T, c TestCase, providers map[string]terraform.Resourc if step.ImportState { step.providers = providers err := testStepNewImportState(t, c, wd, step, appliedCfg) - if err != nil { - t.Fatal(err) + if step.ExpectError != nil { + if err == nil { + t.Fatalf("Step %d/%d error running import: expected an error but got none", i+1, len(c.Steps)) + } + if !step.ExpectError.MatchString(err.Error()) { + t.Fatalf("Step %d/%d error running import, expected an error with pattern (%s), no match on: %s", i+1, len(c.Steps), step.ExpectError.String(), err) + } + } else { + if err != nil { + t.Fatalf("Step %d/%d error running import: %s", i+1, len(c.Steps), err) + } } continue } diff --git a/helper/resource/testing_new_import_state.go b/helper/resource/testing_new_import_state.go index 727a2de180..1fc09b7203 100644 --- a/helper/resource/testing_new_import_state.go +++ b/helper/resource/testing_new_import_state.go @@ -73,7 +73,7 @@ func testStepNewImportState(t *testing.T, c TestCase, wd *tftest.WorkingDir, ste return importWd.Import(step.ResourceName, importId) }, wd, c.ProviderFactories) if err != nil { - return fmt.Errorf("Error running import: %v", err) + return err } var importState *terraform.State