本文整理汇总了Python中mock.mock.MagicMock.return_value方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.return_value方法的具体用法?Python MagicMock.return_value怎么用?Python MagicMock.return_value使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.return_value方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testTransparentHugePage
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import return_value [as 别名]
def testTransparentHugePage(self, open_mock, os_path_isfile_mock, mock_config,
get_tmp_dir_mock, structured_out_mock,
resolve_config_mock):
context_manager_mock = MagicMock()
open_mock.return_value = context_manager_mock
file_mock = MagicMock()
file_mock.read.return_value = "[never] always"
enter_mock = MagicMock()
enter_mock.return_value = file_mock
enter_mock = MagicMock()
enter_mock.return_value = file_mock
exit_mock = MagicMock()
setattr( context_manager_mock, '__enter__', enter_mock )
setattr( context_manager_mock, '__exit__', exit_mock )
os_path_isfile_mock.return_value = True
get_tmp_dir_mock.return_value = "/tmp"
mock_config.return_value = {"commandParams" : {"check_execute_list" : "transparentHugePage"}}
checkHost = CheckHost()
checkHost.actionexecute(None)
self.assertEquals(structured_out_mock.call_args[0][0], {'transparentHugePage' : {'message': 'never', 'exit_code': 0}})
# case 2, file not exists
os_path_isfile_mock.return_value = False
checkHost.actionexecute(None)
self.assertEquals(structured_out_mock.call_args[0][0], {'transparentHugePage' : {'message': '', 'exit_code': 0}})
示例2: test_registerAndHeartbeat
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import return_value [as 别名]
def test_registerAndHeartbeat(self, sleepMock):
registerWithServer = MagicMock(name="registerWithServer")
registerWithServer.return_value = {"response":"resp"}
self.controller.registerWithServer = registerWithServer
heartbeatWithServer = MagicMock(name="heartbeatWithServer")
self.controller.heartbeatWithServer = heartbeatWithServer
actionQueue = MagicMock(name="actionQueue")
self.controller.actionQueue = actionQueue
listener1 = MagicMock()
listener2 = MagicMock()
self.controller.registration_listeners.append(listener1)
self.controller.registration_listeners.append(listener2)
self.controller.isRegistered = True
self.controller.registerAndHeartbeat()
registerWithServer.assert_called_once_with()
heartbeatWithServer.assert_called_once_with()
self.assertTrue(listener1.called)
self.assertTrue(listener2.called)
self.controller.registerWithServer = \
Controller.Controller.registerWithServer
self.controller.heartbeatWithServer = \
Controller.Controller.registerWithServer
示例3: test_recoveryHbCmd
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import return_value [as 别名]
def test_recoveryHbCmd(self, dumpsMock, sleepMock, event_mock, exit_mock):
out = StringIO.StringIO()
sys.stdout = out
hearbeat = MagicMock()
self.controller.heartbeat = hearbeat
event_mock.return_value = False
dumpsMock.return_value = "data"
sendRequest = MagicMock(name="sendRequest")
self.controller.sendRequest = sendRequest
addToQueue = MagicMock(name="addToQueue")
addToStatusQueue = MagicMock(name="addToStatusQueue")
self.addToQueue = addToQueue
self.addToStatusQueue = addToStatusQueue
process_execution_commands = MagicMock(name="process_execution_commands")
self.controller.recovery_manager.process_execution_commands = process_execution_commands
process_status_commands = MagicMock(name="process_status_commands")
self.controller.recovery_manager.process_status_commands = process_status_commands
set_paused = MagicMock(name = "set_paused")
self.controller.recovery_manager.set_paused = set_paused
self.controller.responseId = 0
response = {"responseId":1,
"statusCommands": "commands2",
"executionCommands" : "commands1",
"log":"",
"exitstatus":"0",
"hasPendingTasks": True}
sendRequest.return_value = response
def one_heartbeat(*args, **kwargs):
self.controller.DEBUG_STOP_HEARTBEATING = True
return response
sendRequest.side_effect = one_heartbeat
actionQueue = MagicMock()
actionQueue.isIdle.return_value = True
# one successful request, after stop
self.controller.actionQueue = actionQueue
self.controller.heartbeatWithServer()
self.assertTrue(sendRequest.called)
self.assertTrue(process_execution_commands.called)
self.assertFalse(process_status_commands.called)
process_execution_commands.assert_called_with("commands1")
set_paused.assert_called_with(True)
self.controller.heartbeatWithServer()
sys.stdout = sys.__stdout__
self.controller.sendRequest = Controller.Controller.sendRequest
self.controller.sendRequest = Controller.Controller.addToQueue
self.controller.sendRequest = Controller.Controller.addToStatusQueue
pass
示例4: test_markers_within
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import return_value [as 别名]
def test_markers_within(self, mock_marker):
reload(utils)
polygon = MagicMock()
group = MagicMock()
filter_filter = MagicMock()
markers = MagicMock()
filter_filter.return_value = markers
mock_marker.objects.filter.return_value = filter_filter
self.utils.get_markers_within(polygon, group)
mock_marker.objects.filter.assert_called_once_with(location__within=polygon)
示例5: test_try_to_connect
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import return_value [as 别名]
def test_try_to_connect(self, sleepMock):
netutil = NetUtil.NetUtil()
checkURL = MagicMock(name="checkURL")
checkURL.return_value = True
netutil.checkURL = checkURL
l = MagicMock()
# one successful get
self.assertEqual(0, netutil.try_to_connect("url", 10))
# got successful after N retries
gets = [True, False, False]
def side_effect(*args):
return gets.pop()
checkURL.side_effect = side_effect
self.assertEqual(2, netutil.try_to_connect("url", 10))
# max retries
checkURL.side_effect = None
checkURL.return_value = False
self.assertEqual(5, netutil.try_to_connect("url", 5))
示例6: test_try_to_connect
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import return_value [as 别名]
def test_try_to_connect(self, event_mock,
sleepMock):
event_mock.return_value = False
netutil = NetUtil.NetUtil(MagicMock())
checkURL = MagicMock(name="checkURL")
checkURL.return_value = True, "test"
netutil.checkURL = checkURL
# one successful get
self.assertEqual((0, True, False), netutil.try_to_connect("url", 10))
# got successful after N retries
gets = [[True, ""], [False, ""], [False, ""]]
def side_effect(*args):
return gets.pop()
checkURL.side_effect = side_effect
self.assertEqual((2, True, False), netutil.try_to_connect("url", 10))
# max retries
checkURL.side_effect = None
checkURL.return_value = False, "test"
self.assertEqual((5, False, False), netutil.try_to_connect("url", 5))
示例7: test_registerAndHeartbeat_check_registration_listener
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import return_value [as 别名]
def test_registerAndHeartbeat_check_registration_listener(self, sleepMock):
registerWithServer = MagicMock(name="registerWithServer")
registerWithServer.return_value = {"response":"resp"}
self.controller.registerWithServer = registerWithServer
heartbeatWithServer = MagicMock(name="heartbeatWithServer")
self.controller.heartbeatWithServer = heartbeatWithServer
self.controller.isRegistered = True
self.controller.registerAndHeartbeat()
registerWithServer.assert_called_once_with()
heartbeatWithServer.assert_called_once_with()
self.controller.registerWithServer = \
Controller.Controller.registerWithServer
self.controller.heartbeatWithServer = \
Controller.Controller.registerWithServer
示例8: test_recoveryRegConfig
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import return_value [as 别名]
def test_recoveryRegConfig(self, LiveStatus_mock, randintMock, pformatMock, sleepMock,
dumpsMock, socketGhbnMock):
self.assertEquals(self.controller.recovery_manager.recovery_enabled, False)
self.assertEquals(self.controller.recovery_manager.auto_start_only, False)
self.assertEquals(self.controller.recovery_manager.max_count, 6)
self.assertEquals(self.controller.recovery_manager.window_in_min, 60)
self.assertEquals(self.controller.recovery_manager.retry_gap, 5)
out = StringIO.StringIO()
sys.stdout = out
dumpsMock.return_value = '{"valid_object": true}'
socketGhbnMock.return_value = "host1"
sendRequest = MagicMock(name="sendRequest")
self.controller.sendRequest = sendRequest
register = MagicMock(name="register")
self.controller.register = register
sendRequest.return_value = {
"responseId": 1,
"recoveryConfig": {
"type": "FULL",
"maxCount": 5,
"windowInMinutes": 50,
"retryGap": 3,
"maxLifetimeCount": 7},
"log": "", "exitstatus": "0"}
self.controller.isRegistered = False
self.controller.registerWithServer()
self.assertEquals(self.controller.recovery_manager.recovery_enabled, True)
self.assertEquals(self.controller.recovery_manager.auto_start_only, False)
self.assertEquals(self.controller.recovery_manager.max_count, 5)
self.assertEquals(self.controller.recovery_manager.window_in_min, 50)
self.assertEquals(self.controller.recovery_manager.retry_gap, 3)
self.assertEquals(self.controller.recovery_manager.max_lifetime_count, 7)
sys.stdout = sys.__stdout__
self.controller.sendRequest = Controller.Controller.sendRequest
self.controller.addToStatusQueue = Controller.Controller.addToStatusQueue
pass
示例9: test_transparent_huge_page
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import return_value [as 别名]
def test_transparent_huge_page(self, open_mock, os_path_isfile_mock):
context_manager_mock = MagicMock()
open_mock.return_value = context_manager_mock
file_mock = MagicMock()
file_mock.read.return_value = "[never] always"
enter_mock = MagicMock()
enter_mock.return_value = file_mock
exit_mock = MagicMock()
setattr( context_manager_mock, '__enter__', enter_mock )
setattr( context_manager_mock, '__exit__', exit_mock )
hostInfo = HostInfoLinux()
os_path_isfile_mock.return_value = True
self.assertEqual("never", hostInfo.getTransparentHugePage())
os_path_isfile_mock.return_value = False
self.assertEqual("", hostInfo.getTransparentHugePage())
示例10: test_registerAndHeartbeatWithException
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import return_value [as 别名]
def test_registerAndHeartbeatWithException(self, sleepMock):
registerWithServer = MagicMock(name="registerWithServer")
registerWithServer.return_value = {"response":"resp"}
self.controller.registerWithServer = registerWithServer
heartbeatWithServer = MagicMock(name="heartbeatWithServer")
self.controller.heartbeatWithServer = heartbeatWithServer
Controller.Controller.__sendRequest__ = MagicMock(side_effect=Exception())
self.controller.registerAndHeartbeat()
registerWithServer.assert_called_once_with()
heartbeatWithServer.assert_called_once_with()
self.controller.registerWithServer =\
Controller.Controller.registerWithServer
self.controller.heartbeatWithServer =\
Controller.Controller.registerWithServer
示例11: test_heartbeatWithServer
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import return_value [as 别名]
def test_heartbeatWithServer(self, dumpsMock, sleepMock, event_mock):
out = StringIO.StringIO()
sys.stdout = out
hearbeat = MagicMock()
self.controller.heartbeat = hearbeat
dumpsMock.return_value = "data"
sendRequest = MagicMock(name="sendRequest")
self.controller.sendRequest = sendRequest
self.controller.responseId = 1
response = {"responseId":"2", "restartAgent":"false"}
sendRequest.return_value = response
def one_heartbeat(*args, **kwargs):
self.controller.DEBUG_STOP_HEARTBEATING = True
return response
sendRequest.side_effect = one_heartbeat
actionQueue = MagicMock()
actionQueue.isIdle.return_value = True
# one successful request, after stop
self.controller.actionQueue = actionQueue
self.controller.heartbeatWithServer()
self.assertTrue(sendRequest.called)
calls = []
def retry(*args, **kwargs):
if len(calls) == 0:
calls.append(1)
response["responseId"] = "3"
raise Exception()
if len(calls) > 0:
self.controller.DEBUG_STOP_HEARTBEATING = True
return response
# exception, retry, successful and stop
sendRequest.side_effect = retry
self.controller.DEBUG_STOP_HEARTBEATING = False
self.controller.heartbeatWithServer()
self.assertEqual(1, self.controller.DEBUG_SUCCESSFULL_HEARTBEATS)
# retry registration
self.controller.responseId = 2
response["registrationCommand"] = "true"
sendRequest.side_effect = one_heartbeat
self.controller.DEBUG_STOP_HEARTBEATING = False
self.controller.heartbeatWithServer()
self.assertTrue(self.controller.repeatRegistration)
# components are not mapped
self.controller.responseId = 2
response["registrationCommand"] = "false"
response["hasMappedComponents"] = False
sendRequest.side_effect = one_heartbeat
self.controller.DEBUG_STOP_HEARTBEATING = False
self.controller.heartbeatWithServer()
self.assertFalse(self.controller.hasMappedComponents)
# components are mapped
self.controller.responseId = 2
response["hasMappedComponents"] = True
sendRequest.side_effect = one_heartbeat
self.controller.DEBUG_STOP_HEARTBEATING = False
self.controller.heartbeatWithServer()
self.assertTrue(self.controller.hasMappedComponents)
# components are mapped
self.controller.responseId = 2
del response["hasMappedComponents"]
sendRequest.side_effect = one_heartbeat
self.controller.DEBUG_STOP_HEARTBEATING = False
self.controller.heartbeatWithServer()
self.assertTrue(self.controller.hasMappedComponents)
# wrong responseId => restart
self.controller.responseId = 2
response = {"responseId":"2", "restartAgent":"false"}
restartAgent = MagicMock(name="restartAgent")
self.controller.restartAgent = restartAgent
self.controller.DEBUG_STOP_HEARTBEATING = False
self.controller.heartbeatWithServer()
restartAgent.assert_called_once_with()
# executionCommands
self.controller.responseId = 1
addToQueue = MagicMock(name="addToQueue")
self.controller.addToQueue = addToQueue
response["executionCommands"] = "executionCommands"
#.........这里部分代码省略.........