Files
ServiceMap-Public/backend/tests/test_v163_backups.py
T
NuklearRabbit adc2c70c21 ServiceMap: update tests and backend/app (+7 areas)
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
2026-11-20 13:51:46 +01:00

125 lines
5.3 KiB
Python

"""v1.6.3 verified backups, rotation, legacy cleanup and online restore."""
from __future__ import annotations
import os
import time
from datetime import UTC, datetime, timedelta
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from app import auth
from app import main as main_module
from app.backups import BackupManager
from app.discovery import DiscoveryManager
from app.discovery_store import DiscoveryStore
from app.models import ServiceCreate, ServiceKind
from app.store import Store
def _store(tmp_path: Path) -> Store:
return Store(str(tmp_path / "servicemap.db"))
def _add(store: Store, name: str) -> None:
store.create_service(ServiceCreate(name=name, kind=ServiceKind.HTTP, url=f"https://{name.lower()}.test"))
def test_backup_is_verified_listed_and_rotated(tmp_path: Path) -> None:
store = _store(tmp_path)
_add(store, "API")
manager = BackupManager(store, keep=2)
start = datetime(2026, 11, 18, 3, 0, tzinfo=UTC)
names = [manager.create(now=start + timedelta(days=day)).name for day in range(3)]
listed = manager.list()
assert [item.name for item in listed] == list(reversed(names))[:2] # newest two kept
assert not (manager.directory / names[0]).exists()
copy = Store(str(manager.directory / names[-1]))
assert [item.name for item in copy.list_services()] == ["API"]
with pytest.raises(ValueError):
manager.path_for("../servicemap.db")
with pytest.raises(ValueError):
manager.path_for("servicemap-backup-20990101T000000Z.db")
def test_legacy_preupgrade_copies_are_pruned_to_the_newest_two(tmp_path: Path) -> None:
store = _store(tmp_path)
legacy = [
"servicemap-v1.2.0-pre-discovery.sqlite3",
"servicemap-v1.5.0-pre-7ecd9df.sqlite3",
"servicemap-v1.6.1-pre-20260914.sqlite3",
"servicemap-v1.6.2-pre-20260923T040250Z.sqlite3",
]
for index, name in enumerate(legacy):
path = tmp_path / name
path.write_bytes(b"legacy")
stamp = time.time() - 10_000 + index * 100
os.utime(path, (stamp, stamp))
(tmp_path / "servicemap-v1.6.1-pre-20260914.sqlite3-wal").write_bytes(b"")
(tmp_path / "servicemap-v1.2.0-pre-discovery.sqlite3-shm").write_bytes(b"")
unrelated = tmp_path / "notes-v1.0.0-pre-x.sqlite3"
unrelated.write_bytes(b"keep")
BackupManager(store, legacy_keep=2).create()
remaining = sorted(path.name for path in tmp_path.iterdir() if path.is_file())
assert "servicemap-v1.6.2-pre-20260923T040250Z.sqlite3" in remaining
assert "servicemap-v1.6.1-pre-20260914.sqlite3" in remaining
assert "servicemap-v1.6.1-pre-20260914.sqlite3-wal" in remaining # sidecar of a kept copy
assert "servicemap-v1.2.0-pre-discovery.sqlite3" not in remaining
assert "servicemap-v1.2.0-pre-discovery.sqlite3-shm" not in remaining
assert "servicemap-v1.5.0-pre-7ecd9df.sqlite3" not in remaining
assert unrelated.exists()
assert "servicemap.db" in remaining
def test_restore_replaces_live_data_after_a_safety_copy(tmp_path: Path) -> None:
store = _store(tmp_path)
_add(store, "API")
manager = BackupManager(store)
backup = manager.create(now=datetime(2026, 11, 18, 3, 0, tzinfo=UTC))
_add(store, "Added later")
assert len(store.list_services()) == 2
safety = manager.restore(backup.name)
assert [item.name for item in store.list_services()] == ["API"]
assert safety.kind == "pre-restore"
safety_copy = Store(str(manager.directory / safety.name))
assert sorted(item.name for item in safety_copy.list_services()) == ["API", "Added later"]
def test_backup_routes_need_the_token(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
main_module.store = _store(tmp_path)
main_module.discovery_store = DiscoveryStore(main_module.store)
main_module.discovery_manager = DiscoveryManager(main_module.store, main_module.discovery_store)
main_module.semantic_cache.clear()
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/backups").status_code == 401
headers = {"Authorization": f"Bearer {auth.admin_token()}"}
_add(main_module.store, "API")
created = client.post("/api/v1/backups", headers=headers)
assert created.status_code == 201
name = created.json()["name"]
assert [item["name"] for item in client.get("/api/v1/backups", headers=headers).json()] == [name]
assert client.get(f"/api/v1/backups/{name}").status_code == 401
download = client.get(f"/api/v1/backups/{name}", headers=headers)
assert download.status_code == 200 and download.content[:15] == b"SQLite format 3"
assert client.get("/api/v1/backups/..%2Fservicemap.db", headers=headers).status_code == 404
_add(main_module.store, "Temporary")
restored = client.post(f"/api/v1/backups/{name}/restore", headers=headers)
assert restored.status_code == 200 and restored.json()["kind"] == "pre-restore"
assert [item["service"]["name"] for item in client.get("/api/v1/services").json()] == ["API"]
finally:
auth.reset_token_cache()