1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-04-08 18:15:48 +00:00

fixed some tests

Signed-off-by: Maxim Goncharenko <goncharenko.maxim@apriorit.com>
This commit is contained in:
Maxim Goncharenko 2021-08-19 19:23:17 +03:00
parent 9a7af4d955
commit d928f97795
3 changed files with 51 additions and 74 deletions

View file

@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"time"
"github.com/go-logr/logr"
@ -30,26 +29,6 @@ func ProcessStrategicMergePatch(ruleName string, overlay interface{}, resource u
logger.V(4).Info("finished applying strategicMerge patch", "processingTime", resp.RuleStats.ProcessingTime.String())
}()
// ====== Meet Conditions =======
if path, overlayerr := meetConditions(log, resource.UnstructuredContent(), overlay); !reflect.DeepEqual(overlayerr, overlayError{}) {
switch overlayerr.statusCode {
// anchor key does not exist in the resource, skip applying policy
case conditionNotPresent:
log.V(4).Info("skip applying policy", "path", path, "error", overlayerr)
log.V(3).Info("skip applying rule", "reason", "conditionNotPresent")
resp.Success = true
return resp, resource
// anchor key is not satisfied in the resource, skip applying policy
case conditionFailure:
log.V(4).Info("failed to validate condition", "path", path, "error", overlayerr)
log.V(3).Info("skip applying rule", "reason", "conditionFailure")
resp.Success = true
resp.Message = overlayerr.ErrorMsg()
return resp, resource
}
}
// ============================
overlayBytes, err := json.Marshal(overlay)
if err != nil {
resp.Success = false
@ -119,9 +98,14 @@ func strategicMergePatch(logger logr.Logger, base, overlay string) ([]byte, erro
return baseObj.Bytes(), err
}
var counter = 1
func preProcessStrategicMergePatch(logger logr.Logger, pattern, resource string) (*yaml.RNode, error) {
patternNode := yaml.MustParse(pattern)
resourceNode := yaml.MustParse(resource)
err := preProcessPattern(logger, patternNode, resourceNode)
counter += 1
return patternNode, err
}

View file

@ -524,7 +524,7 @@ func validateConditionsInternal(logger logr.Logger, pattern, resource *yaml.RNod
for _, condition := range conditions {
conditionKey := removeAnchor(condition)
if resource == nil || resource.Field(conditionKey) == nil {
continue
return fmt.Errorf("could not found \"%s\" key in the resource", conditionKey)
}
err = checkCondition(logger, pattern.Field(condition).Value, resource.Field(conditionKey).Value)

View file

@ -650,58 +650,6 @@ func Test_preProcessStrategicMergePatch_multipleAnchors(t *testing.T) {
}
}`),
},
{
rawPolicy: []byte(`{
"metadata": {
"labels": {
"(key1)": "value1",
}
},
"spec": {
"containers": [
{
"name": "busybox",
"image": "gcr.io/google-containers/busybox:latest"
}
],
"imagePullSecrets": [
{
"name": "regcred"
}
]
}
}`),
rawResource: []byte(`{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "hello"
},
"spec": {
"containers": [
{
"name": "hello",
"image": "busybox"
}
]
}
}`),
expectedPatch: []byte(`{
"spec": {
"containers": [
{
"name": "busybox",
"image": "gcr.io/google-containers/busybox:latest"
}
],
"imagePullSecrets": [
{
"name": "regcred"
}
]
}
}`),
},
{
rawPolicy: []byte(`{
"metadata": {
@ -1014,3 +962,48 @@ func Test_ConditionCheck_SeveralElementsMatchExceptOne(t *testing.T) {
assert.Equal(t, len(elements), 2)
}
func Test_NonExistingKeyMustFailPreprocessing(t *testing.T) {
rawPattern := []byte(`{
"metadata": {
"labels": {
"(key1)": "value1",
}
},
"spec": {
"containers": [
{
"name": "busybox",
"image": "gcr.io/google-containers/busybox:latest"
}
],
"imagePullSecrets": [
{
"name": "regcred"
}
]
}
}`)
rawResource := []byte(`{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "hello"
},
"spec": {
"containers": [
{
"name": "hello",
"image": "busybox"
}
]
}
}`)
pattern := yaml.MustParse(string(rawPattern))
resource := yaml.MustParse(string(rawResource))
err := preProcessPattern(log.Log, pattern, resource)
assert.Error(t, err, "Condition failed: could not found \"key1\" key in the resource")
}