|
| 1 | +# Copyright (c) Jupyter Development Team. |
| 2 | +# Distributed under the terms of the Modified BSD License. |
| 3 | + |
| 4 | +import os |
| 5 | +import logging |
| 6 | +import pytest |
| 7 | +from binascii import hexlify |
| 8 | +from traitlets.config import Config |
| 9 | +from kernel_gateway.gatewayapp import KernelGatewayApp |
| 10 | + |
| 11 | +pytest_plugins = ["pytest_jupyter.jupyter_core", "pytest_jupyter.jupyter_server"] |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(scope="function") |
| 15 | +def jp_configurable_serverapp( |
| 16 | + jp_nbconvert_templates, # this fixture must precede jp_environ |
| 17 | + jp_environ, |
| 18 | + jp_server_config, |
| 19 | + jp_argv, |
| 20 | + jp_http_port, |
| 21 | + jp_base_url, |
| 22 | + tmp_path, |
| 23 | + jp_root_dir, |
| 24 | + jp_logging_stream, |
| 25 | + jp_asyncio_loop, |
| 26 | + io_loop, |
| 27 | +): |
| 28 | + """Starts a Jupyter Server instance based on |
| 29 | + the provided configuration values. |
| 30 | + The fixture is a factory; it can be called like |
| 31 | + a function inside a unit test. Here's a basic |
| 32 | + example of how use this fixture: |
| 33 | +
|
| 34 | + .. code-block:: python |
| 35 | +
|
| 36 | + def my_test(jp_configurable_serverapp): |
| 37 | + app = jp_configurable_serverapp(...) |
| 38 | + ... |
| 39 | + """ |
| 40 | + KernelGatewayApp.clear_instance() |
| 41 | + |
| 42 | + def _configurable_serverapp( |
| 43 | + config=jp_server_config, |
| 44 | + base_url=jp_base_url, |
| 45 | + argv=jp_argv, |
| 46 | + http_port=jp_http_port, |
| 47 | + **kwargs, |
| 48 | + ): |
| 49 | + c = Config(config) |
| 50 | + |
| 51 | + if "auth_token" not in c.KernelGatewayApp and not c.IdentityProvider.token: |
| 52 | + default_token = hexlify(os.urandom(4)).decode("ascii") |
| 53 | + c.IdentityProvider.token = default_token |
| 54 | + |
| 55 | + app = KernelGatewayApp.instance( |
| 56 | + # Set the log level to debug for testing purposes |
| 57 | + log_level="DEBUG", |
| 58 | + port=http_port, |
| 59 | + port_retries=0, |
| 60 | + base_url=base_url, |
| 61 | + config=c, |
| 62 | + **kwargs, |
| 63 | + ) |
| 64 | + app.log.propagate = True |
| 65 | + app.log.handlers = [] |
| 66 | + # Initialize app without httpserver |
| 67 | + if jp_asyncio_loop.is_running(): |
| 68 | + app.initialize(argv=argv, new_httpserver=False) |
| 69 | + else: |
| 70 | + |
| 71 | + async def initialize_app(): |
| 72 | + app.initialize(argv=argv, new_httpserver=False) |
| 73 | + |
| 74 | + jp_asyncio_loop.run_until_complete(initialize_app()) |
| 75 | + # Reroute all logging StreamHandlers away from stdin/stdout since pytest hijacks |
| 76 | + # these streams and closes them at unfortunate times. |
| 77 | + stream_handlers = [h for h in app.log.handlers if isinstance(h, logging.StreamHandler)] |
| 78 | + for handler in stream_handlers: |
| 79 | + handler.setStream(jp_logging_stream) |
| 80 | + app.log.propagate = True |
| 81 | + app.log.handlers = [] |
| 82 | + app.start_app() |
| 83 | + return app |
| 84 | + |
| 85 | + return _configurable_serverapp |
| 86 | + |
| 87 | + |
| 88 | +@pytest.fixture(autouse=True) |
| 89 | +def jp_server_cleanup(jp_asyncio_loop): |
| 90 | + yield |
| 91 | + app: KernelGatewayApp = KernelGatewayApp.instance() |
| 92 | + try: |
| 93 | + jp_asyncio_loop.run_until_complete(app.async_shutdown()) |
| 94 | + except (RuntimeError, SystemExit) as e: |
| 95 | + print("ignoring cleanup error", e) |
| 96 | + if hasattr(app, "kernel_manager"): |
| 97 | + app.kernel_manager.context.destroy() |
| 98 | + KernelGatewayApp.clear_instance() |
| 99 | + |
| 100 | + |
| 101 | +@pytest.fixture |
| 102 | +def jp_auth_header(jp_serverapp): |
| 103 | + """Configures an authorization header using the token from the serverapp fixture.""" |
| 104 | + return {"Authorization": f"token {jp_serverapp.identity_provider.token}"} |
0 commit comments