From: Michael Fritch Date: Mon, 28 Jun 2021 17:48:06 +0000 (-0600) Subject: cephadm: add `infer_config` unit test X-Git-Tag: v17.1.0~1355^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=53d07362ff8efa5846fd6067b469923457d5ac8d;p=ceph.git cephadm: add `infer_config` unit test Signed-off-by: Michael Fritch --- diff --git a/src/cephadm/cephadm b/src/cephadm/cephadm index 34a3a9bc15bb8..dba214e3e76f6 100755 --- a/src/cephadm/cephadm +++ b/src/cephadm/cephadm @@ -1697,11 +1697,11 @@ def infer_config(func): logger.debug('Using specified config: %s' % ctx.config) return func(ctx) if ctx.fsid: - name = ctx.name + name = ctx.name if 'name' in ctx else None if not name: daemon_list = list_daemons(ctx, detail=False) for daemon in daemon_list: - if daemon['name'].startswith('mon.'): + if daemon.get('name', '').startswith('mon.'): name = daemon['name'] break if name: diff --git a/src/cephadm/tests/test_cephadm.py b/src/cephadm/tests/test_cephadm.py index f8d4f90d4f6ae..e9ac4cb7611f7 100644 --- a/src/cephadm/tests/test_cephadm.py +++ b/src/cephadm/tests/test_cephadm.py @@ -649,6 +649,86 @@ docker.io/ceph/daemon-base:octopus infer_fsid(ctx) assert ctx.fsid == result + @pytest.mark.parametrize('fsid, config, name, list_daemons, result, ', + [ + ( + None, + '/foo/bar.conf', + None, + [], + '/foo/bar.conf', + ), + ( + '00000000-0000-0000-0000-0000deadbeef', + None, + None, + [], + cd.SHELL_DEFAULT_CONF, + ), + ( + '00000000-0000-0000-0000-0000deadbeef', + None, + None, + [{'name': 'mon.a'}], + '/var/lib/ceph/00000000-0000-0000-0000-0000deadbeef/mon.a/config', + ), + ( + '00000000-0000-0000-0000-0000deadbeef', + None, + None, + [{'name': 'osd.0'}], + cd.SHELL_DEFAULT_CONF, + ), + ( + '00000000-0000-0000-0000-0000deadbeef', + '/foo/bar.conf', + 'mon.a', + [{'name': 'mon.a'}], + '/foo/bar.conf', + ), + ( + '00000000-0000-0000-0000-0000deadbeef', + None, + 'mon.a', + [], + '/var/lib/ceph/00000000-0000-0000-0000-0000deadbeef/mon.a/config', + ), + ( + '00000000-0000-0000-0000-0000deadbeef', + None, + 'osd.0', + [], + '/var/lib/ceph/00000000-0000-0000-0000-0000deadbeef/osd.0/config', + ), + ( + None, + None, + None, + [], + cd.SHELL_DEFAULT_CONF, + ), + ]) + @mock.patch('cephadm.call') + def test_infer_config(self, _call, fsid, config, name, list_daemons, result, cephadm_fs): + # build the context + ctx = cd.CephadmContext() + ctx.fsid = fsid + ctx.config = config + ctx.name = name + + # mock the decorator + mock_fn = mock.Mock() + mock_fn.return_value = 0 + infer_config = cd.infer_config(mock_fn) + + # mock the shell config + cephadm_fs.create_file(cd.SHELL_DEFAULT_CONF) + + # test + with mock.patch('cephadm.list_daemons', return_value=list_daemons): + infer_config(ctx) + assert ctx.config == result + class TestCustomContainer(unittest.TestCase): cc: cd.CustomContainer