Skip to content

Commit

Permalink
Dev: unittest: Change unit test since changed as logging
Browse files Browse the repository at this point in the history
  • Loading branch information
liangxin1300 committed Sep 7, 2021
1 parent eb371fa commit 490005e
Show file tree
Hide file tree
Showing 15 changed files with 237 additions and 671 deletions.
2 changes: 0 additions & 2 deletions test/unittests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
except ImportError as e:
pass

from crmsh import msg
from crmsh import config
from crmsh import options
msg.ERR_STREAM = None
config.core.debug = True
options.regression_tests = True
_here = os.path.dirname(__file__)
Expand Down
168 changes: 85 additions & 83 deletions test/unittests/test_bootstrap.py

Large diffs are not rendered by default.

25 changes: 19 additions & 6 deletions test/unittests/test_bugs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from __future__ import unicode_literals
# Copyright (C) 2014 Kristoffer Gronlund <kgronlund@suse.com>
# See COPYING for license information.

try:
from unittest import mock
except ImportError:
import mock

from crmsh import cibconfig
from lxml import etree
Expand Down Expand Up @@ -528,7 +531,9 @@ def test_existing_node_resource():
assert ok


def test_existing_node_resource_2():
@mock.patch("crmsh.log.LoggerUtils.line_number")
@mock.patch("crmsh.log.LoggerUtils.incr_lineno")
def test_existing_node_resource_2(mock_incr, mock_line_num):
obj = cibconfig.mkset_obj()
assert obj is not None

Expand All @@ -547,7 +552,9 @@ def test_existing_node_resource_2():
assert sorted(text.split('\n')) == sorted(text2.split('\n'))


def test_id_collision_breakage_1():
@mock.patch("crmsh.log.LoggerUtils.line_number")
@mock.patch("crmsh.log.LoggerUtils.incr_lineno")
def test_id_collision_breakage_1(mock_incr, mock_line_num):
from crmsh import clidisplay

obj = cibconfig.mkset_obj()
Expand Down Expand Up @@ -627,7 +634,9 @@ def test_id_collision_breakage_1():
assert original_cib == obj.repr()


def test_id_collision_breakage_3():
@mock.patch("crmsh.log.LoggerUtils.line_number")
@mock.patch("crmsh.log.LoggerUtils.incr_lineno")
def test_id_collision_breakage_3(mock_incr, mock_line_num):
from crmsh import clidisplay

obj = cibconfig.mkset_obj()
Expand Down Expand Up @@ -670,7 +679,9 @@ def test_id_collision_breakage_3():
assert original_cib == obj.repr()


def test_id_collision_breakage_2():
@mock.patch("crmsh.log.LoggerUtils.line_number")
@mock.patch("crmsh.log.LoggerUtils.incr_lineno")
def test_id_collision_breakage_2(mock_incr, mock_line_num):
from crmsh import clidisplay

obj = cibconfig.mkset_obj()
Expand Down Expand Up @@ -780,7 +791,9 @@ def test_bug_110():
assert o.check_sanity() == 0


def test_reordering_resource_sets():
@mock.patch("crmsh.log.LoggerUtils.line_number")
@mock.patch("crmsh.log.LoggerUtils.incr_lineno")
def test_reordering_resource_sets(mock_incr, mock_line_num):
"""
Can we reorder resource sets?
"""
Expand Down
18 changes: 4 additions & 14 deletions test/unittests/test_crashtest_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,15 @@ def test_parse_argument(self, mock_parser, mock_myformatter):

mock_parser_inst.print_help.assert_not_called()

@mock.patch('logging.config.dictConfig')
def test_setup_logging(self, mock_dict_config):
ctx = mock.Mock(logfile="file1")
main.setup_logging(ctx)
mock_dict_config.assert_called_once_with(main.LOGGING_CFG)

def test_setup_basic_context(self):
ctx = mock.Mock(process_name="crash_test")
main.setup_basic_context(ctx)
self.assertEqual(ctx.var_dir, "/var/lib/crmsh/crash_test")
self.assertEqual(ctx.report_path, "/var/lib/crmsh/crash_test")
self.assertEqual(ctx.jsonfile, "/var/lib/crmsh/crash_test/crash_test.json")
self.assertEqual(ctx.logfile, "/var/log/crmsh/crash_test.log")
self.assertEqual(ctx.logfile, "/var/log/crmsh/crmsh.log")

