Skip to content

Commit

Permalink
chore: add unit tests for the utils package
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly committed Mar 5, 2024
1 parent 2d31993 commit 0e69d95
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 12 deletions.
19 changes: 9 additions & 10 deletions pkg/commands/assert/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
"os"
"path"
"path/filepath"
"testing"
"time"

fakeClient "github.com/kyverno/chainsaw/pkg/client/testing"
tclient "github.com/kyverno/chainsaw/pkg/client/testing"
"github.com/kyverno/chainsaw/pkg/commands/root"
fakeNamespacer "github.com/kyverno/chainsaw/pkg/runner/namespacer/testing"
mockReader "github.com/kyverno/chainsaw/pkg/utils/reader"
"github.com/kyverno/chainsaw/pkg/testing"
"github.com/spf13/cobra"
testify "github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -131,7 +130,7 @@ func Test_runE(t *testing.T) {
name string
setupFunc func() *cobra.Command
opts options
client *fakeClient.FakeClient
client *tclient.FakeClient
nspacer *fakeNamespacer.FakeNamespacer
wantErrMsg string
wantErr bool
Expand All @@ -150,7 +149,7 @@ func Test_runE(t *testing.T) {
namespace: "default",
timeout: metav1.Duration{Duration: 5 * time.Second},
},
client: &fakeClient.FakeClient{
client: &tclient.FakeClient{
GetFn: func(ctx context.Context, call int, key ctrlclient.ObjectKey, obj ctrlclient.Object, opts ...ctrlclient.GetOption) error {
obj.(*unstructured.Unstructured).Object = map[string]any{
"apiVersion": "v1",
Expand Down Expand Up @@ -189,7 +188,7 @@ func Test_runE(t *testing.T) {
namespace: "default",
timeout: metav1.Duration{Duration: 5 * time.Second},
},
client: &fakeClient.FakeClient{
client: &tclient.FakeClient{
GetFn: func(ctx context.Context, call int, key ctrlclient.ObjectKey, obj ctrlclient.Object, opts ...ctrlclient.GetOption) error {
obj.(*unstructured.Unstructured).Object = map[string]any{
"apiVersion": "v1",
Expand Down Expand Up @@ -243,7 +242,7 @@ func Test_runE(t *testing.T) {
namespace: "default",
timeout: metav1.Duration{Duration: 5 * time.Second},
},
client: &fakeClient.FakeClient{
client: &tclient.FakeClient{
GetFn: func(ctx context.Context, call int, key ctrlclient.ObjectKey, obj ctrlclient.Object, opts ...ctrlclient.GetOption) error {
obj.(*unstructured.Unstructured).Object = map[string]any{
"apiVersion": "v1",
Expand Down Expand Up @@ -290,7 +289,7 @@ data:
namespace: "default",
timeout: metav1.Duration{Duration: 5 * time.Second},
},
client: &fakeClient.FakeClient{
client: &tclient.FakeClient{
GetFn: func(ctx context.Context, call int, key ctrlclient.ObjectKey, obj ctrlclient.Object, opts ...ctrlclient.GetOption) error {
obj.(*unstructured.Unstructured).Object = map[string]any{
"apiVersion": "v1",
Expand Down Expand Up @@ -321,7 +320,7 @@ data:
cmd.Args = cobra.RangeArgs(0, 1)
cmd.SilenceUsage = true
cmd.SetOut(bytes.NewBufferString(""))
cmd.SetIn(&mockReader.ErrReader{})
cmd.SetIn(&testing.ErrReader{})
return cmd
},
opts: options{
Expand All @@ -330,7 +329,7 @@ data:
namespace: "default",
timeout: metav1.Duration{Duration: 5 * time.Second},
},
client: &fakeClient.FakeClient{
client: &tclient.FakeClient{
GetFn: func(ctx context.Context, call int, key ctrlclient.ObjectKey, obj ctrlclient.Object, opts ...ctrlclient.GetOption) error {
obj.(*unstructured.Unstructured).Object = map[string]any{
"apiVersion": "v1",
Expand Down
6 changes: 4 additions & 2 deletions pkg/utils/reader/mockstdin.go → pkg/testing/err_reader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package reader
package testing

import "errors"
import (
"errors"
)

type ErrReader struct{}

Expand Down
65 changes: 65 additions & 0 deletions pkg/utils/diff/diff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package diff

import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func TestPrettyDiff(t *testing.T) {
var pod unstructured.Unstructured
pod.SetAPIVersion("v1")
pod.SetKind("Pod")
pod.SetName("foo")
pod.SetNamespace("bar")
pod.SetAnnotations(map[string]string{"bar": "baz"})
assert.NoError(t, unstructured.SetNestedSlice(pod.UnstructuredContent(), []any{"foo"}, "spec", "data"))
assert.NoError(t, unstructured.SetNestedMap(pod.UnstructuredContent(), map[string]any{"foo": "foos"}, "spec", "something"))
tests := []struct {
name string
expected unstructured.Unstructured
actual unstructured.Unstructured
want string
wantErr bool
}{{

Check failure on line 25 in pkg/utils/diff/diff_test.go

View workflow job for this annotation

GitHub Actions / required

File is not `gofumpt`-ed (gofumpt)
name: "empty",
}, {
name: "same",
expected: pod,
actual: pod,
}, {
name: "same",
expected: pod,
actual: func() unstructured.Unstructured {
pod := pod.DeepCopy()
pod.SetResourceVersion("123")
assert.NoError(t, unstructured.SetNestedMap(pod.UnstructuredContent(), map[string]any{"bar": "bars"}, "spec", "something", "else"))
assert.NoError(t, unstructured.SetNestedSlice(pod.UnstructuredContent(), []any{"bar"}, "status", "data"))
pod.SetLabels(map[string]string{"foo": "bar"})
return *pod
}(),
}, {
name: "different",
expected: func() unstructured.Unstructured {
pod := pod.DeepCopy()
pod.SetResourceVersion("123")
return *pod
}(),
actual: pod,
want: "--- expected\n+++ actual\n@@ -5,7 +5,6 @@\n bar: baz\n name: foo\n namespace: bar\n- resourceVersion: \"123\"\n spec:\n data:\n - foo\n",
},
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := PrettyDiff(tt.expected, tt.actual)
assert.Equal(t, tt.want, got)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
94 changes: 94 additions & 0 deletions pkg/utils/maps/merge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package maps

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMerge(t *testing.T) {
tests := []struct {
name string
a map[string]any
b map[string]any
want map[string]any
}{{
name: "both null",
want: map[string]any{},
}, {
name: "a null",
b: map[string]any{},
want: map[string]any{},
}, {
name: "b null",
a: map[string]any{},
want: map[string]any{},
}, {
name: "both empty",
a: map[string]any{},
b: map[string]any{},
want: map[string]any{},
}, {
name: "",
a: map[string]any{
"foo": "bar",
},
b: map[string]any{
"foo": 42,
},
want: map[string]any{
"foo": 42,
},
}, {
name: "",
a: map[string]any{
"foo": map[string]any{
"bar": 42,
},
},
b: map[string]any{
"foo": nil,
},
want: map[string]any{
"foo": nil,
},
}, {
name: "",
a: map[string]any{
"foo": map[string]any{
"bar": 42,
},
},
b: map[string]any{
"foo": map[string]any{
"bar": nil,
},
},
want: map[string]any{
"foo": map[string]any{
"bar": nil,
},
},
}, {
name: "",
a: map[string]any{
"foo": 42,
},
b: map[string]any{
"foo": map[string]any{
"bar": nil,
},
},
want: map[string]any{
"foo": map[string]any{
"bar": nil,
},
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Merge(tt.a, tt.b)
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit 0e69d95

Please sign in to comment.