From 2142d06fc7dd7c53fb1c6e0ea4d55df5008fb8f7 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Fri, 25 Nov 2022 19:22:09 +0200 Subject: [PATCH] add more service tests --- .../tests/service/service_test.yaml | 213 +++++++++++++++++- .../common/1.0.0/templates/class/_service.tpl | 18 +- 2 files changed, 214 insertions(+), 17 deletions(-) diff --git a/library/common-test/tests/service/service_test.yaml b/library/common-test/tests/service/service_test.yaml index 124dde2a..40789dd1 100644 --- a/library/common-test/tests/service/service_test.yaml +++ b/library/common-test/tests/service/service_test.yaml @@ -17,6 +17,7 @@ tests: set: service: main: + type: LoadBalancer externalTrafficPolicy: invalid_traffic_policy asserts: - failedTemplate: @@ -37,7 +38,7 @@ tests: main: sessionAffinity: ClientIP sessionAffinityConfig: - ClientIP: + clientIP: timeoutSeconds: -1 asserts: - failedTemplate: @@ -49,7 +50,7 @@ tests: main: sessionAffinity: ClientIP sessionAffinityConfig: - ClientIP: + clientIP: timeoutSeconds: 86401 asserts: - failedTemplate: @@ -250,7 +251,7 @@ tests: some_key1: value1 some_key2: value2 some_key3: value3 -# TODO: add ports etc in the below tests + - it: should pass with ClusterIP service type documentIndex: *serviceDoc set: @@ -259,8 +260,19 @@ tests: type: ClusterIP asserts: - equal: - path: spec.type - value: ClusterIP + path: spec + value: + type: ClusterIP + ports: &defaultPort + - name: main + port: 65535 + protocol: TCP + targetPort: main + selector: &defaultSelector + app: common-test + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/name: common-test + release: RELEASE-NAME - it: should pass with ClusterIP service type and clusterIP set documentIndex: *serviceDoc @@ -271,8 +283,191 @@ tests: clusterIP: 172.16.0.12 asserts: - equal: - path: spec.type - value: ClusterIP + path: spec + value: + type: ClusterIP + clusterIP: 172.16.0.12 + ports: *defaultPort + selector: *defaultSelector + + - it: should pass with ClusterIP service type and externalTrafficPolicy + documentIndex: *serviceDoc + set: + service: + main: + type: ClusterIP + externalTrafficPolicy: Cluster + asserts: - equal: - path: spec.clusterIP - value: 172.16.0.12 + path: spec + value: + type: ClusterIP + ports: *defaultPort + selector: *defaultSelector + - isNull: + path: spec.externalTrafficPolicy + + - it: should pass with ClusterIP service type and sessionAffinity set to None + documentIndex: *serviceDoc + set: + service: + main: + type: ClusterIP + sessionAffinity: None + sessionAffinityConfig: + ClientIP: + timeoutSeconds: 1000 + asserts: + - equal: + path: spec + value: + type: ClusterIP + ports: *defaultPort + selector: *defaultSelector + sessionAffinity: None + - isNull: + path: spec.sessionAffinityConfig + + - it: should pass with ClusterIP service type and sessionAffinity set to ClientIP + documentIndex: *serviceDoc + set: + service: + main: + type: ClusterIP + sessionAffinity: ClientIP + sessionAffinityConfig: + clientIP: + timeoutSeconds: 1000 + asserts: + - equal: + path: spec + value: + type: ClusterIP + ports: *defaultPort + selector: *defaultSelector + sessionAffinity: ClientIP + sessionAffinityConfig: + clientIP: + timeoutSeconds: 1000 + + - it: should pass with ClusterIP service type and sessionAffinity set to ClientIP from tpl + documentIndex: *serviceDoc + set: + timeout: 1000 + service: + main: + type: ClusterIP + sessionAffinity: ClientIP + sessionAffinityConfig: + clientIP: + timeoutSeconds: 1000 + asserts: + - equal: + path: spec + value: + type: ClusterIP + ports: *defaultPort + selector: *defaultSelector + sessionAffinity: ClientIP + sessionAffinityConfig: + clientIP: + timeoutSeconds: 1000 + + - it: should pass with ClusterIP service type and externalIPs + documentIndex: *serviceDoc + set: + service: + main: + type: ClusterIP + externalIPs: + - 192.168.1.20 + - 192.168.1.21 + - 192.168.1.22 + asserts: + - equal: + path: spec + value: + type: ClusterIP + ports: *defaultPort + selector: *defaultSelector + externalIPs: + - 192.168.1.20 + - 192.168.1.21 + - 192.168.1.22 + + - it: should pass with ClusterIP service type and externalIPs from tpl + documentIndex: *serviceDoc + set: + ip1: 192.168.1.20 + ip2: 192.168.1.21 + ip3: 192.168.1.22 + service: + main: + type: ClusterIP + externalIPs: + - "{{ .Values.ip1 }}" + - "{{ .Values.ip2 }}" + - "{{ .Values.ip3 }}" + asserts: + - equal: + path: spec + value: + type: ClusterIP + ports: *defaultPort + selector: *defaultSelector + externalIPs: + - 192.168.1.20 + - 192.168.1.21 + - 192.168.1.22 + + - it: should pass with ClusterIP service type and publishNotReadyAddresses + documentIndex: *serviceDoc + set: + service: + main: + type: ClusterIP + publishNotReadyAddresses: true + asserts: + - equal: + path: spec + value: + type: ClusterIP + ports: *defaultPort + selector: *defaultSelector + publishNotReadyAddresses: true + + - it: should pass with ClusterIP service type and ipFamilyPolicy + documentIndex: *serviceDoc + set: + service: + main: + type: ClusterIP + ipFamilyPolicy: SingleStack + asserts: + - equal: + path: spec + value: + type: ClusterIP + ports: *defaultPort + selector: *defaultSelector + ipFamilyPolicy: SingleStack + + - it: should pass with ClusterIP service type and ipFamilies + documentIndex: *serviceDoc + set: + service: + main: + type: ClusterIP + ipFamilies: + - IPv6 + - IPv4 + asserts: + - equal: + path: spec + value: + type: ClusterIP + ports: *defaultPort + selector: *defaultSelector + ipFamilies: + - IPv6 + - IPv4 diff --git a/library/common/1.0.0/templates/class/_service.tpl b/library/common/1.0.0/templates/class/_service.tpl index f72db4b3..5b450874 100644 --- a/library/common/1.0.0/templates/class/_service.tpl +++ b/library/common/1.0.0/templates/class/_service.tpl @@ -56,11 +56,13 @@ spec: {{- end }} {{- end -}} {{- end -}} -{{- with $svcValues.externalTrafficPolicy -}} - {{- if not (has . (list "Cluster" "Local")) -}} - {{- fail (printf "Invalid option (%s) for . Valid options are Cluster and Local" .) -}} - {{- end }} +{{- if ne $svcType "ClusterIP" -}} + {{- with $svcValues.externalTrafficPolicy -}} + {{- if not (has . (list "Cluster" "Local")) -}} + {{- fail (printf "Invalid option (%s) for . Valid options are Cluster and Local" .) -}} + {{- end }} externalTrafficPolicy: {{ . }} + {{- end -}} {{- end -}} {{- with $svcValues.sessionAffinity }} {{- if not (has . (list "ClientIP" "None")) -}} @@ -69,15 +71,15 @@ spec: sessionAffinity: {{ . }} {{- if eq . "ClientIP" -}} {{- with $svcValues.sessionAffinityConfig -}} - {{- with .ClientIP -}} + {{- with .clientIP -}} {{- if hasKey . "timeoutSeconds" }} {{- $timeout := tpl (toString .timeoutSeconds) $root -}} {{- if or (lt (int $timeout) 0) (gt (int $timeout) 86400) -}} {{- fail (printf "Invalid value (%s) for . Valid values must be with 0 and 86400" $timeout) -}} {{- end }} - sessionAffinityConfig: - ClientIP: - timeoutSeconds: {{ $timeout }} + sessionAffinityConfig: + clientIP: + timeoutSeconds: {{ $timeout }} {{- end -}} {{- end -}} {{- end -}}