diff --git a/library/common-test/tests/externalInterface/validation_test.yaml b/library/common-test/tests/externalInterface/validation_test.yaml new file mode 100644 index 0000000000..fdc0f39935 --- /dev/null +++ b/library/common-test/tests/externalInterface/validation_test.yaml @@ -0,0 +1,114 @@ +suite: external interface validation test +templates: + - common.yaml +tests: + - it: should fail with targetSelector not a list + set: + scaleExternalInterface: + - targetSelector: "not a list" + asserts: + - failedTemplate: + errorMessage: External Interface - Expected to be a [list], but got [string] + + - it: should fail with empty hostInterface + set: + scaleExternalInterface: + - hostInterface: "" + asserts: + - failedTemplate: + errorMessage: External Interface - Expected non-empty + + - it: should fail with empty ipam + set: + scaleExternalInterface: + - hostInterface: enp0s3 + ipam: {} + asserts: + - failedTemplate: + errorMessage: External Interface - Expected non-empty + + - it: should fail with empty ipam.type + set: + scaleExternalInterface: + - hostInterface: enp0s3 + ipam: + type: "" + asserts: + - failedTemplate: + errorMessage: External Interface - Expected non-empty + + - it: should fail with invalid ipam.type + set: + scaleExternalInterface: + - hostInterface: enp0s3 + ipam: + type: invalid + asserts: + - failedTemplate: + errorMessage: External Interface - Expected to be one of [dhcp, static], but got [invalid] + + - it: should fail with non-empty staticIPConfigurations on dhcp + set: + scaleExternalInterface: + - hostInterface: enp0s3 + ipam: + type: dhcp + staticIPConfigurations: + - ipAddress: 1.2.3.4 + asserts: + - failedTemplate: + errorMessage: External Interface - Expected empty and when is not [static] + + - it: should fail with non-empty staticRoutes on dhcp + set: + scaleExternalInterface: + - hostInterface: enp0s3 + ipam: + type: dhcp + staticRoutes: + - gateway: 1.2.3.4 + destination: 1.2.3.4 + asserts: + - failedTemplate: + errorMessage: External Interface - Expected empty and when is not [static] + + - it: should fail with empty staticIPConfigurations on static + set: + scaleExternalInterface: + - hostInterface: enp0s3 + ipam: + type: static + staticIPConfigurations: [] + asserts: + - failedTemplate: + errorMessage: External Interface - Expected non-empty when is [static] + + - it: should fail with empty gateway on staticRoutes on static + set: + scaleExternalInterface: + - hostInterface: enp0s3 + ipam: + type: static + staticIPConfigurations: + - ipAddress: 1.2.3.4 + staticRoutes: + - gateway: "" + destination: 1.2.3.4 + asserts: + - failedTemplate: + errorMessage: External Interface - Expected non-empty in + + - it: should fail with empty destination on staticRoutes on static + set: + scaleExternalInterface: + - hostInterface: enp0s3 + ipam: + type: static + staticIPConfigurations: + - ipAddress: 1.2.3.4 + staticRoutes: + - gateway: 1.2.3.4 + destination: "" + asserts: + - failedTemplate: + errorMessage: External Interface - Expected non-empty in diff --git a/library/common/1.0.0/templates/lib/externalInterface/_validation.tpl b/library/common/1.0.0/templates/lib/externalInterface/_validation.tpl new file mode 100644 index 0000000000..d4f284f624 --- /dev/null +++ b/library/common/1.0.0/templates/lib/externalInterface/_validation.tpl @@ -0,0 +1,53 @@ +{{/* External Interface Validation */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.externalInterface.validation" (dict "objectData" $objectData) -}} +objectData: The object data to validate that contains the external interface configuratioon. +*/}} + +{{- define "ix.v1.common.lib.externalInterface.validation" -}} + {{- $objectData := .objectData -}} + + {{- if and $objectData.targetSelector (not (kindIs "slice" $objectData.targetSelector)) -}} + {{- fail (printf "External Interface - Expected to be a [list], but got [%s]" (kindOf $objectData.targetSelector)) -}} + {{- end -}} + + {{- if not $objectData.hostInterface -}} + {{- fail "External Interface - Expected non-empty " -}} + {{- end -}} + + {{- if not $objectData.ipam -}} + {{- fail "External Interface - Expected non-empty " -}} + {{- end -}} + + {{- if not $objectData.ipam.type -}} + {{- fail "External Interface - Expected non-empty " -}} + {{- end -}} + + {{- $types := (list "dhcp" "static") -}} + {{- if not (mustHas $objectData.ipam.type $types) -}} + {{- fail (printf "External Interface - Expected to be one of [%s], but got [%s]" (join ", " $types) $objectData.ipam.type) -}} + {{- end -}} + + {{- if and (or $objectData.staticIPConfigurations $objectData.staticRoutes) (ne $objectData.ipam.type "static") -}} + {{- fail "External Interface - Expected empty and when is not [static]" -}} + {{- end -}} + + {{- if eq $objectData.ipam.type "static" -}} + {{- if not $objectData.staticIPConfigurations -}} + {{- fail "External Interface - Expected non-empty when is [static]" -}} + {{- end -}} + + {{- with $objectData.staticRoutes -}} + {{- range . -}} + {{- if not .destination -}} + {{- fail "External Interface - Expected non-empty in " -}} + {{- end -}} + + {{- if not .gateway -}} + {{- fail "External Interface - Expected non-empty in " -}} + {{- end -}} + {{- end -}} + {{- end -}} + {{- end -}} + +{{- end -}}