Skip to content

Commit

Permalink
Dev: unittest: Adjust unit test for previous changes
Browse files Browse the repository at this point in the history
  • Loading branch information
liangxin1300 committed Nov 21, 2023
1 parent aff04da commit bed8ad3
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 97 deletions.
7 changes: 3 additions & 4 deletions test/unittests/test_ocfs2.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,16 +445,15 @@ def test_join_ocfs2_return(self, mock_find):
@mock.patch('crmsh.utils.compare_uuid_with_peer_dev')
@mock.patch('crmsh.utils.is_dev_a_plain_raw_disk_or_partition')
@mock.patch('crmsh.ocfs2.OCFS2Manager._verify_packages')
@mock.patch('crmsh.xmlutil.CrmMonXmlParser.is_resource_configured')
@mock.patch('crmsh.xmlutil.CrmMonXmlParser')
@mock.patch('crmsh.log.LoggerUtils.status_long')
@mock.patch('crmsh.ocfs2.OCFS2Manager._find_target_on_join')
def test_join_ocfs2(self, mock_find, mock_long, mock_configured, mock_verify_packages, mock_is_mapper, mock_compare):
def test_join_ocfs2(self, mock_find, mock_long, mock_parser, mock_verify_packages, mock_is_mapper, mock_compare):
mock_find.return_value = "/dev/sda2"
mock_configured.return_value = False
mock_parser("node1").is_resource_configured.return_value = False
mock_is_mapper.return_value = True
self.ocfs2_inst3.join_ocfs2("node1")
mock_find.assert_called_once_with("node1")
mock_configured.assert_called_once_with(constants.LVMLOCKD_RA, "node1")
mock_verify_packages.assert_called_once_with(False)
mock_is_mapper.assert_called_once_with("/dev/sda2", "node1")
mock_compare.assert_called_once_with(["/dev/sda2"], "node1")
Expand Down
14 changes: 6 additions & 8 deletions test/unittests/test_qdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,30 @@ def test_evaluate_qdevice_quorum_effect_reload(mock_get_dict, mock_quorate):
mock_quorate.assert_called_once_with(3, 2)


@mock.patch('crmsh.xmlutil.CrmMonXmlParser.is_any_resource_running')
@mock.patch('crmsh.xmlutil.CrmMonXmlParser')
@mock.patch('crmsh.utils.calculate_quorate_status')
@mock.patch('crmsh.utils.get_quorum_votes_dict')
def test_evaluate_qdevice_quorum_effect_later(mock_get_dict, mock_quorate, mock_ra_running):
def test_evaluate_qdevice_quorum_effect_later(mock_get_dict, mock_quorate, mock_parser):
mock_get_dict.return_value = {'Expected': '2', 'Total': '2'}
mock_quorate.return_value = False
mock_ra_running.return_value = True
mock_parser().is_any_resource_running.return_value = True
res = qdevice.evaluate_qdevice_quorum_effect(qdevice.QDEVICE_REMOVE)
assert res == qdevice.QdevicePolicy.QDEVICE_RESTART_LATER
mock_get_dict.assert_called_once_with()
mock_quorate.assert_called_once_with(2, 1)
mock_ra_running.assert_called_once_with()


@mock.patch('crmsh.xmlutil.CrmMonXmlParser.is_any_resource_running')
@mock.patch('crmsh.xmlutil.CrmMonXmlParser')
@mock.patch('crmsh.utils.calculate_quorate_status')
@mock.patch('crmsh.utils.get_quorum_votes_dict')
def test_evaluate_qdevice_quorum_effect(mock_get_dict, mock_quorate, mock_ra_running):
def test_evaluate_qdevice_quorum_effect(mock_get_dict, mock_quorate, mock_parser):
mock_get_dict.return_value = {'Expected': '2', 'Total': '2'}
mock_quorate.return_value = False
mock_ra_running.return_value = False
mock_parser().is_any_resource_running.return_value = False
res = qdevice.evaluate_qdevice_quorum_effect(qdevice.QDEVICE_REMOVE)
assert res == qdevice.QdevicePolicy.QDEVICE_RESTART
mock_get_dict.assert_called_once_with()
mock_quorate.assert_called_once_with(2, 1)
mock_ra_running.assert_called_once_with()


