Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
ecommerce
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
ecommerce
Commits
7cf6791e
Commit
7cf6791e
authored
Jun 10, 2015
by
Jim Abramson
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #132 from edx/jsa/tweak-health
Don’t let LMS take us down during health checks.
parents
129c8b64
b88205a9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
26 deletions
+26
-26
ecommerce/health/tests/test_views.py
+24
-24
ecommerce/health/views.py
+2
-2
No files found.
ecommerce/health/tests/test_views.py
View file @
7cf6791e
"""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
TestCas
e
from
testfixtures
import
LogCaptur
e
from
django.db
import
DatabaseError
from
django.core.urlresolvers
import
revers
e
from
ecommerce.health.constants
import
Status
,
UnavailabilityMessag
e
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."""
...
...
ecommerce/health/views.py
View file @
7cf6791e
...
@@ -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
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment