From: John Mulligan Date: Thu, 30 Jan 2025 00:04:29 +0000 (-0500) Subject: cephadm: rework with_cephadm_ctx fixture to use exitstack X-Git-Tag: v20.0.0~245^2~6 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=52b573299cd8183a02c87e6c2618a4e5018593fb;p=ceph.git cephadm: rework with_cephadm_ctx fixture to use exitstack 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 --- diff --git a/src/cephadm/tests/fixtures.py b/src/cephadm/tests/fixtures.py index 572c1f9969d..f770bcbf3fd 100644 --- a/src/cephadm/tests/fixtures.py +++ b/src/cephadm/tests/fixtures.py @@ -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()