mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-28 16:58:31 +01:00
Document existing notification delivery contracts in ALERTING.md and WEBHOOKS.md (outbound HTTP_PROXY/HTTPS_PROXY bypass for DNS-pinned SSRF protection on alert webhooks and Apprise HTTP, 30s/10s webhook timeouts, 10/min per-URL webhook rate limiting, Apprise 5-120s timeout bounds and CLI flag stripping, SMTP 60/min default rate limit, AUTH LOGIN fallback, RFC 5322 incident email threading, and the 2,048-character external watchdog URL cap), and align ALERTING.md, ADAPTIVE_POLLING.md, and ADAPTIVE_POLLING_ROLLOUT.md with #5399 retired poll slot release without successor planning. Add source-bound documentation verification tests. Contract-Neutral: Documentation and documentation-test alignment with existing shipped monitoring and notification runtime contracts; no runtime behavior, API schema, or UI state changes. Change-source: pulse-maintainer
483 lines
26 KiB
Python
483 lines
26 KiB
Python
#!/usr/bin/env python3
|
|
"""Exercise copied scheduler diagnostics against secret-free local HTTP fixtures.
|
|
|
|
Run the HTTP tests through pulse-worker-source-proof for guest-local loopback.
|
|
The documentation, not a second hand-written client, supplies every command.
|
|
"""
|
|
|
|
from contextlib import contextmanager
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import shutil
|
|
import stat
|
|
import subprocess
|
|
import tempfile
|
|
import threading
|
|
import unittest
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
DOC = ROOT / "docs/api/SCHEDULER_HEALTH.md"
|
|
ROLLOUT = ROOT / "docs/operations/ADAPTIVE_POLLING_ROLLOUT.md"
|
|
ENDPOINT = "/api/monitoring/scheduler/health"
|
|
TOKEN = "synthetic-scheduler-doc-token"
|
|
|
|
|
|
def blocks():
|
|
return re.findall(r"```bash\n(.*?)```", DOC.read_text(), re.DOTALL)
|
|
|
|
|
|
def request():
|
|
# This also selects the old first query for a parent-source differential.
|
|
return next(block for block in blocks() if ENDPOINT in block)
|
|
|
|
|
|
def snapshot(enabled=True):
|
|
return {
|
|
"updatedAt": "2026-10-03T12:00:00Z", "enabled": enabled,
|
|
"queue": {"depth": 3, "dueWithinSeconds": 1, "perType": {"pve": 3}},
|
|
"deadLetter": {"count": 1, "tasks": []}, "breakers": [], "staleness": [],
|
|
"instances": [
|
|
{"key": "pve::healthy", "pollStatus": {"lastSuccess": "2026-10-03T11:59:58Z", "consecutiveFailures": 0},
|
|
"breaker": {"state": "closed"}, "deadLetter": {"present": False}},
|
|
{"key": "pve::retrying", "pollStatus": {"lastSuccess": "2026-10-03T11:59:00Z", "consecutiveFailures": 2},
|
|
"breaker": {"state": "half_open"}, "deadLetter": {"present": False}},
|
|
{"key": "pve::failed", "pollStatus": {"lastSuccess": "2026-10-03T11:55:00Z", "consecutiveFailures": 5},
|
|
"breaker": {"state": "open"}, "deadLetter": {"present": True, "reason": "permanent_failure"}},
|
|
],
|
|
}
|
|
|
|
|
|
@contextmanager
|
|
def server(payload, status=200, *, incomplete_body=False):
|
|
calls = []
|
|
|
|
class Handler(BaseHTTPRequestHandler):
|
|
def do_GET(self):
|
|
calls.append({"path": self.path, "method": self.command, "headers": dict(self.headers)})
|
|
body = payload if isinstance(payload, bytes) else json.dumps(payload).encode()
|
|
self.send_response(status)
|
|
self.send_header("Content-Type", "application/json")
|
|
self.send_header("Content-Length", str(len(body) + (10 if incomplete_body else 0)))
|
|
self.end_headers()
|
|
self.wfile.write(body)
|
|
|
|
def log_message(self, *_args):
|
|
pass
|
|
|
|
httpd = HTTPServer(("127.0.0.1", 0), Handler)
|
|
# Poll only the fixture server more often so teardown need not wait 500 ms.
|
|
# This does not shorten any copied request, reader or cleanup deadline.
|
|
thread = threading.Thread(target=httpd.serve_forever,
|
|
kwargs={"poll_interval": 0.05}, daemon=True)
|
|
thread.start()
|
|
try:
|
|
yield httpd.server_address[1], calls
|
|
finally:
|
|
httpd.shutdown()
|
|
httpd.server_close()
|
|
thread.join(timeout=5)
|
|
|
|
|
|
class SchedulerPollingDocsTest(unittest.TestCase):
|
|
def exercise(self, payload=None, status=200, *, header="X-API-Token", missing_header=False,
|
|
refused_connection=False, incomplete_body=False):
|
|
payload = snapshot() if payload is None else payload
|
|
with tempfile.TemporaryDirectory(prefix="pulse polling ") as temporary:
|
|
directory = Path(temporary)
|
|
home = directory / "home"
|
|
credential = home / ".config/pulse/api-header"
|
|
credential.parent.mkdir(parents=True, mode=0o700)
|
|
if not missing_header:
|
|
credential.write_text(f"{header}: {'Bearer ' if header == 'Authorization' else ''}{TOKEN}\n")
|
|
credential.chmod(0o600)
|
|
# --disable must defeat unsafe user defaults, not just omit -v itself.
|
|
(home / ".curlrc").write_text("verbose\n")
|
|
tools = directory / "tools"
|
|
tools.mkdir()
|
|
curl = tools / "curl"
|
|
curl.write_text("#!/usr/bin/env python3\nimport json, os, sys\n"
|
|
"from pathlib import Path\n"
|
|
"Path(os.environ['CURL_ARGV']).write_text(json.dumps(sys.argv[1:]))\n"
|
|
"os.execv(os.environ['REAL_CURL'], ['curl', *sys.argv[1:]])\n")
|
|
curl.chmod(0o700)
|
|
argv_file = directory / "argv.json"
|
|
env = dict(os.environ, HOME=str(home), TMPDIR=str(directory),
|
|
PATH=f"{tools}:{os.environ['PATH']}", CURL_ARGV=str(argv_file),
|
|
REAL_CURL=shutil.which("curl"))
|
|
with server(payload, status, incomplete_body=incomplete_body) as (port, calls):
|
|
# A closed fixture port supplies an actual connection refusal.
|
|
if refused_connection:
|
|
with server(payload) as (closed_port, _):
|
|
pass
|
|
port = closed_port
|
|
code = request().replace("http://127.0.0.1:7655", f"http://127.0.0.1:{port}")
|
|
code = code.replace("http://HOST:7655", f"http://127.0.0.1:{port}")
|
|
result = subprocess.run(["bash", "-c", code], env=env, text=True,
|
|
capture_output=True, timeout=25)
|
|
saved = list(directory.glob("pulse-scheduler.*/health.json"))
|
|
files = [(path.read_bytes(), stat.S_IMODE(path.stat().st_mode),
|
|
stat.S_IMODE(path.parent.stat().st_mode)) for path in saved]
|
|
return result, calls, json.loads(argv_file.read_text()), files
|
|
|
|
def test_one_authenticated_request_with_both_supported_header_files(self):
|
|
for header in ("X-API-Token", "Authorization"):
|
|
with self.subTest(header=header):
|
|
result, calls, args, files = self.exercise(header=header)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertEqual(len(calls), 1)
|
|
self.assertEqual((calls[0]["path"], calls[0]["method"]), (ENDPOINT, "GET"))
|
|
self.assertEqual(calls[0]["headers"][header],
|
|
("Bearer " if header == "Authorization" else "") + TOKEN)
|
|
self.assertNotIn("Authorization" if header == "X-API-Token" else "X-API-Token",
|
|
calls[0]["headers"])
|
|
self.assertEqual(args[0], "--disable")
|
|
self.assertNotIn(TOKEN, " ".join(args) + result.stdout + result.stderr)
|
|
self.assertIn("Saved scheduler snapshot:", result.stdout)
|
|
self.assertEqual(json.loads(files[0][0]), snapshot())
|
|
self.assertEqual(files[0][1:], (0o600, 0o700))
|
|
|
|
def test_request_has_bounded_timeouts_and_never_follows_redirects(self):
|
|
_, _, args, _ = self.exercise()
|
|
self.assertEqual(args[args.index("--connect-timeout") + 1], "5")
|
|
self.assertEqual(args[args.index("--max-time") + 1], "15")
|
|
self.assertNotIn("--location", args)
|
|
self.assertNotIn("--insecure", args)
|
|
|
|
def test_http_errors_and_redirects_never_become_successful_snapshots(self):
|
|
for status in (301, 401, 403, 503):
|
|
with self.subTest(status=status):
|
|
# A plausible body cannot hide a failed read or leak its error text.
|
|
payload = dict(snapshot(), error="private fixture error")
|
|
result, calls, _, files = self.exercise(payload, status)
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertEqual(len(calls), 1)
|
|
self.assertEqual(result.stdout, "")
|
|
expected = ("Check access to this Pulse instance." if status in (401, 403)
|
|
else "No poll result.")
|
|
self.assertEqual(result.stderr,
|
|
f"Scheduler snapshot unavailable (HTTP {status}). {expected}\n")
|
|
self.assertEqual(len(files), 1)
|
|
self.assertEqual(json.loads(files[0][0]), payload)
|
|
self.assertEqual(files[0][1:], (0o600, 0o700))
|
|
|
|
def test_transport_failure_is_not_an_empty_queue(self):
|
|
result, calls, _, _ = self.exercise(refused_connection=True)
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertEqual(calls, [])
|
|
self.assertEqual(result.stdout, "")
|
|
self.assertEqual(result.stderr, "Scheduler snapshot unavailable (curl exit 7). No poll result.\n")
|
|
|
|
def test_short_transfer_with_http_200_is_not_a_saved_snapshot(self):
|
|
result, calls, _, files = self.exercise(incomplete_body=True)
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertEqual(len(calls), 1)
|
|
self.assertEqual(result.stdout, "")
|
|
self.assertEqual(result.stderr, "Scheduler snapshot unavailable (curl exit 18). No poll result.\n")
|
|
self.assertEqual(json.loads(files[0][0]), snapshot())
|
|
self.assertEqual(files[0][1:], (0o600, 0o700))
|
|
|
|
def test_missing_header_file_stops_without_an_anonymous_request(self):
|
|
result, calls, _, _ = self.exercise(missing_header=True)
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertEqual(calls, [])
|
|
self.assertNotIn("Saved scheduler snapshot:", result.stdout)
|
|
|
|
def test_invalid_json_and_unexpected_shapes_are_unavailable(self):
|
|
for payload in (b"<html>login</html>", b"", b"{}\n" + json.dumps(snapshot()).encode(),
|
|
{}, {"enabled": True},
|
|
dict(snapshot(), enabled="true"), dict(snapshot(), instances=None)):
|
|
with self.subTest(payload=payload):
|
|
result, _, _, _ = self.exercise(payload)
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertEqual(result.stdout, "")
|
|
self.assertEqual(result.stderr,
|
|
"Scheduler snapshot unavailable. The response was not a scheduler snapshot.\n")
|
|
|
|
def test_fixed_timing_empty_queue_is_preserved_not_reported_as_healthy(self):
|
|
payload = dict(snapshot(False), instances=[], queue={"depth": 0}, deadLetter={"count": 0})
|
|
result, _, _, files = self.exercise(payload)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertFalse(json.loads(files[0][0])["enabled"])
|
|
self.assertNotIn("healthy", result.stdout.lower())
|
|
|
|
def test_local_queries_reuse_one_snapshot_and_preserve_failing_instances(self):
|
|
queries = [block for block in blocks()
|
|
if "jq " in block and ENDPOINT not in block and "--slurpfile" not in block]
|
|
self.assertEqual(len(queries), 4)
|
|
body = snapshot()
|
|
body["queue"]["oldestOverdueSeconds"] = 0
|
|
body["instances"][1]["pollStatus"]["lastError"] = {
|
|
"at": "2026-10-03T11:59:30Z", "category": "transient", "message": "private fixture error",
|
|
}
|
|
body["instances"].extend([
|
|
{"key": "pmg::partial", "connection": "https://private-fixture.invalid",
|
|
"pollStatus": {"lastSuccess": "2026-10-03T11:59:58Z", "consecutiveFailures": 0,
|
|
"lastError": {"at": "2026-10-03T11:59:58Z", "category": "partial",
|
|
"message": "private fixture error"}},
|
|
"breaker": {"state": "closed"}, "deadLetter": {"present": False}},
|
|
{"key": "pbs::no-result-yet", "pollStatus": {"consecutiveFailures": 0},
|
|
"breaker": {"state": "closed"}, "deadLetter": {"present": False}},
|
|
])
|
|
with tempfile.TemporaryDirectory(prefix="saved scheduler ") as temporary:
|
|
path = Path(temporary) / "health.json"
|
|
path.write_text(json.dumps(body))
|
|
results = []
|
|
for query in queries:
|
|
self.assertNotIn("curl", query)
|
|
code = query.replace("/absolute/path/to/health.json", str(path))
|
|
result = subprocess.run(["bash", "-eu", "-c", code], capture_output=True,
|
|
text=True, timeout=5)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertNotIn("private fixture error", result.stdout)
|
|
self.assertNotIn("private-fixture.invalid", result.stdout)
|
|
decoder = json.JSONDecoder()
|
|
text = result.stdout.strip()
|
|
rows = []
|
|
while text:
|
|
row, end = decoder.raw_decode(text)
|
|
rows.append(row)
|
|
text = text[end:].strip()
|
|
results.append(rows)
|
|
self.assertEqual(results[0], [{"updatedAt": snapshot()["updatedAt"], "enabled": True,
|
|
"queueDepth": 3, "oldestQueuedOverdueSeconds": 0,
|
|
"deadLetterCount": 1, "instanceCount": 5}])
|
|
self.assertEqual(results[1], [
|
|
{"key": "pve::retrying", "lastSuccess": "2026-10-03T11:59:00Z", "failures": 2,
|
|
"lastErrorAt": "2026-10-03T11:59:30Z", "lastErrorCategory": "transient"},
|
|
{"key": "pve::failed", "lastSuccess": "2026-10-03T11:55:00Z", "failures": 5,
|
|
"lastErrorAt": None, "lastErrorCategory": None},
|
|
{"key": "pmg::partial", "lastSuccess": "2026-10-03T11:59:58Z", "failures": 0,
|
|
"lastErrorAt": "2026-10-03T11:59:58Z", "lastErrorCategory": "partial"},
|
|
])
|
|
self.assertEqual(results[2], [{"key": "pve::failed", "reason": "permanent_failure"}])
|
|
self.assertEqual(results[3], [{"key": "pve::retrying", "state": "half_open"},
|
|
{"key": "pve::failed", "state": "open"}])
|
|
|
|
def compare(self, before, after):
|
|
query = next(block for block in blocks() if "--slurpfile earlier" in block)
|
|
self.assertNotIn("curl", query)
|
|
with tempfile.TemporaryDirectory(prefix="scheduler comparison ") as temporary:
|
|
code = query
|
|
for name, body in (("earlier", before), ("later", after)):
|
|
path = Path(temporary) / f"{name}.json"
|
|
path.write_bytes(body if isinstance(body, bytes) else json.dumps(body).encode())
|
|
code = code.replace(f"/absolute/path/to/{name}/health.json", str(path))
|
|
return subprocess.run(["bash", "-eu", "-c", code], capture_output=True,
|
|
text=True, timeout=5)
|
|
|
|
def test_fixed_timing_comparison_joins_reordered_keys_and_keeps_partial_results(self):
|
|
first = "2026-10-03T12:00:00.123456789+01:00"
|
|
second = "2026-10-03T12:03:00.3+01:00"
|
|
|
|
def row(key, success=None, *, failure=None, category=None, failures=0):
|
|
status = {"consecutiveFailures": failures}
|
|
if success is not None:
|
|
status["lastSuccess"] = success
|
|
if failure is not None:
|
|
status["lastError"] = {"at": failure, "category": category,
|
|
"message": "private fixture error"}
|
|
return {"key": key, "connection": "https://private-fixture.invalid", "pollStatus": status}
|
|
|
|
before = dict(snapshot(False), updatedAt=first, instances=[
|
|
row("pve::progress", first),
|
|
row("pbs::failing", first, failure=first, category="transient", failures=1),
|
|
row("pve::partial", first),
|
|
row("pbs::unchanged", first),
|
|
row("pbs::unseen"),
|
|
row("pbs::removed", first),
|
|
row("pve::recovered", first, failure=first, category="partial"),
|
|
])
|
|
after = dict(snapshot(False), updatedAt=second, instances=[
|
|
row("pbs::unseen"),
|
|
row("pve::recovered", second),
|
|
row("pve::partial", second, failure=second, category="partial"),
|
|
row("pbs::failing", first, failure=second, category="transient", failures=2),
|
|
row("pbs::added", second),
|
|
row("pbs::unchanged", first),
|
|
row("pve::progress", second),
|
|
])
|
|
result = self.compare(before, after)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertNotIn("private-fixture.invalid", result.stdout)
|
|
self.assertNotIn("private fixture error", result.stdout)
|
|
value = json.loads(result.stdout)
|
|
self.assertEqual(value["earlier"], {"updatedAt": first, "enabled": False})
|
|
self.assertEqual(value["later"], {"updatedAt": second, "enabled": False})
|
|
rows = {r["key"]: r for r in value["polls"]}
|
|
self.assertEqual(len(value["polls"]), 8)
|
|
self.assertEqual({key: r["changes"] for key, r in rows.items()}, {
|
|
"pve::progress": ["lastSuccess"],
|
|
"pbs::failing": ["consecutiveFailures", "lastErrorAt"],
|
|
"pve::partial": ["lastErrorAt", "lastErrorCategory", "lastSuccess"],
|
|
"pbs::unchanged": [],
|
|
"pbs::unseen": [],
|
|
"pbs::removed": None,
|
|
"pbs::added": None,
|
|
"pve::recovered": ["lastErrorAt", "lastErrorCategory", "lastSuccess"],
|
|
})
|
|
self.assertEqual(rows["pve::partial"]["after"], {
|
|
"lastSuccess": second, "lastErrorAt": second, "lastErrorCategory": "partial",
|
|
"consecutiveFailures": 0,
|
|
})
|
|
self.assertEqual(rows["pbs::failing"]["after"]["lastSuccess"], first)
|
|
self.assertEqual(rows["pbs::failing"]["after"]["lastErrorAt"], second)
|
|
self.assertIsNone(rows["pve::recovered"]["after"]["lastErrorAt"])
|
|
self.assertEqual(rows["pbs::unchanged"]["after"]["lastSuccess"], first)
|
|
self.assertIsNone(rows["pbs::unseen"]["after"]["lastSuccess"])
|
|
self.assertIsNone(rows["pbs::removed"]["after"])
|
|
self.assertIsNone(rows["pbs::added"]["before"])
|
|
|
|
def test_comparison_keeps_retry_context_separate_from_finished_polls(self):
|
|
before = snapshot(False)
|
|
before["queue"]["oldestOverdueSeconds"] = 0
|
|
before["instances"][1]["breaker"]["retryAt"] = "2026-10-03T12:00:12.25Z"
|
|
before["instances"][2]["breaker"]["retryAt"] = "2026-10-03T12:01:00Z"
|
|
before["instances"][2]["deadLetter"]["reason"] = "private fault text"
|
|
after = json.loads(json.dumps(before))
|
|
after["updatedAt"] = "2026-10-03T12:01:30Z"
|
|
after["queue"]["oldestOverdueSeconds"] = 90
|
|
after["instances"][1]["breaker"]["retryAt"] = "2026-10-03T12:01:40Z"
|
|
after["instances"][2]["deadLetter"]["nextRetry"] = "2026-10-03T12:02:00Z"
|
|
after["instances"].reverse()
|
|
|
|
result = self.compare(before, after)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertNotIn("private fault text", result.stdout)
|
|
value = json.loads(result.stdout)
|
|
self.assertEqual(value["oldestQueuedOverdueSeconds"], {"earlier": 0, "later": 90})
|
|
rows = {row["key"]: row for row in value["polls"]}
|
|
self.assertTrue(all(row["changes"] == [] for row in rows.values()))
|
|
self.assertEqual(rows["pve::failed"]["retry"], {
|
|
"before": {
|
|
"breaker": {"state": "open", "retryAt": "2026-10-03T12:01:00Z"},
|
|
"deadLetter": {"present": True, "nextRetry": None},
|
|
},
|
|
"after": {
|
|
"breaker": {"state": "open", "retryAt": "2026-10-03T12:01:00Z"},
|
|
"deadLetter": {"present": True, "nextRetry": "2026-10-03T12:02:00Z"},
|
|
},
|
|
})
|
|
self.assertEqual(rows["pve::retrying"]["retry"]["after"], {
|
|
"breaker": {"state": "half_open", "retryAt": "2026-10-03T12:01:40Z"},
|
|
"deadLetter": {"present": False, "nextRetry": None},
|
|
})
|
|
self.assertEqual(rows["pve::healthy"]["retry"]["after"], {
|
|
"breaker": {"state": "closed", "retryAt": None},
|
|
"deadLetter": {"present": False, "nextRetry": None},
|
|
})
|
|
|
|
def test_comparison_refuses_ambiguous_or_incomplete_captures(self):
|
|
good = snapshot()
|
|
later = dict(good, updatedAt="2026-10-03T12:03:00Z")
|
|
for bad, message in (
|
|
(b"", "one saved scheduler snapshot"),
|
|
(json.dumps(later).encode() * 2, "one saved scheduler snapshot"),
|
|
(dict(later, instances=None), "one saved scheduler snapshot"),
|
|
(dict(later, instances=[{"key": "", "pollStatus": {}}]), "one saved scheduler snapshot"),
|
|
(dict(later, instances=[later["instances"][0]] * 2), "Duplicate connection key"),
|
|
(good, "two different capture times"),
|
|
):
|
|
with self.subTest(message=message):
|
|
result = self.compare(good, bad)
|
|
self.assertNotEqual(result.returncode, 0)
|
|
self.assertEqual(result.stdout, "")
|
|
self.assertIn(message, result.stderr)
|
|
|
|
def test_comparison_preserves_two_empty_captures_as_no_rows(self):
|
|
empty = dict(snapshot(False), instances=[])
|
|
later = dict(empty, updatedAt="2026-10-03T12:03:00Z",
|
|
queue=dict(empty["queue"], oldestOverdueSeconds=0))
|
|
result = self.compare(empty, later)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
value = json.loads(result.stdout)
|
|
self.assertEqual(value["polls"], [])
|
|
self.assertEqual(value["oldestQueuedOverdueSeconds"], {"earlier": None, "later": 0})
|
|
|
|
def test_response_example_is_valid_json_with_the_current_field_names(self):
|
|
example = json.loads(re.findall(r"```json\n(.*?)```", DOC.read_text(), re.DOTALL)[0])
|
|
self.assertEqual(set(example), set(snapshot()))
|
|
self.assertIsInstance(example["enabled"], bool)
|
|
self.assertEqual(example["queue"]["oldestOverdueSeconds"], 90)
|
|
self.assertIn("lastSuccess", example["instances"][0]["pollStatus"])
|
|
|
|
def test_rollout_restores_precedence_and_does_not_rewrite_shared_temp_settings(self):
|
|
text = ROLLOUT.read_text()
|
|
self.assertNotIn("sudo mv", text)
|
|
self.assertNotIn("watch -n", text)
|
|
self.assertNotIn('X-API-Token: $TOKEN', text)
|
|
self.assertIn("environment value overrides `system.json`", text)
|
|
self.assertIn("ADAPTIVE_POLLING_ENABLED=false", text)
|
|
self.assertIn("merely removing `true`", text)
|
|
self.assertIn("interrupts monitoring and notifications", text)
|
|
adaptive = (ROOT / "docs/monitoring/ADAPTIVE_POLLING.md").read_text()
|
|
self.assertIn("not a monitoring-health verdict", adaptive)
|
|
|
|
def test_adaptive_polling_guide_matches_metrics_env_and_local_freshness_runtime(self):
|
|
adaptive = " ".join((ROOT / "docs/monitoring/ADAPTIVE_POLLING.md").read_text().split())
|
|
rollout = " ".join(ROLLOUT.read_text().split())
|
|
work_progress = (ROOT / "internal/monitoring/poll_work_progress.go").read_text()
|
|
task_workers = (ROOT / "internal/monitoring/poll_task_workers.go").read_text()
|
|
|
|
self.assertIn('"pulse_scheduler_poll_planning_age_seconds"', work_progress)
|
|
self.assertIn("func (p *pollWorkProgress) beginPlanning(", work_progress)
|
|
self.assertIn("func (p *pollWorkProgress) finishPlanning(", work_progress)
|
|
self.assertIn("defer progress.finish(work)", task_workers)
|
|
self.assertIn("Recovered from panic in scheduled poll task", task_workers)
|
|
|
|
for expected in (
|
|
"`PULSE_METRICS_PORT`",
|
|
"legacy `METRICS_PORT`",
|
|
"`PULSE_METRICS_BIND_ADDRESS`",
|
|
"`PULSE_METRICS_TOKEN`",
|
|
"`pulse_monitor_poll_last_success_timestamp`",
|
|
"`pulse_scheduler_queued_poll_overdue_seconds`",
|
|
"`pulse_scheduler_poll_planning_age_seconds`",
|
|
"`pulse_scheduler_claimed_poll_overdue_seconds`",
|
|
"`pulse_scheduler_dead_letter_depth`",
|
|
"active poll planning pass has not finished",
|
|
"synchronous panic recovery logging after releasing its queue claim",
|
|
"from its own local timestamp rather than the process-wide Prometheus series",
|
|
"Stopped or replaced schedulers drop out of the live queue and dead-letter gauges",
|
|
"unfinished planning pass remains visible in `pulse_scheduler_poll_planning_age_seconds` until it completes",
|
|
"report zero for up to five minutes, then expire from the scrape",
|
|
"without moving the latest completion timestamp or staleness age backwards",
|
|
):
|
|
self.assertIn(expected, adaptive)
|
|
|
|
for expected in (
|
|
"`pulse_scheduler_poll_planning_age_seconds`",
|
|
"`pulse_scheduler_claimed_poll_overdue_seconds`",
|
|
"panic recovery logging",
|
|
"Under `POLL_TASK_WORKERS`, removing a target releases a claim still waiting for a worker slot without starting provider setup so other due targets are not held behind it, while an already-admitted poll still finishes before releasing its claim.",
|
|
):
|
|
self.assertIn(expected, rollout)
|
|
|
|
task_queue = (ROOT / "internal/monitoring/task_queue.go").read_text()
|
|
self.assertIn("func (l *pollTaskWorkerLimiter) acquireClaim(", task_workers)
|
|
self.assertIn("func (q *TaskQueue) watchClaimRetirement(", task_queue)
|
|
self.assertIn("func (q *TaskQueue) canStartClaim(", task_queue)
|
|
for expected in (
|
|
"When a target is removed while its claim is still waiting for a process-wide slot (`POLL_TASK_WORKERS`), the dispatcher releases the retired claim without starting provider setup or changing the worker cap and moves on to another due target",
|
|
"keeping any explicit replacement's own due time and leaving already-admitted `running` polls to finish under normal in-flight fences",
|
|
"Under `POLL_TASK_WORKERS`, a confirmed target removal releases a claim still waiting in `waiting_for_slot` without starting provider setup so another due target can take its turn.",
|
|
"When an already-admitted `running` poll returns after its monitor context or queue claim was retired, the worker releases the slot without starting another provider cadence lookup for a successor, while still preserving any explicit replacement's deadline.",
|
|
):
|
|
self.assertIn(expected, adaptive)
|
|
|
|
self.assertIn(
|
|
"if ctx.Err() != nil || !m.taskQueue.canStartClaim(task)",
|
|
task_workers,
|
|
)
|
|
self.assertIn(
|
|
"When an already-admitted poll returns after its monitor context or claim was retired, the worker releases the slot without reading provider cadence to plan a successor, while keeping any explicit replacement schedule intact.",
|
|
rollout,
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|