本文整理汇总了Python中salttesting.mock.MagicMock类的典型用法代码示例。如果您正苦于以下问题:Python MagicMock类的具体用法?Python MagicMock怎么用?Python MagicMock使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MagicMock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_install_cached_requirements_used
def test_install_cached_requirements_used(self, get_cached_requirements):
get_cached_requirements.return_value = "my_cached_reqs"
mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
pip.install(requirements="salt://requirements.txt")
expected_cmd = "pip install --requirement='my_cached_reqs'"
mock.assert_called_once_with(expected_cmd, saltenv="base", runas=None, cwd=None)
示例2: test_check_mine_cache_is_refreshed_on_container_change_event
def test_check_mine_cache_is_refreshed_on_container_change_event(self, _):
'''
Every command that might modify docker containers state.
Should trig an update on ``mine.send``
'''
for command_name, args in (('create', ()),
('rm_', ()),
('kill', ()),
('pause', ()),
('signal_', ('KILL',)),
('start', ()),
('stop', ()),
('unpause', ()),
('_run', ('command',)),
('_script', ('command',)),
):
mine_send = Mock()
command = getattr(dockerng_mod, command_name)
docker_client = MagicMock()
docker_client.api_version = '1.12'
with patch.dict(dockerng_mod.__salt__,
{'mine.send': mine_send,
'container_resource.run': MagicMock(),
'cp.cache_file': MagicMock(return_value=False)}):
with patch.dict(dockerng_mod.__context__,
{'docker.client': docker_client}):
command('container', *args)
mine_send.assert_called_with('dockerng.ps', verbose=True, all=True,
host=True)
示例3: test__rvm
def test__rvm(self):
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(rvm.__salt__, {'cmd.run_all': mock}):
rvm._rvm('install', '1.9.3')
mock.assert_called_once_with(
'/usr/local/rvm/bin/rvm install 1.9.3', runas=None
)
示例4: test_set_proxy_windows
def test_set_proxy_windows(self):
'''
Test to make sure we can set the proxy settings on Windows
'''
proxy.__grains__['os'] = 'Windows'
expected = {
'changes': {},
'comment': 'Proxy settings updated correctly',
'name': '192.168.0.1',
'result': True
}
set_proxy_mock = MagicMock(return_value=True)
patches = {
'proxy.get_proxy_win': MagicMock(return_value={}),
'proxy.get_proxy_bypass': MagicMock(return_value=[]),
'proxy.set_proxy_win': set_proxy_mock,
}
with patch.dict(proxy.__salt__, patches):
out = proxy.managed('192.168.0.1', '3128', user='frank', password='passw0rd',
bypass_domains=['salt.com', 'test.com'])
set_proxy_mock.assert_called_once_with('192.168.0.1', '3128', ['http', 'https', 'ftp'],
['salt.com', 'test.com'])
self.assertEqual(out, expected)
示例5: test_list_command_with_prefix
def test_list_command_with_prefix(self):
eggs = [
'M2Crypto==0.21.1',
'-e [email protected]:s0undt3ch/[email protected]#egg=SaltTesting-dev',
'bbfreeze==1.1.0',
'bbfreeze-loader==1.1.0',
'pycrypto==2.6'
]
mock = MagicMock(
return_value={
'retcode': 0,
'stdout': '\n'.join(eggs)
}
)
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
ret = pip.list_(prefix='bb')
mock.assert_called_with(
'pip freeze',
runas=None,
cwd=None,
python_shell=False,
)
self.assertEqual(
ret, {
'bbfreeze-loader': '1.1.0',
'bbfreeze': '1.1.0',
}
)
示例6: test_uninstall_log_argument_in_resulting_command
def test_uninstall_log_argument_in_resulting_command(self, mock_path):
pkg = 'pep8'
log_path = '/tmp/pip-install.log'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.uninstall(pkg, log=log_path)
mock.assert_called_once_with(
['pip', 'uninstall', '-y', '--log', log_path, pkg],
saltenv='base',
runas=None,
cwd=None,
use_vt=False,
python_shell=False,
)
# Let's fake a non-writable log file
mock_path.exists.side_effect = IOError('Fooo!')
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
self.assertRaises(
IOError,
pip.uninstall,
pkg,
log=log_path
)
示例7: test_get_managed_object_name
def test_get_managed_object_name(self):
mock_get_managed_object_name = MagicMock()
with patch('salt.utils.vmware.get_managed_object_name',
mock_get_managed_object_name):
vmware.create_cluster(self.mock_dc, 'fake_cluster',
self.mock_cluster_spec)
mock_get_managed_object_name.assert_called_once_with(self.mock_dc)
示例8: test_uwsgi_stats
def test_uwsgi_stats(self):
socket = "127.0.0.1:5050"
mock = MagicMock(return_value='{"a": 1, "b": 2}')
with patch.dict(uwsgi.__salt__, {"cmd.run": mock}):
result = uwsgi.stats(socket)
mock.assert_called_once_with(["uwsgi", "--connect-and-read", "{0}".format(socket)], python_shell=False)
self.assertEqual(result, {"a": 1, "b": 2})
示例9: test_delete_volume
def test_delete_volume(self):
'''
Test if it deletes a gluster volume.
'''
mock_info = MagicMock(return_value={'Newvolume1': {'status': '1'}})
with patch.object(glusterfs, 'info', mock_info):
# volume doesn't exist
self.assertFalse(glusterfs.delete_volume('Newvolume3'))
mock_stop_volume = MagicMock(return_value=True)
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {'cmd.run': mock_run}):
with patch.object(glusterfs, 'stop_volume', mock_stop_volume):
# volume exists, should not be stopped, and is started
self.assertFalse(glusterfs.delete_volume('Newvolume1',
False))
self.assertFalse(mock_run.called)
self.assertFalse(mock_stop_volume.called)
# volume exists, should be stopped, and is started
self.assertTrue(glusterfs.delete_volume('Newvolume1'))
self.assertTrue(mock_run.called)
self.assertTrue(mock_stop_volume.called)
# volume exists and isn't started
mock_info = MagicMock(return_value={'Newvolume1': {'status': '2'}})
with patch.object(glusterfs, 'info', mock_info):
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {'cmd.run': mock_run}):
self.assertTrue(glusterfs.delete_volume('Newvolume1'))
mock_run.return_value = xml_command_fail
self.assertFalse(glusterfs.delete_volume('Newvolume1'))
示例10: test_list_pkgs
def test_list_pkgs(self):
'''
Test for listing installed packages.
'''
def _add_data(data, key, value):
data[key] = value
pkg_info_out = [
'png-1.6.23',
'vim-7.4.1467p1-gtk2', # vim--gtk2
'ruby-2.3.1p1' # ruby%2.3
]
run_stdout_mock = MagicMock(return_value='\n'.join(pkg_info_out))
patches = {
'cmd.run_stdout': run_stdout_mock,
'pkg_resource.add_pkg': _add_data,
'pkg_resource.sort_pkglist': MagicMock(),
'pkg_resource.stringify': MagicMock(),
}
with patch.dict(openbsdpkg.__salt__, patches):
pkgs = openbsdpkg.list_pkgs()
self.assertDictEqual(pkgs, {
'png': '1.6.23',
'vim--gtk2': '7.4.1467p1',
'ruby': '2.3.1p1'})
run_stdout_mock.assert_called_once_with('pkg_info -q -a',
output_loglevel='trace')
示例11: test_symlinks_argument
def test_symlinks_argument(self):
# We test for pyvenv only because with virtualenv this is un
# unsupported option.
mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
with patch.dict(virtualenv_mod.__salt__, {"cmd.run_all": mock}):
virtualenv_mod.create("/tmp/foo", venv_bin="pyvenv", symlinks=True)
mock.assert_called_once_with(["pyvenv", "--symlinks", "/tmp/foo"], runas=None, python_shell=False)
示例12: test_list_command
def test_list_command(self):
eggs = [
"M2Crypto==0.21.1",
"-e [email protected]:s0undt3ch/[email protected]#egg=SaltTesting-dev",
"bbfreeze==1.1.0",
"bbfreeze-loader==1.1.0",
"pycrypto==2.6",
]
mock = MagicMock(
side_effect=[{"retcode": 0, "stdout": "pip MOCKED_VERSION"}, {"retcode": 0, "stdout": "\n".join(eggs)}]
)
with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
ret = pip.list_()
mock.assert_called_with("pip freeze", runas=None, cwd=None)
self.assertEqual(
ret,
{
"SaltTesting-dev": "[email protected]:s0undt3ch/[email protected]",
"M2Crypto": "0.21.1",
"bbfreeze-loader": "1.1.0",
"bbfreeze": "1.1.0",
"pip": "MOCKED_VERSION",
"pycrypto": "2.6",
},
)
# Non zero returncode raises exception?
mock = MagicMock(return_value={"retcode": 1, "stderr": "CABOOOOMMM!"})
with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
self.assertRaises(CommandExecutionError, pip.list_)
示例13: test_install_download_cache_argument_in_resulting_command
def test_install_download_cache_argument_in_resulting_command(self):
mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
pip.install("pep8", download_cache="/tmp/foo")
mock.assert_called_once_with(
"pip install --download-cache=/tmp/foo 'pep8'", saltenv="base", runas=None, cwd=None
)
示例14: test_install_extra_index_url_argument_in_resulting_command
def test_install_extra_index_url_argument_in_resulting_command(self):
mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
pip.install("pep8", extra_index_url="http://foo.tld")
mock.assert_called_once_with(
"pip install --extra-index-url='http://foo.tld' 'pep8'", saltenv="base", runas=None, cwd=None
)
示例15: run_contents_pillar
def run_contents_pillar(self, pillar_value, expected):
def returner(contents, *args, **kwargs):
returner.returned = (contents, args, kwargs)
returner.returned = None
filestate.__salt__ = {
'file.manage_file': returner
}
path = '/tmp/foo'
pillar_path = 'foo:bar'
# the values don't matter here
filestate.__salt__['config.manage_mode'] = MagicMock()
filestate.__salt__['file.source_list'] = MagicMock(return_value=[None, None])
filestate.__salt__['file.get_managed'] = MagicMock(return_value=[None, None, None])
# pillar.get should return the pillar_value
pillar_mock = MagicMock(return_value=pillar_value)
filestate.__salt__['pillar.get'] = pillar_mock
ret = filestate.managed(path, contents_pillar=pillar_path)
# make sure the pillar_mock is called with the given path
pillar_mock.assert_called_once_with(pillar_path)
# make sure no errors are returned
self.assertEquals(None, ret)
# make sure the value is correct
self.assertEquals(expected, returner.returned[1][-1])