]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/cephadm: add test_offline, edit with_cephadm_module
authorMelissa <li.melissa.kun@gmail.com>
Wed, 21 Jul 2021 03:28:04 +0000 (23:28 -0400)
committerMelissa Li <li.melissa.kun@gmail.com>
Fri, 20 Aug 2021 18:27:46 +0000 (14:27 -0400)
add `event_loop` and `tkey` object to with_cephadm_module, and create MockEventLoopThread in fixtures.py to test async functions of ssh.py.
rewrite test_offline to be compatible with asyncssh

Fixes: https://tracker.ceph.com/issues/44676
Signed-off-by: Melissa Li <li.melissa.kun@gmail.com>
src/pybind/mgr/cephadm/tests/fixtures.py
src/pybind/mgr/cephadm/tests/test_cephadm.py

index 11983737bcacf0fe2d48b15c781c51c4e7e7fb56..30314ed381667d8d730fa84d185d353c6119bffe 100644 (file)
@@ -1,4 +1,6 @@
 import fnmatch
+import asyncio
+from tempfile import NamedTemporaryFile
 from contextlib import contextmanager
 
 from ceph.deployment.service_spec import PlacementSpec, ServiceSpec
@@ -33,6 +35,11 @@ def match_glob(val, pat):
         assert pat in val
 
 
+class MockEventLoopThread:
+    def get_result(self, coro):
+        asyncio.run(coro)
+
+
 @contextmanager
 def with_cephadm_module(module_options=None, store=None):
     """
@@ -61,6 +68,10 @@ def with_cephadm_module(module_options=None, store=None):
 
         m.__init__('cephadm', 0, 0)
         m._cluster_fsid = "fsid"
+
+        m.event_loop = MockEventLoopThread()
+        m.tkey = NamedTemporaryFile(prefix='test-cephadm-identity-')
+
         yield m
 
 
index e6082a8c778d78b96856b309cd174e70628135e1..125c4132b90c966494b07c1b309cc15aa403d2a6 100644 (file)
@@ -1,4 +1,5 @@
 import json
+from unittest.mock import AsyncMock
 from contextlib import contextmanager
 
 import pytest
@@ -14,7 +15,7 @@ try:
 except ImportError:
     pass
 
-from execnet.gateway_bootstrap import HostNotFound
+from asyncssh.misc import ConnectionLost
 
 from ceph.deployment.service_spec import ServiceSpec, PlacementSpec, RGWSpec, \
     NFSServiceSpec, IscsiServiceSpec, HostPlacementSpec, CustomContainerSpec
@@ -1076,24 +1077,28 @@ spec:
             assert_rm_daemon(cephadm_module, spec.service_name(), 'host1')  # verifies ok-to-stop
             assert_rm_daemon(cephadm_module, spec.service_name(), 'host2')
 
-    @mock.patch("cephadm.module.CephadmOrchestrator._get_connection")
-    @mock.patch("remoto.process.check")
-    def test_offline(self, _check, _get_connection, cephadm_module):
-        _check.return_value = '{}', '', 0
-        _get_connection.return_value = mock.Mock(), mock.Mock()
-        with with_host(cephadm_module, 'test'):
-            _get_connection.side_effect = HostNotFound
-            code, out, err = cephadm_module.check_host('test')
-            assert out == ''
-            assert "Host 'test' not found" in err
-
-            out = wait(cephadm_module, cephadm_module.get_hosts())[0].to_json()
-            assert out == HostSpec('test', '1.2.3.4', status='Offline').to_json()
-
-            _get_connection.side_effect = None
-            assert CephadmServe(cephadm_module)._check_host('test') is None
-            out = wait(cephadm_module, cephadm_module.get_hosts())[0].to_json()
-            assert out == HostSpec('test', '1.2.3.4').to_json()
+    @mock.patch("cephadm.ssh.SSHManager.execute_command")
+    @mock.patch("cephadm.ssh.SSHManager.check_execute_command")
+    def test_offline(self, check_execute_command, execute_command, cephadm_module):
+        check_execute_command.return_value = ''
+        execute_command.return_value = '', '', 0
+
+        mock_connect = AsyncMock(return_value='')
+        with mock.patch("asyncssh.connect", new=mock_connect) as asyncssh_connect:
+            with with_host(cephadm_module, 'test'):
+                asyncssh_connect.side_effect = ConnectionLost('reason')
+                code, out, err = cephadm_module.check_host('test')
+                assert out == ''
+                assert "Host 'test' not found" in err
+
+                out = wait(cephadm_module, cephadm_module.get_hosts())[0].to_json()
+                assert out == HostSpec('test', '1.2.3.4', status='Offline').to_json()
+
+                asyncssh_connect.return_value = mock.MagicMock()
+                asyncssh_connect.side_effect = None
+                assert CephadmServe(cephadm_module)._check_host('test') is None
+                out = wait(cephadm_module, cephadm_module.get_hosts())[0].to_json()
+                assert out == HostSpec('test', '1.2.3.4').to_json()
 
     @mock.patch("cephadm.serve.CephadmServe._run_cephadm", _run_cephadm('{}'))
     def test_dont_touch_offline_or_maintenance_host_daemons(self, cephadm_module):