Skip to content

Commit

Permalink
Fix linter problems
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagosalles committed Jun 2, 2022
1 parent 8c0d3c6 commit 1edd1cc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
6 changes: 5 additions & 1 deletion barterdude/hooks/healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ def _response(status, body):
body["status"] = "ok" if status == 200 else "fail"
return web.Response(status=status, body=json.dumps(body))


class HealthcheckMonitored(ABC):
@abstractmethod
def healthcheck(self):
pass


class Healthcheck(HttpHook):
def __init__(
self,
Expand Down Expand Up @@ -92,7 +94,9 @@ async def __call__(self, req: web.Request):
fail = _remove_old(self.__fail, old_timestamp)

if success == 0 and fail == 0:
response["message"] = f"No messages in last {self.__health_window}s"
response["message"] = (
f"No messages in last {self.__health_window}s"
)
return _response(status, response)

rate = success / (success + fail)
Expand Down
40 changes: 25 additions & 15 deletions tests_unit/test_hooks/test_healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class HealthcheckMonitoredMock(HealthcheckMonitored):
def __init__(self, healthy=True):
self.healthy = healthy

def healthcheck(self):
return self.healthy

Expand All @@ -20,7 +21,9 @@ def setUp(self):
self.app = MagicMock()
self.monitoredModules = {}
self.app.__iter__.side_effect = lambda: iter(self.monitoredModules)
self.app.__getitem__.side_effect = lambda module: self.monitoredModules[module]
self.app.__getitem__.side_effect = (
lambda module: self.monitoredModules[module]
)
self.healthcheck = Healthcheck(
self.app,
"/healthcheck",
Expand Down Expand Up @@ -119,7 +122,7 @@ async def test_should_fail_healthcheck_when_fail_to_connect(self):
'{"message": "Reached max connection fails (3)", "status": "fail"}'
)

async def test_should_pass_healthcheck_when_has_one_healthy_monitored_module(self):
async def test_should_pass_when_has_one_healthy_monitored_module(self):
self.monitoredModules = {
"testModule": HealthcheckMonitoredMock(True)
}
Expand All @@ -128,10 +131,11 @@ async def test_should_pass_healthcheck_when_has_one_healthy_monitored_module(sel
self.assertEqual(response.content_type, "text/plain")
self.assertEqual(
response.body._value.decode('utf-8'),
'{"testModule": "ok", "message": "No messages in last 60.0s", "status": "ok"}'
'{"testModule": "ok", '
'"message": "No messages in last 60.0s", "status": "ok"}'
)

async def test_should_pass_healthcheck_when_has_two_healthy_monitored_module(self):
async def test_should_pass_when_has_two_healthy_monitored_module(self):
self.monitoredModules = {
"testModule1": HealthcheckMonitoredMock(True),
"testModule2": HealthcheckMonitoredMock(True)
Expand All @@ -141,10 +145,11 @@ async def test_should_pass_healthcheck_when_has_two_healthy_monitored_module(sel
self.assertEqual(response.content_type, "text/plain")
self.assertEqual(
response.body._value.decode('utf-8'),
'{"testModule1": "ok", "testModule2": "ok", "message": "No messages in last 60.0s", "status": "ok"}'
'{"testModule1": "ok", "testModule2": "ok", '
'"message": "No messages in last 60.0s", "status": "ok"}'
)

async def test_should_fail_healthcheck_when_has_one_failing_monitored_module(self):
async def test_should_fail_when_has_one_failing_monitored_module(self):
self.monitoredModules = {
"testModule": HealthcheckMonitoredMock(False)
}
Expand All @@ -153,10 +158,11 @@ async def test_should_fail_healthcheck_when_has_one_failing_monitored_module(sel
self.assertEqual(response.content_type, "text/plain")
self.assertEqual(
response.body._value.decode('utf-8'),
'{"testModule": "fail", "message": "No messages in last 60.0s", "status": "fail"}'
'{"testModule": "fail", '
'"message": "No messages in last 60.0s", "status": "fail"}'
)

async def test_should_fail_healthcheck_when_has_two_failing_monitored_module(self):
async def test_should_fail_when_has_two_failing_monitored_module(self):
self.monitoredModules = {
"testModule1": HealthcheckMonitoredMock(False),
"testModule2": HealthcheckMonitoredMock(False)
Expand All @@ -166,10 +172,11 @@ async def test_should_fail_healthcheck_when_has_two_failing_monitored_module(sel
self.assertEqual(response.content_type, "text/plain")
self.assertEqual(
response.body._value.decode('utf-8'),
'{"testModule1": "fail", "testModule2": "fail", "message": "No messages in last 60.0s", "status": "fail"}'
'{"testModule1": "fail", "testModule2": "fail", '
'"message": "No messages in last 60.0s", "status": "fail"}'
)

async def test_should_fail_healthcheck_when_has_failing_and_healthy_monitored_modules(self):
async def test_should_fail_when_has_failing_and_healthy_modules(self):
self.monitoredModules = {
"testModule1": HealthcheckMonitoredMock(False),
"testModule2": HealthcheckMonitoredMock(True)
Expand All @@ -179,10 +186,11 @@ async def test_should_fail_healthcheck_when_has_failing_and_healthy_monitored_mo
self.assertEqual(response.content_type, "text/plain")
self.assertEqual(
response.body._value.decode('utf-8'),
'{"testModule1": "fail", "testModule2": "ok", "message": "No messages in last 60.0s", "status": "fail"}'
'{"testModule1": "fail", "testModule2": "ok", '
'"message": "No messages in last 60.0s", "status": "fail"}'
)

async def test_should_pass_healthcheck_when_has_one_healthy_monitored_module_and_messages(self):
async def test_should_pass_when_has_one_healthy_module_and_messages(self):
self.monitoredModules = {
"testModule": HealthcheckMonitoredMock(True)
}
Expand All @@ -192,11 +200,12 @@ async def test_should_pass_healthcheck_when_has_one_healthy_monitored_module_and
self.assertEqual(response.content_type, "text/plain")
self.assertEqual(
response.body._value.decode('utf-8'),
'{"testModule": "ok", "message": "Success rate: 1.0 (expected: 0.9)", '
'{"testModule": "ok", '
'"message": "Success rate: 1.0 (expected: 0.9)", '
'"fail": 0, "success": 1, "status": "ok"}'
)

async def test_should_pass_healthcheck_when_has_one_failing_monitored_module_and_messages(self):
async def test_should_pass_when_has_one_failing_module_and_messages(self):
self.monitoredModules = {
"testModule": HealthcheckMonitoredMock(False)
}
Expand All @@ -206,7 +215,8 @@ async def test_should_pass_healthcheck_when_has_one_failing_monitored_module_and
self.assertEqual(response.content_type, "text/plain")
self.assertEqual(
response.body._value.decode('utf-8'),
'{"testModule": "fail", "message": "Success rate: 1.0 (expected: 0.9)", '
'{"testModule": "fail", '
'"message": "Success rate: 1.0 (expected: 0.9)", '
'"fail": 0, "success": 1, "status": "fail"}'
)

Expand Down

0 comments on commit 1edd1cc

Please sign in to comment.