add dnsconfig and split tests a bit

This commit is contained in:
Stavros kois
2022-11-21 00:37:51 +02:00
parent 66b2b452df
commit 85ca01921b
10 changed files with 485 additions and 190 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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:

View File

@@ -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

View File

@@ -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 -}}

View File

@@ -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 -}}

View File

@@ -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 }}

View File

@@ -51,6 +51,8 @@ priorityClassName: ""
dnsPolicy: ""
dnsConfig: {}
# TODO: docs
podSecurityContext:
runAsUser: 568

View File

@@ -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" .