Skip to content

Commit

Permalink
Dev: unittest: Add unit test for previous changes
Browse files Browse the repository at this point in the history
  • Loading branch information
liangxin1300 committed Nov 10, 2021
1 parent 88bc0d8 commit e154460
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 74 deletions.
86 changes: 54 additions & 32 deletions test/unittests/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,21 @@ def tearDownClass(cls):
Global tearDown.
"""

@mock.patch('crmsh.log.LoggerUtils.status_long')
@mock.patch('crmsh.utils.start_service')
@mock.patch('crmsh.sbd.SBDManager.get_suitable_sbd_systemd_timeout')
@mock.patch('crmsh.sbd.SBDManager.is_delay_start')
@mock.patch('crmsh.utils.service_is_enabled')
@mock.patch('crmsh.utils.package_is_installed')
def test_start_pacemaker(self, mock_installed, mock_enabled, mock_delay_start, mock_timeout, mock_start, mock_long):
mock_installed.return_value = True
mock_enabled.return_value = True
mock_delay_start.return_value = True
mock_timeout.return_value = 60
bootstrap.start_pacemaker()
mock_long.assert_called_once_with('Starting pacemaker(waiting for sbd 60s)')
mock_start.assert_called_once_with('pacemaker.service', enable=True, node_list=[])

@mock.patch('crmsh.bootstrap.configure_local_ssh_key')
@mock.patch('crmsh.utils.start_service')
def test_init_ssh(self, mock_start_service, mock_config_ssh):
Expand Down Expand Up @@ -592,88 +607,95 @@ def test_get_cluster_node_hostname_error(self, mock_stdout_stderr, mock_error):
mock_error.assert_called_once_with("error")

@mock.patch('crmsh.utils.this_node')
@mock.patch('re.search')
@mock.patch('crmsh.bootstrap.get_cluster_node_hostname')
def test_is_online_local_offline(self, mock_get_peer, mock_search, mock_this_node):
mock_this_node.return_value = "node1"
mock_search.return_value = None
@mock.patch('crmsh.utils.get_stdout_or_raise_error')
def test_is_online_local_offline(self, mock_run, mock_get_peer, mock_this_node):
mock_run.return_value = """
Node List:
* Online: [ node1 node2 ]
"""
mock_this_node.return_value = "node3"

assert bootstrap.is_online("text") is False
assert bootstrap.is_online() is False

mock_this_node.assert_called_once_with()
mock_get_peer.assert_not_called()
mock_search.assert_called_once_with("Online: .* node1 ", "text")
mock_run.assert_called_once_with(constants.CRM_MON_ONE_SHOT, remote="node3")

@mock.patch('crmsh.utils.this_node')
@mock.patch('re.search')
@mock.patch('crmsh.bootstrap.get_cluster_node_hostname')
def test_is_online_on_init_node(self, mock_get_peer, mock_search, mock_this_node):
mock_search.return_value = mock.Mock()
@mock.patch('crmsh.utils.get_stdout_or_raise_error')
def test_is_online_on_init_node(self, mock_run, mock_get_peer, mock_this_node):
mock_run.return_value = """
Node List:
* Online: [ node1 node2 ]
"""
mock_this_node.return_value = "node1"
mock_get_peer.return_value = None

assert bootstrap.is_online("text") is True
assert bootstrap.is_online() is True

mock_this_node.assert_called_once_with()
mock_get_peer.assert_called_once_with()
mock_search.assert_called_once_with("Online: .* node1 ", "text")
mock_run.assert_called_once_with(constants.CRM_MON_ONE_SHOT, remote="node1")

@mock.patch('crmsh.utils.fatal')
@mock.patch('crmsh.utils.stop_service')
@mock.patch('crmsh.bootstrap.csync2_update')
@mock.patch('crmsh.corosync.conf')
@mock.patch('shutil.copy')
@mock.patch('crmsh.utils.this_node')
@mock.patch('re.search')
@mock.patch('crmsh.bootstrap.get_cluster_node_hostname')
def test_is_online_peer_offline(self, mock_get_peer, mock_search, mock_this_node,
@mock.patch('crmsh.utils.get_stdout_or_raise_error')
def test_is_online_peer_offline(self, mock_run, mock_get_peer, mock_this_node,
mock_copy, mock_corosync_conf, mock_csync2, mock_stop_service, mock_error):
mock_run.return_value = """
Node List:
* Online: [ node1 node2 ]
"""
bootstrap.COROSYNC_CONF_ORIG = "/tmp/crmsh_tmpfile"
mock_search.side_effect = [ mock.Mock(), None ]
mock_this_node.return_value = "node2"
mock_get_peer.return_value = "node1"
mock_get_peer.return_value = "node3"
mock_corosync_conf.side_effect = [ "/etc/corosync/corosync.conf",
"/etc/corosync/corosync.conf"]
mock_error.side_effect = ValueError

bootstrap.is_online("text")
with self.assertRaises(ValueError):
bootstrap.is_online()

mock_run.assert_called_once_with(constants.CRM_MON_ONE_SHOT, remote="node2")
mock_this_node.assert_called_once_with()
mock_get_peer.assert_called_once_with()
mock_search.assert_has_calls([
mock.call("Online: .* node2 ", "text"),
mock.call("Online: .* node1 ", "text")
])
mock_corosync_conf.assert_has_calls([
mock.call(),
mock.call()
])
mock_copy.assert_called_once_with(bootstrap.COROSYNC_CONF_ORIG, "/etc/corosync/corosync.conf")
mock_csync2.assert_called_once_with("/etc/corosync/corosync.conf")
mock_stop_service.assert_called_once_with("corosync")
mock_error.assert_called_once_with("Cannot see peer node \"node1\", please check the communication IP")
mock_error.assert_called_once_with("Cannot see peer node \"node3\", please check the communication IP")

@mock.patch('crmsh.utils.fatal')
@mock.patch('crmsh.utils.stop_service')
@mock.patch('crmsh.bootstrap.csync2_update')
@mock.patch('crmsh.corosync.conf')
@mock.patch('shutil.copy')
@mock.patch('crmsh.utils.this_node')
@mock.patch('re.search')
@mock.patch('crmsh.bootstrap.get_cluster_node_hostname')
def test_is_online_both_online(self, mock_get_peer, mock_search, mock_this_node,
@mock.patch('crmsh.utils.get_stdout_or_raise_error')
def test_is_online_both_online(self, mock_run, mock_get_peer, mock_this_node,
mock_copy, mock_corosync_conf, mock_csync2, mock_stop_service, mock_error):
mock_search.side_effect = [ mock.Mock(), mock.Mock() ]
mock_run.return_value = """
Node List:
* Online: [ node1 node2 ]
"""
mock_this_node.return_value = "node2"
mock_get_peer.return_value = "node1"

assert bootstrap.is_online("text") is True
assert bootstrap.is_online() is True

mock_this_node.assert_called_once_with()
mock_get_peer.assert_called_once_with()
mock_search.assert_has_calls([
mock.call("Online: .* node2 ", "text"),
mock.call("Online: .* node1 ", "text")
])
mock_corosync_conf.assert_not_called()
mock_copy.assert_not_called()
mock_csync2.assert_not_called()
Expand Down Expand Up @@ -1049,7 +1071,7 @@ def test_invokerc(self, mock_invoke):
self.assertEqual(res, True)
mock_invoke.assert_called_once_with("cmd")

@mock.patch('crmsh.utils.is_quorate')
@mock.patch('crmsh.utils.calculate_quorate_status')
@mock.patch('crmsh.utils.get_quorum_votes_dict')
def test_evaluate_qdevice_quorum_effect_reload(self, mock_get_dict, mock_quorate):
mock_get_dict.return_value = {'Expected': '2', 'Total': '2'}
Expand All @@ -1060,7 +1082,7 @@ def test_evaluate_qdevice_quorum_effect_reload(self, mock_get_dict, mock_quorate
mock_quorate.assert_called_once_with(3, 2)

@mock.patch('crmsh.utils.has_resource_running')
@mock.patch('crmsh.utils.is_quorate')
@mock.patch('crmsh.utils.calculate_quorate_status')
@mock.patch('crmsh.utils.get_quorum_votes_dict')
def test_evaluate_qdevice_quorum_effect_reload(self, mock_get_dict, mock_quorate, mock_ra_running):
mock_get_dict.return_value = {'Expected': '2', 'Total': '2'}
Expand All @@ -1073,7 +1095,7 @@ def test_evaluate_qdevice_quorum_effect_reload(self, mock_get_dict, mock_quorate
mock_ra_running.assert_called_once_with()

@mock.patch('crmsh.utils.has_resource_running')
@mock.patch('crmsh.utils.is_quorate')
@mock.patch('crmsh.utils.calculate_quorate_status')
@mock.patch('crmsh.utils.get_quorum_votes_dict')
def test_evaluate_qdevice_quorum_effect(self, mock_get_dict, mock_quorate, mock_ra_running):
mock_get_dict.return_value = {'Expected': '2', 'Total': '2'}
Expand Down
73 changes: 53 additions & 20 deletions test/unittests/test_ui_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,48 +38,81 @@ def tearDownClass(cls):

@mock.patch('logging.Logger.info')
@mock.patch('crmsh.utils.service_is_active')
def test_do_start_already_started(self, mock_active, mock_info):
@mock.patch('crmsh.ui_cluster.parse_option_for_nodes')
def test_do_start_already_started(self, mock_parse_nodes, mock_active, mock_info):
context_inst = mock.Mock()
mock_active.return_value = True
self.ui_cluster_inst.do_start(context_inst)
mock_active.assert_called_once_with("pacemaker.service")
mock_info.assert_called_once_with("Cluster services already started")
mock_parse_nodes.return_value = ["node1", "node2"]
mock_active.side_effect = [True, True]
self.ui_cluster_inst.do_start(context_inst, "node1", "node2")
mock_parse_nodes.assert_called_once_with(context_inst, "node1", "node2")
mock_active.assert_has_calls([
mock.call("pacemaker.service", remote_addr="node1"),
mock.call("pacemaker.service", remote_addr="node2")
])
mock_info.assert_has_calls([
mock.call("Cluster services already started on node1"),
mock.call("Cluster services already started on node2")
])

@mock.patch('crmsh.bootstrap.start_pacemaker')
@mock.patch('logging.Logger.info')
@mock.patch('crmsh.utils.is_qdevice_configured')
@mock.patch('crmsh.utils.start_service')
@mock.patch('crmsh.utils.service_is_active')
def test_do_start(self, mock_active, mock_start, mock_qdevice_configured, mock_info, mock_start_pacemaker):
@mock.patch('crmsh.ui_cluster.parse_option_for_nodes')
def test_do_start(self, mock_parse_nodes, mock_active, mock_start, mock_qdevice_configured, mock_info, mock_start_pacemaker):
context_inst = mock.Mock()
mock_parse_nodes.return_value = ["node1"]
mock_active.return_value = False
mock_qdevice_configured.return_value = True

self.ui_cluster_inst.do_start(context_inst)
self.ui_cluster_inst.do_start(context_inst, "node1")

mock_active.assert_called_once_with("pacemaker.service")
mock_start.assert_has_calls([mock.call("corosync-qdevice")])
mock_active.assert_called_once_with("pacemaker.service", remote_addr="node1")
mock_start.assert_called_once_with("corosync-qdevice", node_list=["node1"])
mock_qdevice_configured.assert_called_once_with()
mock_info.assert_called_once_with("Cluster services started")
mock_info.assert_called_once_with("Cluster services started on node1")

@mock.patch('logging.Logger.info')
@mock.patch('crmsh.utils.service_is_active')
def test_do_stop_already_stopped(self, mock_active, mock_info):
@mock.patch('crmsh.ui_cluster.parse_option_for_nodes')
def test_do_stop_already_stopped(self, mock_parse_nodes, mock_active, mock_info):
context_inst = mock.Mock()
mock_active.return_value = False
self.ui_cluster_inst.do_stop(context_inst)
mock_active.assert_called_once_with("corosync.service")
mock_info.assert_called_once_with("Cluster services already stopped")
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("Cluster services already stopped on node1")

@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_configured')
@mock.patch('crmsh.bootstrap.wait_for_cluster')
@mock.patch('crmsh.utils.service_is_active')
def test_do_stop(self, mock_active, mock_stop, mock_info):
@mock.patch('crmsh.ui_cluster.parse_option_for_nodes')
def test_do_stop(self, mock_parse_nodes, mock_active, mock_wait, mock_dlm_configured, mock_is_quorate, mock_set_dlm, mock_stop, mock_info, mock_debug):
context_inst = mock.Mock()
mock_parse_nodes.return_value = ["node1"]
mock_active.side_effect = [True, True]
mock_dlm_configured.return_value = True
mock_is_quorate.return_value = False

self.ui_cluster_inst.do_stop(context_inst)
self.ui_cluster_inst.do_stop(context_inst, "node1")

mock_active.assert_has_calls([mock.call("corosync.service"), mock.call("corosync-qdevice")])
mock_stop.assert_has_calls([mock.call("corosync-qdevice"), mock.call("corosync")])
mock_info.assert_called_once_with("Cluster services stopped")
mock_active.assert_has_calls([
mock.call("corosync.service", remote_addr="node1"),
mock.call("corosync-qdevice.service", "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("Cluster services stopped on node1")
mock_debug.assert_called_once_with("Quorum is lost; Set enable_quorum_fencing=0 and enable_quorum_lockspace=0 for dlm")
Loading

0 comments on commit e154460

Please sign in to comment.