-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added readiness and liveliness endpoints for all microservices
- Loading branch information
1 parent
cfc2060
commit b1b54a7
Showing
4 changed files
with
140 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
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,58 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import testing.postgresql | ||
from flask_testing import TestCase | ||
from adsmutils import ADSFlask | ||
|
||
class TestCaseDatabase(TestCase): | ||
|
||
postgresql_url_dict = { | ||
'port': 1234, | ||
'host': '127.0.0.1', | ||
'user': 'postgres', | ||
'database': 'test' | ||
} | ||
postgresql_url = 'postgresql://{user}@{host}:{port}/{database}' \ | ||
.format( | ||
user=postgresql_url_dict['user'], | ||
host=postgresql_url_dict['host'], | ||
port=postgresql_url_dict['port'], | ||
database=postgresql_url_dict['database'] | ||
) | ||
|
||
def create_app(self): | ||
'''Start the wsgi application''' | ||
local_config = { | ||
'SQLALCHEMY_DATABASE_URI': self.postgresql_url, | ||
'SQLALCHEMY_ECHO': False, | ||
'TESTING': True, | ||
'PROPAGATE_EXCEPTIONS': True, | ||
'TRAP_BAD_REQUEST_ERRORS': True | ||
} | ||
app = ADSFlask(__name__, static_folder=None, local_config=local_config) | ||
return app | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
cls.postgresql = \ | ||
testing.postgresql.Postgresql(**cls.postgresql_url_dict) | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
cls.postgresql.stop() | ||
|
||
def setUp(self): | ||
pass | ||
|
||
def tearDown(self): | ||
self.app.db.session.remove() | ||
self.app.db.drop_all() | ||
|
||
class TestCase(TestCase): | ||
|
||
def create_app(self): | ||
'''Start the wsgi application''' | ||
local_config = { } | ||
app = ADSFlask(__name__, static_folder=None, local_config=local_config) | ||
return app | ||
|
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,53 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from base import TestCase, TestCaseDatabase | ||
import mock | ||
|
||
class TestServices(TestCase): | ||
|
||
def test_readiness_probe(self): | ||
'''Tests for the existence of a /ready route, and that it returns properly | ||
formatted JSON data''' | ||
r = self.client.get('/ready') | ||
self.assertEqual(r.status_code, 200) | ||
self.assertEqual(r.json['ready'], True) | ||
|
||
def test_liveliness_probe(self): | ||
'''Tests for the existence of a /alive route, and that it returns properly | ||
formatted JSON data''' | ||
r = self.client.get('/alive') | ||
self.assertEqual(r.status_code, 200) | ||
self.assertEqual(r.json['alive'], True) | ||
|
||
|
||
class TestServicesWithDatabase(TestCaseDatabase): | ||
|
||
def test_readiness_probe(self): | ||
'''Tests for the existence of a /ready route, and that it returns properly | ||
formatted JSON data''' | ||
r = self.client.get('/ready') | ||
self.assertEqual(r.status_code, 200) | ||
self.assertEqual(r.json['ready'], True) | ||
|
||
def test_liveliness_probe(self): | ||
'''Tests for the existence of a /alive route, and that it returns properly | ||
formatted JSON data''' | ||
r = self.client.get('/alive') | ||
self.assertEqual(r.status_code, 200) | ||
self.assertEqual(r.json['alive'], True) | ||
|
||
def test_readiness_probe_with_db_failure(self): | ||
'''Tests for the existence of a /ready route, and that it returns properly | ||
formatted JSON data when database connection has been lost''' | ||
self.app._db_failure = mock.MagicMock(return_value=True) | ||
r = self.client.get('/ready') | ||
self.assertEqual(r.status_code, 503) | ||
self.assertEqual(r.json['ready'], False) | ||
|
||
def test_liveliness_probe_with_db_failure(self): | ||
'''Tests for the existence of a /alive route, and that it returns properly | ||
formatted JSON data when database connection has been lost''' | ||
self.app._db_failure = mock.MagicMock(return_value=True) | ||
r = self.client.get('/alive') | ||
self.assertEqual(r.status_code, 503) | ||
self.assertEqual(r.json['alive'], False) |
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 |
---|---|---|
@@ -1,6 +1,9 @@ | ||
pytest==2.8.2 | ||
Flask-Testing==0.7.1 | ||
coveralls==1.1.0 | ||
httpretty==0.8.10 | ||
mock==1.3.0 | ||
coverage==4.0.1 | ||
pytest-cov==2.2.0 | ||
testing.postgresql==1.2.1 | ||
psycopg2==2.7.5 |