From 6c7a33ce1cbdf36739a6bf789c4f788f5cc7c201 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Wed, 18 Jan 2023 15:31:56 +0200 Subject: [PATCH] add tests --- .../tests/daemonset/annotations_test.yaml | 64 ++++++++++ .../tests/daemonset/controller_test.yaml | 63 ++++++++++ .../tests/daemonset/default_test.yaml | 110 ++++++++++++++++++ .../tests/daemonset/label_test.yaml | 79 +++++++++++++ .../tests/daemonset/names_test.yaml | 38 ++++++ .../tests/deployment/controller_test.yaml | 20 ++++ .../tests/deployment/names_test.yaml | 2 +- .../tests/statefulset/annotations_test.yaml | 2 + .../tests/statefulset/controller_test.yaml | 22 ++++ .../tests/statefulset/label_test.yaml | 79 +++++++++++++ .../tests/statefulset/names_test.yaml | 38 ++++++ library/common-test/values.yaml | 7 -- .../1.0.0/templates/pods/_daemonset.tpl | 2 +- .../1.0.0/templates/pods/_statefulset.tpl | 34 +++--- 14 files changed, 534 insertions(+), 26 deletions(-) create mode 100644 library/common-test/tests/daemonset/annotations_test.yaml create mode 100644 library/common-test/tests/daemonset/controller_test.yaml create mode 100644 library/common-test/tests/daemonset/default_test.yaml create mode 100644 library/common-test/tests/daemonset/label_test.yaml create mode 100644 library/common-test/tests/daemonset/names_test.yaml create mode 100644 library/common-test/tests/statefulset/label_test.yaml create mode 100644 library/common-test/tests/statefulset/names_test.yaml diff --git a/library/common-test/tests/daemonset/annotations_test.yaml b/library/common-test/tests/daemonset/annotations_test.yaml new file mode 100644 index 00000000..7f6009f0 --- /dev/null +++ b/library/common-test/tests/daemonset/annotations_test.yaml @@ -0,0 +1,64 @@ +suite: daemonset annotation test +templates: + - common.yaml +tests: + - it: should pass with default values + documentIndex: &daemonsetDoc 0 + set: + controller.type: DaemonSet + asserts: + - hasDocuments: + count: 3 + - isKind: + of: DaemonSet + - isNull: + path: metadata.annotations + - matchRegex: + path: spec.template.metadata.annotations.rollme + pattern: "^[a-zA-Z0-9]{5}$" + + - it: should pass with controller and global annotations + documentIndex: *daemonsetDoc + set: + controller.type: DaemonSet + some_key: some_value + controller: + annotations: + controller_key: controller_value + controller_key2: "{{ .Values.some_key }}" + global: + annotations: + global_key: global_value + global_key2: "{{ .Values.some_key }}" + asserts: + - equal: + path: metadata.annotations + value: + controller_key: controller_value + controller_key2: some_value + global_key: global_value + global_key2: some_value + - isNull: + path: metadata.annotations.rollme + - matchRegex: + path: spec.template.metadata.annotations.rollme + pattern: "^[a-zA-Z0-9]{5}$" + + - it: should pass with podAnnotations set + documentIndex: *daemonsetDoc + set: + controller.type: DaemonSet + some_key: some_value2 + podAnnotations: + test: some_value + test2: "{{ .Values.some_key }}" + asserts: + - isSubset: + path: spec.template.metadata.annotations + content: + app: common-test + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/name: common-test + release: RELEASE-NAME + test2: some_value2 + test: some_value diff --git a/library/common-test/tests/daemonset/controller_test.yaml b/library/common-test/tests/daemonset/controller_test.yaml new file mode 100644 index 00000000..82b9de35 --- /dev/null +++ b/library/common-test/tests/daemonset/controller_test.yaml @@ -0,0 +1,63 @@ +suite: daemonset strategy test +templates: + - common.yaml +tests: + - it: should pass with strategy changed in DaemonSet + documentIndex: &dameonsetDoc 0 + set: + controller: + type: DaemonSet + strategy: OnDelete + rollingUpdate: + surge: 5 + unavailable: 6 + asserts: + - hasDocuments: + count: 3 + - isKind: + of: DaemonSet + - equal: + path: spec.updateStrategy + value: + type: OnDelete + - isNull: + path: spec.updateStrategy.rollingUpdate + + - it: should pass with strategy changed in DaemonSet + documentIndex: *dameonsetDoc + set: + controller: + type: DaemonSet + strategy: RollingUpdate + rollingUpdate: + surge: 5 + unavailable: 6 + asserts: + - equal: + path: spec.updateStrategy + value: + type: RollingUpdate + rollingUpdate: + maxSurge: 5 + maxUnavailable: 6 + + - it: should fail with wrong strategy + documentIndex: *dameonsetDoc + set: + controller: + type: DaemonSet + strategy: not_valid_strategy + asserts: + - failedTemplate: + errorMessage: Not a valid strategy type for DaemonSet (not_valid_strategy) + + - it: should pass with revisionHistoryLimit changed + documentIndex: *dameonsetDoc + set: + controller: + type: DaemonSet + revisionHistoryLimit: 1 + asserts: + - equal: + path: spec.revisionHistoryLimit + value: 1 diff --git a/library/common-test/tests/daemonset/default_test.yaml b/library/common-test/tests/daemonset/default_test.yaml new file mode 100644 index 00000000..a4275783 --- /dev/null +++ b/library/common-test/tests/daemonset/default_test.yaml @@ -0,0 +1,110 @@ +suite: daemonset default test +templates: + - common.yaml +tests: + - it: should pass with controller set to DaemonSet + documentIndex: &daemonsetDoc 0 + set: + controller.type: DaemonSet + asserts: + - hasDocuments: + count: 3 + - isKind: + of: DaemonSet + - isAPIVersion: + of: apps/v1 + - equal: + path: spec.revisionHistoryLimit + value: 3 + - equal: + path: spec.updateStrategy.type + value: RollingUpdate + - equal: + path: spec.template.spec.serviceAccountName + value: default + - equal: + path: spec.template.spec.hostNetwork + value: false + - equal: + path: spec.template.spec.terminationGracePeriodSeconds + value: 10 + - equal: + path: spec.template.spec.enableServiceLinks + value: false + - equal: + path: spec.template.spec.terminationGracePeriodSeconds + value: 10 + - isNull: + path: spec.template.spec.hostname + - equal: + path: spec.template.spec.dnsPolicy + value: ClusterFirst + - isNull: + path: spec.template.spec.dnsConfig + - isNull: + path: spec.template.spec.priorityClassName + - isNull: + path: spec.template.spec.schedulerName + - equal: + path: spec.template.spec.containers[0].name + value: RELEASE-NAME-common-test + - equal: + path: spec.template.spec.containers[0].image + value: repo:tag + - isNull: + path: spec.template.spec.containers[0].command + - isNull: + path: spec.template.spec.containers[0].args + - equal: + path: spec.template.spec.containers[0].tty + value: false + - equal: + path: spec.template.spec.containers[0].stdin + value: false + - isNull: + path: spec.template.spec.containers[0].lifecycle + - isNull: + path: spec.template.spec.containers[0].terminationMessagePath + - isNull: + path: spec.template.spec.containers[0].terminationMessagePolicy + - equal: + path: spec.template.spec.securityContext + value: + fsGroup: 568 + fsGroupChangePolicy: OnRootMismatch + supplementalGroups: [] + - equal: + path: spec.template.spec.containers[0].securityContext + value: + allowPrivilegeEscalation: false + privileged: false + readOnlyRootFilesystem: true + runAsNonRoot: true + runAsUser: 568 + runAsGroup: 568 + capabilities: + add: [] + drop: + - ALL + - equal: + path: spec.template.spec.containers[0].env + value: + - name: TZ + value: UTC + - name: UMASK + value: "002" + - name: UMASK_SET + value: "002" + - name: NVIDIA_VISIBLE_DEVICES + value: void + - name: S6_READ_ONLY_ROOT + value: "1" + - equal: + path: spec.template.spec.containers[0].volumeMounts + value: + - mountPath: /shared + name: shared + - mountPath: /tmp + name: tmp + - mountPath: /var/logs + name: varlogs diff --git a/library/common-test/tests/daemonset/label_test.yaml b/library/common-test/tests/daemonset/label_test.yaml new file mode 100644 index 00000000..7311d4d7 --- /dev/null +++ b/library/common-test/tests/daemonset/label_test.yaml @@ -0,0 +1,79 @@ +suite: daemonset label test +templates: + - common.yaml +chart: + appVersion: &appVer v1.2.3 +tests: + - it: should pass with default values + documentIndex: &daemonsetDoc 0 + set: + controller.type: DaemonSet + asserts: + - hasDocuments: + count: 3 + - isKind: + of: DaemonSet + - equal: + path: metadata.labels + value: + app: common-test + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: common-test + app.kubernetes.io/version: *appVer + helm-revision: "0" + helm.sh/chart: common-test-1.0.0 + release: RELEASE-NAME + - equal: + path: spec.selector.matchLabels + value: + app: common-test + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/name: common-test + release: RELEASE-NAME + + - it: should pass with controller and global labels + documentIndex: *daemonsetDoc + set: + controller.type: DaemonSet + some_key: some_value + controller: + labels: + controller_key: controller_value + controller_key2: "{{ .Values.some_key }}" + global: + labels: + global_key: global_value + global_key2: "{{ .Values.some_key }}" + asserts: + - equal: + path: metadata.labels + value: + controller_key: controller_value + controller_key2: some_value + global_key: global_value + global_key2: some_value + app: common-test + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: common-test + app.kubernetes.io/version: *appVer + helm-revision: "0" + helm.sh/chart: common-test-1.0.0 + release: RELEASE-NAME + + - it: should pass with podLabels set + documentIndex: *daemonsetDoc + set: + controller.type: DaemonSet + some_key: some_value2 + podLabels: + test: some_value + test2: "{{ .Values.some_key }}" + asserts: + - equal: + path: spec.template.metadata.labels.test2 + value: some_value2 + - equal: + path: spec.template.metadata.labels.test + value: some_value diff --git a/library/common-test/tests/daemonset/names_test.yaml b/library/common-test/tests/daemonset/names_test.yaml new file mode 100644 index 00000000..54153b0a --- /dev/null +++ b/library/common-test/tests/daemonset/names_test.yaml @@ -0,0 +1,38 @@ +suite: daemonset name test +templates: + - common.yaml +tests: + - it: should pass with default values + documentIndex: &daemonsetDoc 0 + set: + controller.type: DaemonSet + asserts: + - hasDocuments: + count: 3 + - isKind: + of: DaemonSet + - equal: + path: metadata.name + value: RELEASE-NAME-common-test + + - it: should pass with nameOverride + documentIndex: *daemonsetDoc + set: + controller.type: DaemonSet + nameOverride: overrodeName + asserts: + - equal: + path: metadata.name + value: RELEASE-NAME-overrodeName + + - it: should pass with global.nameOverride + documentIndex: *daemonsetDoc + set: + controller.type: DaemonSet + nameOverride: overrodeName + global: + nameOverride: globalOverrodeName + asserts: + - equal: + path: metadata.name + value: RELEASE-NAME-globalOverrodeName diff --git a/library/common-test/tests/deployment/controller_test.yaml b/library/common-test/tests/deployment/controller_test.yaml index a7aff2ba..a2147d48 100644 --- a/library/common-test/tests/deployment/controller_test.yaml +++ b/library/common-test/tests/deployment/controller_test.yaml @@ -78,3 +78,23 @@ tests: asserts: - failedTemplate: errorMessage: Not a valid strategy type for Deployment (not_valid_strategy) + + - it: should pass with revisionHistoryLimit changed + documentIndex: *deploymentDoc + set: + controller: + revisionHistoryLimit: 1 + asserts: + - equal: + path: spec.revisionHistoryLimit + value: 1 + + - it: should pass with replicas changed + documentIndex: *deploymentDoc + set: + controller: + replicas: 6 + asserts: + - equal: + path: spec.replicas + value: 6 diff --git a/library/common-test/tests/deployment/names_test.yaml b/library/common-test/tests/deployment/names_test.yaml index 8c220ae6..eb48507a 100644 --- a/library/common-test/tests/deployment/names_test.yaml +++ b/library/common-test/tests/deployment/names_test.yaml @@ -8,7 +8,7 @@ tests: - hasDocuments: count: 3 - isKind: - of: Deployment + of: StatefulSet - equal: path: metadata.name value: RELEASE-NAME-common-test diff --git a/library/common-test/tests/statefulset/annotations_test.yaml b/library/common-test/tests/statefulset/annotations_test.yaml index 016e2a52..694068ce 100644 --- a/library/common-test/tests/statefulset/annotations_test.yaml +++ b/library/common-test/tests/statefulset/annotations_test.yaml @@ -20,6 +20,7 @@ tests: - it: should pass with controller and global annotations documentIndex: *statefulsetDoc set: + controller.type: StatefulSet some_key: some_value controller: annotations: @@ -46,6 +47,7 @@ tests: - it: should pass with podAnnotations set documentIndex: *statefulsetDoc set: + controller.type: StatefulSet some_key: some_value2 podAnnotations: test: some_value diff --git a/library/common-test/tests/statefulset/controller_test.yaml b/library/common-test/tests/statefulset/controller_test.yaml index 71bcda66..4e2ec7e9 100644 --- a/library/common-test/tests/statefulset/controller_test.yaml +++ b/library/common-test/tests/statefulset/controller_test.yaml @@ -50,3 +50,25 @@ tests: asserts: - failedTemplate: errorMessage: Not a valid strategy type for StatefulSet (not_valid_strategy) + + - it: should pass with revisionHistoryLimit changed + documentIndex: *statefulsetDoc + set: + controller: + type: StatefulSet + revisionHistoryLimit: 1 + asserts: + - equal: + path: spec.revisionHistoryLimit + value: 1 + + - it: should pass with replicas changed + documentIndex: *statefulsetDoc + set: + controller: + type: StatefulSet + replicas: 6 + asserts: + - equal: + path: spec.replicas + value: 6 diff --git a/library/common-test/tests/statefulset/label_test.yaml b/library/common-test/tests/statefulset/label_test.yaml new file mode 100644 index 00000000..2a4fac55 --- /dev/null +++ b/library/common-test/tests/statefulset/label_test.yaml @@ -0,0 +1,79 @@ +suite: statefulset label test +templates: + - common.yaml +chart: + appVersion: &appVer v1.2.3 +tests: + - it: should pass with default values + documentIndex: &statefulsetDoc 0 + set: + controller.type: StatefulSet + asserts: + - hasDocuments: + count: 3 + - isKind: + of: StatefulSet + - equal: + path: metadata.labels + value: + app: common-test + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: common-test + app.kubernetes.io/version: *appVer + helm-revision: "0" + helm.sh/chart: common-test-1.0.0 + release: RELEASE-NAME + - equal: + path: spec.selector.matchLabels + value: + app: common-test + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/name: common-test + release: RELEASE-NAME + + - it: should pass with controller and global labels + documentIndex: *statefulsetDoc + set: + controller.type: StatefulSet + some_key: some_value + controller: + labels: + controller_key: controller_value + controller_key2: "{{ .Values.some_key }}" + global: + labels: + global_key: global_value + global_key2: "{{ .Values.some_key }}" + asserts: + - equal: + path: metadata.labels + value: + controller_key: controller_value + controller_key2: some_value + global_key: global_value + global_key2: some_value + app: common-test + app.kubernetes.io/instance: RELEASE-NAME + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: common-test + app.kubernetes.io/version: *appVer + helm-revision: "0" + helm.sh/chart: common-test-1.0.0 + release: RELEASE-NAME + + - it: should pass with podLabels set + documentIndex: *statefulsetDoc + set: + controller.type: StatefulSet + some_key: some_value2 + podLabels: + test: some_value + test2: "{{ .Values.some_key }}" + asserts: + - equal: + path: spec.template.metadata.labels.test2 + value: some_value2 + - equal: + path: spec.template.metadata.labels.test + value: some_value diff --git a/library/common-test/tests/statefulset/names_test.yaml b/library/common-test/tests/statefulset/names_test.yaml new file mode 100644 index 00000000..fa013ee6 --- /dev/null +++ b/library/common-test/tests/statefulset/names_test.yaml @@ -0,0 +1,38 @@ +suite: statefulset name test +templates: + - common.yaml +tests: + - it: should pass with default values + documentIndex: &statefulsetDoc 0 + set: + controller.type: StatefulSet + asserts: + - hasDocuments: + count: 3 + - isKind: + of: StatefulSet + - equal: + path: metadata.name + value: RELEASE-NAME-common-test + + - it: should pass with nameOverride + documentIndex: *statefulsetDoc + set: + controller.type: StatefulSet + nameOverride: overrodeName + asserts: + - equal: + path: metadata.name + value: RELEASE-NAME-overrodeName + + - it: should pass with global.nameOverride + documentIndex: *statefulsetDoc + set: + controller.type: StatefulSet + nameOverride: overrodeName + global: + nameOverride: globalOverrodeName + asserts: + - equal: + path: metadata.name + value: RELEASE-NAME-globalOverrodeName diff --git a/library/common-test/values.yaml b/library/common-test/values.yaml index db0e439e..67cebbb6 100644 --- a/library/common-test/values.yaml +++ b/library/common-test/values.yaml @@ -3,10 +3,3 @@ service: ports: main: port: 65535 - -# controller: -# type: Job - -# cron: -# enabled: true -# schedule: "* * * * *" diff --git a/library/common/1.0.0/templates/pods/_daemonset.tpl b/library/common/1.0.0/templates/pods/_daemonset.tpl index 624b7bc7..a3b4e469 100644 --- a/library/common/1.0.0/templates/pods/_daemonset.tpl +++ b/library/common/1.0.0/templates/pods/_daemonset.tpl @@ -22,7 +22,7 @@ spec: revisionHistoryLimit: {{ .Values.controller.revisionHistoryLimit }} {{- $strategy := default "RollingUpdate" .Values.controller.strategy -}} {{- if not (mustHas $strategy (list "OnDelete" "RollingUpdate")) -}} - {{- fail (printf "Not a valid strategy type for Daemonset (%s)" $strategy) -}} + {{- fail (printf "Not a valid strategy type for DaemonSet (%s)" $strategy) -}} {{- end }} updateStrategy: type: {{ $strategy }} diff --git a/library/common/1.0.0/templates/pods/_statefulset.tpl b/library/common/1.0.0/templates/pods/_statefulset.tpl index 44fe4912..b60d60d0 100644 --- a/library/common/1.0.0/templates/pods/_statefulset.tpl +++ b/library/common/1.0.0/templates/pods/_statefulset.tpl @@ -21,9 +21,6 @@ metadata: spec: revisionHistoryLimit: {{ .Values.controller.revisionHistoryLimit }} replicas: {{ .Values.controller.replicas }} - selector: - matchLabels: - {{- include "ix.v1.common.labels.selectorLabels" . | nindent 6 }} serviceName: {{ include "ix.v1.common.names.fullname" . }} {{- $strategy := default "RollingUpdate" .Values.controller.strategy -}} {{- if not (mustHas $strategy (list "OnDelete" "RollingUpdate")) -}} @@ -40,7 +37,22 @@ spec: {{- with $rollingUpdate.partition }} partition: {{ . }} {{- end -}} - {{- end -}} + {{- end }} + selector: + matchLabels: + {{- include "ix.v1.common.labels.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with (mustMerge (include "ix.v1.common.labels.selectorLabels" . | fromYaml) (include "ix.v1.common.annotations.workload" . | fromYaml) (include "ix.v1.common.podAnnotations" . | fromYaml)) }} + annotations: + {{- . | toYaml | trim | nindent 8 }} + {{- end -}} + {{- with (mustMerge (include "ix.v1.common.labels.selectorLabels" . | fromYaml) (include "ix.v1.common.podLabels" . | fromYaml)) }} + labels: + {{- . | toYaml | trim | nindent 8 }} + {{- end }} + spec: + {{- include "ix.v1.common.controller.pod" $ | trim | nindent 6 }} {{- if .Values.volumeClaimTemplates }} volumeClaimTemplates: {{- range $index, $vct := .Values.volumeClaimTemplates }} @@ -56,18 +68,6 @@ spec: requests: storage: {{ tpl ($vct.size | default $.Values.global.defaults.VCTSize) $ | quote }} {{- end -}} - {{- end }} - template: - metadata: - {{- with (mustMerge (include "ix.v1.common.labels.selectorLabels" . | fromYaml) (include "ix.v1.common.annotations.workload" . | fromYaml) (include "ix.v1.common.podAnnotations" . | fromYaml)) }} - annotations: - {{- . | toYaml | trim | nindent 8 }} - {{- end -}} - {{- with (mustMerge (include "ix.v1.common.labels.selectorLabels" . | fromYaml) (include "ix.v1.common.podLabels" . | fromYaml)) }} - labels: - {{- . | toYaml | trim | nindent 8 }} - {{- end }} - spec: - {{- include "ix.v1.common.controller.pod" $ | trim | nindent 6 }} + {{- end -}} {{- end }} {{/*TODO: unittests*/}}