-
Notifications
You must be signed in to change notification settings - Fork 17
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 #173 from sheldor1510/healthcheck-endpoint
Added healthcheck endpoint
- Loading branch information
Showing
3 changed files
with
106 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,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) |
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,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"]) |