Skip to content

Commit a553e89

Browse files
test(orphans): make confirm-endpoint test robust to settings-reference pollution
test_only_current_orphan_candidates_accepted passed in isolation but failed in the full suite: admin_router and endpoint_util bind 'settings' by reference at import, and in the full suite they were first-imported while another test had opi.core.config.settings replaced (mock_settings fixture) or reloaded (test_secret_key_failclosed). That left their module-level reference pointing at a stale mock, surfacing as a 501 (ADMIN_API_KEY unset) and then a MagicMock in the JSON response (DELETION_GRACE_PERIOD_DAYS). Re-point both module references to the live settings before patching the attributes the endpoint reads.
1 parent 23dfc87 commit a553e89

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

operations-manager/python/tests/test_service_orphan_sweep.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,24 @@ def _canned_report(self) -> dict:
187187

188188
@pytest.mark.asyncio
189189
async def test_only_current_orphan_candidates_accepted(self, monkeypatch) -> None:
190+
import opi.core.config
190191
from opi.api import admin_router as admin_module
192+
from opi.api import endpoint_util
191193
from opi.api.admin_router import confirm_orphans
192-
from opi.core.config import settings
193194

194-
monkeypatch.setattr(settings, "ADMIN_API_KEY", "test-admin-key")
195-
monkeypatch.setattr(settings, "CLUSTER_MANAGER", "odcn-production")
195+
# admin_router and endpoint_util both bind ``settings`` by reference at
196+
# import time. In the full suite they may have been first-imported while
197+
# another test had opi.core.config.settings patched (mock_settings) or
198+
# reloaded (test_secret_key_failclosed), leaving their module-level
199+
# reference pointing at a stale mock object -- which then surfaces as a
200+
# 501 (ADMIN_API_KEY) or a MagicMock in the JSON response
201+
# (DELETION_GRACE_PERIOD_DAYS). Re-point both to the live settings, then
202+
# patch the attributes the endpoint reads.
203+
real_settings = opi.core.config.settings
204+
monkeypatch.setattr(admin_module, "settings", real_settings)
205+
monkeypatch.setattr(endpoint_util, "settings", real_settings)
206+
monkeypatch.setattr(real_settings, "ADMIN_API_KEY", "test-admin-key")
207+
monkeypatch.setattr(real_settings, "CLUSTER_MANAGER", "odcn-production")
196208

197209
mock_request = AsyncMock()
198210
mock_request.headers = {"X-API-Key": "test-admin-key"}

0 commit comments

Comments
 (0)