-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
- Loading branch information
1 parent
a1ea0d3
commit 4d9bbf2
Showing
4 changed files
with
158 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |