本文整理汇总了Python中mock.Mock.connect方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.connect方法的具体用法?Python Mock.connect怎么用?Python Mock.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.Mock
的用法示例。
在下文中一共展示了Mock.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_TestNetworkAdaptersRetriever_E0
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import connect [as 别名]
def test_TestNetworkAdaptersRetriever_E0(self):
content = Mock()
si = create_autospec(spec=vim.ServiceInstance)
si.RetrieveContent = Mock(return_value=content)
pvServiceConnectError = Mock()
pvServiceConnectError.connect = Mock(side_effect=Exception('TEST Exception'))
pvServiceConnectNone = Mock()
pvServiceConnectNone.connect = Mock(return_value=si)
pvServiceConnectNone.find_network_by_name = Mock(return_value=None)
csRetrieverService = Mock()
csRetrieverService.getVCenterInventoryPathAttributeData = Mock(
return_value={'vCenter_resource_name': 'Resource Name',
"vm_folder": "TEST_FOLDER"})
helpers.get_resource_context_details = Mock(return_value={})
connDetails = create_autospec(VCenterConnectionDetails)
resourceConnectionDetailsRetriever = Mock()
resourceConnectionDetailsRetriever.getConnectionDetails = Mock(return_value=connDetails)
switchRetriever = NetworkAdaptersRetrieverCommand(pvServiceConnectError, csRetrieverService, resourceConnectionDetailsRetriever)
with self.assertRaises(ConnectionException) as context:
switchRetriever.execute()
self.assertTrue('User:' in context.exception.message)
switchRetriever = NetworkAdaptersRetrieverCommand(pvServiceConnectNone, csRetrieverService, resourceConnectionDetailsRetriever)
nics = switchRetriever.execute()
self.assertIsNone(nics)
示例2: test_create_endpoint_existing_channel
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import connect [as 别名]
def test_create_endpoint_existing_channel(self):
ch = Mock(spec=SendChannel)
e = self._ef.create_endpoint(existing_channel=ch)
self.assertEquals(e.channel, ch)
self.assertEquals(ch.connect.call_count, 0)
ch.connect("exist")
ch.connect.assert_called_once_with('exist')
e.close()
示例3: test_TestNetworkAdaptersRetriever
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import connect [as 别名]
def test_TestNetworkAdaptersRetriever(self):
content = Mock()
si = create_autospec(spec=vim.ServiceInstance)
si.RetrieveContent = Mock(return_value=content)
vNic = create_autospec(spec=vim.vm.device.VirtualEthernetCard)
vNic.deviceInfo = Mock()
vNic.deviceInfo.summary = 'network1'
vNic.macAddress = 'AA-BB'
vNic.connectable = Mock()
vNic.connectable.connected = 'True'
vNic.connectable.startConnected = 'True'
vmMachine = Mock()
vmMachine.config.hardware.device = [vNic]
pvService = Mock()
pvService.connect = Mock(return_value=si)
pvService.get_obj = Mock(return_value=vmMachine)
pvService.find_network_by_name = Mock(return_value=vmMachine)
pvServiceConnectError = Mock()
pvServiceConnectError.connect = Mock(side_effect=Exception('TEST Exception'))
pvServiceConnectNone = Mock()
pvServiceConnectNone.connect = Mock(return_value=None)
csRetrieverService = Mock()
csRetrieverService.getVCenterInventoryPathAttributeData = Mock(
return_value={'vCenter_resource_name': 'Resource Name',
"vm_folder": "TEST_FOLDER"})
helpers.get_resource_context_details = Mock(return_value={})
connDetails = create_autospec(VCenterConnectionDetails)
connDetails.host = 'host'
connDetails.user = 'user'
connDetails.password = 'pwd'
connDetails.port = 443
resourceConnectionDetailsRetriever = Mock()
resourceConnectionDetailsRetriever.getConnectionDetails = Mock(return_value=connDetails)
switchRetriever = NetworkAdaptersRetrieverCommand(pvService, csRetrieverService, resourceConnectionDetailsRetriever)
nics = switchRetriever.execute()
self.assertEqual(nics[0].networkLabel, 'network1')
self.assertEqual(nics[0].macAddress, 'AA-BB')
self.assertEqual(nics[0].connected, 'True')
self.assertEqual(nics[0].connectAtPowerOn, 'True')
示例4: test_command_can_be_executed_succesfully
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import connect [as 别名]
def test_command_can_be_executed_succesfully(self):
ssh_lib_mocked = Mock()
ssh_lib_mocked.connect = Mock(return_value=True)
ssh_lib_mocked.exec_command = Mock(return_value=('test', ['test'], ''))
ssh_lib_mocked.close = Mock(return_value=True)
assert 'test ' == SSHClient(ssh_lib_mocked, self.__generate_test_logger()).exec_command('ls', 'unit', 'test')
示例5: test_success
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import connect [as 别名]
def test_success(self):
"""
Connect a stream via a circuit
"""
reactor = Mock()
torstate = Mock()
target = Mock()
target.connect = Mock(return_value=defer.succeed('fake proto'))
circ = Mock()
circ.state = 'NEW'
src_addr = Mock()
src_addr.host = 'host'
src_addr.port = 1234
target._get_address = Mock(return_value=defer.succeed(src_addr))
stream = Mock()
stream.source_port = 1234
stream.source_addr = 'host'
# okay, so we fire up our circuit-endpoint with mostly mocked
# things, and a circuit that's already in 'FAILED' state.
ep = TorCircuitEndpoint(reactor, torstate, circ, target)
# should get a Failure from the connect()
d = ep.connect(Mock())
attacher = yield _get_circuit_attacher(reactor, torstate)
yield attacher.attach_stream(stream, [circ])
proto = yield d
self.assertEqual(proto, 'fake proto')
示例6: test_cancel
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import connect [as 别名]
def test_cancel(self, fake_sleep):
"""
if we start a component but call .stop before it connects, ever,
it should still exit properly
"""
endpoint = Mock()
directlyProvides(endpoint, IStreamClientEndpoint)
component = Component(
transports={
"type": "websocket",
"url": "ws://127.0.0.1/ws",
"endpoint": endpoint,
}
)
def connect(factory, **kw):
return Deferred()
endpoint.connect = connect
# XXX it would actually be nicer if we *could* support
# passing a reactor in here, but the _batched_timer =
# make_batched_timer() stuff (slash txaio in general)
# makes this "hard".
reactor = Clock()
with replace_loop(reactor):
d = component.start(reactor=reactor)
component.stop()
yield d
示例7: test_connect_no_auth_method
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import connect [as 别名]
def test_connect_no_auth_method(self, fake_sleep):
endpoint = Mock()
directlyProvides(endpoint, IStreamClientEndpoint)
component = Component(
transports={
"type": "websocket",
"url": "ws://127.0.0.1/ws",
"endpoint": endpoint,
},
is_fatal=lambda e: True,
)
def connect(factory, **kw):
proto = factory.buildProtocol('boom')
proto.makeConnection(Mock())
from autobahn.websocket.protocol import WebSocketProtocol
from base64 import b64encode
from hashlib import sha1
key = proto.websocket_key + WebSocketProtocol._WS_MAGIC
proto.data = (
b"HTTP/1.1 101 Switching Protocols\x0d\x0a"
b"Upgrade: websocket\x0d\x0a"
b"Connection: upgrade\x0d\x0a"
b"Sec-Websocket-Protocol: wamp.2.json\x0d\x0a"
b"Sec-Websocket-Accept: " + b64encode(sha1(key).digest()) + b"\x0d\x0a\x0d\x0a"
)
proto.processHandshake()
from autobahn.wamp import role
subrole = role.RoleSubscriberFeatures()
msg = Hello(u"realm", roles=dict(subscriber=subrole), authmethods=[u"anonymous"])
serializer = JsonSerializer()
data, is_binary = serializer.serialize(msg)
proto.onMessage(data, is_binary)
msg = Abort(reason=u"wamp.error.no_auth_method")
proto.onMessage(*serializer.serialize(msg))
proto.onClose(False, 100, u"wamp.error.no_auth_method")
return succeed(proto)
endpoint.connect = connect
# XXX it would actually be nicer if we *could* support
# passing a reactor in here, but the _batched_timer =
# make_batched_timer() stuff (slash txaio in general)
# makes this "hard".
reactor = Clock()
with replace_loop(reactor):
with self.assertRaises(RuntimeError) as ctx:
d = component.start(reactor=reactor)
# make sure we fire all our time-outs
reactor.advance(3600)
yield d
self.assertIn(
"Exhausted all transport",
str(ctx.exception)
)
示例8: test_should_add_menu_iten
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import connect [as 别名]
def test_should_add_menu_iten(self):
# Set
ind = NewrelicIndicator()
# Mocks
method_callback = Mock(name='Method')
menu_obj = Mock(name='Menu')
menu_obj.append = Mock()
menu_iten_obj = Mock(name='MenuItem')
menu_iten_obj.connect = Mock()
menu_iten_obj.show = Mock()
ind.indicator = Mock(name='Indicator')
ind.indicator.set_menu = Mock()
gtk.Menu = Mock(return_value=menu_obj)
gtk.MenuItem = Mock(return_value=menu_iten_obj)
# Assertion
ind._NewrelicIndicator__add_menu_iten('ItemName', method_callback)
gtk.MenuItem.assert_called_once_with('ItemName')
self.assertEquals(ind.menu, menu_obj)
menu_iten_obj.connect.assert_called_once_with("activate", method_callback)
menu_iten_obj.show.assert_called_once()
menu_obj.append.assert_called_once_with(menu_iten_obj)
ind.indicator.set_menu.assert_called_once_with(menu_obj)
示例9: test_connect_to
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import connect [as 别名]
def test_connect_to(self):
from functions import connect_to
with nested(
patch('functions.log_write'),
patch('sys.stdout', new=StringIO()),
patch('functions.get_datetime'),
) as (log_write, stdout, get_dt):
get_dt.return_value = {'date': '42', 'time': '42'}
s = Mock()
s.connect.side_effect = IOError()
address = ('server', 'port')
message = 'Could not connect to {0}\n{1}'.format(address, '\n')
self.assertFalse(connect_to(address, s, 'foo'))
self.assertEqual(stdout.getvalue(), message)
log_write.assert_called_with('foo', '42', ' <> ', message)
s.connect.side_effect = None
s.connect = Mock()
self.assertTrue(connect_to(address, s, 'foo'))
s.connect.assert_called_with(address)
示例10: test_connect_tls_context
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import connect [as 别名]
def test_connect_tls_context(self):
socks_ep = Mock()
transport = proto_helpers.StringTransport()
def connect(factory):
factory.startFactory()
proto = factory.buildProtocol("addr")
proto.makeConnection(transport)
self.assertEqual(b'\x05\x01\x00', transport.value())
proto.dataReceived(b'\x05\x00')
proto.dataReceived(b'\x05\x00\x00\x01\x00\x00\x00\x00\x00\x00')
return proto
socks_ep.connect = connect
protocol = Mock()
factory = Mock()
factory.buildProtocol = Mock(return_value=protocol)
from OpenSSL import SSL
class CertificateOptions(object):
def getContext(self, *args, **kw):
return SSL.Context(SSL.TLSv1_METHOD)
ep = socks.TorSocksEndpoint(socks_ep, u'meejah.ca', 443, tls=CertificateOptions())
proto = yield ep.connect(factory)
self.assertEqual(proto, protocol)
示例11: test_circuit_stream_failure
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import connect [as 别名]
def test_circuit_stream_failure(self):
"""
If the stream-attach fails the error propagates
"""
reactor = Mock()
torstate = Mock()
target = Mock()
target.connect = Mock(return_value=defer.succeed(None))
circ = Mock()
circ.state = 'FAILED'
src_addr = Mock()
src_addr.host = 'host'
src_addr.port = 1234
target._get_address = Mock(return_value=defer.succeed(src_addr))
stream = Mock()
stream.source_port = 1234
stream.source_addr = 'host'
# okay, so we fire up our circuit-endpoint with mostly mocked
# things, and a circuit that's already in 'FAILED' state.
ep = TorCircuitEndpoint(reactor, torstate, circ, target)
# should get a Failure from the connect()
d = ep.connect(Mock())
attacher = yield _get_circuit_attacher(reactor, Mock())
attacher.attach_stream_failure(stream, RuntimeError("a bad thing"))
try:
yield d
self.fail("Should get exception")
except RuntimeError as e:
self.assertEqual("a bad thing", str(e))
示例12: test_connect
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import connect [as 别名]
def test_connect(self):
# Arrange
si = Mock()
py_vmomi_service = Mock()
py_vmomi_service.connect = Mock(return_value=si)
resource_connection_details_retriever = Mock()
dv_port_group_creator = MagicMock()
virtual_machine_port_group_configurer = MagicMock()
virtual_switch_to_machine_connector = VirtualSwitchToMachineConnector(py_vmomi_service,
resource_connection_details_retriever,
dv_port_group_creator,
virtual_machine_port_group_configurer)
virtual_machine_path = 'ParentFlder\\ChildFolder'
virtual_machine_name = 'MachineName'
vm_uuid = uuid.UUID('{12345678-1234-5678-1234-567812345678}')
port_group_path = 'QualiSB'
dv_switch_path = 'QualiSB'
dv_switch_name = 'dvSwitch'
dv_port_name = 'dv_port_name'
# Act
virtual_switch_to_machine_connector.connect(virtual_machine_name, dv_switch_path, dv_switch_name,
dv_port_name, virtual_machine_path, vm_uuid,
port_group_path)
# Assert
dv_port_group_creator.create_dv_port_group.assert_called_with(dv_port_name, dv_switch_name, dv_switch_path, si)
virtual_machine_port_group_configurer.configure_port_group_on_vm.assert_called_with(si, virtual_machine_path,
vm_uuid,
port_group_path,
dv_port_name)
示例13: test_should_not_update_state_if_unaavailable
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import connect [as 别名]
def test_should_not_update_state_if_unaavailable(self):
# Set
app = NewrelicApp('1111','777')
headers = {"X-Api-Key":'1111'}
url ='/v2/applications/777.json'
# Mocks
connection_obj = Mock()
response_obj = Mock()
httplib.HTTPConnection = Mock(return_value=connection_obj)
response_obj.status = 500 # Error
response_obj.read = Mock(return_value='')
connection_obj.connect = Mock()
connection_obj.request = Mock()
connection_obj.getresponse = Mock(return_value=response_obj)
# Assertion
app.update_state()
connection_obj.connect.assert_called_once()
connection_obj.request.assert_called_once_with('GET', url, '', headers)
connection_obj.getresponse.assert_called_once()
self.assertFalse(app.reachable)
示例14: test_progress_updates_system_tor
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import connect [as 别名]
def test_progress_updates_system_tor(self, ftb):
control_ep = Mock()
control_ep.connect = Mock(return_value=defer.succeed(None))
directlyProvides(control_ep, IStreamClientEndpoint)
ep = TCPHiddenServiceEndpoint.system_tor(self.reactor, control_ep, 1234)
ep._tor_progress_update(40, "FOO", "foo to bar")
return ep
示例15: test_delete_all
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import connect [as 别名]
def test_delete_all(self):
# arrange
uuid = 'uuid'
si = Mock()
vm = Mock()
connection_detail = Mock()
connection_detail.host = Mock()
connection_detail.username = Mock()
connection_detail.password = Mock()
connection_detail.port = Mock()
pv_service = Mock()
pv_service.connect = Mock(return_value=si)
pv_service.find_by_uuid = Mock(return_value=vm)
connector = VirtualSwitchToMachineDisconnectCommand(pv_service, Mock(), 'anetwork')
# virtual_switch_to_machine_connector.remove_interfaces_from_vm = Mock(return_value=True)
connector.get_network_by_name = lambda x, y: Mock()
# act
res = connector.disconnect(si, uuid)
# assert
self.assertTrue(pv_service.connect.called_with(connection_detail.host,
connection_detail.username,
connection_detail.password,
connection_detail.port))
self.assertTrue(pv_service.find_by_uuid.called_with(si, uuid))
# self.assertTrue(virtual_switch_to_machine_connector.remove_interfaces_from_vm.called_with(vm))
self.assertTrue(res)