Skip to content

Commit

Permalink
fix(test): ensure jq matchers can handle null values (#1698)
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli authored Feb 26, 2025
1 parent 27809c7 commit 73d70b8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
8 changes: 8 additions & 0 deletions pkg/utils/test/matchers/jq/jq_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ func formattedFailurePath(failurePath []interface{}) string {

//nolint:cyclop
func toType(in any) (any, error) {
valof := reflect.ValueOf(in)
if !valof.IsValid() {
return nil, nil
}
if valof.Kind() == reflect.Ptr && valof.IsNil() {
return nil, nil
}

switch v := in.(type) {
case string:
d, err := byteToType([]byte(v))
Expand Down
52 changes: 36 additions & 16 deletions pkg/utils/test/matchers/jq/jq_support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"testing"

"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/types"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/resources"
Expand Down Expand Up @@ -68,6 +70,12 @@ func TestBytesToType(t *testing.T) {
}
}

func haveType(kind reflect.Kind) func(in any) bool {
return func(in any) bool {
return reflect.TypeOf(in).Kind() == kind
}
}

func TestToType(t *testing.T) {
t.Parallel()

Expand All @@ -76,9 +84,9 @@ func TestToType(t *testing.T) {
g := NewWithT(t)

tests := []struct {
name string
fn func() any
expectedType reflect.Kind
name string
fn func() any
expectation types.GomegaMatcher
}{
{
name: "gbytes",
Expand All @@ -90,70 +98,84 @@ func TestToType(t *testing.T) {

return b
},
expectedType: reflect.Map,
expectation: Satisfy(haveType(reflect.Map)),
},
{
name: "bytes",
fn: func() any {
return typeTestData
},
expectedType: reflect.Map,
expectation: Satisfy(haveType(reflect.Map)),
},
{
name: "string_map",
fn: func() any {
return string(typeTestData)
},
expectedType: reflect.Map,
expectation: Satisfy(haveType(reflect.Map)),
},
{
name: "string_slice",
fn: func() any {
return `[ "foo", "bar" ]`
},
expectedType: reflect.Slice,
expectation: Satisfy(haveType(reflect.Slice)),
},
{
name: "json.RawMessage",
fn: func() any {
return json.RawMessage(typeTestData)
},
expectedType: reflect.Map,
expectation: Satisfy(haveType(reflect.Map)),
},
{
name: "io.Reader",
fn: func() any {
return strings.NewReader(string(typeTestData))
},
expectedType: reflect.Map,
expectation: Satisfy(haveType(reflect.Map)),
},
{
name: "unstructured.Unstructured",
fn: func() any {
return *resources.GvkToUnstructured(gvk.ConfigMap)
},
expectedType: reflect.Map,
expectation: Satisfy(haveType(reflect.Map)),
},
{
name: "*unstructured.Unstructured",
fn: func() any {
return resources.GvkToUnstructured(gvk.ConfigMap)
},
expectedType: reflect.Map,
expectation: Satisfy(haveType(reflect.Map)),
},
{
name: "map",
fn: func() any {
return map[string]string{"foo": "bar"}
},
expectedType: reflect.Map,
expectation: Satisfy(haveType(reflect.Map)),
},
{
name: "slice",
fn: func() any {
return []string{"foo", "bar"}
},
expectedType: reflect.Slice,
expectation: Satisfy(haveType(reflect.Slice)),
},
{
name: "*unstructured.Unstructured(nil)",
fn: func() any {
return (*unstructured.Unstructured)(nil)
},
expectation: BeNil(),
},
{
name: "nil",
fn: func() any {
return nil
},
expectation: BeNil(),
},
}

Expand All @@ -164,9 +186,7 @@ func TestToType(t *testing.T) {
convertedType, err := toType(tt.fn())

g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(convertedType).Should(Satisfy(func(in any) bool {
return reflect.TypeOf(in).Kind() == tt.expectedType
}))
g.Expect(convertedType).Should(tt.expectation)
})
}
}
19 changes: 19 additions & 0 deletions pkg/utils/test/testf/testf_witht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ func TestGet(t *testing.T) {
v := wt.Get(gvk.ConfigMap, key).Consistently().WithTimeout(1 * time.Second).Should(Succeed())
g.Expect(v).ShouldNot(BeNil())
})

t.Run("Get Not Found", func(t *testing.T) {
wt := tc.NewWithT(t)

key := client.ObjectKey{Namespace: "ns", Name: "name"}

v, err := wt.Get(gvk.ConfigMap, key).Get()
g.Expect(err).ShouldNot(HaveOccurred())
g.Expect(v).Should(BeNil())
})

t.Run("Eventually Not Found", func(t *testing.T) {
wt := tc.NewWithT(t)

key := client.ObjectKey{Namespace: "ns", Name: "name"}

v := wt.Get(gvk.ConfigMap, key).Eventually().WithTimeout(1 * time.Second).ShouldNot(matchMetadata)
g.Expect(v).Should(BeNil())
})
}

func TestList(t *testing.T) {
Expand Down

0 comments on commit 73d70b8

Please sign in to comment.