From 6fd599bd4d66c290ba94accd76c869a50a9dd5ce Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Wed, 29 Jul 2020 14:09:00 +0200 Subject: [PATCH] mgr/cephadm/tests: Split `test_daemon_action` Into: * `test_list_daemons` * `test_daemon_action` (now without listing daemons) Signed-off-by: Sebastian Wagner --- src/pybind/mgr/cephadm/tests/test_cephadm.py | 67 +++++++++++++------- 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/src/pybind/mgr/cephadm/tests/test_cephadm.py b/src/pybind/mgr/cephadm/tests/test_cephadm.py index dc60f199656b5..3cd193ea0872f 100644 --- a/src/pybind/mgr/cephadm/tests/test_cephadm.py +++ b/src/pybind/mgr/cephadm/tests/test_cephadm.py @@ -48,6 +48,25 @@ def assert_rm_daemon(cephadm: CephadmOrchestrator, prefix, host): match_glob(out, f"Removed {d_names}* from host '{host}'") +@contextmanager +def with_daemon(cephadm_module: CephadmOrchestrator, spec: ServiceSpec, meth, host: str): + spec.placement = PlacementSpec(hosts=[host], count=1) + + c = meth(cephadm_module, spec) + [out] = wait(cephadm_module, c) + match_glob(out, f"Deployed {spec.service_name()}.* on host '{host}'") + + dds = cephadm_module.cache.get_daemons_by_service(spec.service_name()) + for dd in dds: + if dd.hostname == host: + yield dd.daemon_id + assert_rm_daemon(cephadm_module, spec.service_name(), host) + return + + assert False, 'Daemon not found' + + + class TestCephadm(object): def test_get_unique_name(self, cephadm_module): @@ -164,30 +183,35 @@ class TestCephadm(object): ) ]) )) - def test_daemon_action(self, cephadm_module: CephadmOrchestrator): - + def test_list_daemons(self, cephadm_module: CephadmOrchestrator): cephadm_module.service_cache_timeout = 10 with with_host(cephadm_module, 'test'): c = cephadm_module.list_daemons(refresh=True) - wait(cephadm_module, c) - assert len(c.result) == 1 - c = cephadm_module.daemon_action('redeploy', 'rgw', 'myrgw.foobar') - assert wait(cephadm_module, c) == ["Deployed rgw.myrgw.foobar on host 'test'"] + assert wait(cephadm_module, c)[0].name() == 'rgw.myrgw.foobar' - for what in ('start', 'stop', 'restart'): - c = cephadm_module.daemon_action(what, 'rgw', 'myrgw.foobar') - assert wait(cephadm_module, c) == [what + " rgw.myrgw.foobar from host 'test'"] + @mock.patch("cephadm.module.CephadmOrchestrator._run_cephadm", _run_cephadm('[]')) + @mock.patch("cephadm.services.cephadmservice.RgwService.create_realm_zonegroup_zone", lambda _,__,___: None) + def test_daemon_action(self, cephadm_module: CephadmOrchestrator): + cephadm_module.service_cache_timeout = 10 + with with_host(cephadm_module, 'test'): + with with_daemon(cephadm_module, RGWSpec(service_id='myrgw.foobar'), CephadmOrchestrator.add_rgw, 'test') as daemon_id: - # Make sure, _check_daemons does a redeploy due to monmap change: - cephadm_module._store['_ceph_get/mon_map'] = { - 'modified': datetime.datetime.utcnow().strftime(CEPH_DATEFMT), - 'fsid': 'foobar', - } - cephadm_module.notify('mon_map', None) + c = cephadm_module.daemon_action('redeploy', 'rgw', daemon_id) + assert wait(cephadm_module, c) == [f"Deployed rgw.{daemon_id} on host 'test'"] + + for what in ('start', 'stop', 'restart'): + c = cephadm_module.daemon_action(what, 'rgw', daemon_id) + assert wait(cephadm_module, c) == [what + f" rgw.{daemon_id} from host 'test'"] - cephadm_module._check_daemons() + # Make sure, _check_daemons does a redeploy due to monmap change: + cephadm_module._store['_ceph_get/mon_map'] = { + 'modified': datetime.datetime.utcnow().strftime(CEPH_DATEFMT), + 'fsid': 'foobar', + } + cephadm_module.notify('mon_map', None) + + cephadm_module._check_daemons() - assert_rm_daemon(cephadm_module, 'rgw.myrgw.foobar', 'test') @mock.patch("cephadm.module.CephadmOrchestrator._run_cephadm", _run_cephadm('[]')) def test_mon_add(self, cephadm_module): @@ -442,13 +466,8 @@ class TestCephadm(object): @mock.patch("cephadm.services.cephadmservice.RgwService.create_realm_zonegroup_zone", lambda _,__,___: None) def test_daemon_add(self, spec: ServiceSpec, meth, cephadm_module): with with_host(cephadm_module, 'test'): - spec.placement = PlacementSpec(hosts=['test'], count=1) - - c = meth(cephadm_module, spec) - [out] = wait(cephadm_module, c) - match_glob(out, f"Deployed {spec.service_name()}.* on host 'test'") - - assert_rm_daemon(cephadm_module, spec.service_name(), 'test') + with with_daemon(cephadm_module, spec, meth, 'test'): + pass @mock.patch("cephadm.module.CephadmOrchestrator._run_cephadm", _run_cephadm('{}')) @mock.patch("cephadm.module.CephadmOrchestrator.rados", mock.MagicMock()) -- 2.39.5