本文整理汇总了Python中ansible.compat.tests.mock.Mock类的典型用法代码示例。如果您正苦于以下问题:Python Mock类的具体用法?Python Mock怎么用?Python Mock使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Mock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_create_new_lb_monitor_workflow
def test_create_new_lb_monitor_workflow(self):
set_module_args(dict(
nitro_user='user',
nitro_pass='pass',
nsip='1.1.1.1',
state='present',
save_config=False,
))
from ansible.modules.network.netscaler import netscaler_lb_monitor
lb_monitor_proxy_mock = Mock(diff_object=Mock(return_value={}))
with patch.multiple(
'ansible.modules.network.netscaler.netscaler_lb_monitor',
get_nitro_client=Mock(return_value=Mock()),
lbmonitor_exists=Mock(side_effect=[False, True]),
lbmonitor_identical=Mock(side_effect=[True]),
ConfigProxy=Mock(return_value=lb_monitor_proxy_mock),
ensure_feature_is_enabled=Mock(return_value=True),
):
self.module = netscaler_lb_monitor
result = self.exited()
lb_monitor_proxy_mock.assert_has_calls([call.add()])
self.assertTrue(result['changed'])
示例2: _mock_module
def _mock_module(self):
mock_module = Mock()
mock_module.params = {'gather_subset': self.gather_subset,
'gather_timeout': 5,
'filter': '*'}
mock_module.get_bin_path = Mock(return_value=None)
return mock_module
示例3: test_graceful_nitro_error_on_login
def test_graceful_nitro_error_on_login(self):
set_module_args(dict(
nitro_user='user',
nitro_pass='pass',
nsip='1.1.1.1',
state='present',
))
from ansible.modules.network.netscaler import netscaler_lb_monitor
class MockException(Exception):
def __init__(self, *args, **kwargs):
self.errorcode = 0
self.message = ''
client_mock = Mock()
client_mock.login = Mock(side_effect=MockException)
m = Mock(return_value=client_mock)
with patch.multiple(
'ansible.modules.network.netscaler.netscaler_lb_monitor',
get_nitro_client=m,
nitro_exception=MockException,
):
self.module = netscaler_lb_monitor
result = self.failed()
self.assertTrue(result['msg'].startswith('nitro exception'), msg='nitro exception during login not handled properly')
示例4: test_udevadm_uuid
def test_udevadm_uuid(self):
module = Mock()
module.run_command = Mock(return_value=(0, UDEVADM_OUTPUT, '')) # (rc, out, err)
lh = linux.LinuxHardware(module=module, load_on_init=False)
udevadm_uuid = lh._udevadm_uuid('mock_device')
self.assertEqual(udevadm_uuid, '57b1a3e7-9019-4747-9809-7ec52bba9179')
示例5: test_present_server_identical_flow
def test_present_server_identical_flow(self):
set_module_args(dict(
nitro_user='user',
nitro_pass='pass',
nsip='1.1.1.1',
state='present',
))
from ansible.modules.network.netscaler import netscaler_server
client_mock = Mock()
m = Mock(return_value=client_mock)
server_proxy_attrs = {
'diff_object.return_value': {},
}
server_proxy_mock = Mock()
server_proxy_mock.configure_mock(**server_proxy_attrs)
config_proxy_mock = Mock(return_value=server_proxy_mock)
with patch.multiple(
'ansible.modules.network.netscaler.netscaler_server',
get_nitro_client=m,
diff_list=Mock(return_value={}),
get_immutables_intersection=Mock(return_value=[]),
server_exists=Mock(side_effect=[True, True]),
server_identical=Mock(side_effect=[True, True]),
ConfigProxy=config_proxy_mock,
do_state_change=Mock(return_value=Mock(errorcode=0))
):
self.module = netscaler_server
self.exited()
server_proxy_mock.assert_not_called()
示例6: test_absent_gslb_site_failed_delete
def test_absent_gslb_site_failed_delete(self):
set_module_args(dict(
nitro_user='user',
nitro_pass='pass',
nsip='1.1.1.1',
state='absent',
))
from ansible.modules.network.netscaler import netscaler_gslb_site
client_mock = Mock()
m = Mock(return_value=client_mock)
glsb_site_proxy_attrs = {
'diff_object.return_value': {},
}
gslb_site_proxy_mock = Mock()
gslb_site_proxy_mock.configure_mock(**glsb_site_proxy_attrs)
config_proxy_mock = Mock(return_value=gslb_site_proxy_mock)
with patch.multiple(
'ansible.modules.network.netscaler.netscaler_gslb_site',
nitro_exception=self.MockException,
get_nitro_client=m,
diff_list=Mock(return_value={}),
get_immutables_intersection=Mock(return_value=[]),
gslb_site_exists=Mock(side_effect=[True, True]),
gslb_site_identical=Mock(side_effect=[False, False]),
ensure_feature_is_enabled=Mock(),
ConfigProxy=config_proxy_mock,
):
self.module = netscaler_gslb_site
result = self.failed()
self.assertEqual(result['msg'], 'GSLB site still exists')
self.assertTrue(result['failed'])
示例7: test_absent_server_noop_flow
def test_absent_server_noop_flow(self):
set_module_args(dict(
nitro_user='user',
nitro_pass='pass',
nsip='1.1.1.1',
state='absent',
))
from ansible.modules.network.netscaler import netscaler_ssl_certkey
client_mock = Mock()
m = Mock(return_value=client_mock)
ssl_certkey_proxy_attrs = {
'diff_object.return_value': {},
}
ssl_certkey_proxy_mock = Mock()
ssl_certkey_proxy_mock.configure_mock(**ssl_certkey_proxy_attrs)
config_proxy_mock = Mock(return_value=ssl_certkey_proxy_mock)
with patch.multiple(
'ansible.modules.network.netscaler.netscaler_ssl_certkey',
get_nitro_client=m,
diff_list=Mock(return_value={}),
get_immutables_intersection=Mock(return_value=[]),
key_exists=Mock(side_effect=[False, False]),
key_identical=Mock(side_effect=[False, False]),
ConfigProxy=config_proxy_mock,
):
self.module = netscaler_ssl_certkey
self.exited()
ssl_certkey_proxy_mock.assert_not_called()
示例8: test_ssl_bindings_not_called_for_non_ssl_service
def test_ssl_bindings_not_called_for_non_ssl_service(self):
set_module_args(dict(
nitro_user='user',
nitro_pass='pass',
nsip='1.1.1.1',
state='present',
save_config=False,
servicetype='HTTP',
))
from ansible.modules.network.netscaler import netscaler_lb_vserver
lb_vserver_proxy_mock = Mock()
ssl_sync_mock = Mock()
client_mock = Mock()
with patch.multiple(
'ansible.modules.network.netscaler.netscaler_lb_vserver',
get_nitro_client=Mock(return_value=client_mock),
lb_vserver_exists=Mock(side_effect=[True, True]),
lb_vserver_identical=Mock(side_effect=[False, True]),
servicegroup_bindings_identical=Mock(side_effect=[True, True]),
service_bindings_identical=Mock(side_effect=[True, True]),
ssl_certkey_bindings_identical=Mock(side_effect=[False, True]),
ssl_certkey_bindings_sync=ssl_sync_mock,
ConfigProxy=Mock(return_value=lb_vserver_proxy_mock),
ensure_feature_is_enabled=Mock(return_value=True),
do_state_change=Mock(return_value=Mock(errorcode=0)),
get_immutables_intersection=(Mock(return_value=[])),
):
self.module = netscaler_lb_vserver
result = self.exited()
ssl_sync_mock.assert_not_called()
self.assertTrue(result['changed'])
示例9: test_new_ssl_certkey_execution_flow
def test_new_ssl_certkey_execution_flow(self):
set_module_args(dict(
nitro_user='user',
nitro_pass='pass',
nsip='1.1.1.1',
state='present',
))
from ansible.modules.network.netscaler import netscaler_ssl_certkey
client_mock = Mock()
m = Mock(return_value=client_mock)
ssl_certkey_proxy_attrs = {
'diff_object.return_value': {},
}
ssl_certkey_proxy_mock = Mock()
ssl_certkey_proxy_mock.configure_mock(**ssl_certkey_proxy_attrs)
config_proxy_mock = Mock(return_value=ssl_certkey_proxy_mock)
with patch.multiple(
'ansible.modules.network.netscaler.netscaler_ssl_certkey',
get_nitro_client=m,
key_exists=Mock(side_effect=[False, True]),
key_identical=Mock(side_effect=[True]),
ConfigProxy=config_proxy_mock,
):
self.module = netscaler_ssl_certkey
self.exited()
ssl_certkey_proxy_mock.assert_has_calls([call.add()])
示例10: test_ensure_feature_is_enabled_called
def test_ensure_feature_is_enabled_called(self):
set_module_args(dict(
nitro_user='user',
nitro_pass='pass',
nsip='1.1.1.1',
state='present',
save_config=False,
))
from ansible.modules.network.netscaler import netscaler_lb_vserver
client_mock = Mock()
lb_vserver_proxy_mock = Mock()
feature_mock = Mock()
with patch.multiple(
'ansible.modules.network.netscaler.netscaler_lb_vserver',
get_nitro_client=Mock(return_value=client_mock),
lb_vserver_exists=Mock(side_effect=[True, True]),
lb_vserver_identical=Mock(side_effect=[True, True]),
servicegroup_bindings_identical=Mock(side_effect=[True, True]),
service_bindings_identical=Mock(side_effect=[True, True]),
ConfigProxy=Mock(return_value=lb_vserver_proxy_mock),
ensure_feature_is_enabled=feature_mock,
do_state_change=Mock(return_value=Mock(errorcode=0)),
):
self.module = netscaler_lb_vserver
self.exited()
feature_mock.assert_called_with(client_mock, 'LB')
示例11: test_main_show
def test_main_show(self, mock_module, mock_connect, mock_info, mock_comp,
mock_server_conf, mock_port_conf, mock_conf_action):
''' Test main good with show action.
'''
mock_module_object = Mock()
mock_module_object.params = dict(action='show', switch_name='eos',
switch_port='3', auto_run=False)
mock_module.return_value = mock_module_object
mock_connect.return_value = 'Client'
mock_info.return_value = 'Info'
mock_server_conf.return_value = 'Configlet'
mock_port_conf.return_value = 'Port'
mock_conf_action.return_value = dict()
cv_server_provision.main()
self.assertEqual(mock_connect.call_count, 1)
self.assertEqual(mock_info.call_count, 1)
mock_comp.assert_not_called()
self.assertEqual(mock_server_conf.call_count, 1)
self.assertEqual(mock_port_conf.call_count, 1)
self.assertEqual(mock_conf_action.call_count, 1)
mock_module_object.fail_json.assert_not_called()
return_dict = dict(changed=False, switchInfo='Info',
switchConfigurable=True, portConfigurable=True,
taskCreated=False, taskExecuted=False,
taskCompleted=False)
mock_module_object.exit_json.assert_called_with(**return_dict)
示例12: test_present_cs_action_failed_create
def test_present_cs_action_failed_create(self):
set_module_args(dict(
nitro_user='user',
nitro_pass='pass',
nsip='1.1.1.1',
state='present',
))
from ansible.modules.network.netscaler import netscaler_cs_action
client_mock = Mock()
m = Mock(return_value=client_mock)
server_proxy_attrs = {
'diff_object.return_value': {},
}
cs_action_proxy_mock = Mock()
cs_action_proxy_mock.configure_mock(**server_proxy_attrs)
config_proxy_mock = Mock(return_value=cs_action_proxy_mock)
with patch.multiple(
'ansible.modules.network.netscaler.netscaler_cs_action',
nitro_exception=self.MockException,
get_nitro_client=m,
diff_list=Mock(return_value={}),
get_immutables_intersection=Mock(return_value=[]),
action_exists=Mock(side_effect=[False, False]),
action_identical=Mock(side_effect=[False, False]),
ensure_feature_is_enabled=Mock(return_value=True),
ConfigProxy=config_proxy_mock,
):
self.module = netscaler_cs_action
result = self.failed()
self.assertEqual(result['msg'], 'Content switching action does not exist')
self.assertTrue(result['failed'])
示例13: test_absent_server_failed_delete
def test_absent_server_failed_delete(self):
set_module_args(dict(
nitro_user='user',
nitro_pass='pass',
nsip='1.1.1.1',
state='absent',
))
from ansible.modules.network.netscaler import netscaler_server
client_mock = Mock()
m = Mock(return_value=client_mock)
server_proxy_attrs = {
'diff_object.return_value': {},
}
server_proxy_mock = Mock()
server_proxy_mock.configure_mock(**server_proxy_attrs)
config_proxy_mock = Mock(return_value=server_proxy_mock)
with patch.multiple(
'ansible.modules.network.netscaler.netscaler_server',
nitro_exception=self.MockException,
get_nitro_client=m,
diff_list=Mock(return_value={}),
get_immutables_intersection=Mock(return_value=[]),
server_exists=Mock(side_effect=[True, True]),
server_identical=Mock(side_effect=[False, False]),
ConfigProxy=config_proxy_mock,
do_state_change=Mock(return_value=Mock(errorcode=0))
):
self.module = netscaler_server
result = self.failed()
self.assertEqual(result['msg'], 'Server seems to be present')
self.assertTrue(result['failed'])
示例14: test_config_from_template_fail_no_vlan
def test_config_from_template_fail_no_vlan(self, mock_file_sys, mock_env,
mock_debug, mock_find):
''' Test config_from_template fail. Template needs vlan but none provided.
'''
module = Mock()
module.params = dict(switch_name='eos', switch_port='3',
server_name='new', template='jinja.j2',
port_vlan=None)
mock_file_sys.return_value = 'file'
mock_debug.return_value = 'debug'
template_mock = Mock()
template_mock.render.return_value = ('interface Ethernet3\n'
' description test\n!')
env_mock = Mock()
env_mock.loader.get_source.return_value = ['one', 'two']
env_mock.parse.return_value = 'parsed'
env_mock.get_template.return_value = template_mock
mock_env.return_value = env_mock
mock_find.return_value = dict(server_name=None, switch_port=None,
port_vlan=None)
result = cv_server_provision.config_from_template(module)
self.assertIsNotNone(result)
expected = 'interface Ethernet3\n description test\n!'
self.assertEqual(result, expected)
self.assertEqual(mock_file_sys.call_count, 1)
self.assertEqual(mock_env.call_count, 1)
module.fail_json.assert_called_with(msg='Template jinja.j2 requires a'
' vlan. Please re-run with vlan'
' number provided.')
示例15: test_new_gslb_vserver_execution_flow
def test_new_gslb_vserver_execution_flow(self):
set_module_args(dict(
nitro_user='user',
nitro_pass='pass',
nsip='1.1.1.1',
state='present',
))
from ansible.modules.network.netscaler import netscaler_gslb_vserver
client_mock = Mock()
m = Mock(return_value=client_mock)
glsb_service_proxy_attrs = {
'diff_object.return_value': {},
}
gslb_service_proxy_mock = Mock()
gslb_service_proxy_mock.configure_mock(**glsb_service_proxy_attrs)
config_proxy_mock = Mock(return_value=gslb_service_proxy_mock)
with patch.multiple(
'ansible.modules.network.netscaler.netscaler_gslb_vserver',
get_nitro_client=m,
gslb_vserver_exists=Mock(side_effect=[False, True]),
gslb_vserver_identical=Mock(side_effect=[True]),
nitro_exception=self.MockException,
do_state_change=Mock(return_value=Mock(errorcode=0)),
ensure_feature_is_enabled=Mock(),
ConfigProxy=config_proxy_mock,
):
self.module = netscaler_gslb_vserver
self.exited()
gslb_service_proxy_mock.assert_has_calls([call.add()])