From b4ce71789228794e8d09c0636eea12b99c6ed7b6 Mon Sep 17 00:00:00 2001
From: phala <phala@redhat.com>
Date: Wed, 18 Oct 2023 11:09:17 +0200
Subject: [PATCH] Improve metrics tests skip

- Now it should skip if the userWorkloadMonitoring is off
- Add ConfigMap class for ease of use
---
 testsuite/openshift/objects/config_map.py     | 35 +++++++++++++++++++
 .../kuadrant/authorino/metrics/conftest.py    | 15 +++++++-
 2 files changed, 49 insertions(+), 1 deletion(-)
 create mode 100644 testsuite/openshift/objects/config_map.py

diff --git a/testsuite/openshift/objects/config_map.py b/testsuite/openshift/objects/config_map.py
new file mode 100644
index 00000000..01ef5528
--- /dev/null
+++ b/testsuite/openshift/objects/config_map.py
@@ -0,0 +1,35 @@
+"""Config map"""
+from testsuite.openshift.objects import OpenShiftObject
+
+
+class ConfigMap(OpenShiftObject):
+    """Kubernetes ConfigMap object"""
+
+    @classmethod
+    def create_instance(
+        cls,
+        openshift,
+        name,
+        data: dict[str, str],
+        labels: dict[str, str] = None,
+    ):
+        """Creates new Config Map"""
+        model: dict = {
+            "kind": "ConfigMap",
+            "apiVersion": "v1",
+            "metadata": {
+                "name": name,
+                "labels": labels,
+            },
+            "data": data,
+        }
+        return cls(model, context=openshift.context)
+
+    def __getitem__(self, name):
+        return self.model.data[name]
+
+    def __contains__(self, name):
+        return name in self.model.data
+
+    def __setitem__(self, name, value):
+        self.model.data[name] = value
diff --git a/testsuite/tests/kuadrant/authorino/metrics/conftest.py b/testsuite/tests/kuadrant/authorino/metrics/conftest.py
index ab2faddb..1485826f 100644
--- a/testsuite/tests/kuadrant/authorino/metrics/conftest.py
+++ b/testsuite/tests/kuadrant/authorino/metrics/conftest.py
@@ -1,6 +1,10 @@
 """Conftest for the Authorino metrics tests"""
+import yaml
+
 import pytest
+from openshift import selector
 
+from testsuite.openshift.objects.config_map import ConfigMap
 from testsuite.openshift.objects.metrics import ServiceMonitor, MetricsEndpoint, Prometheus
 
 
@@ -16,9 +20,18 @@ def prometheus(request, openshift):
     Return an instance of OpenShift metrics client
     Skip tests if query route is not properly configured
     """
+    openshift_monitoring = openshift.change_project("openshift-monitoring")
+    # Check if metrics are enabled
+    try:
+        with openshift_monitoring.context:
+            cm = selector("cm/cluster-monitoring-config").object(cls=ConfigMap)
+            assert yaml.safe_load(cm["config.yaml"])["enableUserWorkload"]
+    except Exception:  # pylint: disable=broad-exception-caught
+        pytest.skip("User workload monitoring is disabled")
+
     # find thanos-querier route in the openshift-monitoring project
     # this route allows to query metrics
-    openshift_monitoring = openshift.change_project("openshift-monitoring")
+
     routes = openshift_monitoring.get_routes_for_service("thanos-querier")
     if len(routes) > 0:
         url = ("https://" if "tls" in routes[0].model.spec else "http://") + routes[0].model.spec.host