From 3f8dad30c9d9a748257c0c783c5e1e61402372f7 Mon Sep 17 00:00:00 2001 From: Chris Marchesi Date: Mon, 11 Dec 2017 14:05:40 -0800 Subject: [PATCH] helper/resource: Fail tests with no error that have ExpectError set Looks like while we were checking errors correctly when ExpectError was set, we weren't checking for the *absence* of an error, which is should be checked as well (no error is still not the error we are looking for). Added a few more tests for ExpectError as well. --- helper/resource/testing.go | 9 ++++ helper/resource/testing_test.go | 93 +++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 64f5c899b9..05efc70f9d 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -488,6 +488,15 @@ func Test(t TestT, c TestCase) { } } + // If we expected an error, but did not get one, fail + if err == nil && step.ExpectError != nil { + errored = true + t.Error(fmt.Sprintf( + "Step %d, no error received, but expected a match to:\n\n%s\n\n", + i, step.ExpectError)) + break + } + // If there was an error, exit if err != nil { // Perhaps we expected an error? Check if it matches diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index fcbde3ed9e..7002ea6f85 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -520,6 +520,99 @@ func TestTest_resetError(t *testing.T) { } } +func TestTest_expectError(t *testing.T) { + cases := []struct { + name string + planErr bool + applyErr bool + badErr bool + }{ + { + name: "successful apply", + planErr: false, + applyErr: false, + }, + { + name: "bad plan", + planErr: true, + applyErr: false, + }, + { + name: "bad apply", + planErr: false, + applyErr: true, + }, + { + name: "bad plan, bad err", + planErr: true, + applyErr: false, + badErr: true, + }, + { + name: "bad apply, bad err", + planErr: false, + applyErr: true, + badErr: true, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + mp := testProvider() + expectedText := "test provider error" + var errText string + if tc.badErr { + errText = "wrong provider error" + } else { + errText = expectedText + } + noErrText := "no error received, but expected a match to" + if tc.planErr { + mp.DiffReturnError = errors.New(errText) + } + if tc.applyErr { + mp.ApplyReturnError = errors.New(errText) + } + mt := new(mockT) + Test(mt, TestCase{ + Providers: map[string]terraform.ResourceProvider{ + "test": mp, + }, + Steps: []TestStep{ + TestStep{ + Config: testConfigStr, + ExpectError: regexp.MustCompile(expectedText), + Check: func(*terraform.State) error { return nil }, + ExpectNonEmptyPlan: true, + }, + }, + }, + ) + if mt.FatalCalled { + t.Fatalf("fatal: %+v", mt.FatalArgs) + } + switch { + case len(mt.ErrorArgs) < 1 && !tc.planErr && !tc.applyErr: + t.Fatalf("expected error, got none") + case !tc.planErr && !tc.applyErr: + for _, e := range mt.ErrorArgs { + if regexp.MustCompile(noErrText).MatchString(fmt.Sprintf("%v", e)) { + return + } + } + t.Fatalf("expected error to match %s, got %+v", noErrText, mt.ErrorArgs) + case tc.badErr: + for _, e := range mt.ErrorArgs { + if regexp.MustCompile(expectedText).MatchString(fmt.Sprintf("%v", e)) { + return + } + } + t.Fatalf("expected error to match %s, got %+v", expectedText, mt.ErrorArgs) + } + }) + } +} + func TestComposeAggregateTestCheckFunc(t *testing.T) { check1 := func(s *terraform.State) error { return errors.New("Error 1")