From 7afe5189a0f3c3ec10e65b36a0dfe8b1ac99ab79 Mon Sep 17 00:00:00 2001 From: Nikos Mimigiannis Date: Mon, 11 Feb 2019 05:39:49 -0500 Subject: [PATCH] Create Helm test for redis chart Task: 21711 Story: 2002201 This patch creates Helm test for redis chart Change-Id: Ifac407b5544484f2626ba7ffdbd2e96fca6e51ef Signed-off-by: Nikos Mimigiannis --- redis/templates/configmap-bin.yaml | 4 ++ redis/templates/pod_test.yaml | 66 +++++++++++++++++++ .../templates/test/_python_redis_tests.py.tpl | 54 +++++++++++++++ redis/templates/test/_redis_test.sh.tpl | 12 ++++ redis/values.yaml | 12 ++++ .../common/010-deploy-docker-registry.sh | 3 + 6 files changed, 151 insertions(+) create mode 100644 redis/templates/pod_test.yaml create mode 100644 redis/templates/test/_python_redis_tests.py.tpl create mode 100644 redis/templates/test/_redis_test.sh.tpl diff --git a/redis/templates/configmap-bin.yaml b/redis/templates/configmap-bin.yaml index 76bb0a0ad..a96433fda 100644 --- a/redis/templates/configmap-bin.yaml +++ b/redis/templates/configmap-bin.yaml @@ -24,4 +24,8 @@ metadata: data: image-repo-sync.sh: | {{- include "helm-toolkit.scripts.image_repo_sync" . | indent 4 }} + redis-test.sh: | +{{ tuple "test/_redis_test.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} + python-tests.py: | +{{ tuple "test/_python_redis_tests.py.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} {{- end }} diff --git a/redis/templates/pod_test.yaml b/redis/templates/pod_test.yaml new file mode 100644 index 000000000..86fe5dae9 --- /dev/null +++ b/redis/templates/pod_test.yaml @@ -0,0 +1,66 @@ +{{/* +Copyright 2019 The Openstack-Helm Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/}} + +{{- if .Values.manifests.helm_tests }} +{{- $envAll := . }} + +{{- $serviceAccountName := print .Release.Name "-test" }} +{{ tuple $envAll "tests" $serviceAccountName | include "helm-toolkit.snippets.kubernetes_pod_rbac_serviceaccount" }} +--- +apiVersion: v1 +kind: Pod +metadata: + name: "{{.Release.Name}}-test" + labels: +{{ tuple $envAll "redis" "test" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 4 }} + annotations: + "helm.sh/hook": test-success + {{ tuple $envAll | include "helm-toolkit.snippets.release_uuid" }} +spec: + serviceAccountName: {{ $serviceAccountName }} + nodeSelector: + {{ .Values.labels.test.node_selector_key }}: {{ .Values.labels.test.node_selector_value }} + restartPolicy: Never + containers: + - name: {{.Release.Name}}-helm-tests +{{ tuple $envAll "helm_tests" | include "helm-toolkit.snippets.image" | indent 6 }} +{{ tuple $envAll $envAll.Values.pod.resources.jobs.tests | include "helm-toolkit.snippets.kubernetes_resources" | indent 6 }} + command: + - /tmp/redis-test.sh + env: + - name: REDIS_HOST + value: "redis" + - name: REDIS_PORT + value: "{{ .Values.network.port }}" + - name: REDIS_DB + value: '0' + volumeMounts: + - name: redis-test + mountPath: /tmp/redis-test.sh + subPath: redis-test.sh + - name: redis-python + mountPath: /tmp/python-tests.py + subPath: python-tests.py + volumes: + - name: redis-test + configMap: + name: redis-bin + defaultMode: 0555 + - name: redis-python + configMap: + name: redis-bin + defaultMode: 0555 +{{- end }} diff --git a/redis/templates/test/_python_redis_tests.py.tpl b/redis/templates/test/_python_redis_tests.py.tpl new file mode 100644 index 000000000..47cdd88ba --- /dev/null +++ b/redis/templates/test/_python_redis_tests.py.tpl @@ -0,0 +1,54 @@ +import os +import redis + + +class RedisTest(object): + + def __init__(self): + host = os.environ.get('REDIS_HOST', 'redis') + port = os.environ.get('REDIS_PORT', 6379) + db = os.environ.get('REDIS_DB', 0) + self.redis_conn = redis.Redis(host, port, db) + + def test_connection(self): + ping = self.redis_conn.ping() + assert ping, "No connection to database" + print("Successfully connected to database") + + def database_info(self): + ip_port = [] + for client in self.redis_conn.client_list(): + ip_port.append(client["addr"]) + print(ip_port) + assert self.redis_conn.client_list(), "Database client's list is null" + return ip_port + + def test_insert_delete_data(self): + key = "test" + value = "it's working" + result_set = self.redis_conn.set(key, value) + assert result_set, "Error: SET command failed" + print("Successfully SET keyvalue pair") + result_get = self.redis_conn.get(key) + assert result_get, "Error: GET command failed" + print("Successfully GET keyvalue pair") + db_size = self.redis_conn.dbsize() + assert db_size > 0, "Database size not valid" + result_delete = self.redis_conn.delete(key) + assert result_delete == 1, "Error: Delete command failed" + print("Successfully DELETED keyvalue pair") + + def test_client_kill(self, client_ip_port_list): + for client_ip_port in client_ip_port_list: + result = self.redis_conn.client_kill(client_ip_port) + print(result) + assert result, "Client failed to be removed" + print("Successfully DELETED client") + + +client_ip_port = [] +redis_client = RedisTest() +redis_client.test_connection() +client_ip_port = redis_client.database_info() +redis_client.test_insert_delete_data() +redis_client.test_client_kill(client_ip_port) diff --git a/redis/templates/test/_redis_test.sh.tpl b/redis/templates/test/_redis_test.sh.tpl new file mode 100644 index 000000000..dd633594a --- /dev/null +++ b/redis/templates/test/_redis_test.sh.tpl @@ -0,0 +1,12 @@ +#!/bin/bash +set -ex + +echo "Start Redis Test" +echo "Print Environmental variables" +echo $REDIS_HOST +echo $REDIS_PORT +echo $REDIS_DB +apt-get update +apt-get -y install python-pip +yes w | pip install redis +python /tmp/python-tests.py diff --git a/redis/values.yaml b/redis/values.yaml index 2328ddaa0..1792a2a80 100644 --- a/redis/values.yaml +++ b/redis/values.yaml @@ -20,6 +20,7 @@ images: tags: redis: docker.io/redis:4.0.1 + helm_tests: docker.io/ubuntu dep_check: quay.io/stackanetes/kubernetes-entrypoint:v0.3.1 image_repo_sync: docker.io/docker:17.07.0 pull_policy: IfNotPresent @@ -63,6 +64,13 @@ pod: limits: memory: "1024Mi" cpu: "2000m" + tests: + requests: + memory: "128Mi" + cpu: "100m" + limits: + memory: "1024Mi" + cpu: "2000m" labels: redis: @@ -71,6 +79,9 @@ labels: job: node_selector_key: openstack-control-plane node_selector_value: enabled + test: + node_selector_key: openstack-control-plane + node_selector_value: enabled network: port: 6379 @@ -112,3 +123,4 @@ manifests: deployment: true job_image_repo_sync: true service: true + helm_tests: true diff --git a/tools/deployment/common/010-deploy-docker-registry.sh b/tools/deployment/common/010-deploy-docker-registry.sh index 5689c78fc..deec66bf3 100755 --- a/tools/deployment/common/010-deploy-docker-registry.sh +++ b/tools/deployment/common/010-deploy-docker-registry.sh @@ -58,3 +58,6 @@ helm upgrade --install docker-registry ./registry \ helm status docker-registry-nfs-provisioner helm status docker-registry-redis helm status docker-registry + +#NOTE: Run helm tests +helm test docker-registry-redis