mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-31 03:45:17 +00:00
fixed tests
Signed-off-by: Maxim Goncharenko <goncharenko.maxim@apriorit.com>
This commit is contained in:
parent
88c4e908d0
commit
9a7af4d955
2 changed files with 141 additions and 21 deletions
|
@ -2,7 +2,6 @@ package mutate
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
kyvernov1 "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
|
||||
|
@ -25,33 +24,149 @@ func TestMergePatch(t *testing.T) {
|
|||
},
|
||||
{
|
||||
// condition matches the first element of the array
|
||||
rawPolicy: []byte(`{"spec": {"containers": [{"(image)": "gcr.io/google-containers/busybox:*"}],"imagePullSecrets": [{"name": "regcred"}]}}`),
|
||||
rawResource: []byte(`{"apiVersion": "v1","kind": "Pod","metadata": {"name": "hello"},"spec": {"containers": [{"name": "hello","image": "gcr.io/google-containers/busybox:latest"},{"name": "hello2","image": "gcr.io/google-containers/busybox:latest"},{"name": "hello3","image": "gcr.io/google-containers/nginx:latest"}]}}`),
|
||||
expected: []byte(`{"apiVersion":"v1","kind":"Pod","metadata":{"name":"hello"},"spec":{"containers":[{"image":"gcr.io/google-containers/busybox:latest","name":"hello"},{"image":"gcr.io/google-containers/busybox:latest","name":"hello2"},{"image":"gcr.io/google-containers/nginx:latest","name":"hello3"}],"imagePullSecrets":[{"name":"regcred"}]}}`),
|
||||
rawPolicy: []byte(`{
|
||||
"spec": {
|
||||
"containers": [
|
||||
{
|
||||
"(image)": "gcr.io/google-containers/busybox:*"
|
||||
}
|
||||
],
|
||||
"imagePullSecrets": [
|
||||
{
|
||||
"name": "regcred"
|
||||
}
|
||||
]
|
||||
}
|
||||
}`),
|
||||
rawResource: []byte(`{
|
||||
"apiVersion": "v1",
|
||||
"kind": "Pod",
|
||||
"metadata": {
|
||||
"name": "hello"
|
||||
},
|
||||
"spec": {
|
||||
"containers": [
|
||||
{
|
||||
"name": "hello",
|
||||
"image": "gcr.io/google-containers/busybox:latest"
|
||||
},
|
||||
{
|
||||
"name": "hello2",
|
||||
"image": "gcr.io/google-containers/busybox:latest"
|
||||
},
|
||||
{
|
||||
"name": "hello3",
|
||||
"image": "gcr.io/google-containers/nginx:latest"
|
||||
}
|
||||
]
|
||||
}
|
||||
}`),
|
||||
expected: []byte(`{
|
||||
"apiVersion": "v1",
|
||||
"kind": "Pod",
|
||||
"metadata": {
|
||||
"name": "hello"
|
||||
},
|
||||
"spec": {
|
||||
"containers": [
|
||||
{
|
||||
"image": "gcr.io/google-containers/busybox:latest",
|
||||
"name": "hello"
|
||||
},
|
||||
{
|
||||
"image": "gcr.io/google-containers/busybox:latest",
|
||||
"name": "hello2"
|
||||
},
|
||||
{
|
||||
"image": "gcr.io/google-containers/nginx:latest",
|
||||
"name": "hello3"
|
||||
}
|
||||
],
|
||||
"imagePullSecrets": [
|
||||
{
|
||||
"name": "regcred"
|
||||
}
|
||||
]
|
||||
}
|
||||
}`),
|
||||
},
|
||||
{
|
||||
// condition matches the third element of the array
|
||||
rawPolicy: []byte(`{"spec": {"containers": [{"(image)": "gcr.io/google-containers/nginx:*"}],"imagePullSecrets": [{"name": "regcred"}]}}`),
|
||||
rawResource: []byte(`{"apiVersion": "v1","kind": "Pod","metadata": {"name": "hello"},"spec": {"containers": [{"name": "hello","image": "gcr.io/google-containers/busybox:latest"},{"name": "hello2","image": "gcr.io/google-containers/busybox:latest"},{"name": "hello3","image": "gcr.io/google-containers/nginx:latest"}]}}`),
|
||||
expected: []byte(`{"apiVersion":"v1","kind":"Pod","metadata":{"name":"hello"},"spec":{"containers":[{"image":"gcr.io/google-containers/nginx:latest","name":"hello3"},{"image":"gcr.io/google-containers/busybox:latest","name":"hello"},{"image":"gcr.io/google-containers/busybox:latest","name":"hello2"}],"imagePullSecrets":[{"name":"regcred"}]}}`),
|
||||
rawPolicy: []byte(`{
|
||||
"spec": {
|
||||
"containers": [
|
||||
{
|
||||
"(image)": "gcr.io/google-containers/nginx:*"
|
||||
}
|
||||
],
|
||||
"imagePullSecrets": [
|
||||
{
|
||||
"name": "regcred"
|
||||
}
|
||||
]
|
||||
}
|
||||
}`),
|
||||
rawResource: []byte(`{
|
||||
"apiVersion": "v1",
|
||||
"kind": "Pod",
|
||||
"metadata": {
|
||||
"name": "hello"
|
||||
},
|
||||
"spec": {
|
||||
"containers": [
|
||||
{
|
||||
"name": "hello",
|
||||
"image": "gcr.io/google-containers/busybox:latest"
|
||||
},
|
||||
{
|
||||
"name": "hello2",
|
||||
"image": "gcr.io/google-containers/busybox:latest"
|
||||
},
|
||||
{
|
||||
"name": "hello3",
|
||||
"image": "gcr.io/google-containers/nginx:latest"
|
||||
}
|
||||
]
|
||||
}
|
||||
}`),
|
||||
expected: []byte(`{
|
||||
"apiVersion": "v1",
|
||||
"kind": "Pod",
|
||||
"metadata": {
|
||||
"name": "hello"
|
||||
},
|
||||
"spec": {
|
||||
"containers": [
|
||||
{
|
||||
"image": "gcr.io/google-containers/busybox:latest",
|
||||
"name": "hello"
|
||||
},
|
||||
{
|
||||
"image": "gcr.io/google-containers/busybox:latest",
|
||||
"name": "hello2"
|
||||
},
|
||||
{
|
||||
"image": "gcr.io/google-containers/nginx:latest",
|
||||
"name": "hello3"
|
||||
}
|
||||
],
|
||||
"imagePullSecrets": [
|
||||
{
|
||||
"name": "regcred"
|
||||
}
|
||||
]
|
||||
}
|
||||
}`),
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range testCases {
|
||||
|
||||
// out
|
||||
t.Logf("Running test %d...", i+1)
|
||||
out, err := strategicMergePatch(log.Log, string(test.rawResource), string(test.rawPolicy))
|
||||
assert.NilError(t, err)
|
||||
|
||||
// expect
|
||||
var expectUnstr unstructured.Unstructured
|
||||
err = json.Unmarshal(test.expected, &expectUnstr)
|
||||
assert.NilError(t, err)
|
||||
|
||||
expectString, err := json.Marshal(expectUnstr.Object)
|
||||
assert.NilError(t, err)
|
||||
|
||||
assertnew.Equal(t, string(expectString), string(out), fmt.Sprintf("test %v fails", i))
|
||||
// has assertions inside
|
||||
areEqualJSONs(t, test.expected, out)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -976,7 +976,8 @@ func Test_ConditionCheck_SeveralElementsMatchExceptOne(t *testing.T) {
|
|||
patternRaw := []byte(`{
|
||||
"containers": [
|
||||
{
|
||||
"(image)": "gcr.io/google-containers/busybox:*"
|
||||
"(name)": "hello?",
|
||||
"image": "gcr.io/google-containers/busybox:1"
|
||||
}
|
||||
]
|
||||
}`)
|
||||
|
@ -1004,8 +1005,12 @@ func Test_ConditionCheck_SeveralElementsMatchExceptOne(t *testing.T) {
|
|||
err := preProcessPattern(log.Log, pattern, containers)
|
||||
assert.NilError(t, err)
|
||||
|
||||
containersElements, err := pattern.Field("containers").Value.Elements()
|
||||
patternContainers := pattern.Field("containers")
|
||||
assert.Assert(t, patternContainers != nil)
|
||||
assert.Assert(t, patternContainers.Value != nil)
|
||||
|
||||
elements, err := patternContainers.Value.Elements()
|
||||
assert.NilError(t, err)
|
||||
|
||||
assert.Equal(t, len(containersElements), 2)
|
||||
assert.Equal(t, len(elements), 2)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue