本文整理汇总了Python中mock.mock.MagicMock.assert_has_calls方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.assert_has_calls方法的具体用法?Python MagicMock.assert_has_calls怎么用?Python MagicMock.assert_has_calls使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.assert_has_calls方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_auto_start
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import assert_has_calls [as 别名]
def test_auto_start(self, dumpsMock, loadsMock, timeMock, waitMock, mock_createStatusCommand):
original_value = self.controller.config
self.controller.config = AgentConfig("", "")
out = StringIO.StringIO()
sys.stdout = out
heartbeat = MagicMock()
self.controller.heartbeat = heartbeat
dumpsMock.return_value = "data"
sendRequest = MagicMock(name="sendRequest")
self.controller.sendRequest = sendRequest
self.controller.responseId = 1
response1 = {"responseId": "2", "restartAgent": False, "restartEnabled": True}
response2 = {"responseId": "2", "restartAgent": False, "restartEnabled": False}
loadsMock.side_effect = [response1, response2, response1]
def one_heartbeat(*args, **kwargs):
self.controller.DEBUG_STOP_HEARTBEATING = True
return "data"
sendRequest.side_effect = one_heartbeat
actionQueue = MagicMock()
actionQueue.isIdle.return_value = True
# one successful request, after stop
self.controller.actionQueue = actionQueue
self.controller.componentActualState = State.FAILED
self.controller.componentExpectedState = State.STARTED
self.assertTrue(self.controller.componentActualState, State.FAILED)
self.controller.actionQueue.customServiceOrchestrator.stored_command = {
'commandType': 'EXECUTION_COMMAND',
'role': u'HBASE',
'roleCommand': u'START',
'commandId': '7-1',
'taskId': 7,
"componentName": "HBASE_MASTER",
'clusterName': u'cc',
'serviceName': u'HDFS'
}
addToQueue = MagicMock(name="addToQueue")
self.controller.addToQueue = addToQueue
self.controller.heartbeatWithServer()
self.assertTrue(sendRequest.called)
self.assertTrue(self.controller.componentActualState, State.STARTING)
self.assertTrue(self.controller.componentExpectedState, State.STARTED)
self.assertEquals(self.controller.failureCount, 0)
self.assertFalse(mock_createStatusCommand.called)
addToQueue.assert_has_calls([call([{
'commandType': 'EXECUTION_COMMAND',
'clusterName': u'cc',
'serviceName': u'HDFS',
'role': u'HBASE',
'taskId': 8,
'roleCommand': u'START',
'componentName': 'HBASE_MASTER',
'commandId': '8-1',
'auto_generated': True}])])
self.controller.config = original_value
# restartEnabled = False
self.controller.componentActualState = State.FAILED
self.controller.heartbeatWithServer()
self.assertTrue(sendRequest.called)
self.assertTrue(self.controller.componentActualState, State.FAILED)
self.assertTrue(self.controller.componentExpectedState, State.STARTED)
# restartEnabled = True
self.controller.componentActualState = State.INSTALLED
self.controller.componentExpectedState = State.INSTALLED
self.controller.heartbeatWithServer()
self.assertTrue(sendRequest.called)
self.assertTrue(self.controller.componentActualState, State.INSTALLED)
self.assertTrue(self.controller.componentExpectedState, State.INSTALLED)
pass
示例2: test_heartbeatWithServerTerminateAgent
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import assert_has_calls [as 别名]
def test_heartbeatWithServerTerminateAgent(self, dumpsMock, loadsMock, sleepMock, event_mock):
original_value = self.controller.config
self.controller.config = AgentConfig("", "")
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}
loadsMock.return_value = response
def one_heartbeat(*args, **kwargs):
self.controller.DEBUG_STOP_HEARTBEATING = True
return "data"
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 "data"
# 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)
original_stopApp = self.controller.stopApp
# terminateAgent command - test 1
self.controller.responseId = 1
self.controller.DEBUG_STOP_HEARTBEATING = False
response = {"responseId":"2", "terminateAgent": True}
loadsMock.return_value = response
stopApp = MagicMock(name="stopApp")
self.controller.stopApp = stopApp
self.controller.heartbeatWithServer()
stopApp.assert_called_once_with()
# reset for next test
self.controller.terminateAgent = False
# terminateAgent command - test 2
self.controller.responseId = 1
self.controller.DEBUG_STOP_HEARTBEATING = False
response = {"responseId":"2", "terminateAgent": True}
loadsMock.return_value = response
self.controller.stopApp = original_stopApp
stopCommand = {"roleCommand": "STOP"}
self.controller.stopCommand = stopCommand
addToQueue = MagicMock(name="addToQueue")
self.controller.addToQueue = addToQueue
self.controller.componentActualState = State.STARTED
self.controller.heartbeatWithServer()
self.assertTrue(self.controller.terminateAgent)
self.assertTrue(self.controller.appGracefulStopQueued)
addToQueue.assert_has_calls([call([stopCommand])])
# reset for next test
self.controller.terminateAgent = False
self.controller.appGracefulStopQueued = False
# terminateAgent command - test 3
self.controller.responseId = 2
self.controller.DEBUG_STOP_HEARTBEATING = False
response = {"responseId":"3", "terminateAgent": True}
loadsMock.return_value = response
addToQueue = MagicMock(name="addToQueue")
self.controller.addToQueue = addToQueue
self.controller.stopApp = original_stopApp
self.controller.componentActualState = State.STARTED
self.controller.heartbeatWithServer()
self.assertTrue(self.controller.terminateAgent)
self.assertTrue(self.controller.appGracefulStopQueued)
addToQueue.assert_has_calls([call([stopCommand])])
self.controller.terminateAgent = False
sleepMock.assert_called_with(
#.........这里部分代码省略.........
示例3: test_heartbeatWithServer
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import assert_has_calls [as 别名]
#.........这里部分代码省略.........
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"
self.controller.DEBUG_STOP_HEARTBEATING = False
self.controller.heartbeatWithServer()
addToQueue.assert_has_calls([call("executionCommands")])
# statusCommands
self.controller.responseId = 1
addToStatusQueue = MagicMock(name="addToStatusQueue")
self.controller.addToStatusQueue = addToStatusQueue
response["statusCommands"] = "statusCommands"
self.controller.DEBUG_STOP_HEARTBEATING = False
self.controller.heartbeatWithServer()
addToStatusQueue.assert_has_calls([call("statusCommands")])
# restartAgent command
self.controller.responseId = 1
self.controller.DEBUG_STOP_HEARTBEATING = False
response["restartAgent"] = "true"
restartAgent = MagicMock(name="restartAgent")
self.controller.restartAgent = restartAgent
self.controller.heartbeatWithServer()
restartAgent.assert_called_once_with()
# actionQueue not idle
self.controller.responseId = 1
self.controller.DEBUG_STOP_HEARTBEATING = False
actionQueue.isIdle.return_value = False
response["restartAgent"] = "false"
self.controller.heartbeatWithServer()
sleepMock.assert_called_with(
self.controller.netutil.MINIMUM_INTERVAL_BETWEEN_HEARTBEATS)
# Check that server continues to heartbeat after connection errors
self.controller.responseId = 1
self.controller.TEST_IOERROR_COUNTER = 1
sendRequest.reset()
def util_throw_IOErrors(*args, **kwargs):
"""
Throws IOErrors 100 times and then stops heartbeats/registrations
"""
if self.controller.TEST_IOERROR_COUNTER == 10:
self.controller.DEBUG_STOP_HEARTBEATING = True
self.controller.TEST_IOERROR_COUNTER += 1
raise IOError("Sample error")
self.controller.DEBUG_STOP_HEARTBEATING = False
actionQueue.isIdle.return_value = False
sendRequest.side_effect = util_throw_IOErrors
self.controller.heartbeatWithServer()
self.assertTrue(sendRequest.call_count > 5)
sleepMock.assert_called_with(
self.controller.netutil.MINIMUM_INTERVAL_BETWEEN_HEARTBEATS)
sys.stdout = sys.__stdout__
self.controller.sendRequest = Controller.Controller.sendRequest
self.controller.sendRequest = Controller.Controller.addToQueue
self.controller.sendRequest = Controller.Controller.addToStatusQueue
示例4: test_heartbeatWithServer
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import assert_has_calls [as 别名]
#.........这里部分代码省略.........
self.assertTrue(self.controller.hasMappedComponents)
# components are mapped
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
response = {"responseId":"2", "restartAgent": False}
loadsMock.return_value = response
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"
self.controller.statusCommand = ["statusCommand"]
updateStateBasedOnCommand = MagicMock(name="updateStateBasedOnCommand")
self.controller.updateStateBasedOnCommand = updateStateBasedOnCommand
self.controller.DEBUG_STOP_HEARTBEATING = False
self.controller.heartbeatWithServer()
addToQueue.assert_has_calls([call("executionCommands")])
updateStateBasedOnCommand.assert_has_calls([call("executionCommands")])
# just status command when state = STARTED
self.controller.responseId = 1
response = {"responseId":"2", "restartAgent": False}
loadsMock.return_value = response
addToQueue = MagicMock(name="addToQueue")
self.controller.addToQueue = addToQueue
self.controller.statusCommand = "statusCommand"
self.controller.componentActualState = State.STARTED
self.controller.componentExpectedState = State.STARTED
self.controller.DEBUG_STOP_HEARTBEATING = False
self.controller.heartbeatWithServer()
addToQueue.assert_has_calls([call(["statusCommand"])])
# just status command when state = FAILED
self.controller.responseId = 1
response = {"responseId":"2", "restartAgent": False}
loadsMock.return_value = response
addToQueue = MagicMock(name="addToQueue")
self.controller.addToQueue = addToQueue
self.controller.statusCommand = "statusCommand"
self.controller.componentActualState = State.FAILED
self.controller.componentExpectedState = State.STARTED
self.controller.DEBUG_STOP_HEARTBEATING = False
self.controller.heartbeatWithServer()
addToQueue.assert_has_calls([call(["statusCommand"])])
# no status command when state = STARTING
self.controller.responseId = 1
示例5: test_heartbeatWithServer
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import assert_has_calls [as 别名]
#.........这里部分代码省略.........
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 "data"
# 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
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
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
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
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
response = {"responseId":"2", "restartAgent":"false"}
loadsMock.return_value = response
restartAgent = MagicMock(name="restartAgent")
self.controller.restartAgent = restartAgent
self.controller.DEBUG_STOP_HEARTBEATING = False
self.controller.heartbeatWithServer()
restartAgent.assert_called_once_with()
# executionCommands, statusCommands
self.controller.responseId = 1
addToQueue = MagicMock(name="addToQueue")
self.controller.addToQueue = addToQueue
response["executionCommands"] = "executionCommands"
response["statusCommands"] = "statusCommands"
self.controller.DEBUG_STOP_HEARTBEATING = False
self.controller.heartbeatWithServer()
addToQueue.assert_has_calls([call("executionCommands"),
call("statusCommands")])
# restartAgent command
self.controller.responseId = 1
self.controller.DEBUG_STOP_HEARTBEATING = False
response["restartAgent"] = "true"
restartAgent = MagicMock(name="restartAgent")
self.controller.restartAgent = restartAgent
self.controller.heartbeatWithServer()
restartAgent.assert_called_once_with()
# actionQueue not idle
self.controller.responseId = 1
self.controller.DEBUG_STOP_HEARTBEATING = False
actionQueue.isIdle.return_value = False
response["restartAgent"] = "false"
self.controller.heartbeatWithServer()
sleepMock.assert_called_with(
self.controller.netutil.HEARTBEAT_NOT_IDDLE_INTERVAL_SEC)
sys.stdout = sys.__stdout__
self.controller.sendRequest = Controller.Controller.sendRequest
self.controller.sendRequest = Controller.Controller.addToQueue