diff --git a/pkg/runner/mutate/clean.go b/pkg/runner/mutate/convert.go similarity index 73% rename from pkg/runner/mutate/clean.go rename to pkg/runner/mutate/convert.go index 8ffff534f..38e358972 100644 --- a/pkg/runner/mutate/clean.go +++ b/pkg/runner/mutate/convert.go @@ -1,13 +1,13 @@ package mutate -func clean(in any) map[string]any { +func convert(in any) map[string]any { data, ok := in.(map[any]any) if !ok { return nil } out := map[string]any{} for k, v := range data { - if c := clean(v); c != nil { + if c := convert(v); c != nil { out[k.(string)] = c } else { out[k.(string)] = v diff --git a/pkg/runner/mutate/convert_test.go b/pkg/runner/mutate/convert_test.go new file mode 100644 index 000000000..a2804fd84 --- /dev/null +++ b/pkg/runner/mutate/convert_test.go @@ -0,0 +1,35 @@ +package mutate + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_convert(t *testing.T) { + tests := []struct { + name string + in any + out map[string]any + }{{ + name: "nil", + }, { + name: "int", + in: 42, + out: nil, + }, { + name: "ok", + in: map[any]any{ + "foo": "bar", + }, + out: map[string]any{ + "foo": "bar", + }, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := convert(tt.in) + assert.Equal(t, tt.out, got) + }) + } +} diff --git a/pkg/runner/mutate/merge.go b/pkg/runner/mutate/merge.go index af0e149a6..137447e6b 100644 --- a/pkg/runner/mutate/merge.go +++ b/pkg/runner/mutate/merge.go @@ -46,7 +46,7 @@ func Merge(ctx context.Context, obj unstructured.Unstructured, bindings binding. if err != nil { return obj, err } - obj.SetUnstructuredContent(mapsutils.Merge(obj.UnstructuredContent(), clean(patch))) + obj.SetUnstructuredContent(mapsutils.Merge(obj.UnstructuredContent(), convert(patch))) } } return obj, nil diff --git a/pkg/runner/mutate/merge_test.go b/pkg/runner/mutate/merge_test.go new file mode 100644 index 000000000..6e2199756 --- /dev/null +++ b/pkg/runner/mutate/merge_test.go @@ -0,0 +1,120 @@ +package mutate + +import ( + "context" + "testing" + + "github.com/kyverno/chainsaw/pkg/apis/v1alpha1" + "github.com/stretchr/testify/assert" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" +) + +func TestMerge(t *testing.T) { + in := unstructured.Unstructured{ + Object: map[string]any{ + "foo": "bar", + }, + } + tests := []struct { + name string + in unstructured.Unstructured + out unstructured.Unstructured + modifiers []v1alpha1.Modifier + wantErr bool + }{{ + name: "nil", + in: in, + out: in, + }, { + name: "empty", + in: in, + modifiers: []v1alpha1.Modifier{}, + out: in, + }, { + name: "annotate", + in: in, + modifiers: []v1alpha1.Modifier{{ + Annotate: &v1alpha1.Any{ + Value: map[string]any{ + "foo": "bar", + }, + }, + }}, + out: unstructured.Unstructured{ + Object: map[string]any{ + "foo": "bar", + "metadata": map[string]any{ + "annotations": map[string]any{ + "foo": "bar", + }, + }, + }, + }, + }, { + name: "label", + in: in, + modifiers: []v1alpha1.Modifier{{ + Label: &v1alpha1.Any{ + Value: map[string]any{ + "foo": "bar", + }, + }, + }}, + out: unstructured.Unstructured{ + Object: map[string]any{ + "foo": "bar", + "metadata": map[string]any{ + "labels": map[string]any{ + "foo": "bar", + }, + }, + }, + }, + }, { + name: "merge", + in: in, + modifiers: []v1alpha1.Modifier{{ + Merge: &v1alpha1.Any{ + Value: map[string]any{ + "foo": "baz", + }, + }, + }}, + out: unstructured.Unstructured{ + Object: map[string]any{ + "foo": "baz", + }, + }, + }, { + name: "merge", + in: in, + modifiers: []v1alpha1.Modifier{{ + Match: &v1alpha1.Any{ + Value: map[string]any{ + "foo": "baz", + }, + }, + Merge: &v1alpha1.Any{ + Value: map[string]any{ + "foo": "bar", + }, + }, + }}, + out: unstructured.Unstructured{ + Object: map[string]any{ + "foo": "bar", + }, + }, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := Merge(context.TODO(), tt.in, nil, tt.modifiers...) + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tt.out, got) + } + }) + } +}