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
131 lines
5.3 KiB
Python
131 lines
5.3 KiB
Python
"""v1.6.3 background loops survive any failure and report readiness."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
import sqlite3
|
|
from pathlib import Path
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app import main as main_module
|
|
from app.discovery import DiscoveryManager
|
|
from app.discovery_store import DiscoveryStore
|
|
from app.loops import LoopRegistry, run_periodic
|
|
from app.store import Store
|
|
|
|
|
|
def test_a_failing_cycle_is_logged_and_the_loop_keeps_running(caplog) -> None:
|
|
calls: list[int] = []
|
|
|
|
async def work() -> None:
|
|
calls.append(len(calls))
|
|
if len(calls) == 1:
|
|
# The failure that used to end the observation loop silently.
|
|
raise sqlite3.OperationalError("database is locked")
|
|
|
|
async def scenario() -> LoopRegistry:
|
|
registry = LoopRegistry()
|
|
task = asyncio.create_task(
|
|
run_periodic("observation", work, interval_seconds=0.01, registry=registry, max_backoff_seconds=0.01)
|
|
)
|
|
while len(calls) < 3:
|
|
await asyncio.sleep(0.005)
|
|
status = registry.snapshot()[0]
|
|
task.cancel()
|
|
try:
|
|
await task
|
|
except asyncio.CancelledError:
|
|
pass
|
|
assert registry.snapshot() == [] # unregistered on shutdown
|
|
return status
|
|
|
|
loop_logger = logging.getLogger("servicemap.loops")
|
|
loop_logger.addHandler(caplog.handler)
|
|
try:
|
|
status = asyncio.run(scenario())
|
|
finally:
|
|
loop_logger.removeHandler(caplog.handler)
|
|
assert len(calls) >= 3
|
|
assert status["healthy"] is True
|
|
assert status["consecutive_failures"] == 0
|
|
assert "OperationalError: database is locked" in status["last_error"]
|
|
assert "Background loop observation failed" in caplog.text
|
|
|
|
|
|
def test_a_loop_without_recent_success_is_reported_unhealthy() -> None:
|
|
registry = LoopRegistry()
|
|
status = registry.register("observation", 60)
|
|
status.started_at -= 10_000
|
|
assert registry.all_healthy() is False
|
|
status.paused = True # e.g. demo mode: idle on purpose, not stalled
|
|
assert registry.all_healthy() is True
|
|
|
|
|
|
def test_ready_endpoint_reports_database_and_loops(tmp_path: Path) -> 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)
|
|
client = TestClient(main_module.app)
|
|
|
|
response = client.get("/ready")
|
|
assert response.status_code == 200
|
|
assert response.json()["database"] == "ok"
|
|
|
|
stalled = main_module.loop_registry.register("observation-test", 60)
|
|
stalled.started_at -= 10_000
|
|
try:
|
|
response = client.get("/ready")
|
|
assert response.status_code == 503
|
|
body = response.json()
|
|
assert body["status"] == "not_ready"
|
|
assert any(item["name"] == "observation-test" and not item["healthy"] for item in body["loops"])
|
|
finally:
|
|
main_module.loop_registry.unregister("observation-test")
|
|
|
|
|
|
def test_zero_observation_interval_does_not_register_observer(tmp_path: Path, monkeypatch) -> None:
|
|
store = Store(str(tmp_path / "servicemap.db"))
|
|
monkeypatch.setattr(main_module, "store", store)
|
|
monkeypatch.setattr(main_module, "OBSERVATION_INTERVAL", 0)
|
|
monkeypatch.setattr(main_module, "DISCOVERY_INTERVAL", 0)
|
|
monkeypatch.setattr(main_module, "PROBE_INTERVAL", 0)
|
|
monkeypatch.setattr(main_module, "BACKUP_INTERVAL_HOURS", 0)
|
|
with TestClient(main_module.app) as client:
|
|
assert all(item["name"] != "observation" for item in client.get("/ready").json()["loops"])
|
|
|
|
|
|
def test_maintenance_prunes_observations_of_old_runs_and_checkpoints(tmp_path: Path) -> None:
|
|
store = Store(str(tmp_path / "servicemap.db"))
|
|
discovery = DiscoveryStore(store)
|
|
with sqlite3.connect(store.path) as db:
|
|
for index in range(12):
|
|
db.execute(
|
|
"INSERT INTO discovery_runs (id, status, started_at) VALUES (?, 'completed', ?)",
|
|
(f"run-{index:02d}", f"2026-11-{index + 1:02d}T00:00:00+00:00"),
|
|
)
|
|
db.execute(
|
|
"INSERT INTO discovery_sources (id, source_type, source_id, first_seen_at, last_seen_at) "
|
|
"VALUES ('src', 'docker', 'c1', '2026-11-01T00:00:00+00:00', '2026-11-01T00:00:00+00:00')"
|
|
)
|
|
for index in range(12):
|
|
db.execute(
|
|
"INSERT INTO discovery_observations (id, run_id, source_id, source_type, source_name, lifecycle, "
|
|
"change_type, observed_at) VALUES (?, ?, 'src', 'docker', 'c1', 'active', 'unchanged', ?)",
|
|
(f"obs-{index}", f"run-{index:02d}", f"2026-11-{index + 1:02d}T00:00:00+00:00"),
|
|
)
|
|
db.execute(
|
|
"INSERT INTO topology_change_events (id, event_type, detail, created_at) "
|
|
"VALUES ('old', 'dependency_added', 'x', '2020-01-01T00:00:00+00:00')"
|
|
)
|
|
|
|
assert discovery.prune_observations(10) == 2
|
|
assert discovery.prune_change_events(90) == 1
|
|
store.checkpoint()
|
|
with sqlite3.connect(store.path) as db:
|
|
remaining = {row[0] for row in db.execute("SELECT run_id FROM discovery_observations")}
|
|
runs = db.execute("SELECT COUNT(*) FROM discovery_runs").fetchone()[0]
|
|
assert remaining == {f"run-{index:02d}" for index in range(2, 12)}
|
|
assert runs == 12 # runs are kept; suggestions reference them
|