@mock.patch('crmsh.lock.RemoteLock')
Expand Down
26 changes: 12 additions & 14 deletions test/unittests/test_sbd.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,9 +618,9 @@ def test_sbd_init(self, mock_package, mock_watchdog, mock_get_device, mock_initi
@mock.patch('crmsh.bootstrap.wait_for_cluster')
@mock.patch('crmsh.utils.cluster_run_cmd')
@mock.patch('logging.Logger.info')
@mock.patch('crmsh.xmlutil.CrmMonXmlParser.is_any_resource_running')
def test_restart_cluster_on_needed_no_ra_running(self, mock_ra_running, mock_status, mock_cluster_run, mock_wait, mock_config_sbd_ra):
mock_ra_running.return_value = False
@mock.patch('crmsh.xmlutil.CrmMonXmlParser')
def test_restart_cluster_on_needed_no_ra_running(self, mock_parser, mock_status, mock_cluster_run, mock_wait, mock_config_sbd_ra):
mock_parser().is_any_resource_running.return_value = False
self.sbd_inst._restart_cluster_and_configure_sbd_ra()
mock_status.assert_called_once_with("Restarting cluster service")
mock_cluster_run.assert_called_once_with("crm cluster restart")
Expand All @@ -629,9 +629,9 @@ def test_restart_cluster_on_needed_no_ra_running(self, mock_ra_running, mock_sta

@mock.patch('crmsh.sbd.SBDTimeout.get_stonith_timeout')
@mock.patch('logging.Logger.warning')
@mock.patch('crmsh.xmlutil.CrmMonXmlParser.is_any_resource_running')
def test_restart_cluster_on_needed_diskless(self, mock_ra_running, mock_warn, mock_get_timeout):
mock_ra_running.return_value = True
@mock.patch('crmsh.xmlutil.CrmMonXmlParser')
def test_restart_cluster_on_needed_diskless(self, mock_parser, mock_warn, mock_get_timeout):
mock_parser().is_any_resource_running.return_value = True
mock_get_timeout.return_value = 60
self.sbd_inst_diskless.timeout_inst = mock.Mock(stonith_watchdog_timeout=-1)
self.sbd_inst_diskless._restart_cluster_and_configure_sbd_ra()
Expand All @@ -642,14 +642,13 @@ def test_restart_cluster_on_needed_diskless(self, mock_ra_running, mock_warn, mo

@mock.patch('crmsh.sbd.SBDManager.configure_sbd_resource_and_properties')
@mock.patch('logging.Logger.warning')
@mock.patch('crmsh.xmlutil.CrmMonXmlParser.is_any_resource_running')
def test_restart_cluster_on_needed(self, mock_ra_running, mock_warn, mock_config_sbd_ra):
mock_ra_running.return_value = True
@mock.patch('crmsh.xmlutil.CrmMonXmlParser')
def test_restart_cluster_on_needed(self, mock_parser, mock_warn, mock_config_sbd_ra):
mock_parser().is_any_resource_running.return_value = True
self.sbd_inst._restart_cluster_and_configure_sbd_ra()
mock_warn.assert_has_calls([
mock.call("To start sbd.service, need to restart cluster service manually on each node"),
])
mock_config_sbd_ra.assert_called_once_with()

@mock.patch('crmsh.bootstrap.invoke')
def test_enable_sbd_service_init(self, mock_invoke):
Expand Down Expand Up @@ -677,16 +676,16 @@ def test_configure_sbd_resource_and_properties_not_installed(self, mock_package)
@mock.patch('crmsh.sbd.SBDTimeout.adjust_sbd_timeout_related_cluster_configuration')
@mock.patch('crmsh.utils.set_property')
@mock.patch('crmsh.sh.ClusterShell.get_stdout_or_raise_error')
@mock.patch('crmsh.xmlutil.CrmMonXmlParser.is_resource_configured')
@mock.patch('crmsh.xmlutil.CrmMonXmlParser')
@mock.patch('crmsh.service_manager.ServiceManager.service_is_enabled')
@mock.patch('crmsh.utils.package_is_installed')
def test_configure_sbd_resource_and_properties(
self,
mock_package, mock_enabled, mock_configured, mock_run, mock_set_property, sbd_adjust, mock_is_active,
mock_package, mock_enabled, mock_parser, mock_run, mock_set_property, sbd_adjust, mock_is_active,
):
mock_package.return_value = True
mock_enabled.return_value = True
mock_configured.return_value = False
mock_parser().is_resource_configured.return_value = False
mock_is_active.return_value = False
self.sbd_inst._context = mock.Mock(cluster_is_running=True)
self.sbd_inst._get_sbd_device_from_config = mock.Mock()
Expand All @@ -696,7 +695,6 @@ def test_configure_sbd_resource_and_properties(

mock_package.assert_called_once_with("sbd")
mock_enabled.assert_called_once_with("sbd.service")
mock_configured.assert_called_once_with(sbd.SBDManager.SBD_RA)
mock_run.assert_called_once_with("crm configure primitive {} {}".format(sbd.SBDManager.SBD_RA_ID, sbd.SBDManager.SBD_RA))
mock_set_property.assert_called_once_with("stonith-enabled", "true")

Expand Down
8 changes: 4 additions & 4 deletions test/unittests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1327,11 +1327,11 @@ def test_set_dlm_option(mock_get_dict, mock_run):
mock_run.assert_called_once_with('dlm_tool set_config "key2=test"')


@mock.patch('crmsh.xmlutil.CrmMonXmlParser.is_resource_configured')
def test_is_dlm_configured(mock_configured):
mock_configured.return_value = True
@mock.patch('crmsh.xmlutil.CrmMonXmlParser')
def test_is_dlm_configured(mock_parser):
mock_parser().is_resource_configured.return_value = True
assert utils.is_dlm_configured() is True
mock_configured.assert_called_once_with(constants.DLM_CONTROLD_RA)
mock_parser().is_resource_configured.assert_called_once_with(constants.DLM_CONTROLD_RA)


@mock.patch('crmsh.sh.ClusterShell.get_stdout_or_raise_error')
Expand Down
103 changes: 36 additions & 67 deletions test/unittests/test_xmlutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,81 +12,50 @@ class TestCrmMonXmlParser(unittest.TestCase):
"""
Unitary tests for crmsh.xmlutil.CrmMonXmlParser
"""

@classmethod
def setUpClass(cls):
"""
Global setUp.
"""

def setUp(self):
@mock.patch('crmsh.sh.cluster_shell')
def setUp(self, mock_cluster_shell):
"""
Test setUp.
"""
self.parser_inst = xmlutil.CrmMonXmlParser()
self.nodes_xml = """
data = '''
<data>
<nodes>
<node name="tbw-1" id="1084783148" online="true" standby="false" standby_onfail="false" maintenance="false" pending="false" unclean="false" shutdown="false" expected_up="true" is_dc="true" resources_running="3" type="member"/>
<node name="tbw-2" id="1084783312" online="true" standby="false" standby_onfail="false" maintenance="false" pending="false" unclean="false" shutdown="false" expected_up="true" is_dc="false" resources_running="2" type="member"/>
<node name="tbw-1" id="1084783148" online="true" standby="true" standby_onfail="false" maintenance="false" pending="false" unclean="false" shutdown="false" expected_up="true" is_dc="true" resources_running="3" type="member"/>
<node name="tbw-2" id="1084783312" online="false" standby="false" standby_onfail="false" maintenance="false" pending="false" unclean="false" shutdown="false" expected_up="true" is_dc="false" resources_running="2" type="member"/>
</nodes>
"""
self.resources_xml = """
<resources>
<resource id="ocfs2-dlm" resource_agent="ocf::pacemaker:controld" role="Started" active="true" orphaned="false" blocked="false" managed="true" failed="false" failure_ignored="false" nodes_running_on="1">
<node name="tbw-2" id="1084783312" cached="true"/>
</resource>
<resource id="ocfs2-clusterfs" resource_agent="ocf::heartbeat:Filesystem" role="Started" active="true" orphaned="false" blocked="false" managed="true" failed="false" failure_ignored="false" nodes_running_on="1">
<node name="tbw-2" id="1084783312" cached="true"/>
</resource>
</resources>
"""

def tearDown(self):
"""
Test tearDown.
"""

@classmethod
def tearDownClass(cls):
"""
Global tearDown.
"""
<resources>
<resource id="ocfs2-dlm" resource_agent="ocf::pacemaker:controld" role="Started" active="true" orphaned="false" blocked="false" managed="true" failed="false" failure_ignored="false" nodes_running_on="1">
<node name="tbw-2" id="1084783312" cached="true"/>
</resource>
<resource id="ocfs2-clusterfs" resource_agent="ocf::heartbeat:Filesystem" role="Started" active="true" orphaned="false" blocked="false" managed="true" failed="false" failure_ignored="false" nodes_running_on="1">
<node name="tbw-2" id="1084783312" cached="true"/>
</resource>
</resources>
</data>
'''
mock_cluster_shell().get_rc_stdout_stderr_without_input.return_value = (0, data, '')
self.parser_inst = xmlutil.CrmMonXmlParser()

@mock.patch('crmsh.xmlutil.text2elem')
@mock.patch('crmsh.sh.ClusterShell.get_rc_stdout_stderr_without_input')
def test_load(self, mock_run, mock_text2elem):
mock_run.return_value = (0, "data", "")
mock_text2elem.return_value = mock.Mock()
self.parser_inst._load()
mock_run.assert_called_once_with(None, constants.CRM_MON_XML_OUTPUT)
mock_text2elem.assert_called_once_with("data")
def test_is_node_online(self):
assert self.parser_inst.is_node_online("tbw-1") is True
assert self.parser_inst.is_node_online("tbw-2") is False

@mock.patch('crmsh.sh.ClusterShell.get_rc_stdout_stderr_without_input')
def test_is_node_online(self, mock_run):
mock_run.return_value = (0, self.nodes_xml, "")
assert xmlutil.CrmMonXmlParser.is_node_online("node1") is False
assert xmlutil.CrmMonXmlParser.is_node_online("tbw-2") is True
def test_get_node_list(self):
assert self.parser_inst.get_node_list("standby") == ['tbw-1']
assert self.parser_inst.get_node_list("online") == ['tbw-2']

@mock.patch('crmsh.sh.ClusterShell.get_rc_stdout_stderr_without_input')
def test_is_resource_configured(self, mock_run):
mock_run.return_value = (0, self.resources_xml, "")
assert xmlutil.CrmMonXmlParser.is_resource_configured("test") is False
assert xmlutil.CrmMonXmlParser.is_resource_configured("ocf::heartbeat:Filesystem") is True
def test_is_resource_configured(self):
assert self.parser_inst.is_resource_configured("test") is False
assert self.parser_inst.is_resource_configured("ocf::heartbeat:Filesystem") is True

@mock.patch('crmsh.sh.ClusterShell.get_rc_stdout_stderr_without_input')
def test_is_any_resource_running(self, mock_run):
mock_run.return_value = (0, self.resources_xml, "")
assert xmlutil.CrmMonXmlParser.is_any_resource_running() is True
def test_is_any_resource_running(self):
assert self.parser_inst.is_any_resource_running() is True

@mock.patch('crmsh.sh.ClusterShell.get_rc_stdout_stderr_without_input')
def test_is_resource_started(self, mock_run):
mock_run.return_value = (0, self.resources_xml, "")
assert xmlutil.CrmMonXmlParser.is_resource_started("test") is False
assert xmlutil.CrmMonXmlParser.is_resource_started("ocfs2-clusterfs") is True
assert xmlutil.CrmMonXmlParser.is_resource_started("ocf::pacemaker:controld") is True
def test_is_resource_started(self):
assert self.parser_inst.is_resource_started("test") is False
assert self.parser_inst.is_resource_started("ocfs2-clusterfs") is True
assert self.parser_inst.is_resource_started("ocf::pacemaker:controld") is True

@mock.patch('crmsh.sh.ClusterShell.get_rc_stdout_stderr_without_input')
def test_get_resource_id_list_via_type(self, mock_run):
mock_run.return_value = (0, self.resources_xml, "")
assert xmlutil.CrmMonXmlParser.get_resource_id_list_via_type("test") == []
assert xmlutil.CrmMonXmlParser.get_resource_id_list_via_type("ocf::pacemaker:controld")[0] == "ocfs2-dlm"
def test_get_resource_id_list_via_type(self):
assert self.parser_inst.get_resource_id_list_via_type("test") == []
assert self.parser_inst.get_resource_id_list_via_type("ocf::pacemaker:controld")[0] == "ocfs2-dlm"

0 comments on commit bed8ad3

Please sign in to comment.