]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm: rework with_cephadm_ctx fixture to use exitstack
authorJohn Mulligan <jmulligan@redhat.com>
Thu, 30 Jan 2025 00:04:29 +0000 (19:04 -0500)
committerJohn Mulligan <jmulligan@redhat.com>
Thu, 30 Jan 2025 00:35:46 +0000 (19:35 -0500)
Rework the `with_cephadm_ctx` function in fixtures.py to make use of
contextlib.ExitStack. The use of ExitStack, to quote the python docs,
"is designed to make it easy to programmatically combine other context
managers." This function makes use of many mock.patch context managers
and does already try to programmatically combine them - and the current
method is awkward.
A future change will do even more to control what gets mocked out and
so a this clean up makes things easier to read now and easier to update
later.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
src/cephadm/tests/fixtures.py

index 572c1f9969d66962673c098135d5a3a0244b9183..f770bcbf3fd0167daa543649ec5da19c861baf99 100644 (file)
@@ -1,3 +1,4 @@
+import contextlib
 import mock
 import os
 import pytest
@@ -163,25 +164,24 @@ def with_cephadm_ctx(
         hostname = 'host1'
 
     _cephadm = import_cephadm()
-    with mock.patch('cephadmlib.net_utils.attempt_bind'), \
-         mock.patch('cephadmlib.call_wrappers.call', return_value=('', '', 0)), \
-         mock.patch('cephadmlib.call_wrappers.call_timeout', return_value=0), \
-         mock.patch('cephadm.call', return_value=('', '', 0)), \
-         mock.patch('cephadm.call_timeout', return_value=0), \
-         mock.patch('cephadmlib.exe_utils.find_executable', return_value='foo'), \
-         mock.patch('cephadm.get_container_info', return_value=None), \
-         mock.patch('cephadm.is_available', return_value=True), \
-         mock.patch('cephadm.json_loads_retry', return_value={'epoch' : 1}), \
-         mock.patch('cephadm.logger'), \
-         mock.patch('cephadm.FileLock'), \
-         mock.patch('socket.gethostname', return_value=hostname):
+    with contextlib.ExitStack() as stack:
+        stack.enter_context(mock.patch('cephadmlib.net_utils.attempt_bind'))
+        stack.enter_context(mock.patch('cephadmlib.call_wrappers.call', return_value=('', '', 0)))
+        stack.enter_context(mock.patch('cephadmlib.call_wrappers.call_timeout', return_value=0))
+        stack.enter_context(mock.patch('cephadm.call', return_value=('', '', 0)))
+        stack.enter_context(mock.patch('cephadm.call_timeout', return_value=0))
+        stack.enter_context(mock.patch('cephadmlib.exe_utils.find_executable', return_value='foo'))
+        stack.enter_context(mock.patch('cephadm.get_container_info', return_value=None))
+        stack.enter_context(mock.patch('cephadm.is_available', return_value=True))
+        stack.enter_context(mock.patch('cephadm.json_loads_retry', return_value={'epoch' : 1}))
+        stack.enter_context(mock.patch('cephadm.logger'))
+        stack.enter_context(mock.patch('cephadm.FileLock'))
+        stack.enter_context(mock.patch('socket.gethostname', return_value=hostname))
+        if list_networks is not None:
+            stack.enter_context(mock.patch('cephadm.list_networks', return_value=list_networks))
         ctx: _cephadm.CephadmContext = _cephadm.cephadm_init_ctx(cmd)
         ctx.container_engine = mock_podman()
-        if list_networks is not None:
-            with mock.patch('cephadm.list_networks', return_value=list_networks):
-                yield ctx
-        else:
-            yield ctx
+        yield ctx
 
 
 @pytest.fixture()