Skip to content

Commit

Permalink
helper/resource: Fail tests with no error that have ExpectError set
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vancluever committed Dec 11, 2017
1 parent c729bdf commit 3f8dad3
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
9 changes: 9 additions & 0 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
93 changes: 93 additions & 0 deletions helper/resource/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 3f8dad3

Please sign in to comment.