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_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
  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
示例#4
0
    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
示例#5
0
    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
示例#6
0
  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