From f119593325c03f3050e6a8710e2a72553fb2c41f Mon Sep 17 00:00:00 2001
From: Varac <varac@varac.net>
Date: Thu, 29 Jul 2021 15:57:04 +0200
Subject: [PATCH] Update pytest to check all helmreleases

---
 test/pytest/test_app_deployments.py | 127 +++++++++++-----------------
 1 file changed, 48 insertions(+), 79 deletions(-)

diff --git a/test/pytest/test_app_deployments.py b/test/pytest/test_app_deployments.py
index 4e3c25836..40e25f52c 100644
--- a/test/pytest/test_app_deployments.py
+++ b/test/pytest/test_app_deployments.py
@@ -1,70 +1,19 @@
 """
 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))
-- 
GitLab