Skip to content

Commit 19eb279

Browse files
authored
Merge pull request RedHatInsights#43 from AparnaKarve/liveness_with_test
Index Route test taking Liveness/Readiness into account
2 parents 46da189 + 6579f73 commit 19eb279

File tree

5 files changed

+87
-3
lines changed

5 files changed

+87
-3
lines changed

collector/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@
4242

4343
else:
4444
LOGGER.warning('No worker set.')
45+
WORKER = ''

collector/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
import logging
33
from threading import current_thread
44

5+
import sys
56
import redis
67
import requests
8+
from gunicorn.arbiter import Arbiter
79

810
from .env import SSL_VERIFY, REDIS_ENV, REDIS_PASSWORD, PROCESS_WINDOW
911

@@ -13,10 +15,22 @@
1315
# pylama:ignore=E1101
1416
requests.packages.urllib3.disable_warnings()
1517

18+
if not (REDIS_ENV and REDIS_PASSWORD):
19+
LOGGER.error('Environment not set properly for Redis')
20+
sys.exit(Arbiter.APP_LOAD_ERROR)
1621

1722
REDIS = redis.Redis(**json.loads(REDIS_ENV), password=REDIS_PASSWORD)
1823

1924

25+
def ping_redis() -> bool:
26+
"""Call ping on Redis."""
27+
try:
28+
return REDIS.ping()
29+
except (redis.exceptions.ConnectionError, redis.exceptions.ResponseError):
30+
LOGGER.warning('Redis Ping unsuccessful')
31+
return False
32+
33+
2034
def processed(key: str) -> bool:
2135
"""If an account has been processed within the window."""
2236
return REDIS.get(key)

server.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,24 @@ def create_application():
4141
@APP.route(f'{ROUTE_PREFIX}/', methods=['GET'], strict_slashes=False)
4242
def get_root():
4343
"""Root Endpoint for 3scale."""
44+
if not collector.WORKER:
45+
return_code = 500
46+
status = 'Error'
47+
message = 'No worker set'
48+
elif not collector.utils.ping_redis():
49+
return_code = 500
50+
status = 'Error'
51+
message = 'Required service not operational'
52+
else:
53+
return_code = 200
54+
status = 'OK'
55+
message = 'Up and Running'
56+
4457
return jsonify(
45-
status='OK',
58+
status=status,
4659
version=API_VERSION,
47-
message='Up and Running'
48-
)
60+
message=message
61+
), return_code
4962

5063

5164
@APP.route(f'{ROUTE_PREFIX}/v{API_VERSION}/version', methods=['GET'])
File renamed without changes.

tests/test_server.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import collector
2+
from server import APP
3+
4+
# R0201 = Method could be a function Used when a method doesn't use its bound
5+
# instance, and so could be written as a function.
6+
7+
# pylint: disable=R0201
8+
9+
10+
class TestRoot:
11+
"""Test various use cases for the index route."""
12+
13+
def test_route_with_no_worker(self, mocker):
14+
"""Test index route when there is no worker set."""
15+
client = APP.test_client(mocker)
16+
url = '/'
17+
18+
redis = mocker.MagicMock()
19+
mocker.patch.object(collector.utils, 'REDIS', redis)
20+
21+
response = client.get(url)
22+
assert response.get_data() == \
23+
b'{"message":"No worker set","status":"Error","version":"1.0"}\n'
24+
assert response.status_code == 500
25+
26+
def test_route_with_redis_present(self, mocker):
27+
"""Test index route when redis is present."""
28+
client = APP.test_client(mocker)
29+
30+
worker = mocker.MagicMock()
31+
mocker.patch.object(collector, 'WORKER', worker)
32+
33+
url = '/'
34+
35+
redis = mocker.MagicMock()
36+
mocker.patch.object(collector.utils, 'REDIS', redis)
37+
38+
response = client.get(url)
39+
assert response.get_data() == \
40+
b'{"message":"Up and Running","status":"OK","version":"1.0"}\n'
41+
assert response.status_code == 200
42+
43+
def test_route_with_redis_absent(self, mocker):
44+
"""Test index route when there is no redis."""
45+
client = APP.test_client(mocker)
46+
47+
worker = mocker.MagicMock()
48+
mocker.patch.object(collector, 'WORKER', worker)
49+
50+
url = '/'
51+
52+
response = client.get(url)
53+
assert response.get_data() == \
54+
b'{"message":"Required service not operational",' \
55+
b'"status":"Error","version":"1.0"}\n'
56+
assert response.status_code == 500

0 commit comments

Comments
 (0)