From 9b334fe971977ceebd32b86eba4090d735df9937 Mon Sep 17 00:00:00 2001
From: Melissa
Date: Tue, 20 Jul 2021 23:28:04 -0400
Subject: [PATCH] mgr/cephadm: add test_offline, edit with_cephadm_module
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
---
src/pybind/mgr/cephadm/tests/fixtures.py | 11 +++++
src/pybind/mgr/cephadm/tests/test_cephadm.py | 43 +++++++++++---------
2 files changed, 35 insertions(+), 19 deletions(-)
diff --git a/src/pybind/mgr/cephadm/tests/fixtures.py b/src/pybind/mgr/cephadm/tests/fixtures.py
index 11983737bca..30314ed3816 100644
--- a/src/pybind/mgr/cephadm/tests/fixtures.py
+++ b/src/pybind/mgr/cephadm/tests/fixtures.py
@@ -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
diff --git a/src/pybind/mgr/cephadm/tests/test_cephadm.py b/src/pybind/mgr/cephadm/tests/test_cephadm.py
index e6082a8c778..125c4132b90 100644
--- a/src/pybind/mgr/cephadm/tests/test_cephadm.py
+++ b/src/pybind/mgr/cephadm/tests/test_cephadm.py
@@ -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):
--
2.39.5