From 720adbb7392811368fedbc5e91796ac203b4f255 Mon Sep 17 00:00:00 2001 From: Stavros kois Date: Tue, 17 Jan 2023 20:29:17 +0200 Subject: [PATCH] add initial statefulset tests --- .../tests/deployment/default_test.yaml | 9 ++ .../tests/statefulset/annotations_test.yaml | 62 +++++++++ .../tests/statefulset/controller_test.yaml | 52 ++++++++ .../tests/statefulset/default_test.yaml | 116 ++++++++++++++++ .../tests/statefulset/vct_test.yaml | 125 ++++++++++++++++++ .../templates/lib/container/_volumeMounts.tpl | 2 +- .../1.0.0/templates/pods/_statefulset.tpl | 2 +- 7 files changed, 366 insertions(+), 2 deletions(-) create mode 100644 library/common-test/tests/statefulset/annotations_test.yaml create mode 100644 library/common-test/tests/statefulset/controller_test.yaml create mode 100644 library/common-test/tests/statefulset/default_test.yaml create mode 100644 library/common-test/tests/statefulset/vct_test.yaml diff --git a/library/common-test/tests/deployment/default_test.yaml b/library/common-test/tests/deployment/default_test.yaml index f3f6218802..88d35dea5f 100644 --- a/library/common-test/tests/deployment/default_test.yaml +++ b/library/common-test/tests/deployment/default_test.yaml @@ -11,6 +11,15 @@ tests: of: Deployment - isAPIVersion: of: apps/v1 + - equal: + path: spec.revisionHistoryLimit + value: 3 + - equal: + path: spec.replicas + value: 1 + - equal: + path: spec.strategy.type + value: Recreate - equal: path: spec.template.spec.serviceAccountName value: default diff --git a/library/common-test/tests/statefulset/annotations_test.yaml b/library/common-test/tests/statefulset/annotations_test.yaml new file mode 100644 index 0000000000..016e2a52f8 --- /dev/null +++ b/library/common-test/tests/statefulset/annotations_test.yaml @@ -0,0 +1,62 @@ +suite: statefulset annotation 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 + - 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: *statefulsetDoc + set: + 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: *statefulsetDoc + set: + 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/statefulset/controller_test.yaml b/library/common-test/tests/statefulset/controller_test.yaml new file mode 100644 index 0000000000..71bcda66e5 --- /dev/null +++ b/library/common-test/tests/statefulset/controller_test.yaml @@ -0,0 +1,52 @@ +suite: statefulset strategy test +templates: + - common.yaml +tests: + - it: should pass with strategy changed in StatefulSet + documentIndex: &statefulsetDoc 0 + set: + controller: + type: StatefulSet + strategy: OnDelete + rollingUpdate: + partition: 5 + unavailable: 6 + asserts: + - hasDocuments: + count: 3 + - isKind: + of: StatefulSet + - equal: + path: spec.updateStrategy + value: + type: OnDelete + - isNull: + path: spec.updateStrategy.rollingUpdate + + - it: should pass with strategy changed in StatefulSet + documentIndex: *statefulsetDoc + set: + controller: + type: StatefulSet + strategy: RollingUpdate + rollingUpdate: + partition: 5 + unavailable: 6 + asserts: + - equal: + path: spec.updateStrategy + value: + type: RollingUpdate + rollingUpdate: + partition: 5 + maxUnavailable: 6 + + - it: should fail with wrong strategy + documentIndex: *statefulsetDoc + set: + controller: + type: StatefulSet + strategy: not_valid_strategy + asserts: + - failedTemplate: + errorMessage: Not a valid strategy type for StatefulSet (not_valid_strategy) diff --git a/library/common-test/tests/statefulset/default_test.yaml b/library/common-test/tests/statefulset/default_test.yaml new file mode 100644 index 0000000000..99ad290b6f --- /dev/null +++ b/library/common-test/tests/statefulset/default_test.yaml @@ -0,0 +1,116 @@ +suite: statefulset default test +templates: + - common.yaml +tests: + - it: should pass with controller set to StatefulSet + documentIndex: &statefulsetDoc 0 + set: + controller.type: StatefulSet + asserts: + - hasDocuments: + count: 3 + - isKind: + of: StatefulSet + - isAPIVersion: + of: apps/v1 + - equal: + path: spec.serviceName + value: RELEASE-NAME-common-test + - equal: + path: spec.revisionHistoryLimit + value: 3 + - equal: + path: spec.replicas + value: 1 + - 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/statefulset/vct_test.yaml b/library/common-test/tests/statefulset/vct_test.yaml new file mode 100644 index 0000000000..195f8bdc29 --- /dev/null +++ b/library/common-test/tests/statefulset/vct_test.yaml @@ -0,0 +1,125 @@ +suite: statefulset vct test +templates: + - common.yaml +tests: + - it: should pass with vct defined in StatefulSet + documentIndex: &statefulsetDoc 0 + set: + controller.type: StatefulSet + volumeClaimTemplates: + data: + enabled: true + mountPath: "/bitnami/mariadb" + asserts: + - isKind: + of: StatefulSet + - equal: + path: spec.volumeClaimTemplates[0] + value: + metadata: + name: data + spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 999Gi + + - it: should pass with multiple vct defined in StatefulSet + documentIndex: *statefulsetDoc + set: + controller.type: StatefulSet + volumeClaimTemplates: + data: + enabled: true + mountPath: "/bitnami/mariadb" + other-data: + enabled: true + mountPath: "/bitnami/mariadb" + asserts: + - isKind: + of: StatefulSet + - equal: + path: spec.volumeClaimTemplates[0] + value: + metadata: + name: data + spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 999Gi + - equal: + path: spec.volumeClaimTemplates[1] + value: + metadata: + name: other-data + spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 999Gi + - equal: + path: spec.template.spec.containers[0].volumeMounts + value: + - mountPath: /shared + name: shared + - mountPath: /tmp + name: tmp + - mountPath: /var/logs + name: varlogs + - mountPath: /bitnami/mariadb + name: data + - mountPath: /bitnami/mariadb + name: other-data + + - it: should pass with vct size changed in StatefulSet + documentIndex: *statefulsetDoc + set: + controller.type: StatefulSet + volumeClaimTemplates: + data: + enabled: true + size: 10Gi + mountPath: "/bitnami/mariadb" + asserts: + - isKind: + of: StatefulSet + - equal: + path: spec.volumeClaimTemplates[0] + value: + metadata: + name: data + spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 10Gi + + - it: should pass with accessMode changed in StatefulSet + documentIndex: *statefulsetDoc + set: + controller.type: StatefulSet + volumeClaimTemplates: + data: + enabled: true + size: 10Gi + accessMode: ReadWriteMany + mountPath: "/bitnami/mariadb" + asserts: + - isKind: + of: StatefulSet + - equal: + path: spec.volumeClaimTemplates[0] + value: + metadata: + name: data + spec: + accessModes: + - ReadWriteMany + resources: + requests: + storage: 10Gi diff --git a/library/common/1.0.0/templates/lib/container/_volumeMounts.tpl b/library/common/1.0.0/templates/lib/container/_volumeMounts.tpl index 5e5be10c2f..f70ef17c42 100644 --- a/library/common/1.0.0/templates/lib/container/_volumeMounts.tpl +++ b/library/common/1.0.0/templates/lib/container/_volumeMounts.tpl @@ -16,7 +16,7 @@ {{- end -}} {{/* TODO: write tests when statefulset is ready */}} - {{- if eq $root.Values.controller.type "statefulset" -}} + {{- if eq $root.Values.controller.type "StatefulSet" -}} {{- range $index, $vct := $root.Values.volumeClaimTemplates -}} {{- include "ix.v1.common.container.volumeMount" (dict "root" $root "item" $vct diff --git a/library/common/1.0.0/templates/pods/_statefulset.tpl b/library/common/1.0.0/templates/pods/_statefulset.tpl index 4f834ff0c5..44fe491286 100644 --- a/library/common/1.0.0/templates/pods/_statefulset.tpl +++ b/library/common/1.0.0/templates/pods/_statefulset.tpl @@ -27,7 +27,7 @@ spec: serviceName: {{ include "ix.v1.common.names.fullname" . }} {{- $strategy := default "RollingUpdate" .Values.controller.strategy -}} {{- if not (mustHas $strategy (list "OnDelete" "RollingUpdate")) -}} - {{- fail (printf "Not a valid strategy type for Deployment (%s)" $strategy) -}} + {{- fail (printf "Not a valid strategy type for StatefulSet (%s)" $strategy) -}} {{- end }} updateStrategy: type: {{ $strategy }}