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
This commit is contained in:
1 parent
d7319250e8
commit
adc2c70c21
91 files changed
+9205
-1691
No files matched your search
@@ -0,0 +1,170 @@
|
||||
"""v1.6.3 efficiency guards: one Docker call per observation, fewer evidence writes."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from app import docker_discovery
|
||||
from app import store as store_module
|
||||
from app.models import (
|
||||
EvidenceProvider,
|
||||
EvidenceScope,
|
||||
HealthEvidence,
|
||||
HealthStatus,
|
||||
ServiceCreate,
|
||||
ServiceKind,
|
||||
)
|
||||
from app.store import Store
|
||||
|
||||
|
||||
def test_health_is_read_from_the_list_status_text() -> None:
|
||||
assert docker_discovery.health_from_status("Up 3 hours (healthy)") == "healthy"
|
||||
assert docker_discovery.health_from_status("Up 2 minutes (unhealthy)") == "unhealthy"
|
||||
assert docker_discovery.health_from_status("Up 5 seconds (health: starting)") == "starting"
|
||||
assert docker_discovery.health_from_status("Exited (0) 2 days ago") is None
|
||||
assert docker_discovery.health_from_status("") is None
|
||||
|
||||
|
||||
def test_socket_observer_uses_one_list_call_on_a_long_lived_client(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
created: list[object] = []
|
||||
calls: list[bool] = []
|
||||
|
||||
class _Api:
|
||||
def containers(self, all: bool) -> list[dict[str, object]]:
|
||||
calls.append(all)
|
||||
return [
|
||||
{
|
||||
"Id": "a" * 64,
|
||||
"Names": ["/shop-api"],
|
||||
"State": "running",
|
||||
"Status": "Up 1 hour (healthy)",
|
||||
"Labels": {"com.docker.compose.project": "shop", "com.docker.compose.service": "api"},
|
||||
},
|
||||
{"Id": "b" * 64, "Names": ["/batch"], "State": "exited", "Status": "Exited (1) 3 minutes ago", "Labels": {}},
|
||||
]
|
||||
|
||||
class _Client:
|
||||
def __init__(self, **_: object) -> None:
|
||||
self.api = _Api()
|
||||
created.append(self)
|
||||
|
||||
def close(self) -> None:
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(docker_discovery.docker, "DockerClient", _Client)
|
||||
monkeypatch.setenv("SERVICEMAP_DOCKER_PROVIDER", "socket")
|
||||
docker_discovery._observer_clients.clear()
|
||||
|
||||
first = docker_discovery.observe_container_health("unix:///var/run/docker.sock")
|
||||
docker_discovery.observe_container_health("unix:///var/run/docker.sock")
|
||||
|
||||
assert len(created) == 1 # reused across cycles
|
||||
assert calls == [True, True] # exactly one API call per cycle
|
||||
api, batch = first
|
||||
assert (api.name, api.state, api.health, api.healthcheck_present) == ("shop-api", "running", "healthy", True)
|
||||
assert api.stable_source_ref == "docker:compose:shop:api"
|
||||
assert (batch.state, batch.health, batch.healthcheck_present) == ("exited", None, False)
|
||||
docker_discovery._observer_clients.clear()
|
||||
|
||||
|
||||
def test_unchanged_fresh_evidence_is_not_rewritten_until_half_its_ttl(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
store = Store(str(tmp_path / "servicemap.db"))
|
||||
service = store.create_service(ServiceCreate(name="API", kind=ServiceKind.HTTP, url="https://api.test"))
|
||||
clock = [datetime(2026, 11, 18, 12, 0, tzinfo=UTC)]
|
||||
monkeypatch.setattr(store_module, "_now", lambda: clock[0])
|
||||
|
||||
def claim(status: HealthStatus = HealthStatus.HEALTHY) -> HealthEvidence:
|
||||
now = clock[0]
|
||||
return HealthEvidence(
|
||||
id=f"ev-{now.timestamp()}",
|
||||
subject_type="service",
|
||||
subject_id=service.id,
|
||||
service_id=service.id,
|
||||
provider=EvidenceProvider.DOCKER,
|
||||
provider_ref="api",
|
||||
status=status,
|
||||
scope=EvidenceScope.COMPONENT,
|
||||
confidence=0.95,
|
||||
observed_at=now,
|
||||
valid_until=now + timedelta(seconds=180),
|
||||
received_at=now,
|
||||
summary="Docker healthcheck",
|
||||
)
|
||||
|
||||
store.record_health_evidence(claim())
|
||||
first = store.health_evidence_for_service(service.id)[0]
|
||||
|
||||
clock[0] += timedelta(seconds=60) # 120 s of 180 s left: skip
|
||||
store.record_health_evidence(claim())
|
||||
assert store.health_evidence_for_service(service.id)[0].observed_at == first.observed_at
|
||||
|
||||
clock[0] += timedelta(seconds=40) # 80 s left (< half): renew
|
||||
store.record_health_evidence(claim())
|
||||
renewed = store.health_evidence_for_service(service.id)[0]
|
||||
assert renewed.observed_at == clock[0]
|
||||
assert renewed.id == first.id # same row, no history growth
|
||||
|
||||
clock[0] += timedelta(seconds=10) # a material change is always written
|
||||
store.record_health_evidence(claim(HealthStatus.DOWN))
|
||||
assert store.health_evidence_for_service(service.id)[0].status == HealthStatus.DOWN
|
||||
|
||||
|
||||
def test_every_connection_uses_normal_synchronous_under_wal(tmp_path: Path) -> None:
|
||||
store = Store(str(tmp_path / "servicemap.db"))
|
||||
with store._connect() as db:
|
||||
assert db.execute("PRAGMA journal_mode").fetchone()[0] == "wal"
|
||||
assert db.execute("PRAGMA synchronous").fetchone()[0] == 1 # NORMAL
|
||||
|
||||
|
||||
def test_concurrent_incident_sync_opens_one_incident_per_root(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
import threading
|
||||
|
||||
from app import incidents
|
||||
from app.models import ProbeResult
|
||||
|
||||
real_sync = incidents.sync_incidents
|
||||
store = Store(str(tmp_path / "servicemap.db"))
|
||||
database = store.create_service(ServiceCreate(name="Database", kind=ServiceKind.TCP, host="db.local", port=5432))
|
||||
api = store.create_service(
|
||||
ServiceCreate(name="API", kind=ServiceKind.HTTP, url="https://api.local", depends_on=[database.id])
|
||||
)
|
||||
# Record the outage without the automatic reconciliation in save_probe.
|
||||
monkeypatch.setattr(incidents, "sync_incidents", lambda *_args, **_kwargs: [])
|
||||
for number, (service_id, status) in enumerate(
|
||||
[(database.id, HealthStatus.DOWN), (database.id, HealthStatus.DOWN), (api.id, HealthStatus.HEALTHY)]
|
||||
):
|
||||
store.save_probe(
|
||||
ProbeResult(
|
||||
id=f"probe-{number}",
|
||||
service_id=service_id,
|
||||
status=status,
|
||||
latency_ms=4.0,
|
||||
detail=status.value,
|
||||
checked_at=datetime.now(UTC) + timedelta(microseconds=number),
|
||||
)
|
||||
)
|
||||
monkeypatch.setattr(incidents, "sync_incidents", real_sync)
|
||||
assert store.list_incidents(active_only=True) == []
|
||||
|
||||
barrier = threading.Barrier(6)
|
||||
|
||||
def run() -> None:
|
||||
barrier.wait()
|
||||
real_sync(store)
|
||||
|
||||
threads = [threading.Thread(target=run) for _ in range(6)]
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
for thread in threads:
|
||||
thread.join()
|
||||
|
||||
assert len(store.list_incidents(active_only=True)) == 1
|
||||
opened = [event for event in store.list_events(100) if event.event_type == "incident.opened"]
|
||||
assert len(opened) == 1
|
||||
Reference in new issue
Block a user