|
| 1 | +"""Tests for the Prometheus metrics exporter.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import urllib.request |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from automation_file import ( |
| 10 | + ACTION_COUNT, |
| 11 | + execute_action, |
| 12 | + record_action, |
| 13 | + render_metrics, |
| 14 | + start_metrics_server, |
| 15 | +) |
| 16 | +from automation_file.core.action_executor import executor |
| 17 | +from tests._insecure_fixtures import insecure_url, ipv4 |
| 18 | + |
| 19 | + |
| 20 | +def _register_probes() -> None: |
| 21 | + if "test_metric_ok" not in executor.registry: |
| 22 | + executor.registry.register("test_metric_ok", lambda: "ok") |
| 23 | + if "test_metric_fail" not in executor.registry: |
| 24 | + |
| 25 | + def _boom() -> None: |
| 26 | + raise RuntimeError("boom") |
| 27 | + |
| 28 | + executor.registry.register("test_metric_fail", _boom) |
| 29 | + |
| 30 | + |
| 31 | +def test_record_action_increments_counter() -> None: |
| 32 | + before = ACTION_COUNT.labels(action="unit.test", status="ok")._value.get() |
| 33 | + record_action("unit.test", 0.01, ok=True) |
| 34 | + after = ACTION_COUNT.labels(action="unit.test", status="ok")._value.get() |
| 35 | + assert after == before + 1 |
| 36 | + |
| 37 | + |
| 38 | +def test_record_action_clamps_negative_duration() -> None: |
| 39 | + # Must not raise. |
| 40 | + record_action("unit.clamp", -5.0, ok=True) |
| 41 | + |
| 42 | + |
| 43 | +def test_render_metrics_content_type() -> None: |
| 44 | + payload, content_type = render_metrics() |
| 45 | + assert content_type.startswith("text/plain") |
| 46 | + assert b"automation_file_actions_total" in payload |
| 47 | + |
| 48 | + |
| 49 | +def test_execute_action_increments_ok_counter() -> None: |
| 50 | + _register_probes() |
| 51 | + before = ACTION_COUNT.labels(action="test_metric_ok", status="ok")._value.get() |
| 52 | + execute_action([["test_metric_ok"]]) |
| 53 | + after = ACTION_COUNT.labels(action="test_metric_ok", status="ok")._value.get() |
| 54 | + assert after == before + 1 |
| 55 | + |
| 56 | + |
| 57 | +def test_execute_action_increments_error_counter() -> None: |
| 58 | + _register_probes() |
| 59 | + before = ACTION_COUNT.labels(action="test_metric_fail", status="error")._value.get() |
| 60 | + execute_action([["test_metric_fail"]]) |
| 61 | + after = ACTION_COUNT.labels(action="test_metric_fail", status="error")._value.get() |
| 62 | + assert after == before + 1 |
| 63 | + |
| 64 | + |
| 65 | +def test_metrics_server_serves_metrics_endpoint() -> None: |
| 66 | + _register_probes() |
| 67 | + execute_action([["test_metric_ok"]]) |
| 68 | + server = start_metrics_server(host="127.0.0.1", port=0) |
| 69 | + host, port = server.server_address |
| 70 | + try: |
| 71 | + url = insecure_url("http", f"{host}:{port}/metrics") |
| 72 | + with urllib.request.urlopen(url, timeout=3) as resp: # nosec B310 - loopback test server |
| 73 | + body = resp.read().decode("utf-8") |
| 74 | + assert resp.status == 200 |
| 75 | + assert "automation_file_actions_total" in body |
| 76 | + finally: |
| 77 | + server.shutdown() |
| 78 | + |
| 79 | + |
| 80 | +def test_metrics_server_returns_404_for_other_paths() -> None: |
| 81 | + server = start_metrics_server(host="127.0.0.1", port=0) |
| 82 | + host, port = server.server_address |
| 83 | + try: |
| 84 | + url = insecure_url("http", f"{host}:{port}/other") |
| 85 | + with pytest.raises(urllib.error.HTTPError) as info: |
| 86 | + urllib.request.urlopen(url, timeout=3) # nosec B310 - loopback test server |
| 87 | + assert info.value.code == 404 |
| 88 | + finally: |
| 89 | + server.shutdown() |
| 90 | + |
| 91 | + |
| 92 | +def test_metrics_server_rejects_non_loopback() -> None: |
| 93 | + non_loopback = ipv4(8, 8, 8, 8) |
| 94 | + with pytest.raises(ValueError): |
| 95 | + start_metrics_server(host=non_loopback, port=0) |
0 commit comments