Skip to content
Snippets Groups Projects
Unverified Commit f1195933 authored by Varac's avatar Varac
Browse files

Update pytest to check all helmreleases

parent 2a31800e
Branches
Tags
No related merge requests found
"""
This module contains tests for:
- test_helmreleases: if all the helmreleases in EXPECTED_RELEASES are
deployed successfully
- test_helmreleases: if all the helmreleases are ready
- test_apps_running: if all the applications are running without problems
(i.e., condition = Ready for all the related pods)
Documentation for the kubernetes client:
* https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md
* https://github.com/kubernetes-client/python/tree/master/examples
"""
import os
from kubernetes import client, config
from kubernetes.client.rest import ApiException
import pytest
EXPECTED_KUSTOMIZATIONS_BASE = [
'openappstack',
'monitoring',
'infrastructure',
'core',
]
EXPECTED_RELEASES = {
'cert-manager': ['cert-manager'],
'kube-system': ['local-path-provisioner'],
'oas': [
'ingress',
'kube-prometheus-stack',
'loki',
'promtail',
'eventrouter',
'single-sign-on'
],
'oas-apps': ['nextcloud', 'rocketchat', 'wordpress'],
}
EXPECTED_APP_LABELS = {
'cert-manager': {
'namespace': 'cert-manager',
'label_selector': 'app.kubernetes.io/instance=cert-manager'
},
'eventrouter': {
'namespace': 'oas',
'label_selector': 'app=eventrouter'},
'local-path-provisioner': {
'namespace': 'kube-system',
'label_selector': 'app.kubernetes.io/instance=local-path-provisioner'},
'loki': {
'namespace': 'oas',
'label_selector': 'app=loki'},
'promtail': {
'namespace': 'oas',
'label_selector': 'app=promtail'},
'nextcloud': {
'namespace': 'oas-apps',
'label_selector': 'app.kubernetes.io/instance=nc'},
'kube-prometheus-stack': {
'namespace': 'oas',
'label_selector': 'app in (grafana,prometheus)'},
'rocketchat': {
'namespace': 'oas-apps',
'label_selector': 'app.kubernetes.io/instance=rocketchat'},
'single-sign-on': {
'namespace': 'oas',
'label_selector': 'app.kubernetes.io/name in (hydra,hydra-maester,single-sign-on-userbackend,single-sign-on-consent,single-sign-on-userpanel,single-sign-on-login)'},
'wordpress': {
'namespace': 'oas-apps',
'label_selector': 'release=wordpress'}
}
import pytest
def get_kustomization_status(name, api, namespace="flux-system"):
......@@ -93,7 +42,7 @@ def get_kustomization_status(name, api, namespace="flux-system"):
def get_release_status(name, namespace, api):
"""Returns release status for release `name` in `namespace`"""
print('Testing %s in namespace %s ...' % (name, namespace), end='')
print('Testing helmrelease %s in namespace %s ...' % (name, namespace), end='')
try:
release = api.get_namespaced_custom_object(
group="helm.toolkit.fluxcd.io",
......@@ -102,7 +51,7 @@ def get_release_status(name, namespace, api):
name=name,
namespace=namespace
)
print(release['status'])
# print(release['status'])
release_status = release['status']['conditions'][0]['status']
print(release_status)
except ApiException as ex:
......@@ -110,7 +59,7 @@ def get_release_status(name, namespace, api):
release_status = 'not found'
else:
raise
print("**** NOT DEPLOYED, status: %s *****" % release_status)
print(f'**** NOT DEPLOYED, status: "{release_status}" *****')
except KeyError:
release_status = 'Key error'
print("HelmRelease key error: ")
......@@ -149,7 +98,7 @@ def run_around_tests():
def test_kustomizations(app):
"""
Checks if all desired Kustomizations installed by weave flux are in
'deployed' state.
'Ready' state.
"""
if app == 'base':
kustomizations = EXPECTED_KUSTOMIZATIONS_BASE
......@@ -170,28 +119,46 @@ def test_kustomizations(app):
@pytest.mark.app
@pytest.mark.helmreleases
def test_helmreleases(app):
def test_helmreleases(app, namespace):
"""
Checks if all desired HelmReleases installed by weave flux are in
'deployed' state.
'Ready' state.
"""
if app != 'all':
apps = [item for sublist in EXPECTED_RELEASES.values()
for item in sublist]
assert app in apps, "Error: Unknown app: {}".format(app)
custom_objects = client.CustomObjectsApi()
failed = 0
failed_apps = []
print('\n')
for namespace in EXPECTED_RELEASES:
for app_name in EXPECTED_RELEASES[namespace]:
if app in (app_name, 'all'):
app_status = get_release_status(
app_name, namespace, custom_objects)
if app_status != 'True':
custom_objects = client.CustomObjectsApi()
if app != 'all':
release_status = get_release_status(
app, namespace, custom_objects)
if release_status != 'True':
failed += 1
failed_apps.append(app)
else:
# we can't get a list of custom objects from all namespaces,
# so we have to iterate over all namespaces. See
# https://github.com/kubernetes-client/python/issues/1377
core_api = client.CoreV1Api()
namespaces = core_api.list_namespace(watch=False)
for namespace in namespaces.items:
namespace_name = namespace.metadata.name
releases = custom_objects.list_namespaced_custom_object(
group="helm.toolkit.fluxcd.io",
version="v2beta1",
plural="helmreleases",
namespace=namespace_name,
watch=False
)
for release in releases['items']:
release_name = release['metadata']['name']
release_status = get_release_status(
release_name, namespace_name, custom_objects)
if release_status != 'True':
failed += 1
assert failed == 0, "Error: {} apps not 'deployed'!".format(failed)
failed_apps.append(release_name)
assert failed == 0, f"Error: {failed} helmreleases not ready ({failed_apps})!"
@pytest.mark.app
......@@ -202,12 +169,14 @@ def test_apps_running(app):
ready
"""
if app != 'all':
assert app in EXPECTED_APP_LABELS, "Error: Unknown app: {}".format(app)
api = client.CoreV1Api()
failed = 0
print('\n')
# if app == 'all':
for app_name, info in EXPECTED_APP_LABELS.items():
if app in (app_name, 'all'):
print("{}: ".format(app_name))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment