-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #493 from jsmolar/limitador_metrics
Add test for limitador metrics
- Loading branch information
Showing
12 changed files
with
208 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""Kubernetes monitoring common objects""" | ||
|
||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class MetricsEndpoint: | ||
"""Dataclass for endpoint definition in ServiceMonitor Kubernetes object. | ||
It contains endpoint path and port to the exported metrics.""" | ||
|
||
path: str = "/metrics" | ||
port: str = "http" | ||
interval: str = "30s" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""Module implements Pod Monitor CR""" | ||
|
||
from testsuite.kubernetes import KubernetesObject | ||
from testsuite.kubernetes.client import KubernetesClient | ||
from testsuite.kubernetes.monitoring import MetricsEndpoint | ||
from testsuite.utils import asdict | ||
|
||
|
||
class PodMonitor(KubernetesObject): | ||
"""Represents Pod Monitor object for OpenShift""" | ||
|
||
@classmethod | ||
def create_instance( | ||
cls, | ||
cluster: KubernetesClient, | ||
name: str, | ||
endpoints: list[MetricsEndpoint], | ||
match_labels: dict[str, str], | ||
labels: dict[str, str] = None, | ||
): | ||
"""Creates new instance of ServiceMonitor""" | ||
model = { | ||
"apiVersion": "monitoring.coreos.com/v1", | ||
"kind": "PodMonitor", | ||
"metadata": { | ||
"name": name, | ||
"labels": labels, | ||
}, | ||
"spec": { | ||
"podMetricsEndpoints": [asdict(e) for e in endpoints], | ||
"selector": { | ||
"matchLabels": match_labels, | ||
}, | ||
}, | ||
} | ||
|
||
return cls(model, context=cluster.context) |
15 changes: 2 additions & 13 deletions
15
testsuite/kubernetes/service_monitor.py → .../kubernetes/monitoring/service_monitor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 2 additions & 33 deletions
35
testsuite/tests/singlecluster/authorino/metrics/conftest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
24 changes: 24 additions & 0 deletions
24
testsuite/tests/singlecluster/limitador/metrics/conftest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"""Conftest for limitador metrics tests""" | ||
|
||
import pytest | ||
|
||
from testsuite.kubernetes.monitoring import MetricsEndpoint | ||
from testsuite.kubernetes.monitoring.pod_monitor import PodMonitor | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def pod_monitor(cluster, testconfig, request, blame, limitador): | ||
"""Creates Pod Monitor object to watch over '/metrics' endpoint of limitador pod""" | ||
project = cluster.change_project(testconfig["service_protection"]["system_project"]) | ||
|
||
endpoints = [MetricsEndpoint("/metrics", "http")] | ||
monitor = PodMonitor.create_instance(project, blame("pd"), endpoints, match_labels={"app": limitador.name()}) | ||
request.addfinalizer(monitor.delete) | ||
monitor.commit() | ||
return monitor | ||
|
||
|
||
@pytest.fixture(scope="module", autouse=True) | ||
def wait_for_active_targets(prometheus, pod_monitor): | ||
"""Waits for all endpoints in Pod Monitor to become active targets""" | ||
assert prometheus.is_reconciled(pod_monitor) |
51 changes: 51 additions & 0 deletions
51
testsuite/tests/singlecluster/limitador/metrics/test_metrics.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""Tests for Limitador metrics""" | ||
|
||
import pytest | ||
|
||
from testsuite.kuadrant.policy.rate_limit import Limit | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def rate_limit(rate_limit): | ||
"""Add limit to the policy""" | ||
rate_limit.add_limit("multiple", [Limit(3, 10)]) | ||
return rate_limit | ||
|
||
|
||
@pytest.fixture(scope="module", autouse=True) | ||
def scrape_metrics_created_by_requests(prometheus, pod_monitor, client): | ||
""" | ||
Creates 5 requests, from which 3 are authorized and 2 are rate limited. | ||
Waits until Prometheus scrapes '/metrics' endpoint. | ||
""" | ||
client.get_many("/get", 5) | ||
prometheus.wait_for_scrape(pod_monitor, "/metrics") | ||
|
||
|
||
@pytest.mark.parametrize("metric, expected_value", [("authorized_calls", 3), ("limited_calls", 2)]) | ||
def test_calls_metric(prometheus, limitador, rate_limit, metric, expected_value, pod_monitor): | ||
"""Tests that `authorized_calls` and `limited_calls` are emitted and correctly incremented""" | ||
metrics = prometheus.get_metrics( | ||
labels={ | ||
"pod": limitador.pod.name(), | ||
"limitador_namespace": f"{rate_limit.namespace()}/{rate_limit.name()}", | ||
"job": f"{pod_monitor.namespace()}/{pod_monitor.name()}", | ||
} | ||
) | ||
|
||
authorized = metrics.filter(lambda x: x["metric"]["__name__"] == metric) | ||
assert len(authorized.metrics) == 1 | ||
assert authorized.values[0] == expected_value | ||
|
||
|
||
def test_limitador_status_metric(prometheus, limitador, pod_monitor): | ||
"""Tests that `limitador_up` metric is emitted""" | ||
# We have to use `PodMonitor` here. If `ServiceMonitor` is used, `job` label contains limitador service name, | ||
# therefore it is not possible to test, if the metric was created by this test (by this monitor) | ||
metrics = prometheus.get_metrics( | ||
labels={"pod": limitador.pod.name(), "job": f"{pod_monitor.namespace()}/{pod_monitor.name()}"} | ||
) | ||
|
||
limitador_up = metrics.filter(lambda x: x["metric"]["__name__"] == "limitador_up") | ||
assert len(limitador_up.metrics) == 1 | ||
assert limitador_up.values[0] == 1 |