Extract health-check crash-detection logic into runtime state helper
This commit is contained in:
@@ -77,7 +77,7 @@ class TestRunProbe:
|
||||
"app.tasks.health_check.health_service.probe",
|
||||
new_callable=AsyncMock,
|
||||
return_value=new_status,
|
||||
), patch("app.tasks.health_check.log") as mock_log:
|
||||
), patch("app.utils.runtime_state.log") as mock_log:
|
||||
await _run_probe(app)
|
||||
|
||||
online_calls = [c for c in mock_log.info.call_args_list if c[0][0] == "fail2ban_came_online"]
|
||||
@@ -93,7 +93,7 @@ class TestRunProbe:
|
||||
"app.tasks.health_check.health_service.probe",
|
||||
new_callable=AsyncMock,
|
||||
return_value=new_status,
|
||||
), patch("app.tasks.health_check.log") as mock_log:
|
||||
), patch("app.utils.runtime_state.log") as mock_log:
|
||||
await _run_probe(app)
|
||||
|
||||
offline_calls = [c for c in mock_log.warning.call_args_list if c[0][0] == "fail2ban_went_offline"]
|
||||
@@ -109,7 +109,7 @@ class TestRunProbe:
|
||||
"app.tasks.health_check.health_service.probe",
|
||||
new_callable=AsyncMock,
|
||||
return_value=new_status,
|
||||
), patch("app.tasks.health_check.log") as mock_log:
|
||||
), patch("app.utils.runtime_state.log") as mock_log:
|
||||
await _run_probe(app)
|
||||
|
||||
transition_calls = [
|
||||
@@ -134,7 +134,7 @@ class TestRunProbe:
|
||||
"app.tasks.health_check.health_service.probe",
|
||||
new_callable=AsyncMock,
|
||||
return_value=new_status,
|
||||
), patch("app.tasks.health_check.log") as mock_log:
|
||||
), patch("app.utils.runtime_state.log") as mock_log:
|
||||
await _run_probe(app)
|
||||
|
||||
transition_calls = [
|
||||
@@ -180,7 +180,7 @@ class TestRunProbe:
|
||||
new_callable=AsyncMock,
|
||||
return_value=new_status,
|
||||
),
|
||||
patch("app.tasks.health_check.log"),
|
||||
patch("app.utils.runtime_state.log"),
|
||||
):
|
||||
# Must not raise even with no prior status.
|
||||
await _run_probe(app)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user