From 1fcb08cc6b8bc40e5d0eea34af0234c51acefddb Mon Sep 17 00:00:00 2001 From: xin liang Date: Wed, 9 Aug 2023 16:16:26 +0800 Subject: [PATCH] Dev: unittest: Adjust unit test for previous change --- test/unittests/test_ui_cluster.py | 107 ++++++++++++++++++++---------- test/unittests/test_utils.py | 18 ++--- 2 files changed, 82 insertions(+), 43 deletions(-) diff --git a/test/unittests/test_ui_cluster.py b/test/unittests/test_ui_cluster.py index eb673deb54..da286ebd36 100644 --- a/test/unittests/test_ui_cluster.py +++ b/test/unittests/test_ui_cluster.py @@ -80,52 +80,91 @@ def test_do_start(self, mock_parse_nodes, mock_active, mock_start, mock_qdevice_ mock_qdevice_configured.assert_called_once_with() mock_info.assert_called_once_with("The cluster stack started on node1") - @mock.patch('logging.Logger.info') - @mock.patch('crmsh.utils.service_is_active') + @mock.patch('crmsh.ui_cluster.Cluster._wait_for_dc') + @mock.patch('crmsh.ui_cluster.Cluster._node_ready_to_stop_cluster_service') @mock.patch('crmsh.ui_cluster.parse_option_for_nodes') - def test_do_stop_already_stopped(self, mock_parse_nodes, mock_active, mock_info): + def test_do_stop_return(self, mock_parse_nodes, mock_node_ready_to_stop_cluster_service, mock_dc): + mock_parse_nodes.return_value = ["node1", "node2"] + mock_node_ready_to_stop_cluster_service.side_effect = [False, False] + context_inst = mock.Mock() - mock_parse_nodes.return_value = ["node1"] - mock_active.side_effect = [False, False] - self.ui_cluster_inst.do_stop(context_inst, "node1") - mock_active.assert_has_calls([ - mock.call("corosync.service", remote_addr="node1"), - mock.call("sbd.service", remote_addr="node1") - ]) - mock_info.assert_called_once_with("The cluster stack already stopped on node1") + self.ui_cluster_inst.do_stop(context_inst, "node1", "node2") + + mock_parse_nodes.assert_called_once_with(context_inst, "node1", "node2") + mock_node_ready_to_stop_cluster_service.assert_has_calls([mock.call("node1"), mock.call("node2")]) + mock_dc.assert_not_called() @mock.patch('logging.Logger.debug') @mock.patch('logging.Logger.info') - @mock.patch('crmsh.utils.stop_service') - @mock.patch('crmsh.utils.set_dlm_option') - @mock.patch('crmsh.utils.is_quorate') - @mock.patch('crmsh.utils.is_dlm_running') - @mock.patch('crmsh.utils.get_dc') - @mock.patch('crmsh.utils.check_function_with_timeout') - @mock.patch('crmsh.utils.get_property') @mock.patch('crmsh.utils.service_is_active') + @mock.patch('crmsh.utils.stop_service') + @mock.patch('crmsh.ui_cluster.Cluster._set_dlm') + @mock.patch('crmsh.ui_cluster.Cluster._wait_for_dc') + @mock.patch('crmsh.ui_cluster.Cluster._node_ready_to_stop_cluster_service') @mock.patch('crmsh.ui_cluster.parse_option_for_nodes') - def test_do_stop(self, mock_parse_nodes, mock_active, mock_get_property, mock_check, mock_get_dc, mock_dlm_running, mock_is_quorate, mock_set_dlm, mock_stop, mock_info, mock_debug): + def test_do_stop(self, mock_parse_nodes, mock_node_ready_to_stop_cluster_service, mock_dc, + mock_set_dlm, mock_stop, mock_is_active, mock_info, mock_debug): + mock_parse_nodes.return_value = ["node1", "node2"] + mock_node_ready_to_stop_cluster_service.side_effect = [True, False] + mock_stop.side_effect = [["node1"], ["node1"], ["node1"]] + mock_is_active.return_value = True + context_inst = mock.Mock() - mock_stop.side_effect = [["node1"], ["ndoe1"], ["node1"]] - mock_parse_nodes.return_value = ["node1"] - mock_active.side_effect = [True, True, True] - mock_dlm_running.return_value = True - mock_is_quorate.return_value = False - mock_get_property.return_value = "20s" + self.ui_cluster_inst.do_stop(context_inst, "node1", "node2") - self.ui_cluster_inst.do_stop(context_inst, "node1") + mock_parse_nodes.assert_called_once_with(context_inst, "node1", "node2") + mock_node_ready_to_stop_cluster_service.assert_has_calls([mock.call("node1"), mock.call("node2")]) + mock_debug.assert_called_once_with("stop node list: ['node1']") + mock_dc.assert_called_once_with("node1") + mock_set_dlm.assert_called_once_with("node1") + mock_stop.assert_has_calls([ + mock.call("pacemaker", node_list=["node1"]), + mock.call("corosync-qdevice.service", node_list=["node1"]), + mock.call("corosync", node_list=["node1"]), + ]) + mock_info.assert_called_once_with("The cluster stack stopped on node1") - mock_active.assert_has_calls([ + @mock.patch('logging.Logger.info') + @mock.patch('crmsh.utils.stop_service') + @mock.patch('crmsh.utils.service_is_active') + def test__node_ready_to_stop_cluster_service_corosync(self, mock_is_active, mock_stop, mock_info): + mock_is_active.side_effect = [False, True, False] + res = self.ui_cluster_inst._node_ready_to_stop_cluster_service("node1") + assert res is False + mock_is_active.assert_has_calls([ mock.call("corosync.service", remote_addr="node1"), + mock.call("sbd.service", remote_addr="node1"), mock.call("pacemaker.service", remote_addr="node1"), - mock.call("corosync-qdevice.service") ]) - mock_stop.assert_has_calls([ - mock.call("pacemaker", node_list=["node1"]), - mock.call("corosync-qdevice.service", node_list=["node1"]), - mock.call("corosync", node_list=["node1"]) + mock_stop.assert_called_once_with("corosync", remote_addr="node1") + mock_info.assert_called_once_with("The cluster stack stopped on node1") + + @mock.patch('logging.Logger.info') + @mock.patch('crmsh.utils.stop_service') + @mock.patch('crmsh.utils.service_is_active') + def test__node_ready_to_stop_cluster_service_pacemaker(self, mock_is_active, mock_stop, mock_info): + mock_is_active.side_effect = [True, True, False] + res = self.ui_cluster_inst._node_ready_to_stop_cluster_service("node1") + assert res is False + mock_is_active.assert_has_calls([ + mock.call("corosync.service", remote_addr="node1"), + mock.call("sbd.service", remote_addr="node1"), + mock.call("pacemaker.service", remote_addr="node1"), ]) + mock_stop.assert_called_once_with("corosync", remote_addr="node1") mock_info.assert_called_once_with("The cluster stack stopped on node1") - mock_debug.assert_called_once_with("Quorum is lost; Set enable_quorum_fencing=0 and enable_quorum_lockspace=0 for dlm") - mock_check.assert_called_once_with(mock_get_dc, wait_timeout=25) + + @mock.patch('logging.Logger.info') + @mock.patch('crmsh.utils.stop_service') + @mock.patch('crmsh.utils.service_is_active') + def test__node_ready_to_stop_cluster_service(self, mock_is_active, mock_stop, mock_info): + mock_is_active.side_effect = [True, True, True] + res = self.ui_cluster_inst._node_ready_to_stop_cluster_service("node1") + assert res is True + mock_is_active.assert_has_calls([ + mock.call("corosync.service", remote_addr="node1"), + mock.call("sbd.service", remote_addr="node1"), + mock.call("pacemaker.service", remote_addr="node1"), + ]) + mock_info.assert_not_called() + mock_stop.assert_not_called() diff --git a/test/unittests/test_utils.py b/test/unittests/test_utils.py index ba9f51a0c5..e468e2e16b 100644 --- a/test/unittests/test_utils.py +++ b/test/unittests/test_utils.py @@ -1570,7 +1570,7 @@ def test_get_dlm_option_dict(mock_run): "key1": "value1", "key2": "value2" } - mock_run.assert_called_once_with("dlm_tool dump_config") + mock_run.assert_called_once_with("dlm_tool dump_config", remote=None) @mock.patch('crmsh.utils.get_dlm_option_dict') @@ -1592,14 +1592,14 @@ def test_set_dlm_option(mock_get_dict, mock_run): "key2": "value2" } utils.set_dlm_option(key2="test") - mock_run.assert_called_once_with('dlm_tool set_config "key2=test"') + mock_run.assert_called_once_with('dlm_tool set_config "key2=test"', remote=None) -@mock.patch('crmsh.xmlutil.CrmMonXmlParser.is_resource_configured') +@mock.patch('crmsh.utils.has_resource_configured') def test_is_dlm_configured(mock_configured): mock_configured.return_value = True assert utils.is_dlm_configured() is True - mock_configured.assert_called_once_with(constants.DLM_CONTROLD_RA) + mock_configured.assert_called_once_with(constants.DLM_CONTROLD_RA, peer=None) @mock.patch('crmsh.utils.get_stdout_or_raise_error') @@ -1608,7 +1608,7 @@ def test_is_quorate_exception(mock_run): with pytest.raises(ValueError) as err: utils.is_quorate() assert str(err.value) == "Failed to get quorate status from corosync-quorumtool" - mock_run.assert_called_once_with("corosync-quorumtool -s", success_val_list=[0, 2]) + mock_run.assert_called_once_with("corosync-quorumtool -s", remote=None, success_val_list=[0, 2]) @mock.patch('crmsh.utils.get_stdout_or_raise_error') @@ -1618,7 +1618,7 @@ def test_is_quorate(mock_run): Quorate: Yes """ assert utils.is_quorate() is True - mock_run.assert_called_once_with("corosync-quorumtool -s", success_val_list=[0, 2]) + mock_run.assert_called_once_with("corosync-quorumtool -s", remote=None, success_val_list=[0, 2]) @mock.patch('crmsh.utils.etree.fromstring') @@ -1687,12 +1687,12 @@ def test_list_cluster_nodes(mock_run, mock_env, mock_isfile, mock_file2elem): @mock.patch('os.getenv') -@mock.patch('crmsh.utils.get_stdout_stderr') +@mock.patch('crmsh.utils.get_stdout_or_raise_error') def test_get_property(mock_run, mock_env): - mock_run.return_value = (0, "data", None) + mock_run.return_value = "data" mock_env.return_value = "cib.xml" assert utils.get_property("no-quorum-policy") == "data" - mock_run.assert_called_once_with("CIB_file=cib.xml sudo --preserve-env=CIB_file crm configure get_property no-quorum-policy") + mock_run.assert_called_once_with("CIB_file=cib.xml sudo --preserve-env=CIB_file crm configure get_property no-quorum-policy", remote=None, no_raise=True) @mock.patch('logging.Logger.warning')