1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-15 17:51:20 +00:00
kyverno/pkg/engine/mutate/overlayCondition_test.go

692 lines
12 KiB
Go
Raw Normal View History

package mutate
2019-07-26 20:41:39 +00:00
import (
"encoding/json"
2019-11-07 00:16:38 +00:00
"reflect"
"strings"
2019-07-26 20:41:39 +00:00
"testing"
"gotest.tools/assert"
2020-03-17 23:25:34 +00:00
"sigs.k8s.io/controller-runtime/pkg/log"
2019-07-26 20:41:39 +00:00
)
func TestMeetConditions_NoAnchor(t *testing.T) {
overlayRaw := []byte(`
{
"subsets":[
{
"ports":[
{
"name":"secure-connection",
"port":444,
"protocol":"UDP"
}
]
}
]
}`)
var overlay interface{}
err := json.Unmarshal(overlayRaw, &overlay)
assert.Assert(t, reflect.DeepEqual(err, nil))
2019-07-26 20:41:39 +00:00
2020-03-27 09:37:14 +00:00
_, err = meetConditions(log.Log, nil, overlay)
2019-07-26 20:41:39 +00:00
2019-11-07 00:16:38 +00:00
assert.Assert(t, reflect.DeepEqual(err, overlayError{}))
2019-07-26 20:41:39 +00:00
}
2019-11-01 03:38:24 +00:00
func TestMeetConditions_conditionalAnchorOnMap(t *testing.T) {
2019-07-26 20:41:39 +00:00
resourceRaw := []byte(`
{
"apiVersion":"v1",
"kind":"Endpoints",
"metadata":{
"name":"test-endpoint",
"labels":{
"label":"test"
}
},
"subsets":[
{
"addresses":[
{
"ip":"192.168.10.171"
}
],
"ports":[
{
"name":"secure-connection",
"port":443,
"protocol":"TCP"
}
]
}
]
}`)
overlayRaw := []byte(`
{
"subsets":[
{
"(ports)":[
{
"name":"secure-connection",
"port":444,
"protocol":"UDP"
}
]
}
]
}`)
var resource, overlay interface{}
err := json.Unmarshal(resourceRaw, &resource)
assert.NilError(t, err)
err = json.Unmarshal(overlayRaw, &overlay)
assert.NilError(t, err)
2019-07-26 20:41:39 +00:00
2020-03-27 09:37:14 +00:00
_, err = meetConditions(log.Log, resource, overlay)
2019-11-07 00:16:38 +00:00
assert.Assert(t, !reflect.DeepEqual(err, overlayError{}))
2019-07-26 20:41:39 +00:00
overlayRaw = []byte(`
{
"(subsets)":[
{
"ports":[
{
"name":"secure-connection",
2019-11-01 03:38:24 +00:00
"port":443,
"(protocol)":"TCP"
2019-07-26 20:41:39 +00:00
}
]
}
]
}`)
err = json.Unmarshal(overlayRaw, &overlay)
assert.NilError(t, err)
2019-07-26 20:41:39 +00:00
2020-03-17 23:25:34 +00:00
_, overlayerr := meetConditions(log.Log, resource, overlay)
2019-11-07 00:16:38 +00:00
assert.Assert(t, reflect.DeepEqual(overlayerr, overlayError{}))
2019-07-26 20:41:39 +00:00
}
func TestMeetConditions_DifferentTypes(t *testing.T) {
resourceRaw := []byte(`
{
"apiVersion": "v1",
"kind": "Endpoints",
"metadata": {
"name": "test-endpoint"
2019-07-26 20:41:39 +00:00
},
"subsets": {
"addresses": {
"ip": "192.168.10.171"
2019-07-26 20:41:39 +00:00
}
}
}`)
2019-07-26 20:41:39 +00:00
overlayRaw := []byte(`
{
"subsets":[
{
"ports":[
{
"(name)":"secure-connection",
"port":444,
"protocol":"UDP"
}
]
}
]
}`)
var resource, overlay interface{}
err := json.Unmarshal(resourceRaw, &resource)
assert.NilError(t, err)
err = json.Unmarshal(overlayRaw, &overlay)
assert.NilError(t, err)
2019-07-26 20:41:39 +00:00
// anchor exist
2020-03-27 09:37:14 +00:00
_, err = meetConditions(log.Log, resource, overlay)
assert.Assert(t, strings.Contains(err.Error(), "element type mismatch at path /subsets/"))
2019-07-27 00:52:26 +00:00
}
2019-07-26 20:41:39 +00:00
2019-07-27 00:52:26 +00:00
func TestMeetConditions_anchosInSameObject(t *testing.T) {
resourceRaw := []byte(`
2019-07-26 20:41:39 +00:00
{
2019-07-27 00:52:26 +00:00
"apiVersion":"v1",
"kind":"Endpoints",
"metadata":{
"name":"test-endpoint",
"labels":{
"label":"test"
}
},
2019-07-26 20:41:39 +00:00
"subsets":[
{
2019-07-27 00:52:26 +00:00
"addresses":[
{
"ip":"192.168.10.171"
}
],
2019-07-26 20:41:39 +00:00
"ports":[
{
"name":"secure-connection",
2019-07-27 00:52:26 +00:00
"port":443,
"protocol":"TCP"
}
]
}
]
}`)
overlayRaw := []byte(`
{
"subsets":[
{
"ports":[
{
"(name)":"secure-connection",
"(port)":444,
2019-07-26 20:41:39 +00:00
"protocol":"UDP"
}
]
}
]
}`)
2019-07-27 00:52:26 +00:00
var resource, overlay interface{}
err := json.Unmarshal(resourceRaw, &resource)
assert.NilError(t, err)
err = json.Unmarshal(overlayRaw, &overlay)
assert.NilError(t, err)
2019-07-26 20:41:39 +00:00
2020-03-27 09:37:14 +00:00
_, err = meetConditions(log.Log, resource, overlay)
2019-12-10 17:15:50 +00:00
assert.Error(t, err, "[overlayError:0] Failed validating value 443 with overlay 444")
2019-07-26 20:41:39 +00:00
}
2019-07-27 00:52:26 +00:00
func TestMeetConditions_anchorOnPeer(t *testing.T) {
2019-07-26 20:41:39 +00:00
resourceRaw := []byte(`
{
"apiVersion":"v1",
"kind":"Endpoints",
"metadata":{
"name":"test-endpoint",
"labels":{
"label":"test"
}
},
"subsets":[
{
"addresses":[
{
"ip":"192.168.10.171"
}
],
"ports":[
{
"name":"secure-connection",
"port":443,
"protocol":"TCP"
}
]
}
]
}`)
overlayRaw := []byte(`
{
"subsets":[
{
"addresses":[
{
"(ip)":"192.168.10.171"
}
],
"ports":[
{
"(name)":"secure-connection",
"port":444,
"protocol":"UDP"
}
]
}
]
}`)
var resource, overlay interface{}
err := json.Unmarshal(resourceRaw, &resource)
assert.NilError(t, err)
err = json.Unmarshal(overlayRaw, &overlay)
assert.NilError(t, err)
2020-03-27 09:37:14 +00:00
_, err = meetConditions(log.Log, resource, overlay)
2019-11-07 00:16:38 +00:00
assert.Assert(t, reflect.DeepEqual(err, overlayError{}))
2019-07-26 20:41:39 +00:00
}
func TestMeetConditions_anchorsOnMetaAndSpec(t *testing.T) {
overlayRaw := []byte(`{
"spec": {
"template": {
"metadata": {
"labels": {
"(app)": "nginx"
}
},
"spec": {
"containers": [
{
"(image)": "*:latest",
"imagePullPolicy": "IfNotPresent",
"ports": [
{
"containerPort": 8080
}
]
}
]
}
}
}
}`)
resourceRaw := []byte(`{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "nginx-deployment",
"labels": {
"app": "nginx"
}
},
"spec": {
"replicas": 1,
"selector": {
"matchLabels": {
"app": "nginx"
}
},
"template": {
"metadata": {
"labels": {
"app": "nginx"
}
},
"spec": {
"containers": [
{
"name": "nginx",
"image": "nginx:latest",
"ports": [
{
"containerPort": 80
}
]
},
{
"name": "ghost",
"image": "ghost:latest"
}
]
}
}
}
}`)
var resource, overlay interface{}
err := json.Unmarshal(resourceRaw, &resource)
assert.NilError(t, err)
err = json.Unmarshal(overlayRaw, &overlay)
assert.NilError(t, err)
2019-07-26 20:41:39 +00:00
2020-03-27 09:37:14 +00:00
_, err = meetConditions(log.Log, resource, overlay)
2019-07-26 20:41:39 +00:00
2019-11-07 00:16:38 +00:00
assert.Assert(t, reflect.DeepEqual(err, overlayError{}))
2019-07-26 20:41:39 +00:00
}
var resourceRawAnchorOnPeers = []byte(`{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "psp-demo-unprivileged",
"labels": {
"app.type": "prod"
}
},
"spec": {
"replicas": 1,
"selector": {
"matchLabels": {
"app": "psp"
}
},
"template": {
"metadata": {
"labels": {
"app": "psp"
}
},
"spec": {
"securityContext": {
"runAsNonRoot": true
},
"containers": [
{
"name": "sec-ctx-unprivileged",
"image": "nginxinc/nginx-unprivileged",
"securityContext": {
"runAsNonRoot": true,
"allowPrivilegeEscalation": false
},
"env": [
{
"name": "ENV_KEY",
"value": "ENV_VALUE"
}
]
}
]
}
}
}
}`)
func TestMeetConditions_anchorsOnPeer_single(t *testing.T) {
overlayRaw := []byte(`{
"spec": {
"template": {
"spec": {
"containers": [
{
"(image)": "*/nginx-unprivileged",
"securityContext": {
"runAsNonRoot": true,
"allowPrivilegeEscalation": false
},
"env": [
{
"name": "ENV_KEY",
"value": "ENV_VALUE"
}
]
}
]
}
}
}
}`)
var resource, overlay interface{}
err := json.Unmarshal(resourceRawAnchorOnPeers, &resource)
assert.NilError(t, err)
err = json.Unmarshal(overlayRaw, &overlay)
assert.NilError(t, err)
2019-07-26 20:41:39 +00:00
2020-03-27 09:37:14 +00:00
_, err = meetConditions(log.Log, resource, overlay)
2019-07-26 20:41:39 +00:00
2019-11-07 00:16:38 +00:00
assert.Assert(t, reflect.DeepEqual(err, overlayError{}))
2019-07-26 20:41:39 +00:00
}
func TestMeetConditions_anchorsOnPeer_two(t *testing.T) {
overlayRaw := []byte(`{
"spec": {
"template": {
"spec": {
"containers": [
{
"(image)": "*/nginx-unprivileged",
"securityContext": {
"(runAsNonRoot)": false,
2019-07-26 20:41:39 +00:00
"allowPrivilegeEscalation": false
},
"env": [
{
"name": "ENV_KEY",
"value": "ENV_VALUE"
}
]
}
]
}
}
}
}`)
var resource, overlay interface{}
err := json.Unmarshal(resourceRawAnchorOnPeers, &resource)
assert.NilError(t, err)
err = json.Unmarshal(overlayRaw, &overlay)
assert.NilError(t, err)
2019-07-26 20:41:39 +00:00
2020-03-27 09:37:14 +00:00
_, err = meetConditions(log.Log, resource, overlay)
2019-07-26 20:41:39 +00:00
2019-12-10 17:15:50 +00:00
assert.Error(t, err, "[overlayError:0] Failed validating value true with overlay false")
2019-07-26 20:41:39 +00:00
overlayRaw = []byte(`{
"spec": {
"template": {
"spec": {
"containers": [
{
"(image)": "*/nginx-unprivileged",
"securityContext": {
"runAsNonRoot": true,
"allowPrivilegeEscalation": false
},
"env": [
{
"(name)": "ENV_KEY",
"value": "ENV_VALUE"
}
]
}
]
}
}
}
}`)
err = json.Unmarshal(overlayRaw, &overlay)
assert.NilError(t, err)
2019-07-26 20:41:39 +00:00
2020-03-17 23:25:34 +00:00
_, err = meetConditions(log.Log, resource, overlay)
2019-11-07 00:16:38 +00:00
assert.Assert(t, reflect.DeepEqual(err, overlayError{}))
2019-07-26 20:41:39 +00:00
overlayRaw = []byte(`{
"spec": {
"template": {
"spec": {
"containers": [
{
"image": "*/nginx-unprivileged",
"securityContext": {
"runAsNonRoot": true,
"(allowPrivilegeEscalation)": false
},
"env": [
{
"(name)": "ENV_KEY",
"value": "ENV_VALUE"
}
]
}
]
}
}
}
}`)
err = json.Unmarshal(overlayRaw, &overlay)
assert.NilError(t, err)
2019-07-26 20:41:39 +00:00
2020-03-17 23:25:34 +00:00
_, err = meetConditions(log.Log, resource, overlay)
2019-11-07 00:16:38 +00:00
assert.Assert(t, reflect.DeepEqual(err, overlayError{}))
2019-07-26 20:41:39 +00:00
}
func TestMeetConditions_anchorsOnPeer_multiple(t *testing.T) {
overlayRaw := []byte(`{
"spec": {
"template": {
"spec": {
"containers": [
{
"(image)": "*/nginx-unprivileged",
"securityContext": {
"(runAsNonRoot)": true,
"allowPrivilegeEscalation": false
},
"env": [
{
"(name)": "ENV_KEY",
"value": "ENV_VALUE"
}
]
}
]
}
}
}
}`)
var resource, overlay interface{}
err := json.Unmarshal(resourceRawAnchorOnPeers, &resource)
assert.NilError(t, err)
err = json.Unmarshal(overlayRaw, &overlay)
assert.NilError(t, err)
2019-07-26 20:41:39 +00:00
2020-03-27 09:37:14 +00:00
_, err = meetConditions(log.Log, resource, overlay)
2019-07-26 20:41:39 +00:00
2019-11-07 00:16:38 +00:00
assert.Assert(t, reflect.DeepEqual(err, overlayError{}))
2019-07-26 20:41:39 +00:00
overlayRaw = []byte(`{
"spec": {
"template": {
"spec": {
"containers": [
{
"(image)": "*/nginx-unprivileged",
"securityContext": {
"runAsNonRoot": true,
"(allowPrivilegeEscalation)": false
},
"env": [
{
"(name)": "ENV_KEY",
"value": "ENV_VALUE"
}
]
}
]
}
}
}
}`)
err = json.Unmarshal(overlayRaw, &overlay)
assert.NilError(t, err)
2019-07-26 20:41:39 +00:00
2020-03-17 23:25:34 +00:00
_, err = meetConditions(log.Log, resource, overlay)
2019-11-07 00:16:38 +00:00
assert.Assert(t, reflect.DeepEqual(err, overlayError{}))
2019-07-26 20:41:39 +00:00
overlayRaw = []byte(`{
"spec": {
"template": {
"spec": {
"containers": [
{
"(image)": "*/nginx-unprivileged",
"securityContext": {
"runAsNonRoot": true,
"(allowPrivilegeEscalation)": false
},
"(env)": [
2019-07-26 20:41:39 +00:00
{
"name": "ENV_KEY",
"value": "ENV_VALUE1"
2019-07-26 20:41:39 +00:00
}
]
}
]
}
}
}
}`)
err = json.Unmarshal(overlayRaw, &overlay)
assert.NilError(t, err)
2019-07-26 20:41:39 +00:00
2020-03-17 23:25:34 +00:00
_, err = meetConditions(log.Log, resource, overlay)
2019-12-10 17:15:50 +00:00
assert.Error(t, err, "[overlayError:0] Failed validating value ENV_VALUE with overlay ENV_VALUE1")
2019-07-26 20:41:39 +00:00
}
2019-11-05 19:04:43 +00:00
func TestMeetConditions_AtleastOneExist(t *testing.T) {
overlayRaw := []byte(`
{
"metadata": {
"annotations": {
"+(cluster-autoscaler.kubernetes.io/safe-to-evict)": true
}
},
"spec": {
"volumes": [
{
"(emptyDir)": {}
}
]
}
}`)
// validate when resource has multiple same blocks
resourceRaw := []byte(`
{
"spec": {
"containers": [
{
"image": "k8s.gcr.io/test-webserver",
"name": "test-container",
"volumeMounts": [
{
"mountPath": "/cache",
"name": "cache-volume"
}
]
}
],
"volumes": [
{
"name": "cache-volume1",
"emptyDir": 1
},
{
"name": "cache-volume2",
"emptyDir": 2
},
{
"name": "cache-volume3",
"emptyDir": {}
}
]
}
}`)
var resource, overlay interface{}
err := json.Unmarshal(resourceRaw, &resource)
assert.NilError(t, err)
err = json.Unmarshal(overlayRaw, &overlay)
assert.NilError(t, err)
2019-11-05 19:04:43 +00:00
2020-03-17 23:25:34 +00:00
path, err := meetConditions(log.Log, resource, overlay)
2019-11-05 19:04:43 +00:00
2019-11-07 00:16:38 +00:00
assert.Assert(t, reflect.DeepEqual(err, overlayError{}))
2019-11-05 19:04:43 +00:00
assert.Assert(t, len(path) == 0)
}