From 4f729964697a2e940ee59d6cf1bbdec3f3e83802 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Wed, 6 Dec 2023 16:52:46 +0200 Subject: [PATCH] add more tests --- .../tests/ingress/validation_test.yaml | 63 +++++++++++++++++ .../templates/lib/ingress/_validation.tpl | 67 ++++++++++++++++++- library/common/templates/spawner/_ingress.tpl | 6 +- 3 files changed, 132 insertions(+), 4 deletions(-) diff --git a/library/common-test/tests/ingress/validation_test.yaml b/library/common-test/tests/ingress/validation_test.yaml index 0b13b5fa..632f7bbc 100644 --- a/library/common-test/tests/ingress/validation_test.yaml +++ b/library/common-test/tests/ingress/validation_test.yaml @@ -22,6 +22,9 @@ tests: set: operator: *operator ingress: + my-ing: + enabled: true + primary: true my-ingress-super-long-name-that-is-longer-than-253-characters-my-configmap-super-long-name-that-is-longer-than-253-characters-my-configmap-super-long-name-that-is-longer-than-253-characters-my-configmap-super-long-long-long-long-long-long-long-long-name: enabled: true asserts: @@ -32,6 +35,9 @@ tests: set: operator: *operator ingress: + my-ing: + enabled: true + primary: true _my-ingress: enabled: true asserts: @@ -44,6 +50,7 @@ tests: ingress: my-ingress: enabled: true + primary: true namespace: my-extra-super-duper-long-name-that-is-longer-than-63-characters asserts: - failedTemplate: @@ -58,6 +65,7 @@ tests: ingress: my-ingress: enabled: true + primary: true namespace: my-namespace asserts: - failedTemplate: @@ -69,6 +77,7 @@ tests: ingress: my-ingress: enabled: true + primary: true labels: "not a dict" asserts: - failedTemplate: @@ -80,6 +89,7 @@ tests: ingress: my-ingress: enabled: true + primary: true annotations: "not a dict" asserts: - failedTemplate: @@ -94,3 +104,56 @@ tests: asserts: - failedTemplate: errorMessage: Ingress - Expected the defined key [enabled] in [ingress.my-ingress] to not be empty + + - it: should fail with targetSelector not a map + set: + operator: *operator + ingress: + my-ingress: + enabled: true + primary: true + targetSelector: "not a map" + asserts: + - failedTemplate: + errorMessage: Ingress - Expected [targetSelector] to be a [map], but got [string] + + - it: should fail with targetSelector having more than one key + set: + operator: *operator + ingress: + my-ingress: + enabled: true + primary: true + targetSelector: + main: main + other: other + asserts: + - failedTemplate: + errorMessage: Ingress - Expected [targetSelector] to have exactly one key, but got [2] + + - it: should fail with targetSelector key missing value + set: + operator: *operator + ingress: + my-ingress: + enabled: true + primary: true + targetSelector: + main: + asserts: + - failedTemplate: + errorMessage: Ingress - Expected [targetSelector.main] to have a value + + - it: should fail with targetSelector key having non-string value + set: + operator: *operator + ingress: + my-ingress: + enabled: true + primary: true + targetSelector: + main: + - not a string + asserts: + - failedTemplate: + errorMessage: Ingress - Expected [targetSelector.main] to be a [string], but got [slice] diff --git a/library/common/templates/lib/ingress/_validation.tpl b/library/common/templates/lib/ingress/_validation.tpl index a63bc583..1b6ef41d 100644 --- a/library/common/templates/lib/ingress/_validation.tpl +++ b/library/common/templates/lib/ingress/_validation.tpl @@ -1,14 +1,77 @@ {{/* Ingress Validation */}} {{/* Call this template: -{{ include "tc.v1.common.lib.ingress.validation" (dict "objectData" $objectData) -}} +{{ include "tc.v1.common.lib.ingress.validation" (dict "rootCtx" $ "objectData" $objectData) -}} objectData: rootCtx: The root context of the chart. - objectData: The ingress object. + objectData: The Ingress object. */}} {{- define "tc.v1.common.lib.ingress.validation" -}} {{- $rootCtx := .rootCtx -}} {{- $objectData := .objectData -}} + {{- if $objectData.targetSelector -}} + {{- if not (kindIs "map" $objectData.targetSelector) -}} + {{- fail (printf "Ingress - Expected [targetSelector] to be a [map], but got [%s]" (kindOf $objectData.targetSelector)) -}} + {{- end -}} + + {{- $selectors := $objectData.targetSelector | keys | len -}} + {{- if (gt $selectors 1) -}} + {{ fail (printf "Ingress - Expected [targetSelector] to have exactly one key, but got [%d]" $selectors) -}} + {{- end -}} + + {{- range $k, $v := $objectData.targetSelector -}} + {{- if not $v -}} + {{- fail (printf "Ingress - Expected [targetSelector.%s] to have a value" $k) -}} + {{- end -}} + + {{- if not (kindIs "string" $v) -}} + {{- fail (printf "Ingress - Expected [targetSelector.%s] to be a [string], but got [%s]" $k (kindOf $v)) -}} + {{- end -}} + {{- end -}} + {{- end -}} + +{{- end -}} + +{{/* Ingress Primary Validation */}} +{{/* Call this template: +{{ include "tc.v1.common.lib.ingress.primaryValidation" $ -}} +*/}} + +{{- define "tc.v1.common.lib.ingress.primaryValidation" -}} + + {{/* Initialize values */}} + {{- $hasPrimary := false -}} + {{- $hasEnabled := false -}} + + {{- range $name, $ingress := $.Values.ingress -}} + + {{- $enabled := (include "tc.v1.common.lib.util.enabled" (dict + "rootCtx" $ "objectData" $ingress + "name" $name "caller" "Ingress" + "key" "ingress")) -}} + + {{/* If ingress is enabled */}} + {{- if eq $enabled "true" -}} + {{- $hasEnabled = true -}} + + {{/* And ingress is primary */}} + {{- if and (hasKey $ingress "primary") ($ingress.primary) -}} + {{/* Fail if there is already a primary ingress */}} + {{- if $hasPrimary -}} + {{- fail "Ingress - Only one ingress can be primary" -}} + {{- end -}} + + {{- $hasPrimary = true -}} + + {{- end -}} + + {{- end -}} + {{- end -}} + + {{/* Require at least one primary ingress, if any enabled */}} + {{- if and $hasEnabled (not $hasPrimary) -}} + {{- fail "Ingress - At least one enabled ingress must be primary" -}} + {{- end -}} {{- end -}} diff --git a/library/common/templates/spawner/_ingress.tpl b/library/common/templates/spawner/_ingress.tpl index 5f74ac19..73209824 100644 --- a/library/common/templates/spawner/_ingress.tpl +++ b/library/common/templates/spawner/_ingress.tpl @@ -6,7 +6,9 @@ {{- define "tc.v1.common.spawner.ingress" -}} {{- $fullname := include "tc.v1.common.lib.chart.names.fullname" $ -}} - {{/* Generate named ingresses as required */}} + {{/* Validate that only 1 primary exists */}} + {{- include "tc.v1.common.lib.ingress.primaryValidation" $ -}} + {{- range $name, $ingress := .Values.ingress -}} {{- $enabled := (include "tc.v1.common.lib.util.enabled" (dict @@ -44,7 +46,7 @@ {{/* Perform validations */}} {{- include "tc.v1.common.lib.chart.names.validation" (dict "name" $objectName "length" 253) -}} {{- include "tc.v1.common.lib.metadata.validation" (dict "objectData" $objectData "caller" "Ingress") -}} - {{- /* include "tc.v1.common.lib.service.validation" (dict "rootCtx" $ "objectData" $objectData) */ -}} + {{- include "tc.v1.common.lib.ingress.validation" (dict "rootCtx" $ "objectData" $objectData) -}} {{/* Set the name of the ingress */}} {{- $_ := set $objectData "name" $objectName -}}