From d470445d37896c396f266c6c9ac17ab112a3bb08 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Tue, 15 Nov 2022 19:20:29 +0200 Subject: [PATCH] add port tests --- .../pod/deployment_contaner_port_test.yaml | 183 +++++++++++++++++- .../1.0.0/templates/lib/container/_ports.tpl | 12 +- library/common/1.0.0/values.yaml | 2 +- 3 files changed, 189 insertions(+), 8 deletions(-) diff --git a/library/common-test/tests/pod/deployment_contaner_port_test.yaml b/library/common-test/tests/pod/deployment_contaner_port_test.yaml index bd6f2e78b8..129a570cb0 100644 --- a/library/common-test/tests/pod/deployment_contaner_port_test.yaml +++ b/library/common-test/tests/pod/deployment_contaner_port_test.yaml @@ -10,4 +10,185 @@ tests: isKind: of: Deployment -#TODO: ports + - it: should fail with named port + set: + service: + main: + ports: + main: + port: 443 + targetPort: some_name + asserts: + - failedTemplate: + documentIndex: *deploymentDoc + errorMessage: This common library does not support named ports for targetPort. port name (main), targetPort (some_name) + + - it: should fail without port + set: + service: + main: + ports: + main: + port: + asserts: + - failedTemplate: + documentIndex: *deploymentDoc + errorMessage: Port is required on enabled services. Service (main) + + - it: should fail with invalid protocol + set: + service: + main: + ports: + main: + port: 443 + protocol: NOT_VALID + asserts: + - failedTemplate: + documentIndex: *deploymentDoc + errorMessage: Not valid (NOT_VALID) + + - it: should pass with with only port set + set: + service: + main: + ports: + main: + port: 443 + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.containers[0].ports[0] + value: + name: main + containerPort: 443 + protocol: TCP + + - it: should pass with with port and targetPort set + set: + service: + main: + ports: + main: + port: 443 + targetPort: 8000 + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.containers[0].ports[0] + value: + name: main + containerPort: 8000 + protocol: TCP + + - it: should pass with with port and targetPort and protocol set + set: + service: + main: + ports: + main: + port: 443 + protocol: TCP + targetPort: 8000 + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.containers[0].ports[0] + value: + name: main + containerPort: 8000 + protocol: TCP + + - it: should pass with with port and targetPort and protocol UDP set + set: + service: + main: + ports: + main: + port: 443 + protocol: UDP + targetPort: 8000 + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.containers[0].ports[0] + value: + name: main + containerPort: 8000 + protocol: UDP + + - it: should pass with with port and targetPort and protocol HTTP set + set: + service: + main: + ports: + main: + port: 443 + protocol: HTTP + targetPort: 8000 + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.containers[0].ports[0] + value: + name: main + containerPort: 8000 + protocol: TCP + + - it: should pass with with port and targetPort and protocol HTTPS set + set: + service: + main: + ports: + main: + port: 443 + protocol: HTTPS + targetPort: 8000 + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.containers[0].ports[0] + value: + name: main + containerPort: 8000 + protocol: TCP + + - it: should pass with with multiple port and targetPort and protocol set + set: + service: + main: + ports: + main: + port: 443 + protocol: HTTP + targetPort: 8000 + secondary: + enabled: true + ports: + secondary: + enabled: true + port: 444 + protocol: TCP + targetPort: 8001 + third: + enabled: true + ports: + third: + enabled: true + port: 445 + protocol: UDP + targetPort: 8002 + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.containers[0].ports + value: + - name: main + containerPort: 8000 + protocol: TCP + - name: secondary + containerPort: 8001 + protocol: TCP + - name: third + containerPort: 8002 + protocol: UDP diff --git a/library/common/1.0.0/templates/lib/container/_ports.tpl b/library/common/1.0.0/templates/lib/container/_ports.tpl index 00e929380a..d3fb4c5150 100644 --- a/library/common/1.0.0/templates/lib/container/_ports.tpl +++ b/library/common/1.0.0/templates/lib/container/_ports.tpl @@ -16,20 +16,20 @@ ports: {{- range $_ := $ports }} {{- if .enabled }} - name: {{ tpl .name $ }} - {{- if and .targetPort (kindIs "string" .targetPort) }} - {{- fail (printf "This common library does not support named ports for targetPort. (port name: %s, targetPort: %s)" .name .targetPort) }} - {{- end }} {{- if not .port }} - {{- fail (printf "Port is required on enabled services. Service: (%s)" .name) }} + {{- fail (printf "Port is required on enabled services. Service (%s)" .name) }} + {{- end }} + {{- if and .targetPort (kindIs "string" .targetPort) }} + {{- fail (printf "This common library does not support named ports for targetPort. port name (%s), targetPort (%s)" .name .targetPort) }} {{- end }} containerPort: {{ default .port .targetPort }} {{- with .protocol }} {{- if or (eq (. | upper) "HTTP") (eq (. | upper) "HTTPS") (eq (. | upper) "TCP") }} protocol: TCP {{- else if (eq (. | upper) "UDP") }} - protocol: {{ . }} + protocol: UDP {{- else }} - {{- fail (printf "Not valid (protocl: %s)" .) }} + {{- fail (printf "Not valid (%s)" .) }} {{- end }} {{- else }} {{/* If no protocol is given, default to TCP */}} protocol: TCP diff --git a/library/common/1.0.0/values.yaml b/library/common/1.0.0/values.yaml index 8b638c5d57..428035fb35 100644 --- a/library/common/1.0.0/values.yaml +++ b/library/common/1.0.0/values.yaml @@ -168,7 +168,7 @@ service: enabled: true # enabled: false # -- The port number (Default port is required if enabled: true) - port: 2 + port: 999999 # -- Port protocol. # Support values are `HTTP`, `HTTPS`, `TCP` and `UDP`. # HTTPS and HTTPS spawn a TCP service and get used for internal URL and name generation