Skip to content

Commit

Permalink
Adding tests for the error.go resource (nephio-project#530)
Browse files Browse the repository at this point in the history
This is my first commit. adding tests for
"nephio\controllers\pkg\resource\error.go"

---------

Signed-off-by: Catalin Stratulat <[email protected]>
  • Loading branch information
Catalin-Stratulat-Ericsson authored and PrimalPimmy committed Aug 2, 2024
1 parent 4896c42 commit 4c68fbb
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 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,68 @@ 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 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

0 comments on commit 4c68fbb

Please sign in to comment.