From 50610af82d2e5e784e6ba145174f199090e8b565 Mon Sep 17 00:00:00 2001 From: Denis Belyshev Date: Wed, 5 Jun 2019 17:35:34 +0300 Subject: [PATCH] Added nil support --- pkg/engine/validation.go | 4 +- pkg/engine/validation_test.go | 179 ++++++++++++++++++++++++++++++++++ 2 files changed, 181 insertions(+), 2 deletions(-) diff --git a/pkg/engine/validation.go b/pkg/engine/validation.go index dd1256be16..3e25c8d1e4 100644 --- a/pkg/engine/validation.go +++ b/pkg/engine/validation.go @@ -70,7 +70,7 @@ func validateResourceElement(value, pattern interface{}, path string) result.Rul } return validateArray(typedValue, typedPattern, path) - case string, float64, int, int64, bool: + case string, float64, int, int64, bool, nil: if !ValidateValueWithPattern(value, pattern) { res.FailWithMessagef("Failed to validate value %v with pattern %v. Path: %s", value, pattern, path) } @@ -135,7 +135,7 @@ func validateArray(resourceArray, patternArray []interface{}, path string) resul res.Messages = append(res.Messages, mapValidationResult.Messages...) } } - case string, float64, int, int64, bool: + case string, float64, int, int64, bool, nil: for _, value := range resourceArray { if !ValidateValueWithPattern(value, pattern) { res.FailWithMessagef("Failed to validate value %v with pattern %v. Path: %s", value, pattern, path) diff --git a/pkg/engine/validation_test.go b/pkg/engine/validation_test.go index 013d9822c9..950e6ea96f 100644 --- a/pkg/engine/validation_test.go +++ b/pkg/engine/validation_test.go @@ -692,6 +692,185 @@ func TestValidateMap_AsteriskFieldIsMissing(t *testing.T) { assert.Assert(t, res.ToError() != nil) } +func TestValidateMap_livenessProbeIsNull(t *testing.T) { + rawPattern := []byte(`{ + "spec":{ + "template":{ + "spec":{ + "containers":[ + { + "name":"*", + "livenessProbe":null + } + ] + } + } + } + }`) + rawMap := []byte(`{ + "apiVersion":"apps/v1", + "kind":"StatefulSet", + "metadata":{ + "name":"game-web", + "labels":{ + "originalLabel":"isHere" + } + }, + "spec":{ + "selector":{ + "matchLabels":{ + "app":"nginxo" + } + }, + "serviceName":"nginxo", + "replicas":3, + "template":{ + "metadata":{ + "labels":{ + "app":"nginxo" + } + }, + "spec":{ + "terminationGracePeriodSeconds":10, + "containers":[ + { + "name":"nginxo", + "image":"k8s.gcr.io/nginx-but-no-slim:0.8", + "ports":[ + { + "containerPort":8780, + "name":"webp" + } + ], + "volumeMounts":[ + { + "name":"www", + "mountPath":"/usr/share/nginxo/html" + } + ], + "livenessProbe":null + } + ] + } + }, + "volumeClaimTemplates":[ + { + "metadata":{ + "name":"www" + }, + "spec":{ + "accessModes":[ + "ReadWriteOnce" + ], + "storageClassName":"my-storage-class", + "resources":{ + "requests":{ + "storage":"1Gi" + } + } + } + } + ] + } + }`) + + var pattern, resource map[string]interface{} + json.Unmarshal(rawPattern, &pattern) + json.Unmarshal(rawMap, &resource) + + res := validateMap(resource, pattern, "/") + assert.NilError(t, res.ToError()) +} + +func TestValidateMap_livenessProbeIsMissing(t *testing.T) { + rawPattern := []byte(`{ + "spec":{ + "template":{ + "spec":{ + "containers":[ + { + "name":"*", + "livenessProbe" : null + } + ] + } + } + } + }`) + rawMap := []byte(`{ + "apiVersion":"apps/v1", + "kind":"StatefulSet", + "metadata":{ + "name":"game-web", + "labels":{ + "originalLabel":"isHere" + } + }, + "spec":{ + "selector":{ + "matchLabels":{ + "app":"nginxo" + } + }, + "serviceName":"nginxo", + "replicas":3, + "template":{ + "metadata":{ + "labels":{ + "app":"nginxo" + } + }, + "spec":{ + "terminationGracePeriodSeconds":10, + "containers":[ + { + "name":"nginxo", + "image":"k8s.gcr.io/nginx-but-no-slim:0.8", + "ports":[ + { + "containerPort":8780, + "name":"webp" + } + ], + "volumeMounts":[ + { + "name":"www", + "mountPath":"/usr/share/nginxo/html" + } + ] + } + ] + } + }, + "volumeClaimTemplates":[ + { + "metadata":{ + "name":"www" + }, + "spec":{ + "accessModes":[ + "ReadWriteOnce" + ], + "storageClassName":"my-storage-class", + "resources":{ + "requests":{ + "storage":"1Gi" + } + } + } + } + ] + } + }`) + + var pattern, resource map[string]interface{} + json.Unmarshal(rawPattern, &pattern) + json.Unmarshal(rawMap, &resource) + + res := validateMap(resource, pattern, "/") + assert.NilError(t, res.ToError()) +} + func TestValidateMapElement_TwoElementsInArrayOnePass(t *testing.T) { rawPattern := []byte(`[ {