From f500b8fc747f1767a31e54e6fb4a9a596889d1a1 Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Wed, 29 Jul 2020 14:23:06 +0200 Subject: [PATCH] mgr/cephadm: Add test_daemon_action_fail Signed-off-by: Sebastian Wagner --- src/pybind/mgr/cephadm/inventory.py | 4 ++-- src/pybind/mgr/cephadm/tests/fixtures.py | 11 +++------ src/pybind/mgr/cephadm/tests/test_cephadm.py | 24 +++++++++++++++++++- src/pybind/mgr/tests/__init__.py | 2 ++ 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/pybind/mgr/cephadm/inventory.py b/src/pybind/mgr/cephadm/inventory.py index 10eccf769c2..9b6805136df 100644 --- a/src/pybind/mgr/cephadm/inventory.py +++ b/src/pybind/mgr/cephadm/inventory.py @@ -554,8 +554,8 @@ class EventStore(): for k_s in unknowns: del self.events[k_s] - def get_for_service(self, name): + def get_for_service(self, name) -> List[OrchestratorEvent]: return self.events.get('service:' + name, []) - def get_for_daemon(self, name): + def get_for_daemon(self, name) -> List[OrchestratorEvent]: return self.events.get('daemon:' + name, []) diff --git a/src/pybind/mgr/cephadm/tests/fixtures.py b/src/pybind/mgr/cephadm/tests/fixtures.py index a14917595eb..05683aa2376 100644 --- a/src/pybind/mgr/cephadm/tests/fixtures.py +++ b/src/pybind/mgr/cephadm/tests/fixtures.py @@ -33,9 +33,6 @@ def match_glob(val, pat): assert pat in val -def mon_command(*args, **kwargs): - return 0, '', '' - @contextmanager def with_cephadm_module(module_options=None, store=None): """ @@ -43,11 +40,9 @@ def with_cephadm_module(module_options=None, store=None): :param store: Set the store before module.__init__ is called """ with mock.patch("cephadm.module.CephadmOrchestrator.get_ceph_option", get_ceph_option),\ - mock.patch("cephadm.module.CephadmOrchestrator.remote"), \ - mock.patch("cephadm.services.osd.RemoveUtil._run_mon_cmd"), \ - mock.patch("cephadm.module.CephadmOrchestrator.send_command"), \ - mock.patch("cephadm.module.CephadmOrchestrator.get_osdmap"), \ - mock.patch("cephadm.module.CephadmOrchestrator.mon_command", mon_command): + mock.patch("cephadm.services.osd.RemoveUtil._run_mon_cmd"), \ + mock.patch("cephadm.module.CephadmOrchestrator.get_osdmap"), \ + mock.patch("cephadm.module.CephadmOrchestrator.remote"): m = CephadmOrchestrator.__new__ (CephadmOrchestrator) if module_options is not None: diff --git a/src/pybind/mgr/cephadm/tests/test_cephadm.py b/src/pybind/mgr/cephadm/tests/test_cephadm.py index 3cd193ea087..4d82cea225d 100644 --- a/src/pybind/mgr/cephadm/tests/test_cephadm.py +++ b/src/pybind/mgr/cephadm/tests/test_cephadm.py @@ -22,7 +22,7 @@ from ceph.deployment.inventory import Devices, Device from orchestrator import ServiceDescription, DaemonDescription, InventoryHost, \ HostSpec, OrchestratorError from tests import mock -from .fixtures import cephadm_module, wait, _run_cephadm, mon_command, match_glob, with_host, \ +from .fixtures import cephadm_module, wait, _run_cephadm, match_glob, with_host, \ with_cephadm_module from cephadm.module import CephadmOrchestrator, CEPH_DATEFMT @@ -212,6 +212,28 @@ class TestCephadm(object): cephadm_module._check_daemons() + @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_fail(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: + with mock.patch('ceph_module.BaseMgrModule._ceph_send_command') as _ceph_send_command: + + _ceph_send_command.side_effect = Exception("myerror") + + # 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() + + evs = [e.message for e in cephadm_module.events.get_for_daemon(f'rgw.{daemon_id}')] + + assert 'myerror' in ''.join(evs) @mock.patch("cephadm.module.CephadmOrchestrator._run_cephadm", _run_cephadm('[]')) def test_mon_add(self, cephadm_module): diff --git a/src/pybind/mgr/tests/__init__.py b/src/pybind/mgr/tests/__init__.py index 6ca9315150c..a6f8cc0ff91 100644 --- a/src/pybind/mgr/tests/__init__.py +++ b/src/pybind/mgr/tests/__init__.py @@ -70,6 +70,8 @@ if 'UNITTEST' in os.environ: self._store = {} return self._store.get(f'_ceph_get/{data_name}', mock.MagicMock()) + def _ceph_send_command(self, ev, *args): + ev.complete(0, '', '') def __init__(self, *args): if not hasattr(self, '_store'): -- 2.39.5