Nagios: Add Selenium tests via helm test pod

This adds selenium tests for the Nagios chart via a helm test
pod to help ensure the Nagios deployment is functional and
accessible

Change-Id: I44f30fbac274546abadba0290de029ed2b9d1958
Signed-off-by: Steve Wilkerson <sw5822@att.com>
This commit is contained in:
Steve Wilkerson 2019-06-04 13:26:03 -05:00 committed by Steve Wilkerson
parent d23e847e1f
commit 40769d5a60
5 changed files with 243 additions and 0 deletions

View File

@ -0,0 +1,141 @@
#!/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 os
import logging
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('Nagios 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)
if "NAGIOS_USER" in os.environ:
nagios_user = os.environ['NAGIOS_USER']
logger.info('Found Nagios username')
else:
logger.critical('Nagios username environment variable not set')
sys.exit(1)
if "NAGIOS_PASSWORD" in os.environ:
nagios_password = os.environ['NAGIOS_PASSWORD']
logger.info('Found Nagios password')
else:
logger.critical('Nagios password environment variable not set')
sys.exit(1)
if "NAGIOS_URI" in os.environ:
nagios_uri = os.environ['NAGIOS_URI']
logger.info('Found Nagios URI')
else:
logger.critical('Nagios 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 Chrome webdriver")
try:
browser = webdriver.Chrome('/etc/selenium/chromedriver', chrome_options=options)
logger.info("Successfully opened Chrome webdriver")
except:
logger.error("Unable to open Chrome webdriver")
browser.close()
sys.exit(1)
logger.info("Attempting to login to Nagios dashboard")
try:
browser.get('http://'+nagios_user+':'+nagios_password+'@'+nagios_uri)
logger.info("Successfully logged in to Nagios dashboard")
sideFrame = browser.switch_to.frame('side')
try:
logger.info("Attempting to access Nagios services link")
services = browser.find_element_by_link_text('Services')
services.click()
logger.info("Successfully accessed Nagios services link")
try:
logger.info("Attempting to capture Nagios services screen")
el = WebDriverWait(browser, 15)
browser.save_screenshot('/tmp/artifacts/Nagios_Services.png')
logger.info("Successfully captured Nagios services screen")
except:
logger.error("Unable to capture Nagios services screen")
browser.close()
sys.exit(1)
except:
logger.error("Unable to access Nagios services link")
browser.close()
sys.exit(1)
try:
logger.info("Attempting to access Nagios host groups link")
host_groups = browser.find_element_by_link_text('Host Groups')
host_groups.click()
logger.info("Successfully accessed Nagios host groups link")
try:
logger.info("Attempting to capture Nagios host groups screen")
el = WebDriverWait(browser, 15)
browser.save_screenshot('/tmp/artifacts/Nagios_Host_Groups.png')
logger.info("Successfully captured Nagios host groups screen")
except:
logger.error("Unable to capture Nagios host groups screen")
browser.close()
sys.exit(1)
except:
logger.error("Unable to access Nagios host groups link")
browser.close()
sys.exit(1)
try:
logger.info("Attempting to access Nagios hosts link")
hosts = browser.find_element_by_link_text('Hosts')
hosts.click()
logger.info("Successfully accessed Nagios hosts link")
try:
logger.info("Attempting to capture Nagios hosts screen")
el = WebDriverWait(browser, 15)
browser.save_screenshot('/tmp/artifacts/Nagios_Hosts.png')
logger.info("Successfully captured Nagios hosts screen")
except:
logger.error("Unable to capture Nagios hosts screen")
browser.close()
sys.exit(1)
except:
logger.error("Unable to access Nagios hosts link")
browser.close()
sys.exit(1)
browser.close()
logger.info("The following screenshots were captured:")
for root, dirs, files in os.walk("/tmp/artifacts/"):
for name in files:
logger.info(os.path.join(root, name))
except:
logger.error("Unable to log in to Nagios dashbaord")
browser.close()
sys.exit(1)

View File

@ -26,6 +26,8 @@ data:
{{ tuple "bin/_apache.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }}
nagios-readiness.sh: |
{{ tuple "bin/_nagios-readiness.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 }}
image-repo-sync.sh: |+
{{- include "helm-toolkit.scripts.image_repo_sync" . | indent 4 }}
{{- end }}

View File

@ -0,0 +1,80 @@
{{/*
Copyright 2017 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.pod_helm_test }}
{{- $envAll := . }}
{{- $nagiosUserSecret := .Values.secrets.nagios.admin }}
{{- $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 "nagios" "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:
{{ dict "envAll" $envAll "application" "monitoring" | include "helm-toolkit.snippets.kubernetes_pod_security_context" | indent 2 }}
serviceAccountName: {{ $serviceAccountName }}
nodeSelector:
{{ .Values.labels.test.node_selector_key }}: {{ .Values.labels.test.node_selector_value }}
restartPolicy: Never
initContainers:
{{ tuple $envAll "tests" list | include "helm-toolkit.snippets.kubernetes_entrypoint_init_container" | indent 4 }}
containers:
- name: {{.Release.Name}}-helm-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" "monitoring" "container" "helm_tests" | include "helm-toolkit.snippets.kubernetes_container_security_context" | indent 6 }}
command:
- /tmp/selenium-tests.py
env:
- name: NAGIOS_USER
valueFrom:
secretKeyRef:
name: {{ $nagiosUserSecret }}
key: NAGIOSADMIN_USER
- name: NAGIOS_PASSWORD
valueFrom:
secretKeyRef:
name: {{ $nagiosUserSecret }}
key: NAGIOSADMIN_PASS
- name: NAGIOS_URI
value: {{ tuple "nagios" "pubic" "http" . | include "helm-toolkit.endpoints.host_and_port_endpoint_uri_lookup" }}
volumeMounts:
- name: pod-tmp
mountPath: /tmp
- name: artifacts
mountPath: /tmp/artifacts
- name: nagios-bin
mountPath: /tmp/selenium-tests.py
subPath: selenium-tests.py
readOnly: true
volumes:
- name: pod-tmp
emptyDir: {}
- name: artifacts
emptyDir: {}
- name: nagios-bin
configMap:
name: nagios-bin
defaultMode: 0555
{{- end }}

View File

@ -21,6 +21,7 @@ images:
apache_proxy: docker.io/httpd:2.4
nagios: quay.io/attcomdev/nagios:410fcb08d2586e98e18ced317dab4157eb27456e
dep_check: quay.io/stackanetes/kubernetes-entrypoint:v0.2.1
selenium_tests: docker.io/openstackhelm/osh-selenium:latest-ubuntu_xenial
image_repo_sync: docker.io/docker:17.07.0
pull_policy: IfNotPresent
local_registry:
@ -36,6 +37,9 @@ labels:
job:
node_selector_key: openstack-control-plane
node_selector_value: enabled
test:
node_selector_key: openstack-control-plane
node_selector_value: enabled
dependencies:
dynamic:
@ -52,6 +56,10 @@ dependencies:
endpoint: internal
nagios:
services: null
tests:
services:
- service: nagios
endpoint: internal
secrets:
nagios:
@ -204,6 +212,8 @@ pod:
readOnlyRootFilesystem: false
nagios:
readOnlyRootFilesystem: false
helm_tests:
readOnlyRootFilesystem: false
lifecycle:
upgrades:
revision_history: 3
@ -240,6 +250,13 @@ pod:
requests:
memory: "128Mi"
cpu: "100m"
tests:
limits:
memory: "1024Mi"
cpu: "2000m"
requests:
memory: "128Mi"
cpu: "100m"
manifests:
configmap_bin: true
@ -248,6 +265,7 @@ manifests:
ingress: true
job_image_repo_sync: true
network_policy: false
pod_helm_test: true
secret_nagios: true
secret_ingress_tls: true
service: true

View File

@ -28,3 +28,5 @@ helm upgrade --install nagios ./nagios \
#NOTE: Validate Deployment info
helm status nagios
helm test nagios