Providers should use this package so the virtual kubelet core controllers can understand the errors produced from the provider code.
81 lines
1.5 KiB
Go
81 lines
1.5 KiB
Go
package errdefs
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"gotest.tools/assert"
|
|
"gotest.tools/assert/cmp"
|
|
)
|
|
|
|
type testingNotFoundError bool
|
|
|
|
func (e testingNotFoundError) Error() string {
|
|
return fmt.Sprintf("%v", bool(e))
|
|
}
|
|
|
|
func (e testingNotFoundError) NotFound() bool {
|
|
return bool(e)
|
|
}
|
|
|
|
func TestIsNotFound(t *testing.T) {
|
|
type testCase struct {
|
|
name string
|
|
err error
|
|
xMsg string
|
|
xNotFound bool
|
|
}
|
|
|
|
for _, c := range []testCase{
|
|
{
|
|
name: "NotFoundf",
|
|
err: NotFoundf("%s not found", "foo"),
|
|
xMsg: "foo not found",
|
|
xNotFound: true,
|
|
},
|
|
{
|
|
name: "AsNotFound",
|
|
err: AsNotFound(errors.New("this is a test")),
|
|
xMsg: "this is a test",
|
|
xNotFound: true,
|
|
},
|
|
{
|
|
name: "AsNotFoundWithNil",
|
|
err: AsNotFound(nil),
|
|
xMsg: "",
|
|
xNotFound: false,
|
|
},
|
|
{
|
|
name: "nilError",
|
|
err: nil,
|
|
xMsg: "",
|
|
xNotFound: false,
|
|
},
|
|
{
|
|
name: "customNotFoundFalse",
|
|
err: testingNotFoundError(false),
|
|
xMsg: "false",
|
|
xNotFound: false,
|
|
},
|
|
{
|
|
name: "customNotFoundTrue",
|
|
err: testingNotFoundError(true),
|
|
xMsg: "true",
|
|
xNotFound: true,
|
|
},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
assert.Check(t, cmp.Equal(IsNotFound(c.err), c.xNotFound))
|
|
if c.err != nil {
|
|
assert.Check(t, cmp.Equal(c.err.Error(), c.xMsg))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNotFoundCause(t *testing.T) {
|
|
err := errors.New("test")
|
|
assert.Equal(t, (¬FoundError{err}).Cause(), err)
|
|
}
|