Skip to content

Commit

Permalink
fix(tests) Get rid of custom checker (#2950)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
e-sumin and mergify[bot] authored Jun 25, 2024
1 parent c7b7f99 commit 669f0b1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 91 deletions.
51 changes: 5 additions & 46 deletions pkg/errorchecker/errorchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,14 @@
package errorchecker

import (
"fmt"
"regexp"

"gopkg.in/check.v1"
)

type errorMatchesChecker struct {
*check.CheckerInfo
}

// ErrorMessageMatcher is designed to verify that the error text matches the provided regex pattern.
var ErrorMessageMatcher check.Checker = errorMatchesChecker{
&check.CheckerInfo{Name: "ErrorMatches", Params: []string{"value", "regex"}},
}

// Check implements the checker interface and contains the main logic of the ErrorMessageMatcher checker.
func (checker errorMatchesChecker) Check(
params []interface{},
names []string,
) (result bool, errStr string) {
if params[0] == nil {
return false, "Error value is nil"
}
err, ok := params[0].(error)
if !ok {
return false, "Value is not an error"
}
params[0] = err.Error()
names[0] = "error"
return matches(params[0], params[1])
}

func matches(value, regex interface{}) (result bool, error string) {
reStr, ok := regex.(string)
if !ok {
return false, "Regex must be a string"
}
valueStr, valueIsStr := value.(string)
if !valueIsStr {
if valueWithStr, valueHasStr := value.(fmt.Stringer); valueHasStr {
valueStr, valueIsStr = valueWithStr.String(), true
}
}
if valueIsStr {
matches, err := regexp.MatchString("^"+reStr+"$", valueStr)
if err != nil {
return false, "Can't compile regex: " + err.Error()
}
return matches, ""
}
return false, "Obtained value is not a string and has no .String()"
// AssertErrorMessage is purposed to verify that error message matches wanted pattern
func AssertErrorMessage(c *check.C, err error, wanted string) {
matches, err := regexp.MatchString("^"+wanted+"$", err.Error())
c.Assert(err, check.IsNil)
c.Assert(matches, check.Equals, true)
}
48 changes: 5 additions & 43 deletions pkg/errorchecker/errorchecker_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package errorchecker

import (
. "gopkg.in/check.v1"
"testing"

. "gopkg.in/check.v1"

"github.com/kanisterio/errkit"
"github.com/pkg/errors"
)
Expand All @@ -15,45 +16,6 @@ type ErrorsTestSuite struct{}

var _ = Suite(&ErrorsTestSuite{})

func (ts *ErrorsTestSuite) TestErrorMessageMatcher(c *C) {
errkitError := errkit.New("Some errkit error")
for _, tc := range []struct {
params []any
names []string
expectedResult bool
expectedError string
}{
{
params: []any{nil},
names: []string{},
expectedResult: false,
expectedError: "Error value is nil",
},
{
params: []any{10},
names: []string{},
expectedResult: false,
expectedError: "Value is not an error",
},
{
params: []any{errkitError, ".* errkit .*"},
names: []string{""},
expectedResult: true,
expectedError: "",
},
{
params: []any{errkitError, 1},
names: []string{""},
expectedResult: false,
expectedError: "Regex must be a string",
},
} {
r, s := ErrorMessageMatcher.Check(tc.params, tc.names)
c.Assert(r, Equals, tc.expectedResult)
c.Assert(s, Equals, tc.expectedError)
}
}

func (ts *ErrorsTestSuite) TestWrappingAndMatching(c *C) {
errkitErr := errkit.New("Errkit error")
errkitWrappedErr := errkit.Wrap(errkitErr, "errkit wrapped")
Expand All @@ -65,7 +27,7 @@ func (ts *ErrorsTestSuite) TestWrappingAndMatching(c *C) {
// Ensure that transformation to string still works
c.Assert(errorsWrappedErr1.Error(), Equals, "errors wrapped 1: errors wrapped: errkit wrapped: Errkit error")
// Ensure that error message matching does work as expected
c.Assert(errorsWrappedErr1, ErrorMessageMatcher, ".*errkit wrapped.*")
c.Assert(errorsWrappedErr1, ErrorMessageMatcher, ".*Errkit error")
c.Assert(errorsWrappedErr1, ErrorMessageMatcher, "errors wrapped 1.*")
AssertErrorMessage(c, errorsWrappedErr1, ".*errkit wrapped.*")
AssertErrorMessage(c, errorsWrappedErr1, ".*Errkit error")
AssertErrorMessage(c, errorsWrappedErr1, "errors wrapped 1.*")
}
4 changes: 2 additions & 2 deletions pkg/kube/workload_ready_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *WorkloadReadySuite) TestWaitOnStatefulSetReady(c *C) {
defer cancel()
err := WaitOnStatefulSetReady(ctx, getCli(tc.input), tc.input.namespace, tc.input.name)
if tc.want != "" {
c.Assert(err, errorchecker.ErrorMessageMatcher, tc.want)
errorchecker.AssertErrorMessage(c, err, tc.want)
} else {
c.Assert(err, IsNil)
}
Expand Down Expand Up @@ -100,7 +100,7 @@ func (s *WorkloadReadySuite) TestWaitOnDeploymentReady(c *C) {
defer cancel()
err := WaitOnDeploymentReady(ctx, getCli(tc.input), tc.input.namespace, tc.input.name)
if tc.want != "" {
c.Assert(err, errorchecker.ErrorMessageMatcher, tc.want)
errorchecker.AssertErrorMessage(c, err, tc.want)
} else {
c.Assert(err, IsNil)
}
Expand Down

0 comments on commit 669f0b1

Please sign in to comment.