Extract health-check crash-detection logic into runtime state helper

This commit is contained in:
2026-04-17 16:58:24 +02:00
parent 1e2850a34e
commit 7a1cb0c46c
5 changed files with 122 additions and 69 deletions

View File

@@ -1,8 +1,11 @@
from __future__ import annotations
import datetime
from unittest.mock import MagicMock
from app.config import Settings
from app.models.config import PendingRecovery
from app.models.server import ServerStatus
from app.utils.runtime_state import get_app_settings, get_effective_settings
@@ -45,3 +48,40 @@ def test_get_app_settings_reads_bootstrap_settings() -> None:
app = _FakeApp(_FakeState(settings=settings))
assert get_app_settings(app) is settings
def test_process_health_probe_result_creates_pending_recovery_within_window() -> None:
from app.utils.runtime_state import RuntimeState, process_health_probe_result
now = datetime.datetime.now(tz=datetime.UTC)
runtime_state = RuntimeState(
server_status=ServerStatus(online=True),
last_activation={"jail_name": "sshd", "at": now - datetime.timedelta(seconds=30)},
pending_recovery=None,
)
process_health_probe_result(runtime_state, ServerStatus(online=False), now=now)
assert runtime_state.pending_recovery is not None
assert runtime_state.pending_recovery.jail_name == "sshd"
assert runtime_state.pending_recovery.recovered is False
def test_process_health_probe_result_resolves_existing_pending_recovery() -> None:
from app.utils.runtime_state import RuntimeState, process_health_probe_result
activated_at = datetime.datetime.now(tz=datetime.UTC) - datetime.timedelta(seconds=30)
runtime_state = RuntimeState(
server_status=ServerStatus(online=False),
pending_recovery=PendingRecovery(
jail_name="sshd",
activated_at=activated_at,
detected_at=activated_at + datetime.timedelta(seconds=10),
recovered=False,
),
)
process_health_probe_result(runtime_state, ServerStatus(online=True), now=activated_at + datetime.timedelta(seconds=20))
assert runtime_state.pending_recovery is not None
assert runtime_state.pending_recovery.recovered is True