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