-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from release-engineering/health-check
Add a simple health check API endpoint
- Loading branch information
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# SPDX-License-Identifier: GPL-3.0+ | ||
|
||
from __future__ import unicode_literals | ||
|
||
from neomodel import db | ||
|
||
from estuary import log | ||
|
||
|
||
def health_check(): | ||
"""Determine the health of the API by checking if the database interactions are working.""" | ||
msg = 'Health check OK' | ||
status = 200 | ||
try: | ||
# Just perform a simple math operation that doesn't rely on any data being present in the | ||
# database | ||
results, _ = db.cypher_query('RETURN sqrt(4)') | ||
assert results[0][0] == 2.0 | ||
except: # noqa E722 | ||
log.exception('An exception was encountered when verifying the database connection in the ' | ||
'health check API endpoint') | ||
msg = 'The health check failed while verifying the database connection' | ||
status = 503 | ||
|
||
return (msg, status, [('Content-Type', 'text/plain')]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# SPDX-License-Identifier: GPL-3.0+ | ||
|
||
from __future__ import unicode_literals | ||
|
||
from mock import patch | ||
|
||
|
||
def test_healthcheck(client): | ||
"""Test the health check endpoint is working.""" | ||
rv = client.get('/healthcheck') | ||
assert rv.status_code == 200 | ||
assert rv.data.decode('utf-8') == 'Health check OK' | ||
|
||
|
||
def test_healthcheck_failure(client): | ||
"""Test the health check endpoint shows failures.""" | ||
with patch('estuary.api.health_check.db') as mock_db: | ||
mock_db.cypher_query.side_effect = RuntimeError('Some failure') | ||
rv = client.get('/healthcheck') | ||
assert rv.status_code == 503 | ||
assert rv.data.decode('utf-8') == \ | ||
'The health check failed while verifying the database connection' |