From 25e105f26a8048ef52b4b8e328f4f1e6364ab313 Mon Sep 17 00:00:00 2001 From: Steve Wilkerson Date: Tue, 4 Jun 2019 13:11:25 -0500 Subject: [PATCH] Grafana: Add Selenium tests to helm test pod This adds selenium tests for the grafana chart to the helm test pod to help ensure the Grafana deployment is functional and accessible Change-Id: Idc8d97e5111628d1ed4f25145086d54c5e0136e7 Signed-off-by: Steve Wilkerson --- grafana/templates/bin/_selenium-tests.py.tpl | 87 ++++++++++++++++++++ grafana/templates/configmap-bin.yaml | 2 + grafana/templates/pod-helm-tests.yaml | 26 ++++++ grafana/values.yaml | 1 + 4 files changed, 116 insertions(+) create mode 100644 grafana/templates/bin/_selenium-tests.py.tpl diff --git a/grafana/templates/bin/_selenium-tests.py.tpl b/grafana/templates/bin/_selenium-tests.py.tpl new file mode 100644 index 000000000..15d89b6b8 --- /dev/null +++ b/grafana/templates/bin/_selenium-tests.py.tpl @@ -0,0 +1,87 @@ +#!/usr/bin/env python + +{{/* +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. +*/}} + +import logging +import os +import sys +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver.chrome.options import Options + +# Create logger, console handler and formatter +logger = logging.getLogger('Grafana Selenium Tests') +logger.setLevel(logging.DEBUG) +ch = logging.StreamHandler() +ch.setLevel(logging.DEBUG) +formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') + +# Set the formatter and add the handler +ch.setFormatter(formatter) +logger.addHandler(ch) + +# Get Grafana admin user name +if "GRAFANA_USER" in os.environ: + grafana_user = os.environ['GRAFANA_USER'] + logger.info('Found Grafana username') +else: + logger.critical('Grafana username environment variable not set') + sys.exit(1) + +if "GRAFANA_PASSWORD" in os.environ: + grafana_password = os.environ['GRAFANA_PASSWORD'] + logger.info('Found Grafana password') +else: + logger.critical('Grafana password environment variable not set') + sys.exit(1) + +if "GRAFANA_URI" in os.environ: + grafana_uri = os.environ['GRAFANA_URI'] + logger.info('Found Grafana URI') +else: + logger.critical('Grafana URI environment variable not set') + sys.exit(1) + +options = Options() +options.add_argument('--headless') +options.add_argument('--no-sandbox') +options.add_argument('--window-size=1920x1080') + +logger.info("Attempting to open Grafana dashboard") +try: + browser = webdriver.Chrome('/etc/selenium/chromedriver', chrome_options=options) + logger.info("Successfully opened Grafana dashboard") +except exception, e: + logger.error("Unable to open Grafana") + browser.close() + sys.exit(1) + +logger.info("Attempting to log into Grafana dashboard") +try: + browser.get(grafana_uri) + browser.find_element_by_name('username').send_keys(grafana_user) + browser.find_element_by_name('password').send_keys(grafana_password) + browser.find_element_by_css_selector('body > grafana-app > div.main-view > div > div:nth-child(1) > div > div > div.login-inner-box > form > div.login-button-group > button').click() + logger.info("Successfully logged in to Grafana") +except exception, e: + logger.error("Failed to log in to Grafana") + browser.close() + sys.exit(1) + +browser.close() diff --git a/grafana/templates/configmap-bin.yaml b/grafana/templates/configmap-bin.yaml index 01c1bbfa0..22e9cf7a1 100644 --- a/grafana/templates/configmap-bin.yaml +++ b/grafana/templates/configmap-bin.yaml @@ -32,6 +32,8 @@ data: {{ tuple "bin/_helm-tests.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} grafana.sh: | {{ tuple "bin/_grafana.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} + selenium-tests.py: | +{{ tuple "bin/_selenium-tests.py.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} set-admin-password.sh: | {{ tuple "bin/_set-admin-password.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} {{- end }} diff --git a/grafana/templates/pod-helm-tests.yaml b/grafana/templates/pod-helm-tests.yaml index 9a1188b56..74017cec9 100644 --- a/grafana/templates/pod-helm-tests.yaml +++ b/grafana/templates/pod-helm-tests.yaml @@ -67,6 +67,32 @@ spec: mountPath: /tmp/helm-tests.sh subPath: helm-tests.sh readOnly: true + - name: {{.Release.Name}}-selenium-tests +{{ tuple $envAll "selenium_tests" | include "helm-toolkit.snippets.image" | indent 6 }} +{{ tuple $envAll $envAll.Values.pod.resources.jobs.tests | include "helm-toolkit.snippets.kubernetes_resources" | indent 6 }} +{{ dict "envAll" $envAll "application" "test" "container" "helm_tests" | include "helm-toolkit.snippets.kubernetes_container_security_context" | indent 6 }} + command: + - /tmp/selenium-tests.py + env: + - name: GRAFANA_USER + valueFrom: + secretKeyRef: + name: grafana-admin-creds + key: GRAFANA_ADMIN_USERNAME + - name: GRAFANA_PASSWORD + valueFrom: + secretKeyRef: + name: grafana-admin-creds + key: GRAFANA_ADMIN_PASSWORD + - name: GRAFANA_URI + value: {{ tuple "grafana" "public" "grafana" . | include "helm-toolkit.endpoints.keystone_endpoint_uri_lookup" }} + volumeMounts: + - name: pod-tmp + mountPath: /tmp + - name: grafana-bin + mountPath: /tmp/selenium-tests.py + subPath: selenium-tests.py + readOnly: true volumes: - name: pod-tmp emptyDir: {} diff --git a/grafana/values.yaml b/grafana/values.yaml index 67be70452..5a19d18a3 100644 --- a/grafana/values.yaml +++ b/grafana/values.yaml @@ -23,6 +23,7 @@ images: db_init: docker.io/openstackhelm/heat:newton-ubuntu_xenial grafana_db_session_sync: docker.io/openstackhelm/heat:newton-ubuntu_xenial helm_tests: docker.io/openstackhelm/heat:newton-ubuntu_xenial + selenium_tests: docker.io/openstackhelm/osh-selenium:latest-ubuntu_xenial image_repo_sync: docker.io/docker:17.07.0 pull_policy: IfNotPresent local_registry: