Skip to content

Commit

Permalink
chore: add modifiers tests (#853)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Feb 12, 2024
1 parent a1ea0d3 commit 4d9bbf2
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pkg/runner/mutate/clean.go → pkg/runner/mutate/convert.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
35 changes: 35 additions & 0 deletions pkg/runner/mutate/convert_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
2 changes: 1 addition & 1 deletion pkg/runner/mutate/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
120 changes: 120 additions & 0 deletions pkg/runner/mutate/merge_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 4d9bbf2

Please sign in to comment.