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/context_test.go
Jim Bugwadia 9d3b176def
Nested foreach (#5589)
* updated foreach logic and added tests

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

* uncomment tests

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

* fix vars and unit tests

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

* fix vars and unit tests

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

* fix some tests

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

* fix more tests

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

* format

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

* make codegen

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

* linter

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

* cleanup

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

* fix linter issue

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

* revert local launch

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

* propagate context

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

* uncomment tests

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

* fix propagation of registry client

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

Signed-off-by: Jim Bugwadia <jim@nirmata.com>
Co-authored-by: shuting <shuting@nirmata.com>
2022-12-12 15:20:20 +00:00

121 lines
2.5 KiB
Go

package context
import (
"reflect"
"testing"
urkyverno "github.com/kyverno/kyverno/api/kyverno/v1beta1"
authenticationv1 "k8s.io/api/authentication/v1"
)
func Test_addResourceAndUserContext(t *testing.T) {
var err error
rawResource := []byte(`
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "image-with-hostpath",
"labels": {
"app.type": "prod",
"namespace": "my-namespace"
}
},
"spec": {
"containers": [
{
"name": "image-with-hostpath",
"image": "docker.io/nautiker/curl",
"volumeMounts": [
{
"name": "var-lib-etcd",
"mountPath": "/var/lib"
}
]
}
],
"volumes": [
{
"name": "var-lib-etcd",
"emptyDir": {}
}
]
}
}
`)
userInfo := authenticationv1.UserInfo{
Username: "system:serviceaccount:nirmata:user1",
UID: "014fbff9a07c",
}
userRequestInfo := urkyverno.RequestInfo{
Roles: nil,
ClusterRoles: nil,
AdmissionUserInfo: userInfo,
}
var expectedResult string
ctx := NewContext()
err = AddResource(ctx, rawResource)
if err != nil {
t.Error(err)
}
result, err := ctx.Query("request.object.apiVersion")
if err != nil {
t.Error(err)
}
expectedResult = "v1"
t.Log(result)
if !reflect.DeepEqual(expectedResult, result) {
t.Error("exected result does not match")
}
err = ctx.AddUserInfo(userRequestInfo)
if err != nil {
t.Error(err)
}
result, err = ctx.Query("request.object.apiVersion")
if err != nil {
t.Error(err)
}
expectedResult = "v1"
t.Log(result)
if !reflect.DeepEqual(expectedResult, result) {
t.Error("exected result does not match")
}
result, err = ctx.Query("request.userInfo.username")
if err != nil {
t.Error(err)
}
expectedResult = "system:serviceaccount:nirmata:user1"
t.Log(result)
if !reflect.DeepEqual(expectedResult, result) {
t.Error("exected result does not match")
}
// Add service account Name
err = ctx.AddServiceAccount(userRequestInfo.AdmissionUserInfo.Username)
if err != nil {
t.Error(err)
}
result, err = ctx.Query("serviceAccountName")
if err != nil {
t.Error(err)
}
expectedResult = "user1"
t.Log(result)
if !reflect.DeepEqual(expectedResult, result) {
t.Error("exected result does not match")
}
// Add service account Namespace
result, err = ctx.Query("serviceAccountNamespace")
if err != nil {
t.Error(err)
}
expectedResult = "nirmata"
t.Log(result)
if !reflect.DeepEqual(expectedResult, result) {
t.Error("expected result does not match")
}
}