Extract fail2ban restart orchestration into jail_service
This commit is contained in:
@@ -8,12 +8,12 @@ from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from app.exceptions import Fail2BanConnectionError
|
||||
from app.models.ban import ActiveBanListResponse, JailBannedIpsResponse
|
||||
from app.models.geo import GeoDetail, GeoInfo
|
||||
from app.models.jail import JailDetailResponse, JailListResponse
|
||||
from app.services import jail_service
|
||||
from app.services.jail_service import JailNotFoundError, JailOperationError
|
||||
from app.exceptions import Fail2BanConnectionError
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Helpers
|
||||
@@ -492,6 +492,47 @@ class TestJailControls:
|
||||
):
|
||||
await jail_service.restart(_SOCKET)
|
||||
|
||||
async def test_restart_daemon_returns_true_on_success(self) -> None:
|
||||
"""restart_daemon returns True when stop, start, and probe all succeed."""
|
||||
with (
|
||||
patch("app.services.jail_service.restart", AsyncMock(return_value=None)),
|
||||
patch("app.services.jail_service.start_daemon", AsyncMock(return_value=True)),
|
||||
patch("app.services.jail_service.wait_for_fail2ban", AsyncMock(return_value=True)),
|
||||
):
|
||||
result = await jail_service.restart_daemon(
|
||||
_SOCKET,
|
||||
["fail2ban-client", "start"],
|
||||
)
|
||||
|
||||
assert result is True
|
||||
|
||||
async def test_restart_daemon_returns_false_when_start_fails(self) -> None:
|
||||
"""restart_daemon returns False when the configured start command fails."""
|
||||
with (
|
||||
patch("app.services.jail_service.restart", AsyncMock(return_value=None)),
|
||||
patch("app.services.jail_service.start_daemon", AsyncMock(return_value=False)),
|
||||
):
|
||||
result = await jail_service.restart_daemon(
|
||||
_SOCKET,
|
||||
["fail2ban-client", "start"],
|
||||
)
|
||||
|
||||
assert result is False
|
||||
|
||||
async def test_restart_daemon_returns_false_when_wait_fails(self) -> None:
|
||||
"""restart_daemon returns False when fail2ban does not become responsive."""
|
||||
with (
|
||||
patch("app.services.jail_service.restart", AsyncMock(return_value=None)),
|
||||
patch("app.services.jail_service.start_daemon", AsyncMock(return_value=True)),
|
||||
patch("app.services.jail_service.wait_for_fail2ban", AsyncMock(return_value=False)),
|
||||
):
|
||||
result = await jail_service.restart_daemon(
|
||||
_SOCKET,
|
||||
["fail2ban-client", "start"],
|
||||
)
|
||||
|
||||
assert result is False
|
||||
|
||||
async def test_start_not_found_raises(self) -> None:
|
||||
"""start_jail raises JailNotFoundError for unknown jail."""
|
||||
with _patch_client({"start|ghost": (1, Exception("Unknown jail: 'ghost'"))}), pytest.raises(JailNotFoundError):
|
||||
|
||||
Reference in New Issue
Block a user