diff --git a/library/common-test/Chart.yaml b/library/common-test/Chart.yaml index 76ddcda51b..2e8a5f8ce4 100644 --- a/library/common-test/Chart.yaml +++ b/library/common-test/Chart.yaml @@ -6,5 +6,5 @@ kubeVersion: '>=1.16.0-0' type: application dependencies: - name: common - repository: file://../common - version: n/a + repository: file://../common/1.0.0 + version: ~1.0.0 diff --git a/library/common-test/tests/pod/deployment_container_default_test.yaml b/library/common-test/tests/pod/deployment_container_default_test.yaml index 22ea82acb0..4c9ad8d82a 100644 --- a/library/common-test/tests/pod/deployment_container_default_test.yaml +++ b/library/common-test/tests/pod/deployment_container_default_test.yaml @@ -26,6 +26,13 @@ tests: - isNull: documentIndex: *deploymentDoc path: spec.template.spec.hostname + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.dnsPolicy + value: ClusterFirst + - isNull: + documentIndex: *deploymentDoc + path: spec.template.spec.dnsConfig - isNull: documentIndex: *deploymentDoc path: spec.template.spec.priorityClassName diff --git a/library/common-test/tests/pod/deployment_container_lifecycle_test.yaml b/library/common-test/tests/pod/deployment_container_lifecycle_test.yaml new file mode 100644 index 0000000000..b90649df48 --- /dev/null +++ b/library/common-test/tests/pod/deployment_container_lifecycle_test.yaml @@ -0,0 +1,167 @@ + +suite: deployment container lifecycle test +templates: + - common.yaml +tests: + - it: should pass with default values + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + + - it: should pass with single postStart lifecycle set + set: + lifecycle: + postStart: + command: some_command + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.containers[0].lifecycle + value: + postStart: + exec: + command: + - some_command + + - it: should pass with single postStart lifecycle set from tpl + set: + some_key: some_command + lifecycle: + postStart: + command: "{{ .Values.some_key }}" + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.containers[0].lifecycle + value: + postStart: + exec: + command: + - some_command + + - it: should pass with postStart lifecycle set + set: + lifecycle: + postStart: + command: + - /bin/bash + - test + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.containers[0].lifecycle + value: + postStart: + exec: + command: + - /bin/bash + - test + + - it: should pass with postStart lifecycle set from tpl + set: + some_key: some_value + lifecycle: + postStart: + command: + - /bin/bash + - "{{ .Values.some_key }}" + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.containers[0].lifecycle + value: + postStart: + exec: + command: + - /bin/bash + - some_value + + - it: should pass with single preStop lifecycle set + set: + lifecycle: + preStop: + command: some_command + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.containers[0].lifecycle + value: + preStop: + exec: + command: + - some_command + + - it: should pass with single preStop lifecycle set from tpl + set: + some_key: some_command + lifecycle: + preStop: + command: "{{ .Values.some_key }}" + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.containers[0].lifecycle + value: + preStop: + exec: + command: + - some_command + + + - it: should pass with preStop lifecycle set + set: + lifecycle: + preStop: + command: + - /bin/bash + - test + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.containers[0].lifecycle + value: + preStop: + exec: + command: + - /bin/bash + - test + + - it: should pass with preStop lifecycle set from tpl + set: + some_key: some_value + lifecycle: + preStop: + command: + - /bin/bash + - "{{ .Values.some_key }}" + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.containers[0].lifecycle + value: + preStop: + exec: + command: + - /bin/bash + - some_value + + - it: should fail with no command in preStop + set: + lifecycle: + preStop: + command: "" + asserts: + - failedTemplate: + documentIndex: *deploymentDoc + errorMessage: No commands were given for preStop lifecycle hook + + - it: should fail with no command in postStart + set: + lifecycle: + postStart: + command: "" + asserts: + - failedTemplate: + documentIndex: *deploymentDoc + errorMessage: No commands were given for postStart lifecycle hook diff --git a/library/common-test/tests/pod/deployment_container_other_test.yaml b/library/common-test/tests/pod/deployment_container_other_test.yaml index 0f4d20d5f0..d986adbe14 100644 --- a/library/common-test/tests/pod/deployment_container_other_test.yaml +++ b/library/common-test/tests/pod/deployment_container_other_test.yaml @@ -27,34 +27,6 @@ tests: path: spec.template.spec.containers[0].stdin value: true - - it: should pass with hostNetwork set - set: - hostNetwork: true - asserts: - - equal: - documentIndex: *deploymentDoc - path: spec.template.spec.hostNetwork - value: true - - - it: should pass with hostname set - set: - hostname: some_hostname - asserts: - - equal: - documentIndex: *deploymentDoc - path: spec.template.spec.hostname - value: some_hostname - - - it: should pass with hostname set from tpl - set: - name: some_hostname - hostname: "{{ .Values.name }}" - asserts: - - equal: - documentIndex: *deploymentDoc - path: spec.template.spec.hostname - value: some_hostname - - it: should pass with gracePeriodSeconds set set: termination: @@ -65,15 +37,6 @@ tests: path: spec.template.spec.terminationGracePeriodSeconds value: 25 - - it: should pass with enableServiceLinks set - set: - enableServiceLinks: true - asserts: - - equal: - documentIndex: *deploymentDoc - path: spec.template.spec.enableServiceLinks - value: true - - it: should pass with schedulerName set set: schedulerName: some_scheduler @@ -112,143 +75,6 @@ tests: path: spec.template.spec.priorityClassName value: some_priority_class_name - - it: should pass with single postStart lifecycle set - set: - lifecycle: - postStart: - command: some_command - asserts: - - equal: - documentIndex: *deploymentDoc - path: spec.template.spec.containers[0].lifecycle - value: - postStart: - exec: - command: - - some_command - - - it: should pass with single postStart lifecycle set from tpl - set: - some_key: some_command - lifecycle: - postStart: - command: "{{ .Values.some_key }}" - asserts: - - equal: - documentIndex: *deploymentDoc - path: spec.template.spec.containers[0].lifecycle - value: - postStart: - exec: - command: - - some_command - - - it: should pass with postStart lifecycle set - set: - lifecycle: - postStart: - command: - - /bin/bash - - test - asserts: - - equal: - documentIndex: *deploymentDoc - path: spec.template.spec.containers[0].lifecycle - value: - postStart: - exec: - command: - - /bin/bash - - test - - - it: should pass with postStart lifecycle set from tpl - set: - some_key: some_value - lifecycle: - postStart: - command: - - /bin/bash - - "{{ .Values.some_key }}" - asserts: - - equal: - documentIndex: *deploymentDoc - path: spec.template.spec.containers[0].lifecycle - value: - postStart: - exec: - command: - - /bin/bash - - some_value - - - it: should pass with single preStop lifecycle set - set: - lifecycle: - preStop: - command: some_command - asserts: - - equal: - documentIndex: *deploymentDoc - path: spec.template.spec.containers[0].lifecycle - value: - preStop: - exec: - command: - - some_command - - - it: should pass with single preStop lifecycle set from tpl - set: - some_key: some_command - lifecycle: - preStop: - command: "{{ .Values.some_key }}" - asserts: - - equal: - documentIndex: *deploymentDoc - path: spec.template.spec.containers[0].lifecycle - value: - preStop: - exec: - command: - - some_command - - - - it: should pass with preStop lifecycle set - set: - lifecycle: - preStop: - command: - - /bin/bash - - test - asserts: - - equal: - documentIndex: *deploymentDoc - path: spec.template.spec.containers[0].lifecycle - value: - preStop: - exec: - command: - - /bin/bash - - test - - - it: should pass with preStop lifecycle set from tpl - set: - some_key: some_value - lifecycle: - preStop: - command: - - /bin/bash - - "{{ .Values.some_key }}" - asserts: - - equal: - documentIndex: *deploymentDoc - path: spec.template.spec.containers[0].lifecycle - value: - preStop: - exec: - command: - - /bin/bash - - some_value - - it: should pass with terminationMessagePath set set: termination: diff --git a/library/common-test/tests/pod/deployment_dns-network_test.yaml b/library/common-test/tests/pod/deployment_dns-network_test.yaml new file mode 100644 index 0000000000..fef16390b9 --- /dev/null +++ b/library/common-test/tests/pod/deployment_dns-network_test.yaml @@ -0,0 +1,250 @@ + +suite: deployment dns-network test +templates: + - common.yaml +tests: + - it: should pass with default values + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + + - it: should pass with hostNetwork set + set: + hostNetwork: true + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.hostNetwork + value: true + + - it: should pass with hostname set + set: + hostname: some_hostname + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.hostname + value: some_hostname + + - it: should pass with hostname set from tpl + set: + name: some_hostname + hostname: "{{ .Values.name }}" + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.hostname + value: some_hostname + + - it: should pass with enableServiceLinks set + set: + enableServiceLinks: true + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.enableServiceLinks + value: true + + - it: should fail with invalid dnsPolicy + set: + dnsPolicy: invalid + asserts: + - failedTemplate: + documentIndex: *deploymentDoc + errorMessage: Not valid dnsPolicy. Valid options are ClusterFirst, Default, ClusterFirstWithHostNet, None + + - it: should pass with changed dnsPolicy + set: + dnsPolicy: Default + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.dnsPolicy + value: Default + + - it: should pass with hostNet dnsPolicy + set: + hostNetwork: true + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.dnsPolicy + value: ClusterFirstWithHostNet + + - it: should pass with hostNet and changed dnsPolicy + set: + dnsPolicy: Default + hostNetwork: true + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.dnsPolicy + value: Default + + - it: should fail with more than 3 nameservers + set: + dnsConfig: + nameservers: + - ns1 + - ns2 + - ns3 + - ns4 + asserts: + - failedTemplate: + documentIndex: *deploymentDoc + errorMessage: There can be at most 3 nameservers specified in dnsConfig + + - it: should fail with more than 6 domain searches + set: + dnsConfig: + searches: + - domain_search_1 + - domain_search_2 + - domain_search_3 + - domain_search_4 + - domain_search_5 + - domain_search_6 + - domain_search_7 + asserts: + - failedTemplate: + documentIndex: *deploymentDoc + errorMessage: There can be at most 6 search domains specified in dnsConfig + + - it: should fail with no nameservers and dnsPolicy set to None + set: + dnsPolicy: None + asserts: + - failedTemplate: + documentIndex: *deploymentDoc + errorMessage: With dnsPolicy set to None, you must specify at least 1 nameservers on dnsConfig + + - it: should pass with dnsConfig defined + set: + dnsConfig: + nameservers: + - ns1 + - ns2 + searches: + - domain_search_1 + - domain_search_2 + options: + - name: ndots + value: 2 + - name: edns0 + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.dnsConfig + value: + nameservers: + - ns1 + - ns2 + searches: + - domain_search_1 + - domain_search_2 + options: + - name: ndots + value: "2" + - name: edns0 + + - it: should pass with dnsConfig nameservers only + set: + dnsConfig: + nameservers: + - ns1 + - ns2 + - 1.1.1.1 + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.dnsConfig + value: + nameservers: + - ns1 + - ns2 + - 1.1.1.1 + + - it: should pass with dnsConfig searches only + set: + dnsConfig: + searches: + - domain_search_1 + - domain_search_2 + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.dnsConfig + value: + searches: + - domain_search_1 + - domain_search_2 + + - it: should pass with dnsConfig options only + set: + dnsConfig: + options: + - name: ndots + value: 2 + - name: edns0 + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.dnsConfig + value: + options: + - name: ndots + value: "2" + - name: edns0 + + - it: should pass with dnsConfig nameservers only from tpl + set: + ns1: ns1 + dnsConfig: + nameservers: + - "{{ .Values.ns1 }}" + - ns2 + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.dnsConfig + value: + nameservers: + - ns1 + - ns2 + + - it: should pass with dnsConfig searches only from tpl + set: + ds: domain_search_1 + dnsConfig: + searches: + - "{{ .Values.ds }}" + - domain_search_2 + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.dnsConfig + value: + searches: + - domain_search_1 + - domain_search_2 + + - it: should pass with dnsConfig options only from tpl + set: + opt1_name: ndots + opt1_value: 2 + opt2_name: edns0 + dnsConfig: + options: + - name: "{{ .Values.opt1_name }}" + value: "{{ .Values.opt1_value }}" + - name: "{{ .Values.opt2_name }}" + asserts: + - equal: + documentIndex: *deploymentDoc + path: spec.template.spec.dnsConfig + value: + options: + - name: ndots + value: "2" + - name: edns0 diff --git a/library/common/1.0.0/templates/lib/chart/_dns.tpl b/library/common/1.0.0/templates/lib/chart/_dns.tpl new file mode 100644 index 0000000000..b37ad78740 --- /dev/null +++ b/library/common/1.0.0/templates/lib/chart/_dns.tpl @@ -0,0 +1,49 @@ +{{/* Returns dnsPolicy */}} +{{- define "ix.v1.common.dnsPolicy" -}} + {{- $policy := "ClusterFirst" -}} + {{- if .Values.dnsPolicy -}} + {{- if and (ne .Values.dnsPolicy "Default") (ne .Values.dnsPolicy "ClusterFirst") (ne .Values.dnsPolicy "ClusterFirstWithHostNet") (ne .Values.dnsPolicy "None") -}} + {{- fail "Not valid dnsPolicy. Valid options are ClusterFirst, Default, ClusterFirstWithHostNet, None" -}} + {{- end -}} + {{- $policy = .Values.dnsPolicy -}} + {{- else if .Values.hostNetwork -}} + {{- $policy = "ClusterFirstWithHostNet" -}} + {{- end -}} +{{- $policy -}} +{{- end -}} + +{{/* Returns dnsConfig */}} +{{- define "ix.v1.common.dnsConfig" -}} + {{- if and (eq .Values.dnsPolicy "None") (not .Values.dnsConfig.nameservers) -}} + {{- fail "With dnsPolicy set to None, you must specify at least 1 nameservers on dnsConfig" -}} + {{- end -}} + {{- if or .Values.dnsConfig.nameservers .Values.dnsConfig.searches .Values.dnsConfig.options -}} + {{- with .Values.dnsConfig.nameservers -}} + {{- if gt (len .) 3 -}} + {{- fail "There can be at most 3 nameservers specified in dnsConfig" -}} + {{- end -}} +nameservers: + {{- range . }} + - {{ tpl . $ }} + {{- end }} + {{- end -}} + {{- with .Values.dnsConfig.searches -}} + {{- if gt (len .) 6 -}} + {{- fail "There can be at most 6 search domains specified in dnsConfig" -}} + {{- end }} +searches: + {{- range . }} + - {{ tpl . $ }} + {{- end }} + {{- end -}} + {{- with .Values.dnsConfig.options }} +options: + {{- range . }} + - name: {{ tpl .name $ }} + {{- with .value }} + value: {{ tpl (toString .) $ | quote }} + {{- end }} + {{- end -}} + {{- end -}} + {{- end -}} +{{- end -}} diff --git a/library/common/1.0.0/templates/lib/chart/_dnsPolicy.tpl b/library/common/1.0.0/templates/lib/chart/_dnsPolicy.tpl deleted file mode 100644 index 75d0824b41..0000000000 --- a/library/common/1.0.0/templates/lib/chart/_dnsPolicy.tpl +++ /dev/null @@ -1,13 +0,0 @@ -{{/* Returns dnsPolicy */}} -{{- define "ix.v1.common.dnsPolicy" -}} - {{- $policy := "ClusterFirst" -}} - {{- if .Values.dnsPolicy -}} - {{- if and (ne .Values.dnsPolicy "Default") (ne .Values.dnsPolicy "ClusterFirst") (ne .Values.dnsPolicy "ClusterFirstWithHostNet") (ne .Values.dnsPolicy "None") -}} - {{- fail "Not valid dnsPolicy. Valid options are ClusterFirst, Default, ClusterFirstWithHostNet, None" -}} - {{- end -}} - {{- $policy = .Values.dnsPolicy -}} - {{- else if .Values.hostNetwork -}} - {{- $policy = "ClusterFirstWithHostNet" -}} - {{- end -}} -{{- $policy -}} -{{- end -}} diff --git a/library/common/1.0.0/templates/lib/controller/_pod.tpl b/library/common/1.0.0/templates/lib/controller/_pod.tpl index 01b46022e6..993b5d879c 100644 --- a/library/common/1.0.0/templates/lib/controller/_pod.tpl +++ b/library/common/1.0.0/templates/lib/controller/_pod.tpl @@ -13,6 +13,10 @@ hostname: {{ tpl . $ }} {{- with (include "ix.v1.common.dnsPolicy" . | trim ) }} dnsPolicy: {{ . }} {{- end -}} +{{- with (include "ix.v1.common.dnsConfig" . | trim ) }} +dnsConfig: + {{- . | nindent 2 }} +{{- end -}} {{- with .Values.termination.gracePeriodSeconds }} terminationGracePeriodSeconds: {{ . }} {{- end }} diff --git a/library/common/1.0.0/values.yaml b/library/common/1.0.0/values.yaml index 58fdd8075e..16ae67d355 100644 --- a/library/common/1.0.0/values.yaml +++ b/library/common/1.0.0/values.yaml @@ -51,6 +51,8 @@ priorityClassName: "" dnsPolicy: "" +dnsConfig: {} + # TODO: docs podSecurityContext: runAsUser: 568 diff --git a/run_common_tests.sh b/run_common_tests.sh index 57454f07fe..5762d04c4e 100755 --- a/run_common_tests.sh +++ b/run_common_tests.sh @@ -7,8 +7,11 @@ curr_dir=${pwd} common_test_path="library/common-test" +echo "Cleaning old built charts..." +rm -r "$common_test_path/charts" + echo "Building common..." -helm dependency build "$common_test_path" +helm dependency update "$common_test_path" echo "Running tests..." cd "$common_test_path" helm unittest --helm3 -f "tests/*/*.yaml" .