From: Michael Fritch Date: Mon, 24 May 2021 13:53:04 +0000 (-0600) Subject: cephadm: add cephadm ContextManager X-Git-Tag: v16.2.5~72^2~9 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=d68abab8327c71ea60959ad342a84636653a78f5;p=ceph.git cephadm: add cephadm ContextManager to mock the CephadmContext during unit tests Signed-off-by: Michael Fritch (cherry picked from commit 3e714ef1dcd4d30816932fdb35e8abcde773dca8) --- diff --git a/src/cephadm/tests/fixtures.py b/src/cephadm/tests/fixtures.py index ab5dcab652d3..ec4244abede8 100644 --- a/src/cephadm/tests/fixtures.py +++ b/src/cephadm/tests/fixtures.py @@ -3,7 +3,9 @@ import os import pytest import time +from contextlib import contextmanager from pyfakefs import fake_filesystem +from typing import Dict, List, Optional with mock.patch('builtins.open', create=True): @@ -62,3 +64,26 @@ def cephadm_fs( fs.create_dir(cd.UNIT_DIR) yield fs + + +@contextmanager +def with_cephadm_ctx( + cmd: List[str], + list_networks: Optional[Dict[str,Dict[str,List[str]]]] = None +): + """ + :param cmd: cephadm command argv + :param list_networks: mock 'list-networks' return + """ + if not list_networks: + list_networks = {} + + with mock.patch('cephadm.get_parm'), \ + mock.patch('cephadm.attempt_bind'), \ + mock.patch('cephadm.call', return_value=('', '', 0)), \ + mock.patch('cephadm.find_executable', return_value='foo'), \ + mock.patch('cephadm.is_available', return_value=True), \ + mock.patch('cephadm.json_loads_retry', return_value={'epoch' : 1}), \ + mock.patch('cephadm.list_networks', return_value=list_networks): + ctx: cd.CephadmContext = cd.cephadm_init_ctx(cmd) + yield ctx