2019-12-31 01:08:50 +00:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2020-01-07 18:33:28 +00:00
|
|
|
kyverno "github.com/nirmata/kyverno/pkg/api/kyverno/v1"
|
2019-12-31 01:08:50 +00:00
|
|
|
authenticationv1 "k8s.io/api/authentication/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_addResourceAndUserContext(t *testing.T) {
|
|
|
|
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{
|
2020-01-07 23:13:57 +00:00
|
|
|
Username: "system:serviceaccount:nirmata:user1",
|
2019-12-31 01:08:50 +00:00
|
|
|
UID: "014fbff9a07c",
|
|
|
|
}
|
2020-01-07 18:33:28 +00:00
|
|
|
userRequestInfo := kyverno.RequestInfo{
|
|
|
|
Roles: nil,
|
|
|
|
ClusterRoles: nil,
|
|
|
|
AdmissionUserInfo: userInfo}
|
2019-12-31 01:08:50 +00:00
|
|
|
|
|
|
|
var expectedResult string
|
|
|
|
ctx := NewContext()
|
|
|
|
ctx.AddResource(rawResource)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2020-01-07 18:33:28 +00:00
|
|
|
ctx.AddUserInfo(userRequestInfo)
|
2019-12-31 01:08:50 +00:00
|
|
|
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)
|
|
|
|
}
|
2020-01-07 23:13:57 +00:00
|
|
|
expectedResult = "system:serviceaccount:nirmata:user1"
|
|
|
|
t.Log(result)
|
|
|
|
if !reflect.DeepEqual(expectedResult, result) {
|
|
|
|
t.Error("exected result does not match")
|
|
|
|
}
|
|
|
|
// Add service account Name
|
|
|
|
ctx.AddSA(userRequestInfo.AdmissionUserInfo.Username)
|
|
|
|
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"
|
2019-12-31 01:08:50 +00:00
|
|
|
t.Log(result)
|
|
|
|
if !reflect.DeepEqual(expectedResult, result) {
|
|
|
|
t.Error("exected result does not match")
|
|
|
|
}
|
|
|
|
}
|