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.
This commit is contained in:
Chris Marchesi 2017-12-11 14:05:40 -08:00
parent c729bdff43
commit 3f8dad30c9
No known key found for this signature in database
GPG Key ID: 8D6F1589D9834498
2 changed files with 102 additions and 0 deletions

View File

@ -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 there was an error, exit
if err != nil { if err != nil {
// Perhaps we expected an error? Check if it matches // Perhaps we expected an error? Check if it matches

View File

@ -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) { func TestComposeAggregateTestCheckFunc(t *testing.T) {
check1 := func(s *terraform.State) error { check1 := func(s *terraform.State) error {
return errors.New("Error 1") return errors.New("Error 1")