Files
awoooi/apps/api/tests/test_alert_chain_smoke_metric.py
Your Name ced36f2521
Some checks failed
CD Pipeline / tests (push) Failing after 6s
CD Pipeline / build-and-deploy (push) Has been skipped
CD Pipeline / post-deploy-checks (push) Has been skipped
Code Review / ai-code-review (push) Failing after 8s
feat(awooop): add source provider freshness heartbeat
2026-05-20 19:32:22 +08:00

139 lines
5.2 KiB
Python

from __future__ import annotations
import importlib.util
import sys
import time
import unittest
from pathlib import Path
SCRIPT_PATH = Path(__file__).resolve().parents[3] / "scripts" / "alert_chain_smoke_test.py"
SPEC = importlib.util.spec_from_file_location("alert_chain_smoke_test", SCRIPT_PATH)
alert_chain_smoke_test = importlib.util.module_from_spec(SPEC)
assert SPEC and SPEC.loader
sys.dont_write_bytecode = True
sys.modules[SPEC.name] = alert_chain_smoke_test
SPEC.loader.exec_module(alert_chain_smoke_test)
class AlertChainSmokeMetricTest(unittest.TestCase):
def test_parse_app_alert_chain_metric_samples(self):
samples = alert_chain_smoke_test.parse_app_alert_chain_metric_samples(
"\n".join([
"# HELP awoooi_alert_chain_last_success_timestamp Last successful alert chain",
'awoooi_alert_chain_last_success_timestamp{source="alertmanager"} 123.5',
'awoooi_alert_chain_last_success_timestamp{source="sentry"} 120',
"unrelated_metric 1",
])
)
self.assertEqual(
samples,
[
alert_chain_smoke_test.AlertChainMetricSample(
source="alertmanager",
timestamp=123.5,
evidence_path="app_metrics",
),
alert_chain_smoke_test.AlertChainMetricSample(
source="sentry",
timestamp=120.0,
evidence_path="app_metrics",
),
],
)
def test_newest_sample_for_source_prefers_requested_source(self):
samples = [
alert_chain_smoke_test.AlertChainMetricSample("sentry", 999.0, "prometheus"),
alert_chain_smoke_test.AlertChainMetricSample("alertmanager", 100.0, "prometheus"),
alert_chain_smoke_test.AlertChainMetricSample("alertmanager", 200.0, "app_metrics"),
]
sample = alert_chain_smoke_test._newest_sample_for_source(samples, "alertmanager")
self.assertEqual(sample.timestamp, 200.0)
self.assertEqual(sample.evidence_path, "app_metrics")
def test_alert_chain_metric_result_marks_recent_app_metric_as_scrape_delay(self):
sample = alert_chain_smoke_test.AlertChainMetricSample(
source="alertmanager",
timestamp=time.time() - 60,
evidence_path="app_metrics",
)
result = alert_chain_smoke_test._alert_chain_metric_result(sample, fallback=True)
self.assertTrue(result.passed)
self.assertIn("Prometheus scrape 尚未看到", result.message)
def test_alert_chain_metric_result_fails_persistent_silence(self):
sample = alert_chain_smoke_test.AlertChainMetricSample(
source="alertmanager",
timestamp=time.time() - alert_chain_smoke_test.MAX_ALERT_CHAIN_SILENCE_SECONDS - 60,
evidence_path="prometheus",
)
result = alert_chain_smoke_test._alert_chain_metric_result(sample)
self.assertFalse(result.passed)
self.assertTrue(result.critical)
def test_source_provider_heartbeat_requires_operator_key(self):
result = alert_chain_smoke_test.send_source_provider_heartbeat(
"https://awoooi.example",
providers=["sentry", "signoz"],
operator_key=None,
operator_id="gitea-e2e-health",
)
self.assertFalse(result.passed)
self.assertTrue(result.critical)
self.assertIn("AWOOOP_OPERATOR_API_KEY", result.message)
def test_source_provider_heartbeat_posts_expected_payload(self):
calls = []
def fake_post(url, payload, *, headers=None, timeout=None):
calls.append(
{
"url": url,
"payload": payload,
"headers": headers,
"timeout": timeout,
}
)
return alert_chain_smoke_test.HttpGetResult(
200,
(
'{"status":"recorded","items":['
'{"provider":"sentry"},{"provider":"signoz"}]}'
),
)
original_post = alert_chain_smoke_test.http_post_json
try:
alert_chain_smoke_test.http_post_json = fake_post
result = alert_chain_smoke_test.send_source_provider_heartbeat(
"https://awoooi.example",
providers=["sentry", "signoz"],
operator_key="secret",
operator_id="gitea-e2e-health",
run_ref="run-123",
)
finally:
alert_chain_smoke_test.http_post_json = original_post
self.assertTrue(result.passed)
self.assertEqual(
calls[0]["url"],
"https://awoooi.example/api/v1/platform/events/dossier/provider-heartbeat",
)
self.assertEqual(calls[0]["payload"]["providers"], ["sentry", "signoz"])
self.assertEqual(calls[0]["payload"]["run_ref"], "run-123")
self.assertEqual(calls[0]["headers"]["X-AwoooP-Operator-Id"], "gitea-e2e-health")
self.assertEqual(calls[0]["headers"]["X-AwoooP-Operator-Key"], "secret")
if __name__ == "__main__":
unittest.main()