示例#1
0
class TestThreadedHealthChecker(unittest.TestCase):
  def setUp(self):
    self.signaler = mock.Mock(spec=HttpSignaler)
    self.signaler.health.return_value = (True, 'Fake')
    self.initial_interval_secs = 1
    self.interval_secs = 5
    self.max_consecutive_failures = 2
    self.clock = mock.Mock(spec=time)
    self.threaded_health_checker = ThreadedHealthChecker(
        self.signaler.health,
        self.interval_secs,
        self.initial_interval_secs,
        self.max_consecutive_failures,
        self.clock)

  def test_maybe_update_failure_count(self):
    assert self.threaded_health_checker.current_consecutive_failures == 0
    assert self.threaded_health_checker.healthy is True

    self.threaded_health_checker._maybe_update_failure_count(True, 'reason')
    assert self.threaded_health_checker.current_consecutive_failures == 0

    self.threaded_health_checker._maybe_update_failure_count(False, 'reason')
    assert self.threaded_health_checker.current_consecutive_failures == 1
    assert self.threaded_health_checker.healthy is True

    self.threaded_health_checker._maybe_update_failure_count(False, 'reason')
    assert self.threaded_health_checker.current_consecutive_failures == 2
    assert self.threaded_health_checker.healthy is True

    self.threaded_health_checker._maybe_update_failure_count(False, 'reason')
    assert self.threaded_health_checker.healthy is False
    assert self.threaded_health_checker.reason == 'reason'

  @mock.patch('apache.aurora.executor.common.health_checker.ThreadedHealthChecker'
      '._maybe_update_failure_count',
      spec=ThreadedHealthChecker._maybe_update_failure_count)
  def test_run(self, mock_maybe_update_failure_count):
    mock_is_set = mock.Mock(spec=threading._Event.is_set)
    self.threaded_health_checker.dead.is_set = mock_is_set
    liveness = [False, False, True]
    def is_set():
      return liveness.pop(0)
    self.threaded_health_checker.dead.is_set.side_effect = is_set
    self.threaded_health_checker.run()
    assert self.clock.sleep.call_count == 3
    assert mock_maybe_update_failure_count.call_count == 2

  @mock.patch('apache.aurora.executor.common.health_checker.ExceptionalThread.start',
      spec=ExceptionalThread.start)
  def test_start(self, mock_start):
    assert mock_start.call_count == 0
    self.threaded_health_checker.start()
    mock_start.assert_called_once_with(self.threaded_health_checker)

  def test_stop(self):
    assert not self.threaded_health_checker.dead.is_set()
    self.threaded_health_checker.stop()
    assert self.threaded_health_checker.dead.is_set()
示例#2
0
class TestThreadedHealthChecker(unittest.TestCase):
  def setUp(self):
    self.signaler = mock.Mock(spec=HttpSignaler)
    self.signaler.health.return_value = (True, 'Fake')

    self.sandbox = mock.Mock(spec_set=SandboxInterface)
    self.sandbox.exists.return_value = True
    self.sandbox.root = '/root'

    self.initial_interval_secs = 1
    self.interval_secs = 5
    self.max_consecutive_failures = 2
    self.clock = mock.Mock(spec=time)
    self.threaded_health_checker = ThreadedHealthChecker(
        self.signaler.health,
        None,
        self.interval_secs,
        self.initial_interval_secs,
        self.max_consecutive_failures,
        self.clock)

    self.threaded_health_checker_sandbox_exists = ThreadedHealthChecker(
        self.signaler.health,
        self.sandbox,
        self.interval_secs,
        self.initial_interval_secs,
        self.max_consecutive_failures,
        self.clock)

  def test_perform_check_if_not_disabled_snooze_file_is_none(self):
    self.threaded_health_checker.snooze_file = None

    assert self.signaler.health.call_count == 0
    self.threaded_health_checker._perform_check_if_not_disabled()
    assert self.signaler.health.call_count == 1

  @mock.patch('os.path', spec_set=os.path)
  def test_perform_check_if_not_disabled_no_snooze_file(self, mock_os_path):
    mock_os_path.isfile.return_value = False

    assert self.signaler.health.call_count == 0
    self.threaded_health_checker_sandbox_exists._perform_check_if_not_disabled()
    assert self.signaler.health.call_count == 1

  @mock.patch('os.path', spec_set=os.path)
  def test_perform_check_if_not_disabled_snooze_file_exists(self, mock_os_path):
    mock_os_path.isfile.return_value = True

    assert self.signaler.health.call_count == 0
    result = self.threaded_health_checker_sandbox_exists._perform_check_if_not_disabled()
    assert self.signaler.health.call_count == 0
    assert result == (True, None)

  def test_maybe_update_failure_count(self):
    assert self.threaded_health_checker.current_consecutive_failures == 0
    assert self.threaded_health_checker.healthy is True

    self.threaded_health_checker._maybe_update_failure_count(True, 'reason')
    assert self.threaded_health_checker.current_consecutive_failures == 0

    self.threaded_health_checker._maybe_update_failure_count(False, 'reason')
    assert self.threaded_health_checker.current_consecutive_failures == 1
    assert self.threaded_health_checker.healthy is True

    self.threaded_health_checker._maybe_update_failure_count(False, 'reason')
    assert self.threaded_health_checker.current_consecutive_failures == 2
    assert self.threaded_health_checker.healthy is True

    self.threaded_health_checker._maybe_update_failure_count(False, 'reason')
    assert self.threaded_health_checker.healthy is False
    assert self.threaded_health_checker.reason == 'reason'

  @mock.patch('apache.aurora.executor.common.health_checker.ThreadedHealthChecker'
      '._maybe_update_failure_count',
      spec=ThreadedHealthChecker._maybe_update_failure_count)
  def test_run(self, mock_maybe_update_failure_count):
    mock_is_set = mock.Mock(spec=threading._Event.is_set)
    self.threaded_health_checker.dead.is_set = mock_is_set
    liveness = [False, False, True]
    def is_set():
      return liveness.pop(0)
    self.threaded_health_checker.dead.is_set.side_effect = is_set
    self.threaded_health_checker.run()
    assert self.clock.sleep.call_count == 3
    assert mock_maybe_update_failure_count.call_count == 2

  @mock.patch('apache.aurora.executor.common.health_checker.ExceptionalThread.start',
      spec=ExceptionalThread.start)
  def test_start(self, mock_start):
    assert mock_start.call_count == 0
    self.threaded_health_checker.start()
    mock_start.assert_called_once_with(self.threaded_health_checker)

  def test_stop(self):
    assert not self.threaded_health_checker.dead.is_set()
    self.threaded_health_checker.stop()
    assert self.threaded_health_checker.dead.is_set()
