Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tests for the error.go resource #530

Merged
merged 3 commits into from
Feb 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions controllers/pkg/resource/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
kerrors "k8s.io/apimachinery/pkg/api/errors"
)

func TestIgnore(t *testing.T) {
Expand Down Expand Up @@ -108,6 +109,103 @@ func TestIgnoreAny(t *testing.T) {
}
}

func TestIgnoreNotFound(t *testing.T) {
errBoom := errors.New("boom")
type args struct {
err error
}
cases := map[string]struct {
args args
want error
}{
"IgnoreNotFound": {
args: args{
err: errBoom,
},
want: errBoom,
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := IgnoreNotFound(tc.args.err)
if diff := cmp.Diff(tc.want, got, EquateErrors()); diff != "" {
t.Errorf("Ignore(...): -want error, +got error:\n%s", diff)
}
})
}
}

func TestIsAPIError(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can prob remove this as we get full coverage with the next test

errBoom := errors.New("boom")
k8sError := kerrors.NewBadRequest("BadRequest Reason")

type args struct {
err error
}
cases := map[string]struct {
args args
want bool
}{
"isKubernetesAPIError": {
args: args{
err: k8sError,
},
want: true,
},
"isNotKubernetesAPIError": {
args: args{
err: errBoom,
},
want: false,
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := IsAPIError(tc.args.err)
if diff := cmp.Diff(tc.want, got, EquateErrors()); diff != "" {
t.Errorf("Ignore(...): -want %t, +got error:\n%s", tc.want, diff)
}
})
}
}

func TestIsAPIErrorWrapped(t *testing.T) {
errBoom := errors.New("boom")
k8sError := kerrors.NewBadRequest("BadRequest Reason")

type args struct {
err error
}
cases := map[string]struct {
args args
want bool
}{
"isKubernetesAPIErrorWrapped": {
args: args{
err: k8sError,
},
want: true,
},
"isNotKubernetesAPIErrorWrapped": {
args: args{
err: errBoom,
},
want: false,
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := IsAPIErrorWrapped(tc.args.err)
if diff := cmp.Diff(tc.want, got, EquateErrors()); diff != "" {
t.Errorf("Ignore(...): -want %t, +got error:\n%s", tc.want, diff)
}
})
}
}

func EquateErrors() cmp.Option {
return cmp.Comparer(func(a, b error) bool {
if a == nil || b == nil {
Expand Down