From 6dda3bc197c4950b57eec459c6fb83d04494ebc1 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Sat, 4 Feb 2023 22:55:34 +0200 Subject: [PATCH] simplify error messages --- .../tests/service/validation_test.yaml | 22 ++++++++++++++++--- library/common/1.0.0/docs/service.md | 21 +++++++++--------- .../common/1.0.0/templates/lib/pod/_dns.tpl | 5 +++-- .../templates/lib/pod/_restartPolicy.tpl | 8 ++++--- .../1.0.0/templates/lib/pod/_tolerations.tpl | 10 +++++---- .../validation/_cronjobValidation.tpl | 5 +++-- .../validation/_daemonsetValidation.tpl | 5 +++-- .../validation/_deploymentValidation.tpl | 5 +++-- .../validation/_statefusetValidation.tpl | 5 +++-- .../validation/_workloadValidation.tpl | 5 +++-- 10 files changed, 59 insertions(+), 32 deletions(-) diff --git a/library/common-test/tests/service/validation_test.yaml b/library/common-test/tests/service/validation_test.yaml index af4132537f..3581064f78 100644 --- a/library/common-test/tests/service/validation_test.yaml +++ b/library/common-test/tests/service/validation_test.yaml @@ -115,7 +115,7 @@ tests: primary: true asserts: - failedTemplate: - errorMessage: Service - Expected service's to be [string], but got [map] + errorMessage: Service - Expected to be [string], but got [map] - it: should fail with container targetSelector not a string set: @@ -131,7 +131,7 @@ tests: container: not-a-string asserts: - failedTemplate: - errorMessage: Service - Expected port's to be [string], but got [map] + errorMessage: Service - Expected to be [string], but got [map] - it: should fail with selected pod not defined set: @@ -188,4 +188,20 @@ tests: protocol: not-a-protocol asserts: - failedTemplate: - errorMessage: Service - Expected port's protocol to be one of [TCP, UDP, HTTP, HTTPS] but got [not-a-protocol] + errorMessage: Service - Expected to be one of [TCP, UDP, HTTP, HTTPS] but got [not-a-protocol] + + - it: should fail with invalid service type + set: + service: + service-name1: + enabled: true + primary: true + type: not-a-type + ports: + port-name1: + enabled: true + primary: true + protocol: not-a-protocol + asserts: + - failedTemplate: + errorMessage: Service - Expected to be one of [ClusterIP, LoadBalancer, NodePort, ExternalName, ExternalIP] but got [not-a-type] diff --git a/library/common/1.0.0/docs/service.md b/library/common/1.0.0/docs/service.md index de959ca156..2a1c76641d 100644 --- a/library/common/1.0.0/docs/service.md +++ b/library/common/1.0.0/docs/service.md @@ -1,15 +1,16 @@ # Service -| Key | Type | Required | Helm Template | Default | Description | -| :------------------------------------ | :-------: | :------: | :----------------: | :---------: | :---------------------------------------------------------------------- | -| service | `dict` | ❌ | ❌ | `{}` | Define the service as dicts | -| service.[service-name] | `dict` | ✅ | ❌ | `{}` | Holds service definition | -| service.[service-name].enabled | `boolean` | ✅ | ❌ | `false` | Enables or Disables the service | -| service.[service-name].labels | `dict` | ❌ | ✅ (On value only) | `{}` | Additional labels for service | -| service.[service-name].annotations | `dict` | ❌ | ✅ (On value only) | `{}` | Additional annotations for service | -| service.[service-name].type | `string` | ❌ | ✅ | `ClusterIP` | Define the service type | -| service.[service-name].sharedKey | `string` | ❌ | ✅ | `$FullName` | Custom Shared Key for MetalLB Annotation | -| service.[service-name].targetSelector | `string` | ❌ | ✅ | `""` | Define the pod to link the service, by default will use the primary pod | +| Key | Type | Required | Helm Template | Default | Description | +| :------------------------------------ | :-------: | :------: | :----------------: | :---------: | :------------------------------------------------------------------------------------ | +| service | `dict` | ❌ | ❌ | `{}` | Define the service as dicts | +| service.[service-name] | `dict` | ✅ | ❌ | `{}` | Holds service definition | +| service.[service-name].enabled | `boolean` | ✅ | ❌ | `false` | Enables or Disables the service | +| service.[service-name].labels | `dict` | ❌ | ✅ (On value only) | `{}` | Additional labels for service | +| service.[service-name].annotations | `dict` | ❌ | ✅ (On value only) | `{}` | Additional annotations for service | +| service.[service-name].type | `string` | ❌ | ✅ | `ClusterIP` | Define the service type (ClusterIP, LoadBalancer, NodePort, ExternalIP, ExternalName) | +| service.[service-name].sharedKey | `string` | ❌ | ✅ | `$FullName` | Custom Shared Key for MetalLB Annotation | +| service.[service-name].targetSelector | `string` | ❌ | ✅ | `""` | Define the pod to link the service, by default will use the primary pod | +| service.[service-name].ports | `list` | ✅ | ❌ | `{}` | Define the ports of the service | --- diff --git a/library/common/1.0.0/templates/lib/pod/_dns.tpl b/library/common/1.0.0/templates/lib/pod/_dns.tpl index 5f18e43e7d..92dc72cce6 100644 --- a/library/common/1.0.0/templates/lib/pod/_dns.tpl +++ b/library/common/1.0.0/templates/lib/pod/_dns.tpl @@ -38,8 +38,9 @@ objectData: The object data to be used to render the Pod. {{- $policy = "ClusterFirstWithHostNet" -}} {{- end -}} - {{- if not (mustHas $policy (list "ClusterFirst" "ClusterFirstWithHostNet" "Default" "None")) -}} - {{- fail (printf "Expected to be one of [ClusterFirst, ClusterFirstWithHostNet, Default, None], but got [%s]" $policy) -}} + {{- $policies := (list "ClusterFirst" "ClusterFirstWithHostNet" "Default" "None") -}} + {{- if not (mustHas $policy $policies) -}} + {{- fail (printf "Expected to be one of [%s], but got [%s]" (join ", " $policies) $policy) -}} {{- end -}} {{/* When policy is set to None all keys are required */}} diff --git a/library/common/1.0.0/templates/lib/pod/_restartPolicy.tpl b/library/common/1.0.0/templates/lib/pod/_restartPolicy.tpl index 122da0b384..a695a50766 100644 --- a/library/common/1.0.0/templates/lib/pod/_restartPolicy.tpl +++ b/library/common/1.0.0/templates/lib/pod/_restartPolicy.tpl @@ -20,11 +20,13 @@ objectData: The object data to be used to render the Pod. {{- $policy = tpl . $rootCtx -}} {{- end -}} - {{- if not (mustHas $policy (list "Always" "Never" "OnFailure")) -}} - {{- fail (printf "Expected to be one of [Exists, Equal] but got [%s]" $operator) -}} + {{- $operators := (list "Exists" "Equal") -}} + {{- if not (mustHas $operator $operators) -}} + {{- fail (printf "Expected to be one of [%s] but got [%s]" (join ", " $operators) $operator) -}} {{- end -}} {{- if and (eq $operator "Equal") (or (not $key) (not $value)) -}} @@ -40,8 +41,9 @@ objectData: The object data to be used to render the Pod. {{- fail (printf "Expected empty with set to [Exists], but got [%s]" $value) -}} {{- end -}} - {{- if and $effect (not (mustHas $effect (list "NoExecute" "NoSchedule" "PreferNoSchedule"))) -}} - {{- fail (printf "Expected to be one of [NoExecute, NoSchedule, PreferNoSchedule], but got [%s]" $effect) -}} + {{- $effects := (list "NoExecute" "NoSchedule" "PreferNoSchedule") -}} + {{- if and $effect (not (mustHas $effect $effects)) -}} + {{- fail (printf "Expected to be one of [%s], but got [%s]" (join ", " $effects) $effect) -}} {{- end -}} {{- if and (not (kindIs "invalid" $tolSeconds)) (not (mustHas (kindOf $tolSeconds) (list "int" "float64"))) -}} diff --git a/library/common/1.0.0/templates/lib/workload/validation/_cronjobValidation.tpl b/library/common/1.0.0/templates/lib/workload/validation/_cronjobValidation.tpl index aa3961768b..896fd403ed 100644 --- a/library/common/1.0.0/templates/lib/workload/validation/_cronjobValidation.tpl +++ b/library/common/1.0.0/templates/lib/workload/validation/_cronjobValidation.tpl @@ -13,8 +13,9 @@ objectData: {{- if $objectData.concurrencyPolicy -}} {{- $concurrencyPolicy := $objectData.concurrencyPolicy -}} - {{- if not (mustHas $concurrencyPolicy (list "Allow" "Forbid" "Replace")) -}} - {{- fail (printf "CronJob - Expected to be one of [Allow, Forbid, Replace], but got [%v]" $concurrencyPolicy) -}} + {{- $policies := (list "Allow" "Forbid" "Replace") -}} + {{- if not (mustHas $concurrencyPolicy $policies) -}} + {{- fail (printf "CronJob - Expected to be one of [%s], but got [%v]" (join ", " $policies) $concurrencyPolicy) -}} {{- end -}} {{- end -}} diff --git a/library/common/1.0.0/templates/lib/workload/validation/_daemonsetValidation.tpl b/library/common/1.0.0/templates/lib/workload/validation/_daemonsetValidation.tpl index d0892067cf..79929a23c8 100644 --- a/library/common/1.0.0/templates/lib/workload/validation/_daemonsetValidation.tpl +++ b/library/common/1.0.0/templates/lib/workload/validation/_daemonsetValidation.tpl @@ -12,8 +12,9 @@ objectData: {{- if $objectData.strategy -}} {{- $strategy := $objectData.strategy -}} - {{- if not (mustHas $strategy (list "OnDelete" "RollingUpdate")) -}} - {{- fail (printf "DaemonSet - Expected to be one of [OnDelete, RollingUpdate], but got [%v]" $strategy) -}} + {{- $strategies := (list "OnDelete" "RollingUpdate") -}} + {{- if not (mustHas $strategy $strategies) -}} + {{- fail (printf "DaemonSet - Expected to be one of [%s], but got [%v]" (join ", " $strategies) $strategy) -}} {{- end -}} {{- end -}} diff --git a/library/common/1.0.0/templates/lib/workload/validation/_deploymentValidation.tpl b/library/common/1.0.0/templates/lib/workload/validation/_deploymentValidation.tpl index df39f74b40..75532ba69a 100644 --- a/library/common/1.0.0/templates/lib/workload/validation/_deploymentValidation.tpl +++ b/library/common/1.0.0/templates/lib/workload/validation/_deploymentValidation.tpl @@ -12,8 +12,9 @@ objectData: {{- if $objectData.strategy -}} {{- $strategy := $objectData.strategy -}} - {{- if not (mustHas $strategy (list "Recreate" "RollingUpdate")) -}} - {{- fail (printf "Deployment - Expected to be one of [Recreate, RollingUpdate], but got [%v]" $strategy) -}} + {{- $strategies := (list "Recreate" "RollingUpdate") -}} + {{- if not (mustHas $strategy $strategies) -}} + {{- fail (printf "Deployment - Expected to be one of [%s], but got [%v]" (join ", " $strategies) $strategy) -}} {{- end -}} {{- end -}} diff --git a/library/common/1.0.0/templates/lib/workload/validation/_statefusetValidation.tpl b/library/common/1.0.0/templates/lib/workload/validation/_statefusetValidation.tpl index 21b2492701..c5729a8076 100644 --- a/library/common/1.0.0/templates/lib/workload/validation/_statefusetValidation.tpl +++ b/library/common/1.0.0/templates/lib/workload/validation/_statefusetValidation.tpl @@ -12,8 +12,9 @@ objectData: {{- if $objectData.strategy -}} {{- $strategy := $objectData.strategy -}} - {{- if not (mustHas $strategy (list "OnDelete" "RollingUpdate")) -}} - {{- fail (printf "StatefulSet - Expected to be one of [OnDelete, RollingUpdate], but got [%v]" $strategy) -}} + {{- $strategies := (list "OnDelete" "RollingUpdate") -}} + {{- if not (mustHas $strategy $strategies) -}} + {{- fail (printf "StatefulSet - Expected to be one of [%s], but got [%v]" (join ", " $strategies) $strategy) -}} {{- end -}} {{- end -}} diff --git a/library/common/1.0.0/templates/lib/workload/validation/_workloadValidation.tpl b/library/common/1.0.0/templates/lib/workload/validation/_workloadValidation.tpl index 3934aeacf5..1d9b5dada1 100644 --- a/library/common/1.0.0/templates/lib/workload/validation/_workloadValidation.tpl +++ b/library/common/1.0.0/templates/lib/workload/validation/_workloadValidation.tpl @@ -11,8 +11,9 @@ {{/* Go over workload */}} {{- range $name, $workload := .Values.workload -}} - {{- if not (mustHas $workload.type (list "Deployment" "StatefulSet" "DaemonSet" "Job" "CronJob")) -}} - {{- fail (printf "Workload - Expected to be one of [Deployment, StatefulSet, DaemonSet, Job, CronJob], but got [%s]" $workload.type) -}} + {{- $types := (list "Deployment" "StatefulSet" "DaemonSet" "Job" "CronJob") -}} + {{- if not (mustHas $workload.type $types) -}} + {{- fail (printf "Workload - Expected to be one of [%s], but got [%s]" (join ", " $types) $workload.type) -}} {{- end -}} {{/* If workload is enabled */}}