class TestThreadedHealthChecker(unittest.TestCase):
    def setUp(self):
        self.signaler = mock.Mock(spec=HttpSignaler)
        self.signaler.health.return_value = (True, 'Fake')

        self.sandbox = mock.Mock(spec_set=SandboxInterface)
        self.sandbox.exists.return_value = True
        self.sandbox.root = '/root'

        self.initial_interval_secs = 1
        self.interval_secs = 5
        self.max_consecutive_failures = 2
        self.clock = mock.Mock(spec=time)
        self.threaded_health_checker = ThreadedHealthChecker(
            self.signaler.health, None, self.interval_secs,
            self.initial_interval_secs, self.max_consecutive_failures,
            self.clock)

        self.threaded_health_checker_sandbox_exists = ThreadedHealthChecker(
            self.signaler.health, self.sandbox, self.interval_secs,
            self.initial_interval_secs, self.max_consecutive_failures,
            self.clock)

    def test_perform_check_if_not_disabled_snooze_file_is_none(self):
        self.threaded_health_checker.snooze_file = None

        assert self.signaler.health.call_count == 0
        self.threaded_health_checker._perform_check_if_not_disabled()
        assert self.signaler.health.call_count == 1

    @mock.patch('os.path', spec_set=os.path)
    def test_perform_check_if_not_disabled_no_snooze_file(self, mock_os_path):
        mock_os_path.isfile.return_value = False

        assert self.signaler.health.call_count == 0
        self.threaded_health_checker_sandbox_exists._perform_check_if_not_disabled(
        )
        assert self.signaler.health.call_count == 1

    @mock.patch('os.path', spec_set=os.path)
    def test_perform_check_if_not_disabled_snooze_file_exists(
            self, mock_os_path):
        mock_os_path.isfile.return_value = True

        assert self.signaler.health.call_count == 0
        result = self.threaded_health_checker_sandbox_exists._perform_check_if_not_disabled(
        )
        assert self.signaler.health.call_count == 0
        assert result == (True, None)

    def test_maybe_update_failure_count(self):
        assert self.threaded_health_checker.current_consecutive_failures == 0
        assert self.threaded_health_checker.healthy is True

        self.threaded_health_checker._maybe_update_failure_count(
            True, 'reason')
        assert self.threaded_health_checker.current_consecutive_failures == 0

        self.threaded_health_checker._maybe_update_failure_count(
            False, 'reason')
        assert self.threaded_health_checker.current_consecutive_failures == 1
        assert self.threaded_health_checker.healthy is True

        self.threaded_health_checker._maybe_update_failure_count(
            False, 'reason')
        assert self.threaded_health_checker.current_consecutive_failures == 2
        assert self.threaded_health_checker.healthy is True

        self.threaded_health_checker._maybe_update_failure_count(
            False, 'reason')
        assert self.threaded_health_checker.healthy is False
        assert self.threaded_health_checker.reason == 'reason'

    @mock.patch(
        'apache.aurora.executor.common.health_checker.ThreadedHealthChecker'
        '._maybe_update_failure_count',
        spec=ThreadedHealthChecker._maybe_update_failure_count)
    def test_run(self, mock_maybe_update_failure_count):
        mock_is_set = mock.Mock(spec=threading._Event.is_set)
        self.threaded_health_checker.dead.is_set = mock_is_set
        liveness = [False, False, True]

        def is_set():
            return liveness.pop(0)

        self.threaded_health_checker.dead.is_set.side_effect = is_set
        self.threaded_health_checker.run()
        assert self.clock.sleep.call_count == 3
        assert mock_maybe_update_failure_count.call_count == 2

    @mock.patch(
        'apache.aurora.executor.common.health_checker.ExceptionalThread.start',
        spec=ExceptionalThread.start)
    def test_start(self, mock_start):
        assert mock_start.call_count == 0
        self.threaded_health_checker.start()
        mock_start.assert_called_once_with(self.threaded_health_checker)

    def test_stop(self):
        assert not self.threaded_health_checker.dead.is_set()
        self.threaded_health_checker.stop()
        assert self.threaded_health_checker.dead.is_set()