This repository has been archived by the owner on May 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Endpoint '/v1/health/ping' provides info about service status * OSBS-6692 Signed-off-by: Martin Bašti <mbasti@redhat.com>
- Loading branch information
1 parent
231c06f
commit b98c34b
Showing
9 changed files
with
319 additions
and
2 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
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,79 @@ | ||
# | ||
# Copyright (C) 2019 Red Hat, Inc | ||
# see the LICENSE file for license | ||
# | ||
|
||
import logging | ||
|
||
from flask import jsonify | ||
import requests | ||
from requests.exceptions import RequestException | ||
|
||
from . import API | ||
from omps.errors import KojiError | ||
from omps.quay import get_cnr_api_version | ||
from omps.koji_util import KOJI | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@API.route("/health/ping", methods=('GET', )) | ||
def ping(): | ||
"""Provides status report | ||
* 200 if everything is ok | ||
* 503 if service is not working as expected | ||
:return: HTTP Response | ||
""" | ||
def _ok(): | ||
return { | ||
"ok": True, | ||
"details": "It works!", | ||
} | ||
|
||
def _err(exc): | ||
return { | ||
"ok": False, | ||
"details": str(exc) | ||
} | ||
|
||
everything_ok = True | ||
|
||
# quay.io status | ||
try: | ||
# try to retrieve API version to check if quay.io is alive | ||
get_cnr_api_version() | ||
except RequestException as e: | ||
logger.error('Koji version check: %s', e) | ||
quay_result = _err(e) | ||
everything_ok = False | ||
else: | ||
quay_result = _ok() | ||
|
||
# koji status | ||
try: | ||
# try to retrieve API version to check if koji is alive | ||
KOJI.get_api_version() | ||
except KojiError as e: | ||
logger.error('Quay version check: %s', e) | ||
koji_result = _err(e) | ||
everything_ok = False | ||
else: | ||
koji_result = _ok() | ||
|
||
status_code = ( | ||
requests.codes.ok if everything_ok | ||
else requests.codes.unavailable | ||
) | ||
|
||
data = { | ||
"ok": everything_ok, | ||
"status": status_code, | ||
"services": { | ||
"koji": koji_result, | ||
"quay": quay_result | ||
} | ||
} | ||
|
||
resp = jsonify(data) | ||
resp.status_code = status_code | ||
return resp |
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
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,92 @@ | ||
# | ||
# Copyright (C) 2019 Red Hat, Inc | ||
# see the LICENSE file for license | ||
# | ||
|
||
import requests | ||
import requests_mock | ||
|
||
from omps.errors import KojiError | ||
|
||
|
||
PING_ENDPOINT = '/v1/health/ping' | ||
|
||
|
||
def test_health_ping(client, mocked_koji_get_api_version, mocked_quay_version): | ||
"""Test reporting of ok status""" | ||
rv = client.get(PING_ENDPOINT) | ||
|
||
expected = { | ||
"ok": True, | ||
"status": requests.codes.ok, | ||
"services": { | ||
"koji": { | ||
"ok": True, | ||
"details": "It works!" | ||
}, | ||
"quay": { | ||
"ok": True, | ||
"details": "It works!" | ||
}, | ||
} | ||
} | ||
|
||
assert rv.status_code == requests.codes.ok | ||
assert rv.get_json() == expected | ||
|
||
|
||
def test_health_ping_broken_quay(client, mocked_koji_get_api_version): | ||
"""Test if broken quay is reported properly""" | ||
with requests_mock.Mocker() as m: | ||
m.get( | ||
'https://quay.io/cnr/version', | ||
status_code=requests.codes.server_error | ||
) | ||
rv = client.get(PING_ENDPOINT) | ||
|
||
expected = { | ||
"ok": False, | ||
"status": requests.codes.unavailable, | ||
"services": { | ||
"koji": { | ||
"ok": True, | ||
"details": "It works!" | ||
}, | ||
"quay": { | ||
"ok": False, | ||
"details": ( | ||
"500 Server Error: None for url: " | ||
"https://quay.io/cnr/version" | ||
) | ||
}, | ||
} | ||
} | ||
assert rv.status_code == requests.codes.unavailable | ||
assert rv.get_json() == expected | ||
|
||
|
||
def test_health_ping_broken_koji( | ||
client, mocked_quay_version, mocked_koji_get_api_version | ||
): | ||
"""Test if broken koji is reported properly""" | ||
e_msg = "something happened" | ||
mocked_koji_get_api_version.side_effect = KojiError(e_msg) | ||
|
||
rv = client.get(PING_ENDPOINT) | ||
|
||
expected = { | ||
"ok": False, | ||
"status": requests.codes.unavailable, | ||
"services": { | ||
"koji": { | ||
"ok": False, | ||
"details": e_msg | ||
}, | ||
"quay": { | ||
"ok": True, | ||
"details": "It works!" | ||
}, | ||
} | ||
} | ||
assert rv.status_code == requests.codes.unavailable | ||
assert rv.get_json() == expected |
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
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