Skip to content

Commit

Permalink
Merge pull request Kuadrant#360 from averevki/fix-metrics-assert
Browse files Browse the repository at this point in the history
Fix metrics tests
  • Loading branch information
averevki authored Apr 9, 2024
2 parents e3ba41c + 5c5c927 commit 7e72827
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
9 changes: 5 additions & 4 deletions testsuite/openshift/metrics/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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:
Expand All @@ -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():
Expand All @@ -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"""
Expand Down
24 changes: 9 additions & 15 deletions testsuite/tests/kuadrant/authorino/metrics/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
17 changes: 10 additions & 7 deletions testsuite/tests/kuadrant/authorino/metrics/test_deep_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()})
Expand All @@ -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):
Expand All @@ -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
10 changes: 8 additions & 2 deletions testsuite/tests/kuadrant/authorino/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 7e72827

Please sign in to comment.