Skip to content

Commit

Permalink
Implement health API proxy for ceagle
Browse files Browse the repository at this point in the history
  • Loading branch information
teferi authored and boris-42 committed Dec 3, 2016
1 parent 948b149 commit 389713a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
24 changes: 20 additions & 4 deletions ceagle/api/v1/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
# License for the specific language governing permissions and limitations
# under the License.

import json

import flask

from ceagle.api import client
from ceagle.api_fake_data import fake_status


Expand All @@ -29,9 +32,15 @@ def get_status(period):


@bp_status.route("/health/<period>")
@fake_status.get_status_health
def get_status_health(period):
return flask.jsonify("fixme!")
health_client = client.get_client("health")
if not health_client:
return flask.make_response(
json.dumps({"error": "No health endpoint configured"}), 404)

api_endpoint = "/api/v1/health/{}".format(period)
result = health_client.get(api_endpoint)
return flask.make_response(json.dumps(result), result["status_code"])


@bp_status.route("/performance/<period>")
Expand All @@ -53,9 +62,16 @@ def get_region_status(region, period):


@bp_region_status.route("/<region>/status/health/<period>")
@fake_status.get_region_status_health
def get_region_status_health(region, period):
return flask.jsonify("fixme!")
health_client = client.get_client("health")
if not health_client:
return flask.make_response(
json.dumps({"error": "No health endpoint configured"}), 404)

api_endpoint = "/api/v1/region/{region}/health/{period}".format(
region=region, period=period)
result = health_client.get(api_endpoint)
return flask.make_response(json.dumps(result), result["status_code"])


@bp_region_status.route("/<region>/status/performance/<period>")
Expand Down
43 changes: 41 additions & 2 deletions tests/unit/api/v1/test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.

import mock

from tests.unit import test


Expand All @@ -21,11 +23,9 @@ class ApiTestCase(test.TestCase):
def test_api_response_code(self):
region = "north-2.piedpiper.net"
for urlpath in ("/api/v1/status",
"/api/v1/status/health",
"/api/v1/status/performance",
"/api/v1/status/availability",
"/api/v1/region/%s/status" % region,
"/api/v1/region/%s/status/health" % region,
"/api/v1/region/%s/status/performance" % region,
"/api/v1/region/%s/status/availability" % region):
for suffix, code in (
Expand All @@ -38,3 +38,42 @@ def test_api_response_code(self):
urlpath = "/api/v1/region/unexpected_region/status/day"
code, resp = self.get(urlpath)
self.assertEqual(404, code, urlpath)


@mock.patch("ceagle.config.get_config")
class HealthApiTestCase(test.TestCase):

def setUp(self):
super(HealthApiTestCase, self).setUp()
self.health_config = {"health": {
"endpoint": "http://dummy.example.org/api/health"
}}

@mock.patch("ceagle.api.client.Client")
def test_status_health_api(self, mock_client, mock_get_config):
mock_get_config.return_value = self.health_config
mock_client.return_value.get.return_value = {"status_code": 200}
code, resp = self.get("/api/v1/status/health/day")
mock_client.return_value.get.assert_called_with(
"/api/v1/health", params={"period": u"day"})
self.assertEqual(200, code)

@mock.patch("ceagle.api.client.Client")
def test_region_status_health_api(self, mock_client, mock_get_config):
mock_get_config.return_value = self.health_config
mock_client.return_value.get.return_value = {"status_code": 200}

code, resp = self.get("/api/v1/region/test_region/status/health/day")
mock_client.return_value.get.assert_called_with(
"/api/v1/health/test_region", params={"period": u"day"})
self.assertEqual(200, code)

def test_health_api_no_endpoint(self, mock_get_config):
mock_get_config.return_value = {}
code, resp = self.get("/api/v1/status/health/day")
self.assertEqual(404, code)
self.assertEqual({"error": "No health endpoint configured"}, resp)

code, resp = self.get("/api/v1/region/test_region/status/health/day")
self.assertEqual(404, code)
self.assertEqual({"error": "No health endpoint configured"}, resp)

0 comments on commit 389713a

Please sign in to comment.