Skip to content

Commit

Permalink
Merge pull request #173 from sheldor1510/healthcheck-endpoint
Browse files Browse the repository at this point in the history
Added healthcheck endpoint
  • Loading branch information
tzumainn authored Aug 7, 2024
2 parents be4ea41 + 24d2b83 commit a2bad03
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
3 changes: 3 additions & 0 deletions esi_leap/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from keystonemiddleware import auth_token
from oslo_context import context
from esi_leap.common.health import Health
import pecan
from pecan import hooks

Expand Down Expand Up @@ -60,6 +61,8 @@ def setup_app(config=None):
if CONF.pecan.auth_enable:
app = auth_token.AuthProtocol(app, dict(CONF.keystone_authtoken))

app = Health(app=app, path="/")

return app


Expand Down
31 changes: 31 additions & 0 deletions esi_leap/common/health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from oslo_serialization import jsonutils


def root(host_url):
return {
"name": "ESI Leap API",
"description": "ESI Leap is an OpenStack service for leasing baremetal nodes, designed to run on top of multi-tenant Ironic.",
"versions": [
{
"id": "v1.0",
"status": "CURRENT",
"links": [{"href": "{0}/v1".format(host_url), "rel": "self"}],
}
],
}


class Health(object):
def __init__(self, app, path):
self.app = app
self.path = path

def __call__(self, environ, start_response):
if environ["PATH_INFO"] == self.path:
start_response("200 OK", [("Content-Type", "application/json")])
return [
jsonutils.dump_as_bytes(
root(environ["wsgi.url_scheme"] + "://" + environ["HTTP_HOST"])
)
]
return self.app(environ, start_response)
72 changes: 72 additions & 0 deletions esi_leap/tests/common/test_health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from esi_leap.common import health
from esi_leap.tests import base
from oslo_serialization import jsonutils


class TestRootFunction(base.TestCase):
def test_root(self):
host_url = "http://example.com"
expected_result = {
"name": "ESI Leap API",
"description": "ESI Leap is an OpenStack service for leasing baremetal nodes, designed to run on top of multi-tenant Ironic.",
"versions": [
{
"id": "v1.0",
"status": "CURRENT",
"links": [{"href": "{0}/v1".format(host_url), "rel": "self"}],
}
],
}
result = health.root(host_url)
self.assertEqual(result, expected_result)


class TestHealthMiddleware(base.TestCase):
def setUp(self):
super().setUp()
self.app = self.simple_app
self.path = "/health"
self.health = health.Health(self.app, self.path)

def simple_app(self, environ, start_response):
start_response("401 Unauthorized", [("Content-Type", "text/plain")])
return [b"401 Unauthorized"]

def start_response(self, status, headers):
self.status = status
self.headers = headers

def test_health_path(self):
environ = {
"PATH_INFO": self.path,
"wsgi.url_scheme": "http",
"HTTP_HOST": "example.com",
}
result = self.health(environ, self.start_response)
self.assertEqual(self.status, "200 OK")
self.assertEqual(dict(self.headers)["Content-Type"], "application/json")
expected_body = jsonutils.dump_as_bytes(health.root("http://example.com"))
self.assertEqual(result, [expected_body])

def test_non_health_path(self):
environ = {
"PATH_INFO": "/not_health",
"wsgi.url_scheme": "http",
"HTTP_HOST": "example.com",
}
result = self.health(environ, self.start_response)
self.assertEqual(self.status, "401 Unauthorized")
self.assertEqual(dict(self.headers)["Content-Type"], "text/plain")
self.assertEqual(result, [b"401 Unauthorized"])

0 comments on commit a2bad03

Please sign in to comment.