From 5c5c9276486eda76914081b257c868452efa8d64 Mon Sep 17 00:00:00 2001 From: averevki Date: Wed, 6 Mar 2024 11:03:55 +0100 Subject: [PATCH] Fix metrics assert --- testsuite/openshift/metrics/prometheus.py | 9 +++---- .../kuadrant/authorino/metrics/conftest.py | 24 +++++++------------ .../authorino/metrics/test_deep_metrics.py | 17 +++++++------ .../authorino/metrics/test_metrics.py | 10 ++++++-- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/testsuite/openshift/metrics/prometheus.py b/testsuite/openshift/metrics/prometheus.py index 80376c6e..27c5d639 100644 --- a/testsuite/openshift/metrics/prometheus.py +++ b/testsuite/openshift/metrics/prometheus.py @@ -3,10 +3,11 @@ from typing import Callable from datetime import datetime -import httpx import backoff from apyproxy import ApyProxy +from testsuite.httpx import KuadrantClient + def _params(key: str = "", labels: dict[str, str] = None) -> dict[str, str]: """Generate metrics query parameters based on key and labels""" @@ -48,7 +49,7 @@ def __init__(self, url: str, token: str, namespace: str = None): self.headers = {"Authorization": f"Bearer {self.token}"} self.namespace = namespace - self._client = httpx.Client(headers=self.headers, verify=False) + self._client = KuadrantClient(headers=self.headers, verify=False) self.client = ApyProxy(url, self._client).api.v1 def get_targets(self) -> dict: @@ -73,7 +74,7 @@ def wait_for_scrape(self, target_service: str, metrics_path: str = "/metrics"): """Wait before next metrics scrape on service is finished""" call_time = datetime.utcnow() - @backoff.on_predicate(backoff.constant, interval=10, jitter=None, max_tries=10) + @backoff.on_predicate(backoff.constant, interval=10, jitter=None, max_tries=20) def _wait_for_scrape(): """Wait for new scrape after the function call time""" for target in self.get_targets(): @@ -85,7 +86,7 @@ def _wait_for_scrape(): return call_time < datetime.fromisoformat(target["lastScrape"][:26]) return False - _wait_for_scrape() + assert _wait_for_scrape() def close(self): """Close httpx client connection""" diff --git a/testsuite/tests/kuadrant/authorino/metrics/conftest.py b/testsuite/tests/kuadrant/authorino/metrics/conftest.py index 2c091677..9bcc64b8 100644 --- a/testsuite/tests/kuadrant/authorino/metrics/conftest.py +++ b/testsuite/tests/kuadrant/authorino/metrics/conftest.py @@ -38,27 +38,21 @@ def prometheus(request, openshift): @pytest.fixture(scope="module") -def label_metrics_service(authorino, module_label): +def authorino(authorino, module_label): """Label Authorino controller-metrics service for the proper discovery""" authorino.metrics_service.label({"app": module_label}) + return authorino -# pylint: disable=unused-argument -@pytest.fixture(scope="module", autouse=True) -def create_service_monitor(openshift, blame, module_label, label_metrics_service): +@pytest.fixture(scope="module") +def service_monitor(openshift, blame, module_label): """Create ServiceMonitor object to follow Authorino /metrics and /server-metrics endpoints""" endpoints = [MetricsEndpoint("/metrics", "http"), MetricsEndpoint("/server-metrics", "http")] - service_monitor = ServiceMonitor.create_instance( - openshift, blame("sm"), endpoints, match_labels={"app": module_label} - ) - - service_monitor.commit() - yield service_monitor - service_monitor.delete() + return ServiceMonitor.create_instance(openshift, blame("sm"), endpoints, match_labels={"app": module_label}) @pytest.fixture(scope="module", autouse=True) -def send_request(client, auth): - """Send a simple get request so that a few metrics can appear""" - response = client.get("/get", auth=auth) - assert response.status_code == 200 +def commit(commit, request, service_monitor): # pylint: disable=unused-argument + """Commit service monitor object""" + request.addfinalizer(service_monitor.delete) + service_monitor.commit() diff --git a/testsuite/tests/kuadrant/authorino/metrics/test_deep_metrics.py b/testsuite/tests/kuadrant/authorino/metrics/test_deep_metrics.py index 1ec9abdf..8b9c9f68 100644 --- a/testsuite/tests/kuadrant/authorino/metrics/test_deep_metrics.py +++ b/testsuite/tests/kuadrant/authorino/metrics/test_deep_metrics.py @@ -34,8 +34,11 @@ def authorization(authorization, mockserver_expectation): @pytest.fixture(scope="module") -def deep_metrics(authorino, prometheus): - """Return all evaluator(deep) metrics""" +def deep_metrics(authorino, prometheus, client, auth): + """Send a simple get request so that a few metrics can appear and return all scraped evaluator(deep) metrics""" + response = client.get("/get", auth=auth) + assert response.status_code == 200 + prometheus.wait_for_scrape(authorino.metrics_service.name(), "/server-metrics") return prometheus.get_metrics("auth_server_evaluator_total", labels={"service": authorino.metrics_service.name()}) @@ -44,10 +47,10 @@ def deep_metrics(authorino, prometheus): @pytest.mark.parametrize( "metric_name, metric_type", [ - ("opa", "AUTHORIZATION_OPA"), - ("anonymous", "IDENTITY_NOOP"), - ("http", "METADATA_GENERIC_HTTP"), - ("json", "RESPONSE_JSON"), + pytest.param("opa", "AUTHORIZATION_OPA", id="authorization"), + pytest.param("anonymous", "IDENTITY_NOOP", id="identity"), + pytest.param("http", "METADATA_GENERIC_HTTP", id="metadata"), + pytest.param("json", "RESPONSE_JSON", id="response"), ], ) def test_deep_metrics(metric_name, metric_type, deep_metrics): @@ -56,5 +59,5 @@ def test_deep_metrics(metric_name, metric_type, deep_metrics): lambda x: x["metric"]["evaluator_name"] == metric_name and x["metric"]["evaluator_type"] == metric_type ) - assert metrics + assert len(metrics.metrics) == 1 assert metrics.values[0] == 1 diff --git a/testsuite/tests/kuadrant/authorino/metrics/test_metrics.py b/testsuite/tests/kuadrant/authorino/metrics/test_metrics.py index 011b5e5f..cceb5630 100644 --- a/testsuite/tests/kuadrant/authorino/metrics/test_metrics.py +++ b/testsuite/tests/kuadrant/authorino/metrics/test_metrics.py @@ -39,8 +39,14 @@ @pytest.fixture(scope="module") -def metrics_keys(authorino, prometheus): - """Return all metrics defined for the Authorino metrics service""" +def metrics_keys(authorino, prometheus, client, auth): + """ + Send a simple get request so that a few metrics can appear and + return all metrics defined for the Authorino metrics service + """ + response = client.get("/get", auth=auth) + assert response.status_code == 200 + prometheus.wait_for_scrape(authorino.metrics_service.name(), "/metrics") prometheus.wait_for_scrape(authorino.metrics_service.name(), "/server-metrics")