Skip to content

Commit

Permalink
chore: test for the 'strict_status_code' parm
Browse files Browse the repository at this point in the history
  • Loading branch information
luislhl committed Oct 16, 2023
1 parent 6ecd7db commit 131ccb9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
4 changes: 3 additions & 1 deletion hathor/healthcheck/resources/healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def render_GET(self, request):
checks={c.component_name: [c] for c in components_health_checks},
)

if not strict_status_code:
if strict_status_code:
request.setResponseCode(200)
else:
status_code = health_check.get_http_status_code()
request.setResponseCode(status_code)

Expand Down
31 changes: 28 additions & 3 deletions tests/resources/healthcheck/test_healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from twisted.internet.defer import inlineCallbacks

from hathor.conf.get_settings import get_settings
from hathor.healthcheck.resources.healthcheck import HealthcheckResource
from hathor.manager import HathorManager
from hathor.simulator import FakeConnection
Expand All @@ -25,6 +24,29 @@ def test_get_no_recent_activity(self):
response = yield self.web.get("/health")
data = response.json_value()

self.assertEqual(response.responseCode, 503)
self.assertEqual(data, {
'status': 'fail',
'description': ANY,
'checks': {
'sync': [{
'componentType': 'internal',
'status': 'fail',
'output': HathorManager.UnhealthinessReason.NO_RECENT_ACTIVITY,
'time': ANY
}]
}
})

@inlineCallbacks
def test_strict_status_code(self):
"""Make sure the 'strict_status_code' parameter is working.
The node should return 200 even if it's not ready.
"""
response = yield self.web.get("/health", {b'strict_status_code': b'1'})
data = response.json_value()

self.assertEqual(response.responseCode, 200)
self.assertEqual(data, {
'status': 'fail',
'description': ANY,
Expand All @@ -50,6 +72,7 @@ def test_get_no_connected_peer(self):
response = yield self.web.get("/health")
data = response.json_value()

self.assertEqual(response.responseCode, 503)
self.assertEqual(data, {
'status': 'fail',
'description': ANY,
Expand Down Expand Up @@ -78,9 +101,10 @@ def test_get_peer_out_of_sync(self):

self.assertEqual(self.manager2.state, self.manager2.NodeState.READY)

response = yield self.web.get("p2p/readiness")
response = yield self.web.get("/health")
data = response.json_value()

self.assertEqual(response.responseCode, 503)
self.assertEqual(data, {
'status': 'fail',
'description': ANY,
Expand Down Expand Up @@ -109,9 +133,10 @@ def test_get_ready(self):
self.conn1.run_one_step(debug=True)
self.clock.advance(0.1)

response = yield self.web.get("p2p/readiness")
response = yield self.web.get("/health")
data = response.json_value()

self.assertEqual(response.responseCode, 200)
self.assertEqual(data, {
'status': 'pass',
'description': ANY,
Expand Down

0 comments on commit 131ccb9

Please sign in to comment.