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 Dec 2, 2021
1 parent b7f8bec commit 36a3966
Show file tree
Hide file tree
Showing 5 changed files with 390 additions and 287 deletions.
7 changes: 4 additions & 3 deletions test/unittests/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,18 @@ def tearDownClass(cls):

@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.sbd.SBDTimeout.get_sbd_delay_start_sec_from_sysconfig')
@mock.patch('crmsh.sbd.SBDTimeout.is_sbd_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):
bootstrap._context = None
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_long.assert_called_once_with('Starting pacemaker(delaying start of sbd for 60s)')
mock_start.assert_called_once_with('pacemaker.service', enable=True, node_list=[])

@mock.patch('crmsh.bootstrap.configure_local_ssh_key')
Expand Down
30 changes: 30 additions & 0 deletions test/unittests/test_corosync.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,36 @@ def test_is_unicast(mock_get_value):
mock_get_value.assert_called_once_with("totem.transport")


@mock.patch('crmsh.corosync.get_corosync_value_dict')
def test_token_and_consensus_timeout(mock_get_dict):
mock_get_dict.return_value = {"token": 10, "consensus": 12}
assert corosync.token_and_consensus_timeout() == 22


@mock.patch('crmsh.corosync.get_corosync_value')
def test_get_corosync_value_dict(mock_get_value):
mock_get_value.side_effect = ["10000", None]
res = corosync.get_corosync_value_dict()
assert res == {"token": 10, "consensus": 12}


@mock.patch('crmsh.corosync.get_value')
@mock.patch('crmsh.utils.get_stdout_or_raise_error')
def test_get_corosync_value_raise(mock_run, mock_get_value):
mock_run.side_effect = ValueError
mock_get_value.return_value = None
assert corosync.get_corosync_value("xxx") is None
mock_run.assert_called_once_with("corosync-cmapctl xxx")
mock_get_value.assert_called_once_with("xxx")


@mock.patch('crmsh.utils.get_stdout_or_raise_error')
def test_get_corosync_value(mock_run):
mock_run.return_value = "totem.token = 10000"
assert corosync.get_corosync_value("totem.token") == "10000"
mock_run.assert_called_once_with("corosync-cmapctl totem.token")


class TestCorosyncParser(unittest.TestCase):
def test_parse(self):
p = Parser(F1)
Expand Down
26 changes: 6 additions & 20 deletions test/unittests/test_crashtest_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,44 +62,44 @@ def tearDownClass(cls):
Global tearDown.
"""

@mock.patch('crmsh.crash_test.utils.get_property')
@mock.patch('crmsh.crash_test.utils.crmshutils.get_property')
def test_fence_enabled_false(self, mock_get_property):
mock_get_property.return_value = None
res = self.fence_info_inst.fence_enabled
self.assertEqual(res, False)
mock_get_property.assert_called_once_with("stonith-enabled")

@mock.patch('crmsh.crash_test.utils.get_property')
@mock.patch('crmsh.crash_test.utils.crmshutils.get_property')
def test_fence_enabled_true(self, mock_get_property):
mock_get_property.return_value = "True"
res = self.fence_info_inst.fence_enabled
self.assertEqual(res, True)
mock_get_property.assert_called_once_with("stonith-enabled")

@mock.patch('crmsh.crash_test.utils.msg_error')
@mock.patch('crmsh.crash_test.utils.get_property')
@mock.patch('crmsh.crash_test.utils.crmshutils.get_property')
def test_fence_action_none(self, mock_get_property, mock_error):
mock_get_property.return_value = None
res = self.fence_info_inst.fence_action
self.assertEqual(res, None)
mock_get_property.assert_called_once_with("stonith-action")
mock_error.assert_called_once_with('Cluster property "stonith-action" should be reboot|off|poweroff')

@mock.patch('crmsh.crash_test.utils.get_property')
@mock.patch('crmsh.crash_test.utils.crmshutils.get_property')
def test_fence_action(self, mock_get_property):
mock_get_property.return_value = "reboot"
res = self.fence_info_inst.fence_action
self.assertEqual(res, "reboot")
mock_get_property.assert_called_once_with("stonith-action")

@mock.patch('crmsh.crash_test.utils.get_property')
@mock.patch('crmsh.crash_test.utils.crmshutils.get_property')
def test_fence_timeout(self, mock_get_property):
mock_get_property.return_value = "60s"
res = self.fence_info_inst.fence_timeout
self.assertEqual(res, "60")
mock_get_property.assert_called_once_with("stonith-timeout")

@mock.patch('crmsh.crash_test.utils.get_property')
@mock.patch('crmsh.crash_test.utils.crmshutils.get_property')
def test_fence_timeout_default(self, mock_get_property):
mock_get_property.return_value = None
res = self.fence_info_inst.fence_timeout
Expand Down Expand Up @@ -381,20 +381,6 @@ def test_online_nodes(self, mock_run):
self.assertEqual(res, ["15sp2-1", "15sp2-2"])
mock_run.assert_called_once_with("crm_mon -1")

@mock.patch('crmsh.crash_test.utils.crmshutils.get_stdout_stderr')
def test_get_property_none(self, mock_run):
mock_run.return_value = (1, None, "error")
res = utils.get_property("test")
self.assertEqual(res, None)
mock_run.assert_called_once_with("crm configure get_property test")

@mock.patch('crmsh.crash_test.utils.crmshutils.get_stdout_stderr')
def test_get_property(self, mock_run):
mock_run.return_value = (0, "data", None)
res = utils.get_property("test")
self.assertEqual(res, "data")
mock_run.assert_called_once_with("crm configure get_property test")

@mock.patch('crmsh.crash_test.utils.online_nodes')
def test_peer_node_list_empty(self, mock_online):
mock_online.return_value = None
Expand Down
Loading

0 comments on commit 36a3966

Please sign in to comment.