From 9250ae962b96ac00e0160e80af15b4adf4750d66 Mon Sep 17 00:00:00 2001 From: Lukas Holecek Date: Dec 10 2019 17:06:27 +0000 Subject: Add healthcheck API endpoint JIRA: FACTORY-5428 Signed-off-by: Lukas Holecek --- diff --git a/resultsdb/controllers/api_v2.py b/resultsdb/controllers/api_v2.py index 69c6b59..d1927e3 100644 --- a/resultsdb/controllers/api_v2.py +++ b/resultsdb/controllers/api_v2.py @@ -804,6 +804,24 @@ def create_testcase(): return jsonify(SERIALIZE(testcase)), 201 +@api.route('/healthcheck', methods=['GET']) +def healthcheck(): + """ + Request handler for performing an application-level health check. This is + not part of the published API, it is intended for use by OpenShift or other + monitoring tools. + + Returns a 200 response if the application is alive and able to serve requests. + """ + try: + db.session.execute("SELECT 1 FROM result LIMIT 0").fetchall() + except Exception: + app.logger.exception('Healthcheck failed on DB query.') + return jsonify({"message": 'Unable to communicate with database'}), 503 + + return jsonify({"message": 'Health check OK'}), 200 + + @api.route('', methods=['GET']) @api.route('/', methods=['GET']) def landing_page(): diff --git a/testing/functest_api_v20.py b/testing/functest_api_v20.py index 0a4e10b..4605d25 100644 --- a/testing/functest_api_v20.py +++ b/testing/functest_api_v20.py @@ -1012,3 +1012,18 @@ class TestFuncApiV20(): data = json.loads(r.data) assert r.status_code == 300 assert data['outcomes'] == ['PASSED', 'INFO', 'FAILED', 'NEEDS_INSPECTION', 'AMAZING'] + + def test_healthcheck_success(self): + r = self.app.get('/api/v2.0/healthcheck') + assert r.status_code == 200 + + data = json.loads(r.data) + assert data.get('message') == 'Health check OK' + + def test_healthcheck_fail(self): + resultsdb.db.session.execute('DROP TABLE result') + r = self.app.get('/api/v2.0/healthcheck') + assert r.status_code == 503 + + data = json.loads(r.data) + assert data.get('message') == 'Unable to communicate with database'