Public changes compared with the previous published export: 89 files changed. Affected areas: - backend/app: 21 files - dependencies: 5 files - deployment and CI: 6 files - documentation: 2 files - frontend: 3 files - frontend/src: 20 files - project files: 1 file - tests: 30 files - unraid: 1 file Changed files: - Updated: ".env.example" - Updated: "Dockerfile.unraid" - Updated: "README.md" - Updated: "backend/Dockerfile" - Added: "backend/app/alerts.py" - Updated: "backend/app/auth.py" - Added: "backend/app/backups.py" - Updated: "backend/app/config_io.py" - Updated: "backend/app/demo.py" - Updated: "backend/app/discovery.py" - Updated: "backend/app/discovery_store.py" - Updated: "backend/app/docker_discovery.py" - Updated: "backend/app/events.py" - Updated: "backend/app/evidence.py" - Updated: "backend/app/health.py" - Updated: "backend/app/incidents.py" - Added: "backend/app/loops.py" - Updated: "backend/app/main.py" - Updated: "backend/app/models.py" - Updated: "backend/app/observation.py" - Updated: "backend/app/probes.py" - Added: "backend/app/response_cache.py" - Updated: "backend/app/scheduler.py" - Updated: "backend/app/semantic.py" - Updated: "backend/app/store.py" - Updated: "backend/pyproject.toml" - Updated: "backend/tests/test_api.py" - Updated: "backend/tests/test_config_io.py" - Updated: "backend/tests/test_demo.py" - Updated: "backend/tests/test_discovery_v120.py" - Updated: "backend/tests/test_health.py" - Updated: "backend/tests/test_probes.py" - Updated: "backend/tests/test_scheduler.py" - Updated: "backend/tests/test_v130_probe_reachability.py" - Updated: "backend/tests/test_v160_evidence_changes.py" - Added: "backend/tests/test_v163_alerts.py" - Added: "backend/tests/test_v163_backups.py" - Added: "backend/tests/test_v163_data_integrity.py" - Added: "backend/tests/test_v163_efficiency.py" - Added: "backend/tests/test_v163_health_performance.py" - Added: "backend/tests/test_v163_loops.py" - Added: "backend/tests/test_v163_management.py" - Added: "backend/tests/test_v163_realtime.py" - Added: "backend/tests/test_v163_security.py" - Added: "backend/tests/test_v163_semantic_etag.py" - Added: "compose.demo.yaml" - Updated: "compose.yaml" - Added: "docker-compose.broker.yml" - Updated: "docker-compose.yml" - Updated: "docs/ARCHITECTURE.md" - Added: "frontend/e2e/a11y.spec.ts" - Updated: "frontend/e2e/demo.spec.ts" - Updated: "frontend/index.html" - Updated: "frontend/nginx.conf" - Updated: "frontend/package-lock.json" - Updated: "frontend/package.json" - Updated: "frontend/playwright.config.ts" - Updated: "frontend/src/App.tsx" - Updated: "frontend/src/LivingEdge.tsx" - Added: "frontend/src/activityView.test.ts" - Added: "frontend/src/activityView.ts" - Updated: "frontend/src/api.ts" - Added: "frontend/src/apiAuth.test.ts" - Added: "frontend/src/components/ActionsMenu.tsx" - Added: "frontend/src/components/ConfirmDialog.test.tsx" - Added: "frontend/src/components/ConfirmDialog.tsx" - Added: "frontend/src/components/StatusIcon.tsx" - Updated: "frontend/src/demo/DemoWalkthrough.test.ts" - Updated: "frontend/src/demo/DemoWalkthrough.tsx" - Added: "frontend/src/elk-worker-url.d.ts" - Added: "frontend/src/labels.ts" - Updated: "frontend/src/main.tsx" - Added: "frontend/src/realtime/refreshPlan.test.ts" - Added: "frontend/src/realtime/refreshPlan.ts" - Updated: "frontend/src/realtime/useRealtimeEvents.ts" - Added: "frontend/src/routing/urlState.test.ts" - Added: "frontend/src/routing/urlState.ts" - Updated: "frontend/src/styles.css" - Added: "frontend/src/topology/GraphListView.test.tsx" - Added: "frontend/src/topology/GraphListView.tsx" - Updated: "frontend/src/topology/ServiceNode.tsx" - Updated: "frontend/src/topology/TopologyCommandBar.tsx" - Added: "frontend/src/topology/semanticGraph.test.ts" - Updated: "frontend/src/topology/semanticGraph.ts" - Added: "frontend/src/topologyLayout.test.ts" - Updated: "frontend/src/topologyLayout.ts" - Updated: "package-lock.json" - Updated: "package.json" - Updated: "unraid/my-servicemap.xml" Export provenance/manifests refreshed; excluded from the change summary. Publish curated ServiceMap source from af46f684e468dffd3a5190e28d3b572d4c26bfd7
154 lines
6.4 KiB
Python
154 lines
6.4 KiB
Python
"""v1.6.3 ntfy incident notifications."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime, timedelta
|
|
from pathlib import Path
|
|
|
|
import httpx
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app import alerts as alerts_module
|
|
from app import auth
|
|
from app import main as main_module
|
|
from app.alerts import AlertManager, AlertSettingsUpdate
|
|
from app.discovery import DiscoveryManager
|
|
from app.discovery_store import DiscoveryStore
|
|
from app.models import Criticality, IncidentRecord, IncidentStatus
|
|
from app.store import Store
|
|
|
|
|
|
@pytest.fixture()
|
|
def sent(monkeypatch: pytest.MonkeyPatch) -> list[httpx.Request]:
|
|
requests: list[httpx.Request] = []
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
requests.append(request)
|
|
return httpx.Response(200, json={"id": "x"})
|
|
|
|
real_client = httpx.Client
|
|
|
|
def fake_client(*args, **kwargs):
|
|
kwargs["transport"] = httpx.MockTransport(handler)
|
|
return real_client(*args, **kwargs)
|
|
|
|
monkeypatch.setattr(alerts_module.httpx, "Client", fake_client)
|
|
return requests
|
|
|
|
|
|
def _incident(
|
|
store: Store,
|
|
*,
|
|
status: IncidentStatus = IncidentStatus.OPEN,
|
|
severity: Criticality = Criticality.HIGH,
|
|
detected: datetime,
|
|
) -> IncidentRecord:
|
|
record = IncidentRecord(
|
|
id="inc-1",
|
|
status=status,
|
|
root_service_id="svc",
|
|
root_service_name="Payments",
|
|
started_at=detected,
|
|
detected_at=detected,
|
|
resolved_at=detected + timedelta(minutes=5) if status == IncidentStatus.RESOLVED else None,
|
|
title="Payments is down",
|
|
root_cause_reason="Service is down and no configured dependency is also down",
|
|
affected_service_ids=["a", "b"],
|
|
severity=severity,
|
|
latest_update_at=detected,
|
|
)
|
|
store.upsert_incident(record)
|
|
return record
|
|
|
|
|
|
def _manager(tmp_path: Path, **settings) -> AlertManager:
|
|
manager = AlertManager(Store(str(tmp_path / "servicemap.db")))
|
|
manager.update(AlertSettingsUpdate(enabled=True, ntfy_topic="sm-alerts", public_url="http://sm.lan:1253", **settings))
|
|
return manager
|
|
|
|
|
|
def test_opened_after_delay_once_then_resolved(tmp_path: Path, sent: list[httpx.Request]) -> None:
|
|
manager = _manager(tmp_path, delay_seconds=60)
|
|
detected = datetime.now(UTC)
|
|
_incident(manager.store, detected=detected)
|
|
|
|
# Within the delay: a short flap stays quiet.
|
|
assert manager.run_cycle(detected + timedelta(seconds=30)) == 0
|
|
assert manager.run_cycle(detected + timedelta(seconds=61)) == 1
|
|
# Never twice.
|
|
assert manager.run_cycle(detected + timedelta(seconds=120)) == 0
|
|
request = sent[0]
|
|
assert str(request.url) == "https://ntfy.sh/sm-alerts"
|
|
assert request.headers["Title"] == "Uitgevallen: Payments is down"
|
|
assert request.headers["Priority"] == "urgent"
|
|
assert request.headers["Click"].startswith("http://sm.lan:1253/#/incidents?incident=inc-1")
|
|
assert "2 afhankelijke service(s) geraakt" in request.content.decode()
|
|
|
|
_incident(manager.store, status=IncidentStatus.RESOLVED, detected=detected)
|
|
assert manager.run_cycle(detected + timedelta(minutes=6)) == 1
|
|
assert sent[-1].headers["Title"] == "Opgelost: Payments is down"
|
|
assert manager.run_cycle(detected + timedelta(minutes=7)) == 0
|
|
|
|
|
|
def test_severity_threshold_reminders_and_disabled(tmp_path: Path, sent: list[httpx.Request]) -> None:
|
|
manager = _manager(tmp_path, delay_seconds=0, min_severity=Criticality.CRITICAL, reminder_minutes=10)
|
|
detected = datetime.now(UTC)
|
|
_incident(manager.store, severity=Criticality.HIGH, detected=detected)
|
|
assert manager.run_cycle(detected + timedelta(minutes=30)) == 0 # below threshold
|
|
|
|
_incident(manager.store, severity=Criticality.CRITICAL, detected=detected)
|
|
assert manager.run_cycle(detected + timedelta(seconds=1)) == 1 # opened
|
|
assert manager.run_cycle(detected + timedelta(minutes=10, seconds=1)) == 1 # reminder 1
|
|
assert manager.run_cycle(detected + timedelta(minutes=12)) == 0
|
|
assert sent[-1].headers["Title"].startswith("Nog steeds uitgevallen")
|
|
|
|
manager.update(AlertSettingsUpdate(enabled=False, ntfy_topic="sm-alerts"))
|
|
assert manager.run_cycle(detected + timedelta(minutes=50)) == 0
|
|
|
|
|
|
def test_settings_validation_and_secret_token(tmp_path: Path) -> None:
|
|
manager = AlertManager(Store(str(tmp_path / "servicemap.db")))
|
|
with pytest.raises(ValueError, match="topic"):
|
|
manager.update(AlertSettingsUpdate(enabled=True, ntfy_topic=""))
|
|
with pytest.raises(ValueError):
|
|
AlertSettingsUpdate(ntfy_topic="bad topic!")
|
|
with pytest.raises(ValueError):
|
|
AlertSettingsUpdate(ntfy_server="file:///etc/passwd")
|
|
|
|
view = manager.update(AlertSettingsUpdate(ntfy_topic="t", ntfy_token="secret-value"))
|
|
assert view.ntfy_token_set and "secret-value" not in view.model_dump_json()
|
|
# None keeps the token, "" removes it.
|
|
assert manager.update(AlertSettingsUpdate(ntfy_topic="t")).ntfy_token_set
|
|
assert not manager.update(AlertSettingsUpdate(ntfy_topic="t", ntfy_token="")).ntfy_token_set
|
|
|
|
|
|
def test_settings_routes_need_the_token_even_to_read(
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, sent: list[httpx.Request]
|
|
) -> None:
|
|
main_module.store = Store(str(tmp_path / "servicemap.db"))
|
|
main_module.discovery_store = DiscoveryStore(main_module.store)
|
|
main_module.discovery_manager = DiscoveryManager(main_module.store, main_module.discovery_store)
|
|
monkeypatch.setenv("SERVICEMAP_DB", str(tmp_path / "servicemap.db"))
|
|
monkeypatch.setenv("SERVICEMAP_AUTH_MODE", "token")
|
|
monkeypatch.delenv("SERVICEMAP_ADMIN_TOKEN_FILE", raising=False)
|
|
auth.reset_token_cache()
|
|
client = TestClient(main_module.app)
|
|
try:
|
|
assert client.get("/api/v1/settings/alerts").status_code == 401
|
|
assert client.get("/api/v1/services").status_code == 200 # ordinary reads stay open
|
|
token = auth.admin_token()
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
saved = client.put(
|
|
"/api/v1/settings/alerts",
|
|
json={"enabled": True, "ntfy_topic": "sm-test", "ntfy_token": "tk-123"},
|
|
headers=headers,
|
|
)
|
|
assert saved.status_code == 200 and saved.json()["ntfy_token_set"] is True
|
|
assert "tk-123" not in client.get("/api/v1/settings/alerts", headers=headers).text
|
|
result = client.post("/api/v1/settings/alerts/test", headers=headers).json()
|
|
assert result["ok"] is True
|
|
assert sent[-1].headers["Authorization"] == "Bearer tk-123"
|
|
finally:
|
|
auth.reset_token_cache()
|