From cee53726907039d9b56396d2c5af6d5da17e37da Mon Sep 17 00:00:00 2001 From: Lukas Date: Tue, 14 Apr 2026 10:24:14 +0200 Subject: [PATCH] Add backend capability cache reset helper for jail service tests --- Docs/Tasks.md | 2 ++ backend/app/services/jail_service.py | 12 ++++++++++++ backend/tests/test_services/test_jail_service.py | 6 +++--- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Docs/Tasks.md b/Docs/Tasks.md index 957dd42..bfc6087 100644 --- a/Docs/Tasks.md +++ b/Docs/Tasks.md @@ -466,6 +466,8 @@ Imports for side effects are an anti-pattern that creates hidden startup depende ### Task 16 — Reset _backend_cmd_supported between test runs +**Status:** Completed + **Severity:** Medium **Where:** diff --git a/backend/app/services/jail_service.py b/backend/app/services/jail_service.py index b611eef..ac0b88b 100644 --- a/backend/app/services/jail_service.py +++ b/backend/app/services/jail_service.py @@ -248,6 +248,18 @@ async def _check_backend_cmd_supported( return _backend_cmd_supported +async def _reset_backend_capability_cache() -> None: + """Reset the cached backend/idle capability detection state. + + This helper is intended for test isolation and for any scenario where the + cached probe result must be invalidated before the next detection attempt. + """ + global _backend_cmd_supported + + async with _backend_cmd_lock: + _backend_cmd_supported = None + + # --------------------------------------------------------------------------- # Public API — Jail listing & detail # --------------------------------------------------------------------------- diff --git a/backend/tests/test_services/test_jail_service.py b/backend/tests/test_services/test_jail_service.py index aeb85c5..a300509 100644 --- a/backend/tests/test_services/test_jail_service.py +++ b/backend/tests/test_services/test_jail_service.py @@ -192,7 +192,7 @@ class TestListJails: fail2ban log. """ # Reset the capability cache to test detection. - jail_service._backend_cmd_supported = None + await jail_service._reset_backend_capability_cache() responses = { "status": _make_global_status("sshd"), @@ -217,7 +217,7 @@ class TestListJails: async def test_backend_idle_commands_supported(self) -> None: """list_jails detects and sends backend/idle commands when supported.""" # Reset the capability cache to test detection. - jail_service._backend_cmd_supported = None + await jail_service._reset_backend_capability_cache() responses = { "status": _make_global_status("sshd"), @@ -243,7 +243,7 @@ class TestListJails: async def test_backend_idle_commands_cached_after_first_probe(self) -> None: """list_jails caches capability result and reuses it across polling cycles.""" # Reset the capability cache. - jail_service._backend_cmd_supported = None + await jail_service._reset_backend_capability_cache() responses = { "status": _make_global_status("sshd, nginx"),