def test_check_connection_success(): phab = PhabricatorClient(api_key='api-key') success_json = CANNED_EMPTY_RESULT.copy() with requests_mock.mock() as m: m.get(phab_url('conduit.ping'), status_code=200, json=success_json) phab.check_connection() assert m.called
def test_raise_exception_if_api_ping_times_out(): phab = PhabricatorClient(api_key='api-key') with requests_mock.mock() as m: # Test with the generic Timeout exception, which all other timeout # exceptions derive from. m.get(phab_url('conduit.ping'), exc=requests.Timeout) with pytest.raises(PhabricatorAPIException): phab.check_connection() assert m.called
def test_raise_exception_if_ping_encounters_connection_error(): phab = PhabricatorClient(api_key='api-key') with requests_mock.mock() as m: # Test with the generic ConnectionError, which is a superclass for # other connection error types. m.get(phab_url('conduit.ping'), exc=requests.ConnectionError) with pytest.raises(PhabricatorAPIException): phab.check_connection() assert m.called
def test_raise_exception_if_api_returns_error_json_response(): phab = PhabricatorClient(api_key='api-key') error_json = { "result": None, "error_code": "ERR-CONDUIT-CORE", "error_info": "BOOM" } with requests_mock.mock() as m: # Test with the generic Timeout exception, which all other timeout # exceptions derive from. m.get(phab_url('conduit.ping'), status_code=500, json=error_json) with pytest.raises(PhabricatorAPIException): phab.check_connection() assert m.called
def heartbeat(): """Perform an in-depth service health check. This should check all the services that this service depends on and return a 200 iff those services and the app itself are performing normally. Return a 5XX if something goes wrong. """ phab = PhabricatorClient(api_key='') try: phab.check_connection() except PhabricatorAPIException: logger.warning( { 'msg': 'problem connecting to Phabricator', }, 'heartbeat' ) return 'heartbeat: problem', 502 logger.info({'msg': 'ok, all services are up'}, 'heartbeat') return 'heartbeat: ok', 200