本文整理汇总了Python中salttesting.mock.MagicMock.assert_not_called方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.assert_not_called方法的具体用法?Python MagicMock.assert_not_called怎么用?Python MagicMock.assert_not_called使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类salttesting.mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.assert_not_called方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_verify_log
# 需要导入模块: from salttesting.mock import MagicMock [as 别名]
# 或者: from salttesting.mock.MagicMock import assert_not_called [as 别名]
def test_verify_log(self):
"""
Test that verify_log works as expected
"""
message = "Insecure logging configuration detected! Sensitive data may be logged."
mock_cheese = MagicMock()
with patch.object(log, "warning", mock_cheese):
verify_log({"log_level": "cheeseshop"})
mock_cheese.assert_called_once_with(message)
mock_trace = MagicMock()
with patch.object(log, "warning", mock_trace):
verify_log({"log_level": "trace"})
mock_trace.assert_called_once_with(message)
mock_none = MagicMock()
with patch.object(log, "warning", mock_none):
verify_log({})
mock_none.assert_called_once_with(message)
mock_info = MagicMock()
with patch.object(log, "warning", mock_info):
verify_log({"log_level": "info"})
mock_info.assert_not_called()
示例2: _runner
# 需要导入模块: from salttesting.mock import MagicMock [as 别名]
# 或者: from salttesting.mock.MagicMock import assert_not_called [as 别名]
def _runner(self, expected_ret, test=False, check_set=False, new_set=None,
new_set_assertion=True):
mock_check_set = MagicMock(return_value=check_set)
mock_new_set = MagicMock() if new_set is None else MagicMock(return_value=new_set)
with patch.dict(ipset.__salt__, {'ipset.check_set': mock_check_set,
'ipset.new_set': mock_new_set}):
with patch.dict(ipset.__opts__, {'test': test}):
actual_ret = ipset.set_present(self.fake_name, self.fake_set_type)
mock_check_set.assert_called_once_with(self.fake_name)
if new_set_assertion:
mock_new_set.assert_called_once_with(self.fake_name, self.fake_set_type, 'ipv4')
else:
mock_new_set.assert_not_called()
self.assertDictEqual(actual_ret, expected_ret)
示例3: test_list_pkgs_as_list
# 需要导入模块: from salttesting.mock import MagicMock [as 别名]
# 或者: from salttesting.mock.MagicMock import assert_not_called [as 别名]
def test_list_pkgs_as_list(self):
'''
Test if it lists the packages currently installed in a dict
'''
cmdmock = MagicMock(return_value='A 1.0\nB 2.0')
sortmock = MagicMock()
stringifymock = MagicMock()
mock_ret = {'A': ['1.0'], 'B': ['2.0']}
with patch.dict(pacman.__salt__, {
'cmd.run': cmdmock,
'pkg_resource.add_pkg': lambda pkgs, name, version: pkgs.setdefault(name, []).append(version),
'pkg_resource.sort_pkglist': sortmock,
'pkg_resource.stringify': stringifymock
}):
self.assertDictEqual(pacman.list_pkgs(True), mock_ret)
sortmock.assert_called_with(mock_ret)
stringifymock.assert_not_called()
示例4: _mask_unmask
# 需要导入模块: from salttesting.mock import MagicMock [as 别名]
# 或者: from salttesting.mock.MagicMock import assert_not_called [as 别名]
def _mask_unmask(self, action, runtime):
'''
Common code for mask/unmask tests
'''
# We want the traceback if the function name can't be found in the
# systemd execution module, so don't provide a fallback value for the
# call to getattr() here.
func = getattr(systemd, action)
systemctl_command = ['systemctl', action]
if runtime:
systemctl_command.append('--runtime')
systemctl_command.append(self.unit_name + '.service')
args = [self.unit_name, runtime]
masked_mock = self.mock_true if action == 'unmask' else self.mock_false
with patch.object(systemd, '_check_for_unit_changes', self.mock_none):
if action == 'unmask':
mock_not_run = MagicMock(return_value={'retcode': 0,
'stdout': '',
'stderr': '',
'pid': 12345})
with patch.dict(systemd.__salt__, {'cmd.run_all': mock_not_run}):
with patch.object(systemd, 'masked', self.mock_false):
# Test not masked (should take no action and return True)
self.assertTrue(systemd.unmask(self.unit_name))
# Also should not have called cmd.run_all
mock_not_run.assert_not_called()
with patch.object(systemd, 'masked', masked_mock):
# Has scopes available
with patch.object(salt.utils.systemd, 'has_scope', self.mock_true):
# Scope enabled, successful
with patch.dict(
systemd.__salt__,
{'config.get': self.mock_true,
'cmd.run_all': self.mock_run_all_success}):
ret = func(*args)
self.assertTrue(ret)
self.mock_run_all_success.assert_called_with(
['systemd-run', '--scope'] + systemctl_command,
python_shell=False,
redirect_stderr=True)
# Scope enabled, failed
with patch.dict(
systemd.__salt__,
{'config.get': self.mock_true,
'cmd.run_all': self.mock_run_all_failure}):
self.assertRaises(
CommandExecutionError,
func, *args)
self.mock_run_all_failure.assert_called_with(
['systemd-run', '--scope'] + systemctl_command,
python_shell=False,
redirect_stderr=True)
# Scope disabled, successful
with patch.dict(
systemd.__salt__,
{'config.get': self.mock_false,
'cmd.run_all': self.mock_run_all_success}):
ret = func(*args)
self.assertTrue(ret)
self.mock_run_all_success.assert_called_with(
systemctl_command,
python_shell=False,
redirect_stderr=True)
# Scope disabled, failed
with patch.dict(
systemd.__salt__,
{'config.get': self.mock_false,
'cmd.run_all': self.mock_run_all_failure}):
self.assertRaises(
CommandExecutionError,
func, *args)
self.mock_run_all_failure.assert_called_with(
systemctl_command,
python_shell=False,
redirect_stderr=True)
# Does not have scopes available
with patch.object(salt.utils.systemd, 'has_scope', self.mock_false):
# The results should be the same irrespective of
# whether or not scope is enabled, since scope is not
# available, so we repeat the below tests with it both
# enabled and disabled.
for scope_mock in (self.mock_true, self.mock_false):
# Successful
with patch.dict(
systemd.__salt__,
{'config.get': scope_mock,
'cmd.run_all': self.mock_run_all_success}):
ret = func(*args)
#.........这里部分代码省略.........
示例5: test_disable
# 需要导入模块: from salttesting.mock import MagicMock [as 别名]
# 或者: from salttesting.mock.MagicMock import assert_not_called [as 别名]
def test_disable(self):
"""
Test for Disable the named service to start at boot
"""
rc_update_mock = MagicMock(return_value=0)
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.disable('name'))
rc_update_mock.assert_called_once_with('rc-update delete name', python_shell=False)
rc_update_mock.reset_mock()
# disable service
service_name = 'name'
runlevels = ['l1']
level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels}))
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.disable('name', runlevels='l1'))
rc_update_mock.assert_called_once_with('rc-update delete name l1',
python_shell=False)
rc_update_mock.reset_mock()
# same as above with list
runlevels = ['l1']
level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels}))
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.disable('name', runlevels=['l1']))
rc_update_mock.assert_called_once_with('rc-update delete name l1',
python_shell=False)
rc_update_mock.reset_mock()
# remove from 'l1', and leave at 'l2'
runlevels = ['l1', 'l2']
level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels}))
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.disable('name', runlevels=['l1']))
rc_update_mock.assert_called_once_with('rc-update delete name l1',
python_shell=False)
rc_update_mock.reset_mock()
# remove from non-enabled level
runlevels = ['l2']
level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels}))
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.disable('name', runlevels=['l1']))
rc_update_mock.assert_not_called()
rc_update_mock.reset_mock()
# remove from 'l1' and 'l3', leave at 'l2'
runlevels = ['l1', 'l2', 'l3']
level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels}))
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.disable('name', runlevels=['l1', 'l3']))
rc_update_mock.assert_called_once_with('rc-update delete name l1 l3',
python_shell=False)
rc_update_mock.reset_mock()
# rc-update failed
rc_update_mock = MagicMock(return_value=1)
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertFalse(gentoo_service.disable('name'))
rc_update_mock.assert_called_once_with('rc-update delete name', python_shell=False)
rc_update_mock.reset_mock()
# move service delete failed
runlevels = ['l1']
level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels}))
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertFalse(gentoo_service.disable('name', runlevels='l1'))
rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False)
rc_update_mock.reset_mock()
# move service delete succeeds. add fails
runlevels = ['l1', 'l2', 'l3']
level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels}))
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertFalse(gentoo_service.disable('name', runlevels=['l1', 'l3']))
rc_update_mock.assert_called_once_with('rc-update delete name l1 l3',
python_shell=False)
rc_update_mock.reset_mock()
示例6: test_enable
# 需要导入模块: from salttesting.mock import MagicMock [as 别名]
# 或者: from salttesting.mock.MagicMock import assert_not_called [as 别名]
def test_enable(self):
"""
Test for Enable the named service to start at boot
"""
rc_update_mock = MagicMock(return_value=0)
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.enable('name'))
rc_update_mock.assert_called_once_with('rc-update add name', python_shell=False)
rc_update_mock.reset_mock()
# move service from 'l1' to 'l2' runlevel
service_name = 'name'
runlevels = ['l1']
level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels}))
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.enable('name', runlevels='l2'))
rc_update_mock.assert_has_calls([call('rc-update delete name l1', python_shell=False),
call('rc-update add name l2', python_shell=False)])
rc_update_mock.reset_mock()
# requested levels are the same as the current ones
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.enable('name', runlevels='l1'))
rc_update_mock.assert_not_called()
rc_update_mock.reset_mock()
# same as above with the list instead of the string
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.enable('name', runlevels=['l1']))
rc_update_mock.assert_not_called()
rc_update_mock.reset_mock()
# add service to 'l2' runlevel
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.enable('name', runlevels=['l2', 'l1']))
rc_update_mock.assert_called_once_with('rc-update add name l2', python_shell=False)
rc_update_mock.reset_mock()
# remove service from 'l1' runlevel
runlevels = ['l1', 'l2']
level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels}))
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.enable('name', runlevels=['l2']))
rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False)
rc_update_mock.reset_mock()
# move service from 'l2' add to 'l3', leaving at l1
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.enable('name', runlevels=['l1', 'l3']))
rc_update_mock.assert_has_calls([call('rc-update delete name l2', python_shell=False),
call('rc-update add name l3', python_shell=False)])
rc_update_mock.reset_mock()
# remove from l1, l3, and add to l2, l4, and leave at l5
runlevels = ['l1', 'l3', 'l5']
level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels}))
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertTrue(gentoo_service.enable('name', runlevels=['l2', 'l4', 'l5']))
rc_update_mock.assert_has_calls([call('rc-update delete name l1 l3', python_shell=False),
call('rc-update add name l2 l4', python_shell=False)])
rc_update_mock.reset_mock()
# rc-update failed
rc_update_mock = MagicMock(return_value=1)
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertFalse(gentoo_service.enable('name'))
rc_update_mock.assert_called_once_with('rc-update add name', python_shell=False)
rc_update_mock.reset_mock()
# move service delete failed
runlevels = ['l1']
level_list_mock = MagicMock(return_value=self.__services({service_name: runlevels}))
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertFalse(gentoo_service.enable('name', runlevels='l2'))
rc_update_mock.assert_called_once_with('rc-update delete name l1', python_shell=False)
rc_update_mock.reset_mock()
# move service delete succeeds. add fails
rc_update_mock = MagicMock()
rc_update_mock.side_effect = [0, 1]
with patch.dict(gentoo_service.__salt__, {'cmd.run': level_list_mock}):
with patch.dict(gentoo_service.__salt__, {'cmd.retcode': rc_update_mock}):
self.assertFalse(gentoo_service.enable('name', runlevels='l2'))
rc_update_mock.assert_has_calls([call('rc-update delete name l1', python_shell=False),
call('rc-update add name l2', python_shell=False)])
rc_update_mock.reset_mock()