1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/pkg/engine/context/evaluate_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

88 lines
2.1 KiB
Go

package context
import (
"testing"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
"github.com/stretchr/testify/assert"
admissionv1 "k8s.io/api/admission/v1"
)
func TestHasChanged(t *testing.T) {
ctx := createTestContext(`{"a": {"b": 1, "c": 2}, "d": 3}`, `{"a": {"b": 2, "c": 2}, "d": 4}`)
val, err := ctx.HasChanged("a.b")
assert.NoError(t, err)
assert.True(t, val)
val, err = ctx.HasChanged("a.c")
assert.NoError(t, err)
assert.False(t, val)
val, err = ctx.HasChanged("d")
assert.NoError(t, err)
assert.True(t, val)
_, err = ctx.HasChanged("a.x.y")
assert.Error(t, err)
}
func TestRequestNotInitialize(t *testing.T) {
request := admissionv1.AdmissionRequest{}
ctx := NewContext(jp)
ctx.AddRequest(request)
_, err := ctx.HasChanged("x.y.z")
assert.Error(t, err)
}
func TestMissingOldObject(t *testing.T) {
request := admissionv1.AdmissionRequest{}
ctx := NewContext(jp)
ctx.AddRequest(request)
request.Object.Raw = []byte(`{"a": {"b": 1, "c": 2}, "d": 3}`)
_, err := ctx.HasChanged("a.b")
assert.Error(t, err)
}
func TestMissingObject(t *testing.T) {
request := admissionv1.AdmissionRequest{}
ctx := NewContext(jp)
ctx.AddRequest(request)
request.OldObject.Raw = []byte(`{"a": {"b": 1, "c": 2}, "d": 3}`)
_, err := ctx.HasChanged("a.b")
assert.Error(t, err)
}
func createTestContext(obj, oldObj string) Interface {
request := admissionv1.AdmissionRequest{}
request.Operation = "UPDATE"
request.Object.Raw = []byte(obj)
request.OldObject.Raw = []byte(oldObj)
ctx := NewContext(jp)
ctx.AddRequest(request)
return ctx
}
func TestQueryOperation(t *testing.T) {
ctx := createTestContext(`{"a": {"b": 1, "c": 2}, "d": 3}`, `{"a": {"b": 2, "c": 2}, "d": 4}`)
assert.Equal(t, ctx.QueryOperation(), "UPDATE")
request := admissionv1.AdmissionRequest{
Operation: admissionv1.Delete,
}
err := ctx.AddRequest(request)
assert.Nil(t, err)
assert.Equal(t, ctx.QueryOperation(), "DELETE")
err = ctx.AddOperation(string(kyvernov1.Connect))
assert.Nil(t, err)
assert.Equal(t, ctx.QueryOperation(), "CONNECT")
err = ctx.AddRequest(admissionv1.AdmissionRequest{})
assert.Nil(t, err)
assert.Equal(t, ctx.QueryOperation(), "")
}