Assertions

hospital provides a set of assertions to write healthchecks.

HTTP

Assertions around HTTP resources.

hospital.assertions.http.assert_http_response(url, status_code=200, timeout=1, msg=None)

Assert that GET url returns status_code within timeout.

>>> from hospital import assert_http_response
>>> assert_http_response('http://hospital.readthedocs.org', 200)

Raises AssertionError in case of failure.

>>> assert_http_response('http://hospital.readthedocs.org', 401)
... 
Traceback (most recent call last):
    ...
AssertionError: GET "..." returned 200 status code. Expected 401.

Use timeout argument as a ceil for tolerable latency (in seconds).

>>> assert_http_response('http://hospital.readthedocs.org', timeout=10)

Default value for timeout is 1 second. This value was chosen with the idea that if you cannot get a response from external services within 1 second, then there is a performance issue.

Networking

Assertions related to networking.

hospital.assertions.networking.assert_ping(host, timeout=1, msg=None)

Assert host responds to ping within timeout.

>>> from hospital import assert_ping
>>> assert_ping('hospital.readthedocs.org')

Packaging

Assertions related to Python packaging.

hospital.assertions.packaging.assert_supported_python_version(distribution, version=None, msg=None)

Assert that distribution claims support for Python version.

Typically used to check theorical compatibility between runtime Python version and installed Python software.

distribution

Distribution object, as returned by pkg_resources.get_distribution().

>>> from hospital import assert_supported_python_version
>>> import pkg_resources
>>> hospital_dist = pkg_resources.get_distribution('hospital')
>>> assert_supported_python_version(hospital_dist)
version

Python version, as a string. If omitted or None (the default), the current Python version is retrieved from sys.version_info.

As an example, hospital claims support for Python 2.7 and 3.3, but not for version 2.6.

>>> assert_supported_python_version(hospital_dist, version='2.7')
>>> assert_supported_python_version(hospital_dist, version='3.3')
>>> try:
...     assert_supported_python_version(hospital_dist, version='2.6')
... except AssertionError:
...     pass

See also DistributionHealthCheck.