Commit 7cf6791e by Jim Abramson

Merge pull request #132 from edx/jsa/tweak-health

Don’t let LMS take us down during health checks.
parents 129c8b64 b88205a9
"""Tests of the service health endpoint.""" """Tests of the service health endpoint."""
import json import json
import logging
from django.core.urlresolvers import reverse
from django.db import DatabaseError
from django.test import TestCase
import mock import mock
from requests import Response from requests import Response
from requests.exceptions import RequestException from requests.exceptions import RequestException
from rest_framework import status from rest_framework import status
from django.test import TestCase from testfixtures import LogCapture
from django.db import DatabaseError
from django.core.urlresolvers import reverse from ecommerce.health.constants import Status, UnavailabilityMessage
from ecommerce.health.constants import Status LOGGER_NAME = 'ecommerce.health.views'
@mock.patch('requests.get') @mock.patch('requests.get')
...@@ -19,12 +21,6 @@ class HealthTests(TestCase): ...@@ -19,12 +21,6 @@ class HealthTests(TestCase):
def setUp(self): def setUp(self):
self.fake_lms_response = Response() self.fake_lms_response = Response()
# Override all loggers, suppressing logging calls of severity CRITICAL and below
logging.disable(logging.CRITICAL)
# Remove logger override
self.addCleanup(logging.disable, logging.NOTSET)
def test_all_services_available(self, mock_lms_request): def test_all_services_available(self, mock_lms_request):
"""Test that the endpoint reports when all services are healthy.""" """Test that the endpoint reports when all services are healthy."""
self.fake_lms_response.status_code = status.HTTP_200_OK self.fake_lms_response.status_code = status.HTTP_200_OK
...@@ -46,27 +42,31 @@ class HealthTests(TestCase): ...@@ -46,27 +42,31 @@ class HealthTests(TestCase):
) )
def test_lms_outage(self, mock_lms_request): def test_lms_outage(self, mock_lms_request):
"""Test that the endpoint reports when the LMS is experiencing difficulties.""" """Test that the endpoint reports when the LMS is unhealthy."""
self.fake_lms_response.status_code = status.HTTP_503_SERVICE_UNAVAILABLE self.fake_lms_response.status_code = status.HTTP_503_SERVICE_UNAVAILABLE
mock_lms_request.return_value = self.fake_lms_response mock_lms_request.return_value = self.fake_lms_response
self._assert_health( with LogCapture(LOGGER_NAME) as l:
status.HTTP_503_SERVICE_UNAVAILABLE, self._assert_health(
Status.UNAVAILABLE, status.HTTP_200_OK,
Status.OK, Status.OK,
Status.UNAVAILABLE Status.OK,
) Status.UNAVAILABLE
)
l.check((LOGGER_NAME, 'CRITICAL', UnavailabilityMessage.LMS))
def test_lms_connection_failure(self, mock_lms_request): def test_lms_connection_failure(self, mock_lms_request):
"""Test that the endpoint reports when it cannot contact the LMS.""" """Test that the endpoint reports when it cannot contact the LMS."""
mock_lms_request.side_effect = RequestException mock_lms_request.side_effect = RequestException
self._assert_health( with LogCapture(LOGGER_NAME) as l:
status.HTTP_503_SERVICE_UNAVAILABLE, self._assert_health(
Status.UNAVAILABLE, status.HTTP_200_OK,
Status.OK, Status.OK,
Status.UNAVAILABLE Status.OK,
) Status.UNAVAILABLE
)
l.check((LOGGER_NAME, 'CRITICAL', UnavailabilityMessage.LMS))
def _assert_health(self, status_code, overall_status, database_status, lms_status): def _assert_health(self, status_code, overall_status, database_status, lms_status):
"""Verify that the response matches expectations.""" """Verify that the response matches expectations."""
......
...@@ -48,7 +48,7 @@ def health(_): ...@@ -48,7 +48,7 @@ def health(_):
database_status = Status.UNAVAILABLE database_status = Status.UNAVAILABLE
try: try:
response = requests.get(LMS_HEALTH_PAGE) response = requests.get(LMS_HEALTH_PAGE, timeout=1)
if response.status_code == status.HTTP_200_OK: if response.status_code == status.HTTP_200_OK:
lms_status = Status.OK lms_status = Status.OK
...@@ -59,7 +59,7 @@ def health(_): ...@@ -59,7 +59,7 @@ def health(_):
logger.critical(UnavailabilityMessage.LMS) logger.critical(UnavailabilityMessage.LMS)
lms_status = Status.UNAVAILABLE lms_status = Status.UNAVAILABLE
overall_status = Status.OK if (database_status == lms_status == Status.OK) else Status.UNAVAILABLE overall_status = Status.OK if (database_status == Status.OK) else Status.UNAVAILABLE
data = { data = {
'overall_status': overall_status, 'overall_status': overall_status,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment