fix: handle socket close errors properly in PapertrailLogHandler
- Replace contextlib.suppress with try/except + warning log - Add test for fail2ban client - Remove stale Issue #21 from Tasks.md (indexes) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -214,6 +214,38 @@ class TestCoerceCommandToken:
|
||||
assert _coerce_command_token(None) == "None"
|
||||
|
||||
|
||||
class TestSendCommandSyncCloseError:
|
||||
"""Tests for socket close error handling in :func:`_send_command_sync`."""
|
||||
|
||||
def test_close_error_is_logged_but_not_reRaised(self) -> None:
|
||||
"""close() raising must log a warning, not mask the original error."""
|
||||
fake_client = MagicMock()
|
||||
fake_client.send.return_value = [0, "ok"]
|
||||
fake_client.close.side_effect = RuntimeError("close failed")
|
||||
fake_cls = MagicMock(return_value=fake_client)
|
||||
|
||||
with (
|
||||
patch(
|
||||
"app.utils.fail2ban_client._load_vendored_fail2ban_client",
|
||||
return_value=fake_cls,
|
||||
),
|
||||
patch("app.utils.fail2ban_client.log") as mock_log,
|
||||
):
|
||||
result = _send_command_sync(
|
||||
socket_path="/fake/fail2ban.sock",
|
||||
command=["status"],
|
||||
timeout=1.0,
|
||||
)
|
||||
|
||||
assert result == [0, "ok"]
|
||||
warning_calls = [
|
||||
c for c in mock_log.warning.call_args_list
|
||||
if c[0][0] == "fail2ban_socket_close_error"
|
||||
]
|
||||
assert len(warning_calls) == 1
|
||||
assert warning_calls[0][1]["error"] == "close failed"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Extended tests for Fail2BanClient.send
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user