async def test_is_ready(test_client, loop, test_name, mock_return, expected_code, expected_msg): """Assert that the readiness check works as expected.""" # setup class Service(): def __init__(self, mock_return): self.mock_return = mock_return @property async def is_ready(self): return self.mock_return if mock_return is None: mock_service = None else: mock_service = [Service(mock_return)] probe = Probes(loop=loop, components=mock_service) client = await test_client(probe.get_app()) # test it resp = await client.get('/readyz') # verify assert resp.status == expected_code assert expected_msg == await resp.json()
async def test_probe_stop_no_session(loop): """Assert the probe stops without an exception being thrown.""" probe = Probes(loop=loop, port=unused_port()) await probe.stop() # no exception was thrown assert True
async def run(self, loop, config, callback): # pylint: disable=too-many-locals """Run the main application loop for the service. This runs the main top level service functions for working with the Queue. """ self.service = ServiceWorker(loop=loop, cb_handler=callback, config=config) self.probe = Probes(components=[self.service], loop=loop) try: await self.probe.start() await self.service.connect() # register the signal handler for sig in ('SIGINT', 'SIGTERM'): loop.add_signal_handler( getattr(signal, sig), functools.partial(signal_handler, sig_loop=loop, task=self.close)) except Exception as e: # pylint: disable=broad-except # noqa B902 # TODO tighten this error and decide when to bail on the infinite reconnect logger.error(e)
async def test_probe_stop_with_session(loop): """Assert that call to probe stop doesn't throw an exception.""" probe = Probes(loop=loop, port=unused_port()) info = await probe.start() print(info) await probe.stop() assert True
async def test_probe_web_runner(loop): """Assert that the web app can be started successfully.""" probe = Probes(loop=loop, port=unused_port()) info = await probe.start() print(info) async with ClientSession() as session: async with session.get( f'http://{probe.host}:{str(probe.port)}/healthz') as resp: r_json = await resp.json() print(r_json) assert r_json == {'status': 'unhealthy'}
async def test_probe_meta_web_no_components(loop): """Assert that the web app can be started successfully.""" probe = Probes(loop=loop, port=unused_port()) info = await probe.start() print(info) async with ClientSession() as session: async with session.get( f'http://{probe.host}:{str(probe.port)}/meta') as resp: r_json = await resp.json() print(r_json) assert r_json == {'probe': probe.__version__}
async def test_probe_meta_web(loop): """Assert that the web app can be started successfully.""" class Comp: __version__ = 'component_version' def __init__(self): self.name = 'component_callback_name' self.version = 'component_callback_version' probe = Probes(loop=loop, port=unused_port(), components=[Comp()]) info = await probe.start() print(info) async with ClientSession() as session: async with session.get( f'http://{probe.host}:{str(probe.port)}/meta') as resp: r_json = await resp.json() print(r_json) assert r_json == { 'Comp': 'component_version', 'component_callback_name': 'component_callback_version', 'probe': probe.__version__ }
def test_get_app(loop): """Assert that get_app returns the same instance everytime.""" probe = Probes(loop=loop, port=unused_port()) app1 = probe.get_app() app2 = probe.get_app() assert app1 == app2