1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/pkg/engine/context/utils_test.go
Jim Bugwadia 46f02a8ba7
optimize JSON context processing using in-memory maps (#8322)
* optimize JSON context processing using in memory maps

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

* fix excessive logs

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

* fix mutate resource diff

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

* uncomment tests

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

* copy resource, as it can be modified

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

* clear prior resource to prevent mutating original

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

* linter fix

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

* fix ImageInfo to unstructured conversion

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

* fix custom image extractors

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

* do not update mutated resource in JSON context

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

* address review comments

Signed-off-by: Jim Bugwadia <jim@nirmata.com>

---------

Signed-off-by: Jim Bugwadia <jim@nirmata.com>
Signed-off-by: shuting <shuting@nirmata.com>
Co-authored-by: Vishal Choudhary <sendtovishalchoudhary@gmail.com>
Co-authored-by: shuting <shuting@nirmata.com>
2023-12-04 07:35:36 +00:00

144 lines
3.3 KiB
Go

package context
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMergeMaps(t *testing.T) {
map1 := map[string]interface{}{
"strVal": "bar1",
"strVal2": "bar2",
"intVal": 2,
"arrayVal": []string{"1", "2", "3"},
"mapVal": map[string]interface{}{
"foo": "bar",
},
"mapVal2": map[string]interface{}{
"foo2": map[string]interface{}{
"foo3": 3,
},
},
}
map2 := map[string]interface{}{
"strVal": "bar2",
"intVal": 3,
"intVal2": 3,
"arrayVal": []string{"1", "2", "3", "4"},
"mapVal": map[string]interface{}{
"foo1": "bar1",
"foo2": "bar2",
},
}
mergeMaps(map1, map2)
assert.Equal(t, "bar1", map2["strVal"])
assert.Equal(t, "bar2", map2["strVal2"])
assert.Equal(t, 2, map2["intVal"])
assert.Equal(t, 3, map2["intVal2"])
assert.Equal(t, []string{"1", "2", "3"}, map2["arrayVal"])
assert.Equal(t, map[string]interface{}{"foo": "bar", "foo1": "bar1", "foo2": "bar2"}, map2["mapVal"])
assert.Equal(t, map1["mapVal2"], map2["mapVal2"])
requestObj := map[string]interface{}{
"request": map[string]interface{}{
"object": map[string]interface{}{
"foo": "bar",
},
},
}
ctxMap := map[string]interface{}{}
mergeMaps(requestObj, ctxMap)
r := ctxMap["request"].(map[string]interface{})
o := r["object"].(map[string]interface{})
assert.Equal(t, o["foo"], "bar")
requestObj2 := map[string]interface{}{
"request": map[string]interface{}{
"object": map[string]interface{}{
"foo": "bar2",
"foo2": "bar2",
},
},
}
mergeMaps(requestObj2, ctxMap)
r2 := ctxMap["request"].(map[string]interface{})
o2 := r2["object"].(map[string]interface{})
assert.Equal(t, "bar2", o2["foo"])
assert.Equal(t, "bar2", o2["foo2"])
request3 := map[string]interface{}{
"request": map[string]interface{}{
"userInfo": "user1",
},
}
mergeMaps(request3, ctxMap)
r3 := ctxMap["request"].(map[string]interface{})
o3 := r3["object"].(map[string]interface{})
assert.NotNil(t, o3)
assert.Equal(t, "bar2", o2["foo"])
assert.Equal(t, "bar2", o2["foo2"])
assert.Equal(t, "user1", r3["userInfo"])
}
func TestStructToUntypedMap(t *testing.T) {
type SampleStuct struct {
Name string `json:"name"`
ID int32 `json:"identifier"`
}
sample := &SampleStuct{
Name: "user1",
ID: 12345,
}
result, err := toUnstructured(sample)
assert.Nil(t, err)
assert.Equal(t, "user1", result["name"])
assert.Equal(t, int64(12345), result["identifier"])
}
func TestClearLeaf(t *testing.T) {
request := map[string]interface{}{
"request": map[string]interface{}{
"object": map[string]interface{}{
"key1": "val1",
"key2": "val2",
},
},
}
result := clearLeafValue(request, "request", "object", "key1")
assert.True(t, result)
r := request["request"].(map[string]interface{})
o := r["object"].(map[string]interface{})
_, exists := o["key1"]
assert.Equal(t, false, exists)
_, exists = o["key2"]
assert.Equal(t, true, exists)
result = clearLeafValue(request, "request", "object", "key3")
assert.True(t, result)
_, exists = o["key3"]
assert.Equal(t, false, exists)
result = clearLeafValue(request, "request", "object-bad", "key3")
assert.Equal(t, false, result)
result = clearLeafValue(request, "request", "object")
assert.True(t, result)
_, exists = r["object"]
assert.Equal(t, false, exists)
}