-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytest_plugin.py
619 lines (491 loc) · 19 KB
/
pytest_plugin.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import importlib
import io
import json
import logging
import os
import shutil
import sys
import urllib.parse
from binascii import hexlify
import jupyter_core.paths
import nbformat
import pytest
import tornado
from tornado.escape import url_escape
from tornado.httpclient import HTTPClientError
from tornado.websocket import WebSocketHandler
from traitlets.config import Config, re
from jupyter_server.auth import Authorizer
from jupyter_server.extension import serverextension
from jupyter_server.serverapp import JUPYTER_SERVICE_HANDLERS, ServerApp
from jupyter_server.services.contents.filemanager import FileContentsManager
from jupyter_server.services.contents.largefilemanager import LargeFileManager
from jupyter_server.utils import url_path_join
# List of dependencies needed for this plugin.
pytest_plugins = [
"pytest_tornasync",
# Once the chunk below moves to Jupyter Core, we'll uncomment
# This plugin and use the fixtures directly from Jupyter Core.
# "jupyter_core.pytest_plugin"
]
import asyncio
if os.name == "nt" and sys.version_info >= (3, 7):
asyncio.set_event_loop_policy(
asyncio.WindowsSelectorEventLoopPolicy() # type:ignore[attr-defined]
)
# ============ Move to Jupyter Core =============
def mkdir(tmp_path, *parts):
path = tmp_path.joinpath(*parts)
if not path.exists():
path.mkdir(parents=True)
return path
@pytest.fixture
def jp_home_dir(tmp_path):
"""Provides a temporary HOME directory value."""
return mkdir(tmp_path, "home")
@pytest.fixture
def jp_data_dir(tmp_path):
"""Provides a temporary Jupyter data dir directory value."""
return mkdir(tmp_path, "data")
@pytest.fixture
def jp_config_dir(tmp_path):
"""Provides a temporary Jupyter config dir directory value."""
return mkdir(tmp_path, "config")
@pytest.fixture
def jp_runtime_dir(tmp_path):
"""Provides a temporary Jupyter runtime dir directory value."""
return mkdir(tmp_path, "runtime")
@pytest.fixture
def jp_system_jupyter_path(tmp_path):
"""Provides a temporary Jupyter system path value."""
return mkdir(tmp_path, "share", "jupyter")
@pytest.fixture
def jp_env_jupyter_path(tmp_path):
"""Provides a temporary Jupyter env system path value."""
return mkdir(tmp_path, "env", "share", "jupyter")
@pytest.fixture
def jp_system_config_path(tmp_path):
"""Provides a temporary Jupyter config path value."""
return mkdir(tmp_path, "etc", "jupyter")
@pytest.fixture
def jp_env_config_path(tmp_path):
"""Provides a temporary Jupyter env config path value."""
return mkdir(tmp_path, "env", "etc", "jupyter")
@pytest.fixture
def jp_environ(
monkeypatch,
tmp_path,
jp_home_dir,
jp_data_dir,
jp_config_dir,
jp_runtime_dir,
jp_system_jupyter_path,
jp_system_config_path,
jp_env_jupyter_path,
jp_env_config_path,
):
"""Configures a temporary environment based on Jupyter-specific environment variables."""
monkeypatch.setenv("HOME", str(jp_home_dir))
monkeypatch.setenv("PYTHONPATH", os.pathsep.join(sys.path))
# monkeypatch.setenv("JUPYTER_NO_CONFIG", "1")
monkeypatch.setenv("JUPYTER_CONFIG_DIR", str(jp_config_dir))
monkeypatch.setenv("JUPYTER_DATA_DIR", str(jp_data_dir))
monkeypatch.setenv("JUPYTER_RUNTIME_DIR", str(jp_runtime_dir))
monkeypatch.setattr(jupyter_core.paths, "SYSTEM_JUPYTER_PATH", [str(jp_system_jupyter_path)])
monkeypatch.setattr(jupyter_core.paths, "ENV_JUPYTER_PATH", [str(jp_env_jupyter_path)])
monkeypatch.setattr(jupyter_core.paths, "SYSTEM_CONFIG_PATH", [str(jp_system_config_path)])
monkeypatch.setattr(jupyter_core.paths, "ENV_CONFIG_PATH", [str(jp_env_config_path)])
# ================= End: Move to Jupyter core ================
@pytest.fixture
def jp_server_config():
"""Allows tests to setup their specific configuration values."""
return Config(
{
"jpserver_extensions": {"jupyter_server_terminals": True},
}
)
@pytest.fixture
def jp_root_dir(tmp_path):
"""Provides a temporary Jupyter root directory value."""
return mkdir(tmp_path, "root_dir")
@pytest.fixture
def jp_template_dir(tmp_path):
"""Provides a temporary Jupyter templates directory value."""
return mkdir(tmp_path, "templates")
@pytest.fixture
def jp_argv():
"""Allows tests to setup specific argv values."""
return []
@pytest.fixture
def jp_extension_environ(jp_env_config_path, monkeypatch):
"""Monkeypatch a Jupyter Extension's config path into each test's environment variable"""
monkeypatch.setattr(serverextension, "ENV_CONFIG_PATH", [str(jp_env_config_path)])
@pytest.fixture
def jp_http_port(http_server_port):
"""Returns the port value from the http_server_port fixture."""
return http_server_port[-1]
@pytest.fixture
def jp_nbconvert_templates(jp_data_dir):
"""Setups up a temporary directory consisting of the nbconvert templates."""
# Get path to nbconvert template directory *before*
# monkeypatching the paths env variable via the jp_environ fixture.
possible_paths = jupyter_core.paths.jupyter_path("nbconvert", "templates")
nbconvert_path = None
for path in possible_paths:
if os.path.exists(path):
nbconvert_path = path
break
nbconvert_target = jp_data_dir / "nbconvert" / "templates"
# copy nbconvert templates to new tmp data_dir.
if nbconvert_path:
shutil.copytree(nbconvert_path, str(nbconvert_target))
@pytest.fixture
def jp_logging_stream():
"""StringIO stream intended to be used by the core
Jupyter ServerApp logger's default StreamHandler. This
helps avoid collision with stdout which is hijacked
by Pytest.
"""
logging_stream = io.StringIO()
yield logging_stream
output = logging_stream.getvalue()
# If output exists, print it.
if output:
print(output)
return output
@pytest.fixture(scope="function")
def jp_configurable_serverapp(
jp_nbconvert_templates, # this fixture must preceed jp_environ
jp_environ,
jp_server_config,
jp_argv,
jp_http_port,
jp_base_url,
tmp_path,
jp_root_dir,
io_loop,
jp_logging_stream,
):
"""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(...)
...
"""
ServerApp.clear_instance()
# Inject jupyter_server_terminals into config unless it was
# explicitly put in config.
serverapp_config = jp_server_config.setdefault("ServerApp", {})
exts = serverapp_config.setdefault("jpserver_extensions", {})
if "jupyter_server_terminals" not in exts:
exts["jupyter_server_terminals"] = True
def _configurable_serverapp(
config=jp_server_config,
base_url=jp_base_url,
argv=jp_argv,
environ=jp_environ,
http_port=jp_http_port,
tmp_path=tmp_path,
root_dir=jp_root_dir,
**kwargs,
):
c = Config(config)
c.NotebookNotary.db_file = ":memory:"
token = hexlify(os.urandom(4)).decode("ascii")
c.IdentityProvider.token = token
# Allow tests to configure root_dir via a file, argv, or its
# default (cwd) by specifying a value of None.
if root_dir is not None:
kwargs["root_dir"] = str(root_dir)
app = ServerApp.instance(
# Set the log level to debug for testing purposes
log_level="DEBUG",
port=http_port,
port_retries=0,
open_browser=False,
base_url=base_url,
config=c,
allow_root=True,
**kwargs,
)
app.init_signal = lambda: None
app.log.propagate = True
app.log.handlers = []
# Initialize app without httpserver
app.initialize(argv=argv, new_httpserver=False)
# 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 = []
# Start app without ioloop
app.start_app()
return app
return _configurable_serverapp
@pytest.fixture
def jp_ensure_app_fixture(request):
"""Ensures that the 'app' fixture used by pytest-tornasync
is set to `jp_web_app`, the Tornado Web Application returned
by the ServerApp in Jupyter Server, provided by the jp_web_app
fixture in this module.
Note, this hardcodes the `app_fixture` option from
pytest-tornasync to `jp_web_app`. If this value is configured
to something other than the default, it will raise an exception.
"""
app_option = request.config.getoption("app_fixture")
if app_option not in ["app", "jp_web_app"]:
raise Exception(
"jp_serverapp requires the `app-fixture` option "
"to be set to 'jp_web_app`. Try rerunning the "
"current tests with the option `--app-fixture "
"jp_web_app`."
)
elif app_option == "app":
# Manually set the app_fixture to `jp_web_app` if it's
# not set already.
request.config.option.app_fixture = "jp_web_app"
@pytest.fixture(scope="function")
def jp_serverapp(jp_ensure_app_fixture, jp_server_config, jp_argv, jp_configurable_serverapp):
"""Starts a Jupyter Server instance based on the established configuration values."""
return jp_configurable_serverapp(config=jp_server_config, argv=jp_argv)
@pytest.fixture
def jp_web_app(jp_serverapp):
"""app fixture is needed by pytest_tornasync plugin"""
return jp_serverapp.web_app
@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}"}
@pytest.fixture
def jp_base_url():
"""Returns the base url to use for the test."""
return "/a%40b/"
@pytest.fixture
def jp_fetch(jp_serverapp, http_server_client, jp_auth_header, jp_base_url):
"""Sends an (asynchronous) HTTP request to a test server.
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
async def my_test(jp_fetch):
response = await jp_fetch("api", "spec.yaml")
...
"""
def client_fetch(*parts, headers=None, params=None, **kwargs):
if not headers:
headers = {}
if not params:
params = {}
# Handle URL strings
path_url = url_escape(url_path_join(*parts), plus=False)
base_path_url = url_path_join(jp_base_url, path_url)
params_url = urllib.parse.urlencode(params)
url = base_path_url + "?" + params_url
# Add auth keys to header
headers.update(jp_auth_header)
# Make request.
return http_server_client.fetch(url, headers=headers, request_timeout=20, **kwargs)
return client_fetch
@pytest.fixture
def jp_ws_fetch(jp_serverapp, http_server_client, jp_auth_header, jp_http_port, jp_base_url):
"""Sends a websocket request to a test server.
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
async def my_test(jp_fetch, jp_ws_fetch):
# Start a kernel
r = await jp_fetch(
'api', 'kernels',
method='POST',
body=json.dumps({
'name': "python3"
})
)
kid = json.loads(r.body.decode())['id']
# Open a websocket connection.
ws = await jp_ws_fetch(
'api', 'kernels', kid, 'channels'
)
...
"""
def client_fetch(*parts, headers=None, params=None, **kwargs):
if not headers:
headers = {}
if not params:
params = {}
# Handle URL strings
path_url = url_escape(url_path_join(*parts), plus=False)
base_path_url = url_path_join(jp_base_url, path_url)
urlparts = urllib.parse.urlparse(f"ws://localhost:{jp_http_port}")
urlparts = urlparts._replace(path=base_path_url, query=urllib.parse.urlencode(params))
url = urlparts.geturl()
# Add auth keys to header
headers.update(jp_auth_header)
# Make request.
req = tornado.httpclient.HTTPRequest(url, headers=headers, connect_timeout=120)
return tornado.websocket.websocket_connect(req)
return client_fetch
some_resource = "The very model of a modern major general"
sample_kernel_json = {
"argv": ["cat", "{connection_file}"],
"display_name": "Test kernel",
}
@pytest.fixture
def jp_kernelspecs(jp_data_dir):
"""Configures some sample kernelspecs in the Jupyter data directory."""
spec_names = ["sample", "sample2", "bad"]
for name in spec_names:
sample_kernel_dir = jp_data_dir.joinpath("kernels", name)
sample_kernel_dir.mkdir(parents=True)
# Create kernel json file
sample_kernel_file = sample_kernel_dir.joinpath("kernel.json")
kernel_json = sample_kernel_json.copy()
if name == "bad":
kernel_json["argv"] = ["non_existent_path"]
sample_kernel_file.write_text(json.dumps(kernel_json))
# Create resources text
sample_kernel_resources = sample_kernel_dir.joinpath("resource.txt")
sample_kernel_resources.write_text(some_resource)
@pytest.fixture(params=[True, False])
def jp_contents_manager(request, tmp_path):
"""Returns a FileContentsManager instance based on the use_atomic_writing parameter value."""
return FileContentsManager(root_dir=str(tmp_path), use_atomic_writing=request.param)
@pytest.fixture
def jp_large_contents_manager(tmp_path):
"""Returns a LargeFileManager instance."""
return LargeFileManager(root_dir=str(tmp_path))
@pytest.fixture
def jp_create_notebook(jp_root_dir):
"""Creates a notebook in the test's home directory."""
def inner(nbpath):
nbpath = jp_root_dir.joinpath(nbpath)
# Check that the notebook has the correct file extension.
if nbpath.suffix != ".ipynb":
raise Exception("File extension for notebook must be .ipynb")
# If the notebook path has a parent directory, make sure it's created.
parent = nbpath.parent
parent.mkdir(parents=True, exist_ok=True)
# Create a notebook string and write to file.
nb = nbformat.v4.new_notebook()
nbtext = nbformat.writes(nb, version=4)
nbpath.write_text(nbtext)
return inner
@pytest.fixture(autouse=True)
def jp_server_cleanup(io_loop):
yield
app: ServerApp = ServerApp.instance()
loop = io_loop.asyncio_loop
loop.run_until_complete(app._cleanup())
ServerApp.clear_instance()
@pytest.fixture
def jp_cleanup_subprocesses(jp_serverapp):
"""DEPRECATED: The jp_server_cleanup fixture automatically cleans up the singleton ServerApp class"""
async def _():
pass
return _
@pytest.fixture
def send_request(jp_fetch, jp_ws_fetch):
"""Send to Jupyter Server and return response code."""
async def _(url, **fetch_kwargs):
if url.endswith("channels") or "/websocket/" in url:
fetch = jp_ws_fetch
else:
fetch = jp_fetch
try:
r = await fetch(url, **fetch_kwargs, allow_nonstandard_methods=True)
code = r.code
except HTTPClientError as err:
code = err.code
else:
if fetch is jp_ws_fetch:
r.close()
return code
return _
@pytest.fixture
def jp_server_auth_core_resources():
modules = []
for mod_name in JUPYTER_SERVICE_HANDLERS.values():
if mod_name:
modules.extend(mod_name)
resource_map = {}
for handler_module in modules:
mod = importlib.import_module(handler_module)
name = mod.AUTH_RESOURCE
for handler in mod.default_handlers:
url_regex = handler[0]
resource_map[url_regex] = name
return resource_map
@pytest.fixture
def jp_server_auth_resources(jp_server_auth_core_resources):
return jp_server_auth_core_resources
@pytest.fixture
def jp_server_authorizer(jp_server_auth_resources):
class _(Authorizer):
# Set these class attributes from within a test
# to verify that they match the arguments passed
# by the REST API.
permissions: dict = {}
HTTP_METHOD_TO_AUTH_ACTION = {
"GET": "read",
"HEAD": "read",
"OPTIONS": "read",
"POST": "write",
"PUT": "write",
"PATCH": "write",
"DELETE": "write",
"WEBSOCKET": "execute",
}
def match_url_to_resource(self, url, regex_mapping=None):
"""Finds the JupyterHandler regex pattern that would
match the given URL and returns the resource name (str)
of that handler.
e.g.
/api/contents/... returns "contents"
"""
if not regex_mapping:
regex_mapping = jp_server_auth_resources
for regex, auth_resource in regex_mapping.items():
pattern = re.compile(regex)
if pattern.fullmatch(url):
return auth_resource
def normalize_url(self, path):
"""Drop the base URL and make sure path leads with a /"""
base_url = self.parent.base_url
# Remove base_url
if path.startswith(base_url):
path = path[len(base_url) :]
# Make sure path starts with /
if not path.startswith("/"):
path = "/" + path
return path
def is_authorized(self, handler, user, action, resource):
# Parse Request
if isinstance(handler, WebSocketHandler):
method = "WEBSOCKET"
else:
method = handler.request.method
url = self.normalize_url(handler.request.path)
# Map request parts to expected action and resource.
expected_action = self.HTTP_METHOD_TO_AUTH_ACTION[method]
expected_resource = self.match_url_to_resource(url)
# Assert that authorization layer returns the
# correct action + resource.
assert action == expected_action
assert resource == expected_resource
# Now, actually apply the authorization layer.
return all(
[
action in self.permissions.get("actions", []),
resource in self.permissions.get("resources", []),
]
)
return _