From 542ed0e6cfa677011667e3f56d790ff908d260d5 Mon Sep 17 00:00:00 2001 From: Adam Heinz Date: Tue, 25 Aug 2020 13:10:39 +0000 Subject: [PATCH] [ADD] Health checks for load balancers. https://tools.ietf.org/html/draft-inadarei-api-health-check-04 closes odoo/odoo#56522 Signed-off-by: Olivier Dony (odo) --- addons/bus/controllers/main.py | 13 ++++++++++++- addons/bus/tests/__init__.py | 1 + addons/bus/tests/test_health.py | 12 ++++++++++++ addons/web/controllers/main.py | 10 ++++++++++ addons/web/tests/__init__.py | 2 +- addons/web/tests/test_health.py | 12 ++++++++++++ 6 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 addons/bus/tests/test_health.py create mode 100644 addons/web/tests/test_health.py diff --git a/addons/bus/controllers/main.py b/addons/bus/controllers/main.py index 99fcc82ed81..f1d45920e9c 100644 --- a/addons/bus/controllers/main.py +++ b/addons/bus/controllers/main.py @@ -1,4 +1,6 @@ -# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import json from odoo import exceptions, _ from odoo.http import Controller, request, route @@ -42,3 +44,12 @@ class BusController(Controller): @route('/longpolling/im_status', type="json", auth="user") def im_status(self, partner_ids): return request.env['res.partner'].with_context(active_test=False).search([('id', 'in', partner_ids)]).read(['im_status']) + + @route('/longpolling/health', type='http', auth='none', save_session=False) + def health(self): + data = json.dumps({ + 'status': 'pass', + }) + headers = [('Content-Type', 'application/json'), + ('Cache-Control', 'no-store')] + return request.make_response(data, headers) diff --git a/addons/bus/tests/__init__.py b/addons/bus/tests/__init__.py index 06c6ab0e440..7cd3a9d428b 100644 --- a/addons/bus/tests/__init__.py +++ b/addons/bus/tests/__init__.py @@ -1 +1,2 @@ from . import test_assetsbundle +from . import test_health diff --git a/addons/bus/tests/test_health.py b/addons/bus/tests/test_health.py new file mode 100644 index 00000000000..906ddc98806 --- /dev/null +++ b/addons/bus/tests/test_health.py @@ -0,0 +1,12 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo.tests import HttpCase + + +class TestBusController(HttpCase): + def test_health(self): + response = self.url_open('/longpolling/health') + self.assertEqual(response.status_code, 200) + payload = response.json() + self.assertEqual(payload['status'], 'pass') + self.assertNotIn('session_id', response.cookies) diff --git a/addons/web/controllers/main.py b/addons/web/controllers/main.py index b5938d136d7..78bc5117a8f 100644 --- a/addons/web/controllers/main.py +++ b/addons/web/controllers/main.py @@ -903,6 +903,16 @@ class Home(http.Controller): return request.redirect(self._login_redirect(uid)) + @http.route('/web/health', type='http', auth='none', save_session=False) + def health(self): + data = json.dumps({ + 'status': 'pass', + }) + headers = [('Content-Type', 'application/json'), + ('Cache-Control', 'no-store')] + return request.make_response(data, headers) + + class WebClient(http.Controller): @http.route('/web/webclient/locale/', type='http', auth="none") diff --git a/addons/web/tests/__init__.py b/addons/web/tests/__init__.py index 157ef045f1b..62810336e66 100644 --- a/addons/web/tests/__init__.py +++ b/addons/web/tests/__init__.py @@ -1,6 +1,6 @@ -# -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +from . import test_health from . import test_image from . import test_js from . import test_menu diff --git a/addons/web/tests/test_health.py b/addons/web/tests/test_health.py new file mode 100644 index 00000000000..cec042b12d0 --- /dev/null +++ b/addons/web/tests/test_health.py @@ -0,0 +1,12 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo.tests import HttpCase + + +class TestWebController(HttpCase): + def test_health(self): + response = self.url_open('/web/health') + self.assertEqual(response.status_code, 200) + payload = response.json() + self.assertEqual(payload['status'], 'pass') + self.assertNotIn('session_id', response.cookies)