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
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
class FileMasterTest(LogTestCase): def setUp(self): self.create_file_loader = MagicMock() self.file_master = FileMaster.start(self.create_file_loader) def tearDown(self): ActorRegistry.stop_all() def test_only_one_file_loader_created(self): self.file_master.tell(MISSING_MESSAGE) self.file_master.tell(MISSING_MESSAGE) sleep(TEST_WAIT) self.create_file_loader.assert_called_once_with( MISSING_MESSAGE, self.file_master) def test_restart_file_loader_when_error(self): self.file_master.tell(MISSING_MESSAGE) sleep(TEST_WAIT) self.create_file_loader.assert_called_with(MISSING_MESSAGE, self.file_master) self.file_master.tell(load_failed_message(TEST_FILE)) sleep(TEST_WAIT) self.create_file_loader.assert_called_with(MISSING_MESSAGE, self.file_master)
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
def test_write(self): mock_fp = MagicMock() mock_fp.__enter__ = MagicMock(return_value=mock_fp) codec_open = MagicMock(return_value=mock_fp) json_dump = MagicMock() os_mkdir = MagicMock() with patch(FINDER_CODECS_OPEN, codec_open): with patch(FINDER_JSON_DUMP, json_dump): with patch(FINDER_OS_MK_DIR, os_mkdir): self.writer.write() os_mkdir.assert_called_once_with(os.path.dirname(self.writer.path)) json_dump.assert_called_once_with(self.snapshot, fp=mock_fp, cls=DjangoJSONEncoder, ensure_ascii=False, indent=4)
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
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.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
def test_write(self): mock_fp = MagicMock() mock_fp.__enter__ = MagicMock(return_value=mock_fp) open_fp = MagicMock() finder_mkdir = MagicMock() finder_open = MagicMock(return_value=open_fp) codec_open = MagicMock(return_value=mock_fp) render_return = 'TOKEN' finder_render_to_string = MagicMock(return_value=render_return) with patch(FINDER_OS_MK_DIR, finder_mkdir): with patch(FINDER_OPEN, finder_open): with patch(FINDER_CODECS_OPEN, codec_open): with patch(FINDER_RENDER_TO_STRING, finder_render_to_string): self.writer.write() migration_dir = os.path.dirname(self.writer.path) finder_mkdir.assert_called_once_with(migration_dir) finder_open.assert_called_once_with( os.path.join(migration_dir, "__init__.py"), 'w') open_fp.close.assert_called_once() finder_render_to_string.assert_called_once_with( MIGRATION_TEMPLATE_NAME, { 'snapshot_name': self.writer.snapshot_name, 'app_label': self.writer.app_label, 'initial': True, 'last_migration_name': None }) mock_fp.write.assert_called_once_with(render_return)
def test_update_and_get_list(self): """Test the update_and_get_list() method""" vocabulary_manager = VocabularyManager() mock_get_list = MagicMock(return_value=[{ 'foo': 'bar' }, { 'Revision': 'baz' }]) mock_update = MagicMock() # test call without force self.assertEqual( vocabulary_manager.update_and_get_list(mock_get_list, mock_update, False), [{ 'foo': 'bar' }]) mock_get_list.assert_called_once_with() mock_update.assert_not_called() mock_get_list.reset_mock() mock_update.reset_mock() # test call with force and specified version self.assertEqual( vocabulary_manager.update_and_get_list(mock_get_list, mock_update, True, version='9.1.5'), [{ 'foo': 'bar' }]) mock_get_list.assert_called_once_with() mock_update.assert_called_once_with(version='9.1.5')
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( self.controller.netutil.MINIMUM_INTERVAL_BETWEEN_HEARTBEATS) sys.stdout = sys.__stdout__ self.controller.sendRequest = Controller.Controller.sendRequest self.controller.addToQueue = Controller.Controller.addToQueue self.controller.config = original_value pass
def test_heartbeatWithServer(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) # retry registration response["registrationCommand"] = {"command": "register"} 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"] = {"command": "register"} 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 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 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.STARTING self.controller.componentExpectedState = State.STARTED self.controller.DEBUG_STOP_HEARTBEATING = False self.controller.heartbeatWithServer() addToQueue.assert_has_calls([]) # statusCommands response["statusCommands"] = "statusCommands" self.controller.DEBUG_STOP_HEARTBEATING = False self.controller.heartbeatWithServer() addToQueue.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) sys.stdout = sys.__stdout__ self.controller.sendRequest = Controller.Controller.sendRequest self.controller.addToQueue = Controller.Controller.addToQueue self.controller.config = original_value pass
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" 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) sys.stdout = sys.__stdout__ self.controller.sendRequest = Controller.Controller.sendRequest self.controller.sendRequest = Controller.Controller.addToQueue self.controller.sendRequest = Controller.Controller.addToStatusQueue
def test_heartbeatWithServer(self, dumpsMock, sleepMock, event_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 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.alert_scheduler_handler = MagicMock() 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" 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() # 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) sys.stdout = sys.__stdout__ self.controller.sendRequest = Controller.Controller.sendRequest self.controller.sendRequest = Controller.Controller.addToQueue self.controller.sendRequest = Controller.Controller.addToStatusQueue
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" 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
def test_heartbeatWithServer(self, dumpsMock, loadsMock, sleepMock): 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) # 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