|
1 | 1 | import batchtools.prom_metrics as pm |
| 2 | +import types |
2 | 3 | from datetime import datetime |
3 | 4 | from unittest import mock |
4 | 5 |
|
5 | 6 |
|
6 | 7 | def test_now_rfc3339_parses_with_timezone(): |
7 | 8 | s = pm.now_rfc3339() |
| 9 | + # ISO 8601 parseable and includes timezone info (+00:00) |
8 | 10 | dt = datetime.fromisoformat(s) |
9 | 11 | assert dt.tzinfo is not None |
10 | 12 | assert s.endswith("+00:00") |
11 | 13 |
|
12 | 14 |
|
13 | 15 | def _labels(job="job-x", gpu="none", queue="dummy-localqueue", instance="ns"): |
14 | | - return {"job_name": job, "gpu": gpu, "queue": queue, "instance": instance} |
| 16 | + return {"job": job, "gpu": gpu, "queue": queue, "instance": instance} |
15 | 17 |
|
16 | 18 |
|
17 | 19 | def _registry_text() -> str: |
@@ -76,58 +78,61 @@ def test_generate_metrics_text_returns_valid_payload_and_ctype(): |
76 | 78 |
|
77 | 79 |
|
78 | 80 | def test_push_registry_text_no_url_prints_payload(capsys): |
79 | | - # check "no PUSHGATEWAY_ADDR" branch |
80 | | - with mock.patch.object(pm, "PUSHGATEWAY_ADDR", ""): |
| 81 | + # Temporarily clear the push URL so it prints instead of POSTing |
| 82 | + with mock.patch.object(pm, "PROMETHEUS_PUSH_URL", "", create=True): |
81 | 83 | pm.push_registry_text() |
82 | | - |
83 | 84 | out = capsys.readouterr().out |
84 | | - assert "PROM: PUSHGATEWAY_ADDR not set" in out |
85 | | - assert "# HELP" in out |
86 | | - assert "# TYPE" in out |
| 85 | + assert "PROMETHEUS_PUSH_URL not set" in out |
| 86 | + assert "# HELP" in out # the metrics text is printed |
87 | 87 |
|
88 | 88 |
|
89 | 89 | def test_push_registry_text_posts_success(capsys): |
90 | 90 | with ( |
91 | | - mock.patch.object(pm, "PUSHGATEWAY_ADDR", "pushgateway.example:9091"), |
92 | | - mock.patch.object(pm, "pushadd_to_gateway") as mock_push, |
| 91 | + mock.patch.object( |
| 92 | + pm, "PROMETHEUS_PUSH_URL", "http://example/metrics", create=True |
| 93 | + ), |
| 94 | + mock.patch.object(pm.subprocess, "run") as mock_run, |
93 | 95 | ): |
94 | | - pm.push_registry_text(grouping_key={"instance": "test-ns"}) |
| 96 | + mock_run.return_value = types.SimpleNamespace(returncode=0) |
95 | 97 |
|
96 | | - out = capsys.readouterr().out |
97 | | - assert "PROM: metrics pushed to pushgateway=pushgateway.example:9091" in out |
| 98 | + pm.push_registry_text() |
| 99 | + out = capsys.readouterr().out |
| 100 | + assert "metrics successfully pushed" in out |
98 | 101 |
|
99 | | - # verify pushadd_to_gateway was called with expected args |
100 | | - mock_push.assert_called_once() |
101 | | - args, kwargs = mock_push.call_args |
102 | | - assert args[0] == "pushgateway.example:9091" |
103 | | - # job and registry are passed as keyword args |
104 | | - assert kwargs.get("job") == "batchtools" |
105 | | - assert kwargs.get("registry") is pm.registry |
106 | | - assert kwargs.get("grouping_key") == {"instance": "test-ns"} |
| 102 | + # Verify we invoked curl with POST and sent our payload |
| 103 | + assert mock_run.called |
| 104 | + args, kwargs = mock_run.call_args |
| 105 | + argv = args[0] |
| 106 | + assert "curl" in argv[0] |
| 107 | + assert "-X" in argv and "POST" in argv |
| 108 | + assert "http://example/metrics" in argv |
| 109 | + |
| 110 | + payload = kwargs.get("input", b"").decode("utf-8") |
| 111 | + assert "# HELP" in payload |
| 112 | + assert "# TYPE" in payload |
107 | 113 |
|
108 | 114 |
|
109 | 115 | def test_push_registry_text_posts_failure(capsys): |
110 | 116 | with ( |
111 | | - mock.patch.object(pm, "PUSHGATEWAY_ADDR", "pushgateway.example:9091"), |
112 | | - mock.patch.object(pm, "pushadd_to_gateway", side_effect=RuntimeError("boom")), |
| 117 | + mock.patch.object( |
| 118 | + pm, "PROMETHEUS_PUSH_URL", "http://example/metrics", create=True |
| 119 | + ), |
| 120 | + mock.patch.object(pm.subprocess, "run") as mock_run, |
113 | 121 | ): |
114 | | - pm.push_registry_text(grouping_key={"instance": "test-ns"}) |
115 | | - |
116 | | - out = capsys.readouterr().out |
117 | | - assert "PROM: failed to push metrics to pushgateway pushgateway.example:9091" in out |
118 | | - assert "boom" in out |
| 122 | + mock_run.return_value = types.SimpleNamespace(returncode=7) |
119 | 123 |
|
| 124 | + pm.push_registry_text() |
| 125 | + out = capsys.readouterr().out |
| 126 | + assert "curl returned nonzero exit 7" in out |
120 | 127 |
|
121 | | -def test_push_registry_text_handles_generic_exception(capsys): |
122 | | - class WeirdError(Exception): |
123 | | - pass |
124 | 128 |
|
| 129 | +def test_push_registry_text_handles_exception(capsys): |
125 | 130 | with ( |
126 | | - mock.patch.object(pm, "PUSHGATEWAY_ADDR", "pushgateway.example:9091"), |
127 | | - mock.patch.object(pm, "pushadd_to_gateway", side_effect=WeirdError("weird")), |
| 131 | + mock.patch.object( |
| 132 | + pm, "PROMETHEUS_PUSH_URL", "http://example/metrics", create=True |
| 133 | + ), |
| 134 | + mock.patch.object(pm.subprocess, "run", side_effect=RuntimeError("boom")), |
128 | 135 | ): |
129 | | - pm.push_registry_text(grouping_key={"instance": "test-ns"}) |
130 | | - |
| 136 | + pm.push_registry_text() |
131 | 137 | out = capsys.readouterr().out |
132 | | - assert "PROM: failed to push metrics to pushgateway pushgateway.example:9091" in out |
133 | | - assert "weird" in out |
| 138 | + assert "failed to push metrics via curl" in out |
0 commit comments