@mock.patch('logging.fatal')
@mock.patch('logging.Logger.fatal')
@mock.patch('crmsh.crash_test.utils.is_root')
@mock.patch('crmsh.crash_test.main.parse_argument')
@mock.patch('crmsh.crash_test.main.setup_basic_context')
Expand All @@ -85,14 +79,13 @@ def test_run_non_root(self, mock_setup, mock_parse, mock_is_root, mock_log_fatal
@mock.patch('crmsh.crash_test.main.kill_process')
@mock.patch('crmsh.crash_test.main.check.check')
@mock.patch('crmsh.crash_test.main.check.fix')
@mock.patch('crmsh.crash_test.main.setup_logging')
@mock.patch('os.makedirs')
@mock.patch('os.path.exists')
@mock.patch('crmsh.crash_test.utils.is_root')
@mock.patch('crmsh.crash_test.main.parse_argument')
@mock.patch('crmsh.crash_test.main.setup_basic_context')
def test_run(self, mock_setup, mock_parse, mock_is_root, mock_exists, mock_mkdir,
mock_setup_logging, mock_fix, mock_check, mock_kill, mock_fence, mock_sb):
mock_fix, mock_check, mock_kill, mock_fence, mock_sb):
mock_is_root.return_value = True
ctx = mock.Mock(var_dir="/var/lib/crash_test")
mock_exists.return_value = False
Expand All @@ -104,7 +97,6 @@ def test_run(self, mock_setup, mock_parse, mock_is_root, mock_exists, mock_mkdir
mock_is_root.assert_called_once_with()
mock_exists.assert_called_once_with(ctx.var_dir)
mock_mkdir.assert_called_once_with(ctx.var_dir, exist_ok=True)
mock_setup_logging.assert_called_once_with(ctx)
mock_check.assert_called_once_with(ctx)
mock_fix.assert_called_once_with(ctx)
mock_kill.assert_called_once_with(ctx)
Expand All @@ -114,13 +106,12 @@ def test_run(self, mock_setup, mock_parse, mock_is_root, mock_exists, mock_mkdir
@mock.patch('crmsh.crash_test.utils.json_dumps')
@mock.patch('crmsh.crash_test.main.check.check')
@mock.patch('crmsh.crash_test.main.check.fix')
@mock.patch('crmsh.crash_test.main.setup_logging')
@mock.patch('os.path.exists')
@mock.patch('crmsh.crash_test.utils.is_root')
@mock.patch('crmsh.crash_test.main.parse_argument')
@mock.patch('crmsh.crash_test.main.setup_basic_context')
def test_run_except(self, mock_setup, mock_parse, mock_is_root, mock_exists,
mock_setup_logging, mock_fix, mock_check, mock_dumps):
mock_fix, mock_check, mock_dumps):
mock_is_root.return_value = True
ctx = mock.Mock(var_dir="/var/lib/crash_test")
mock_exists.return_value = True
Expand All @@ -133,7 +124,6 @@ def test_run_except(self, mock_setup, mock_parse, mock_is_root, mock_exists,
mock_parse.assert_called_once_with(ctx)
mock_is_root.assert_called_once_with()
mock_exists.assert_called_once_with(ctx.var_dir)
mock_setup_logging.assert_called_once_with(ctx)
mock_check.assert_called_once_with(ctx)
mock_fix.assert_called_once_with(ctx)
mock_dumps.assert_called_once_with()
Expand Down
2 changes: 1 addition & 1 deletion test/unittests/test_crashtest_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def test_msg_raw(self, mock_handler):
utils.logger = mock.Mock()
utils.logger.log = mock.Mock()
utils.msg_raw("level1", "msg1")
mock_handler.assert_called_once_with("stream", True)
mock_handler.assert_called_once_with("console", True)
utils.logger.log.assert_called_once_with("level1", "msg1")

@mock.patch('crmsh.crash_test.utils.msg_raw')
Expand Down
8 changes: 4 additions & 4 deletions test/unittests/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def test_lock_or_wait_break(self, mock_time, mock_time_out, mock_create):
mock_time_out.assert_called_once_with()

@mock.patch('time.sleep')
@mock.patch('crmsh.lock.common_warn')
@mock.patch('logging.Logger.warning')
@mock.patch('crmsh.lock.RemoteLock._get_online_nodelist')
@mock.patch('crmsh.lock.Lock._create_lock_dir')
@mock.patch('crmsh.lock.RemoteLock.lock_timeout', new_callable=mock.PropertyMock)
Expand All @@ -216,11 +216,11 @@ def test_lock_or_wait_timed_out(self, mock_time, mock_time_out, mock_create,
mock_time_out.assert_has_calls([mock.call(), mock.call(), mock.call()])
mock_create.assert_called_once_with()
mock_get_nodelist.assert_called_once_with()
mock_warn.assert_called_once_with("Might have unfinished process on other nodes, wait 120s...")
mock_warn.assert_called_once_with('Might have unfinished process on other nodes, wait %ss...', 120)
mock_sleep.assert_called_once_with(10)

@mock.patch('time.sleep')
@mock.patch('crmsh.lock.common_warn')
@mock.patch('logging.Logger.warning')
@mock.patch('crmsh.lock.RemoteLock._get_online_nodelist')
@mock.patch('crmsh.lock.Lock._create_lock_dir')
@mock.patch('crmsh.lock.RemoteLock.lock_timeout', new_callable=mock.PropertyMock)
Expand All @@ -238,7 +238,7 @@ def test_lock_or_wait_again(self, mock_time, mock_time_out, mock_create,
mock_time_out.assert_has_calls([mock.call(), mock.call(), mock.call()])
mock_create.assert_has_calls([mock.call(), mock.call(), mock.call()])
mock_get_nodelist.assert_has_calls([mock.call(), mock.call()])
mock_warn.assert_called_once_with("Might have unfinished process on other nodes, wait 120s...")
mock_warn.assert_called_once_with('Might have unfinished process on other nodes, wait %ss...', 120)
mock_sleep.assert_has_calls([mock.call(10), mock.call(10)])

@mock.patch('crmsh.lock.Lock._unlock')
Expand Down
24 changes: 13 additions & 11 deletions test/unittests/test_ocfs2.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import logging
import unittest
try:
from unittest import mock
except ImportError:
import mock
from crmsh import ocfs2, utils, ra

logging.basicConfig(level=logging.INFO)

class TestOCFS2Manager(unittest.TestCase):
"""
Expand Down Expand Up @@ -134,15 +136,14 @@ def test_verify_devices_mounted(self, mock_is_block, mock_lvm, mock_mounted):
def test_check_if_already_configured_return(self):
self.ocfs2_inst3._check_if_already_configured()

@mock.patch('crmsh.ocfs2.err_buf')
@mock.patch('logging.Logger.info')
@mock.patch('crmsh.utils.get_stdout_or_raise_error')
def test_check_if_already_configured(self, mock_run, mock_buf):
def test_check_if_already_configured(self, mock_run, mock_info):
mock_run.return_value = "data xxx fstype=ocfs2 sss"
mock_buf.info = mock.Mock()
with self.assertRaises(utils.TerminateSubCommand):
self.ocfs2_inst2._check_if_already_configured()
mock_run.assert_called_once_with("crm configure show")
mock_buf.info.assert_called_once_with("Already configured OCFS2 related resources")
mock_info.assert_called_once_with("Already configured OCFS2 related resources")

@mock.patch('crmsh.ocfs2.OCFS2Manager._verify_devices')
@mock.patch('crmsh.ocfs2.OCFS2Manager._check_if_already_configured')
Expand Down Expand Up @@ -243,7 +244,7 @@ def test_gen_ra_scripts(self, mock_gen_unused):

@mock.patch('crmsh.utils.get_stdout_or_raise_error')
@mock.patch('crmsh.corosync.get_value')
@mock.patch('crmsh.bootstrap.status_long')
@mock.patch('crmsh.log.LoggerUtils.status_long')
def test_mkfs(self, mock_long, mock_get_value, mock_run):
mock_get_value.return_value = "hacluster"
self.ocfs2_inst3._mkfs("/dev/sdb2")
Expand All @@ -265,7 +266,7 @@ def test_vg_change(self, mock_run):
@mock.patch('crmsh.utils.gen_unused_id')
@mock.patch('crmsh.utils.get_all_vg_name')
@mock.patch('crmsh.utils.get_stdout_or_raise_error')
@mock.patch('crmsh.bootstrap.status_long')
@mock.patch('crmsh.log.LoggerUtils.status_long')
def test_create_lv(self, mock_long, mock_run, mock_all_vg, mock_unused, mock_pe_num):
mock_all_vg.return_value = []
mock_unused.return_value = "vg1"
Expand Down Expand Up @@ -376,7 +377,7 @@ def test_config_resource_stack_ocfs2_along(self, mock_dlm, mock_mkfs, mock_fs):
@mock.patch('crmsh.ocfs2.OCFS2Manager._config_resource_stack_lvm2')
@mock.patch('crmsh.utils.all_exist_id')
@mock.patch('crmsh.ocfs2.OCFS2Manager._dynamic_verify')
@mock.patch('crmsh.bootstrap.status')
@mock.patch('logging.Logger.info')
def test_init_ocfs2_lvm2(self, mock_status, mock_dynamic_verify, mock_all_id, mock_lvm2, mock_run):
mock_all_id.return_value = []
mock_run.return_value = "freeze"
Expand All @@ -385,7 +386,7 @@ def test_init_ocfs2_lvm2(self, mock_status, mock_dynamic_verify, mock_all_id, mo
self.ocfs2_inst7.init_ocfs2()
mock_status.assert_has_calls([
mock.call("Configuring OCFS2"),
mock.call(" OCFS2 device /dev/vg1/lv1 mounted on /data")
mock.call(' OCFS2 device %s mounted on %s', '/dev/vg1/lv1', '/data')
])
mock_dynamic_verify.assert_called_once_with()
mock_all_id.assert_called_once_with()
Expand All @@ -395,7 +396,7 @@ def test_init_ocfs2_lvm2(self, mock_status, mock_dynamic_verify, mock_all_id, mo
@mock.patch('crmsh.ocfs2.OCFS2Manager._config_resource_stack_ocfs2_along')
@mock.patch('crmsh.utils.all_exist_id')
@mock.patch('crmsh.ocfs2.OCFS2Manager._dynamic_verify')
@mock.patch('crmsh.bootstrap.status')
@mock.patch('logging.Logger.info')
def test_init_ocfs2(self, mock_status, mock_dynamic_verify, mock_all_id, mock_ocfs2, mock_run):
mock_all_id.return_value = []
mock_run.side_effect = ["stop", None]
Expand All @@ -404,7 +405,8 @@ def test_init_ocfs2(self, mock_status, mock_dynamic_verify, mock_all_id, mock_oc
self.ocfs2_inst3.init_ocfs2()
mock_status.assert_has_calls([
mock.call("Configuring OCFS2"),
mock.call(" OCFS2 device /dev/sda1 mounted on /data")
mock.call(' OCFS2 device %s mounted on %s', '/dev/sda1', '/data'),
mock.call(' \'no-quorum-policy\' is changed to "freeze"')
])
mock_dynamic_verify.assert_called_once_with()
mock_all_id.assert_called_once_with()
Expand Down Expand Up @@ -450,7 +452,7 @@ def test_join_ocfs2_return(self, mock_find):
@mock.patch('crmsh.utils.is_dev_a_plain_raw_disk_or_partition')
@mock.patch('crmsh.ocfs2.OCFS2Manager._verify_packages')
@mock.patch('crmsh.utils.has_resource_configured')
@mock.patch('crmsh.bootstrap.status_long')
@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):
mock_find.return_value = "/dev/sda2"
Expand Down
Loading

0 comments on commit 490005e

Please sign in to comment.