forked from jupyter-server/jupyter_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_gateway.py
562 lines (441 loc) · 19.8 KB
/
test_gateway.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
"""Test GatewayClient"""
import asyncio
import json
import logging
import os
import uuid
from datetime import datetime
from io import BytesIO
from queue import Empty
from unittest.mock import MagicMock, patch
import pytest
import tornado
from tornado.httpclient import HTTPRequest, HTTPResponse
from tornado.web import HTTPError
from jupyter_server.gateway.managers import (
ChannelQueue,
GatewayClient,
GatewayKernelManager,
)
from jupyter_server.utils import ensure_async
from .utils import expected_http_error
def generate_kernelspec(name):
argv_stanza = ["python", "-m", "ipykernel_launcher", "-f", "{connection_file}"]
spec_stanza = {
"spec": {
"argv": argv_stanza,
"env": {},
"display_name": name,
"language": "python",
"interrupt_mode": "signal",
"metadata": {},
}
}
kernelspec_stanza = {"name": name, "spec": spec_stanza, "resources": {}}
return kernelspec_stanza
# We'll mock up two kernelspecs - kspec_foo and kspec_bar
kernelspecs: dict = {
"default": "kspec_foo",
"kernelspecs": {
"kspec_foo": generate_kernelspec("kspec_foo"),
"kspec_bar": generate_kernelspec("kspec_bar"),
},
}
# maintain a dictionary of expected running kernels. Key = kernel_id, Value = model.
running_kernels = {}
def generate_model(name):
"""Generate a mocked kernel model. Caller is responsible for adding model to running_kernels dictionary."""
dt = datetime.utcnow().isoformat() + "Z"
kernel_id = str(uuid.uuid4())
model = {
"id": kernel_id,
"name": name,
"last_activity": str(dt),
"execution_state": "idle",
"connections": 1,
}
return model
async def mock_gateway_request(url, **kwargs):
method = "GET"
if kwargs["method"]:
method = kwargs["method"]
request = HTTPRequest(url=url, **kwargs)
endpoint = str(url)
# Fetch all kernelspecs
if endpoint.endswith("/api/kernelspecs") and method == "GET":
response_buf = BytesIO(json.dumps(kernelspecs).encode("utf-8"))
response = await ensure_async(HTTPResponse(request, 200, buffer=response_buf))
return response
# Fetch named kernelspec
if endpoint.rfind("/api/kernelspecs/") >= 0 and method == "GET":
requested_kernelspec = endpoint.rpartition("/")[2]
kspecs: dict = kernelspecs["kernelspecs"]
if requested_kernelspec in kspecs:
response_str = json.dumps(kspecs.get(requested_kernelspec))
response_buf = BytesIO(response_str.encode("utf-8"))
response = await ensure_async(HTTPResponse(request, 200, buffer=response_buf))
return response
else:
raise HTTPError(404, message="Kernelspec does not exist: %s" % requested_kernelspec)
# Create kernel
if endpoint.endswith("/api/kernels") and method == "POST":
json_body = json.loads(kwargs["body"])
name = json_body.get("name")
env = json_body.get("env")
kspec_name = env.get("KERNEL_KSPEC_NAME")
assert name == kspec_name # Ensure that KERNEL_ env values get propagated
model = generate_model(name)
running_kernels[model.get("id")] = model # Register model as a running kernel
response_buf = BytesIO(json.dumps(model).encode("utf-8"))
response = await ensure_async(HTTPResponse(request, 201, buffer=response_buf))
return response
# Fetch list of running kernels
if endpoint.endswith("/api/kernels") and method == "GET":
kernels = []
for kernel_id in running_kernels.keys():
model = running_kernels.get(kernel_id)
kernels.append(model)
response_buf = BytesIO(json.dumps(kernels).encode("utf-8"))
response = await ensure_async(HTTPResponse(request, 200, buffer=response_buf))
return response
# Interrupt or restart existing kernel
if endpoint.rfind("/api/kernels/") >= 0 and method == "POST":
requested_kernel_id, sep, action = endpoint.rpartition("/api/kernels/")[2].rpartition("/")
if action == "interrupt":
if requested_kernel_id in running_kernels:
response = await ensure_async(HTTPResponse(request, 204))
return response
else:
raise HTTPError(404, message="Kernel does not exist: %s" % requested_kernel_id)
elif action == "restart":
if requested_kernel_id in running_kernels:
response_str = json.dumps(running_kernels.get(requested_kernel_id))
response_buf = BytesIO(response_str.encode("utf-8"))
response = await ensure_async(HTTPResponse(request, 204, buffer=response_buf))
return response
else:
raise HTTPError(404, message="Kernel does not exist: %s" % requested_kernel_id)
else:
raise HTTPError(404, message="Bad action detected: %s" % action)
# Shutdown existing kernel
if endpoint.rfind("/api/kernels/") >= 0 and method == "DELETE":
requested_kernel_id = endpoint.rpartition("/")[2]
if requested_kernel_id not in running_kernels:
raise HTTPError(404, message="Kernel does not exist: %s" % requested_kernel_id)
running_kernels.pop(
requested_kernel_id
) # Simulate shutdown by removing kernel from running set
response = await ensure_async(HTTPResponse(request, 204))
return response
# Fetch existing kernel
if endpoint.rfind("/api/kernels/") >= 0 and method == "GET":
requested_kernel_id = endpoint.rpartition("/")[2]
if requested_kernel_id in running_kernels:
response_str = json.dumps(running_kernels.get(requested_kernel_id))
response_buf = BytesIO(response_str.encode("utf-8"))
response = await ensure_async(HTTPResponse(request, 200, buffer=response_buf))
return response
else:
raise HTTPError(404, message="Kernel does not exist: %s" % requested_kernel_id)
mocked_gateway = patch("jupyter_server.gateway.managers.gateway_request", mock_gateway_request)
mock_gateway_url = "http://mock-gateway-server:8889"
mock_http_user = "alice"
def mock_websocket_create_connection(recv_side_effect=None):
def helper(*args, **kwargs):
mock = MagicMock()
mock.recv = MagicMock(side_effect=recv_side_effect)
return mock
return helper
@pytest.fixture
def init_gateway(monkeypatch):
"""Initializes the server for use as a gateway client."""
# Clear the singleton first since previous tests may not have used a gateway.
GatewayClient.clear_instance()
monkeypatch.setenv("JUPYTER_GATEWAY_URL", mock_gateway_url)
monkeypatch.setenv("JUPYTER_GATEWAY_HTTP_USER", mock_http_user)
monkeypatch.setenv("JUPYTER_GATEWAY_REQUEST_TIMEOUT", "44.4")
monkeypatch.setenv("JUPYTER_GATEWAY_CONNECT_TIMEOUT", "44.4")
monkeypatch.setenv("JUPYTER_GATEWAY_LAUNCH_TIMEOUT_PAD", "1")
yield
GatewayClient.clear_instance()
async def test_gateway_env_options(init_gateway, jp_serverapp):
assert jp_serverapp.gateway_config.gateway_enabled is True
assert jp_serverapp.gateway_config.url == mock_gateway_url
assert jp_serverapp.gateway_config.http_user == mock_http_user
assert (
jp_serverapp.gateway_config.connect_timeout == jp_serverapp.gateway_config.request_timeout
)
assert jp_serverapp.gateway_config.connect_timeout == 44.4
assert jp_serverapp.gateway_config.launch_timeout_pad == 1
GatewayClient.instance().init_static_args()
assert GatewayClient.instance().KERNEL_LAUNCH_TIMEOUT == 43
async def test_gateway_cli_options(jp_configurable_serverapp):
argv = [
"--gateway-url=" + mock_gateway_url,
"--GatewayClient.http_user=" + mock_http_user,
"--GatewayClient.connect_timeout=44.4",
"--GatewayClient.request_timeout=96.0",
"--GatewayClient.launch_timeout_pad=6",
]
GatewayClient.clear_instance()
app = jp_configurable_serverapp(argv=argv)
assert app.gateway_config.gateway_enabled is True
assert app.gateway_config.url == mock_gateway_url
assert app.gateway_config.http_user == mock_http_user
assert app.gateway_config.connect_timeout == 44.4
assert app.gateway_config.request_timeout == 96.0
assert app.gateway_config.launch_timeout_pad == 6
GatewayClient.instance().init_static_args()
assert (
GatewayClient.instance().KERNEL_LAUNCH_TIMEOUT == 90
) # Ensure KLT gets set from request-timeout - launch_timeout_pad
GatewayClient.clear_instance()
@pytest.mark.parametrize(
"request_timeout,kernel_launch_timeout,expected_request_timeout,expected_kernel_launch_timeout",
[(50, 10, 50, 45), (10, 50, 55, 50)],
)
async def test_gateway_request_timeout_pad_option(
jp_configurable_serverapp,
monkeypatch,
request_timeout,
kernel_launch_timeout,
expected_request_timeout,
expected_kernel_launch_timeout,
):
argv = [
f"--GatewayClient.request_timeout={request_timeout}",
"--GatewayClient.launch_timeout_pad=5",
]
GatewayClient.clear_instance()
app = jp_configurable_serverapp(argv=argv)
monkeypatch.setattr(GatewayClient, "KERNEL_LAUNCH_TIMEOUT", kernel_launch_timeout)
GatewayClient.instance().init_static_args()
assert app.gateway_config.request_timeout == expected_request_timeout
assert GatewayClient.instance().KERNEL_LAUNCH_TIMEOUT == expected_kernel_launch_timeout
GatewayClient.clear_instance()
async def test_gateway_class_mappings(init_gateway, jp_serverapp):
# Ensure appropriate class mappings are in place.
assert jp_serverapp.kernel_manager_class.__name__ == "GatewayMappingKernelManager"
assert jp_serverapp.session_manager_class.__name__ == "GatewaySessionManager"
assert jp_serverapp.kernel_spec_manager_class.__name__ == "GatewayKernelSpecManager"
async def test_gateway_get_kernelspecs(init_gateway, jp_fetch):
# Validate that kernelspecs come from gateway.
with mocked_gateway:
r = await jp_fetch("api", "kernelspecs", method="GET")
assert r.code == 200
content = json.loads(r.body.decode("utf-8"))
kspecs = content.get("kernelspecs")
assert len(kspecs) == 2
assert kspecs.get("kspec_bar").get("name") == "kspec_bar"
async def test_gateway_get_named_kernelspec(init_gateway, jp_fetch):
# Validate that a specific kernelspec can be retrieved from gateway (and an invalid spec can't)
with mocked_gateway:
r = await jp_fetch("api", "kernelspecs", "kspec_foo", method="GET")
assert r.code == 200
kspec_foo = json.loads(r.body.decode("utf-8"))
assert kspec_foo.get("name") == "kspec_foo"
with pytest.raises(tornado.httpclient.HTTPClientError) as e:
await jp_fetch("api", "kernelspecs", "no_such_spec", method="GET")
assert expected_http_error(e, 404)
async def test_gateway_session_lifecycle(init_gateway, jp_root_dir, jp_fetch):
# Validate session lifecycle functions; create and delete.
# create
session_id, kernel_id = await create_session(jp_root_dir, jp_fetch, "kspec_foo")
# ensure kernel still considered running
assert await is_kernel_running(jp_fetch, kernel_id) is True
# interrupt
await interrupt_kernel(jp_fetch, kernel_id)
# ensure kernel still considered running
assert await is_kernel_running(jp_fetch, kernel_id) is True
# restart
await restart_kernel(jp_fetch, kernel_id)
# ensure kernel still considered running
assert await is_kernel_running(jp_fetch, kernel_id) is True
# delete
await delete_session(jp_fetch, session_id)
assert await is_kernel_running(jp_fetch, kernel_id) is False
async def test_gateway_kernel_lifecycle(init_gateway, jp_fetch):
# Validate kernel lifecycle functions; create, interrupt, restart and delete.
# create
kernel_id = await create_kernel(jp_fetch, "kspec_bar")
# ensure kernel still considered running
assert await is_kernel_running(jp_fetch, kernel_id) is True
# interrupt
await interrupt_kernel(jp_fetch, kernel_id)
# ensure kernel still considered running
assert await is_kernel_running(jp_fetch, kernel_id) is True
# restart
await restart_kernel(jp_fetch, kernel_id)
# ensure kernel still considered running
assert await is_kernel_running(jp_fetch, kernel_id) is True
# delete
await delete_kernel(jp_fetch, kernel_id)
assert await is_kernel_running(jp_fetch, kernel_id) is False
@pytest.mark.parametrize("missing_kernel", [True, False])
async def test_gateway_shutdown(init_gateway, jp_serverapp, jp_fetch, missing_kernel):
# Validate server shutdown when multiple gateway kernels are present or
# we've lost track of at least one (missing) kernel
# create two kernels
k1 = await create_kernel(jp_fetch, "kspec_bar")
k2 = await create_kernel(jp_fetch, "kspec_bar")
# ensure they're considered running
assert await is_kernel_running(jp_fetch, k1) is True
assert await is_kernel_running(jp_fetch, k2) is True
if missing_kernel:
running_kernels.pop(k1) # "terminate" kernel w/o our knowledge
with mocked_gateway:
await jp_serverapp.kernel_manager.shutdown_all()
assert await is_kernel_running(jp_fetch, k1) is False
assert await is_kernel_running(jp_fetch, k2) is False
@patch("websocket.create_connection", mock_websocket_create_connection(recv_side_effect=Exception))
async def test_kernel_client_response_router_notifies_channel_queue_when_finished(
init_gateway, jp_serverapp, jp_fetch
):
# create
kernel_id = await create_kernel(jp_fetch, "kspec_bar")
# get kernel manager
km: GatewayKernelManager = jp_serverapp.kernel_manager.get_kernel(kernel_id)
# create kernel client
kc = km.client()
await ensure_async(kc.start_channels())
with pytest.raises(RuntimeError):
await kc.iopub_channel.get_msg(timeout=10)
all_channels = [
kc.shell_channel,
kc.iopub_channel,
kc.stdin_channel,
kc.hb_channel,
kc.control_channel,
]
assert all(channel.response_router_finished if True else False for channel in all_channels)
await ensure_async(kc.stop_channels())
# delete
await delete_kernel(jp_fetch, kernel_id)
async def test_channel_queue_get_msg_with_invalid_timeout():
queue = ChannelQueue("iopub", MagicMock(), logging.getLogger())
with pytest.raises(ValueError):
await queue.get_msg(timeout=-1)
async def test_channel_queue_get_msg_raises_empty_after_timeout():
queue = ChannelQueue("iopub", MagicMock(), logging.getLogger())
with pytest.raises(Empty):
await asyncio.wait_for(queue.get_msg(timeout=0.1), 2)
async def test_channel_queue_get_msg_without_timeout():
queue = ChannelQueue("iopub", MagicMock(), logging.getLogger())
with pytest.raises(asyncio.TimeoutError):
await asyncio.wait_for(queue.get_msg(timeout=None), 1)
async def test_channel_queue_get_msg_with_existing_item():
sent_message = {"msg_id": 1, "msg_type": 2}
queue = ChannelQueue("iopub", MagicMock(), logging.getLogger())
queue.put_nowait(sent_message)
received_message = await asyncio.wait_for(queue.get_msg(timeout=None), 1)
assert received_message == sent_message
async def test_channel_queue_get_msg_when_response_router_had_finished():
queue = ChannelQueue("iopub", MagicMock(), logging.getLogger())
queue.response_router_finished = True
with pytest.raises(RuntimeError):
await queue.get_msg()
#
# Test methods below...
#
async def create_session(root_dir, jp_fetch, kernel_name):
"""Creates a session for a kernel. The session is created against the server
which then uses the gateway for kernel management.
"""
with mocked_gateway:
nb_path = root_dir / "testgw.ipynb"
body = json.dumps(
{"path": str(nb_path), "type": "notebook", "kernel": {"name": kernel_name}}
)
# add a KERNEL_ value to the current env and we'll ensure that that value exists in the mocked method
os.environ["KERNEL_KSPEC_NAME"] = kernel_name
# Create the kernel... (also tests get_kernel)
r = await jp_fetch("api", "sessions", method="POST", body=body)
assert r.code == 201
model = json.loads(r.body.decode("utf-8"))
assert model.get("path") == str(nb_path)
kernel_id = model.get("kernel").get("id")
# ensure its in the running_kernels and name matches.
running_kernel = running_kernels.get(kernel_id)
assert running_kernel is not None
assert kernel_id == running_kernel.get("id")
assert model.get("kernel").get("name") == running_kernel.get("name")
session_id = model.get("id")
# restore env
os.environ.pop("KERNEL_KSPEC_NAME")
return session_id, kernel_id
async def delete_session(jp_fetch, session_id):
"""Deletes a session corresponding to the given session id."""
with mocked_gateway:
# Delete the session (and kernel)
r = await jp_fetch("api", "sessions", session_id, method="DELETE")
assert r.code == 204
assert r.reason == "No Content"
async def is_kernel_running(jp_fetch, kernel_id):
"""Issues request to get the set of running kernels"""
with mocked_gateway:
# Get list of running kernels
r = await jp_fetch("api", "kernels", method="GET")
assert r.code == 200
kernels = json.loads(r.body.decode("utf-8"))
assert len(kernels) == len(running_kernels)
for model in kernels:
if model.get("id") == kernel_id:
return True
return False
async def create_kernel(jp_fetch, kernel_name):
"""Issues request to retart the given kernel"""
with mocked_gateway:
body = json.dumps({"name": kernel_name})
# add a KERNEL_ value to the current env and we'll ensure that that value exists in the mocked method
os.environ["KERNEL_KSPEC_NAME"] = kernel_name
r = await jp_fetch("api", "kernels", method="POST", body=body)
assert r.code == 201
model = json.loads(r.body.decode("utf-8"))
kernel_id = model.get("id")
# ensure its in the running_kernels and name matches.
running_kernel = running_kernels.get(kernel_id)
assert running_kernel is not None
assert kernel_id == running_kernel.get("id")
assert model.get("name") == kernel_name
# restore env
os.environ.pop("KERNEL_KSPEC_NAME")
return kernel_id
async def interrupt_kernel(jp_fetch, kernel_id):
"""Issues request to interrupt the given kernel"""
with mocked_gateway:
r = await jp_fetch(
"api",
"kernels",
kernel_id,
"interrupt",
method="POST",
allow_nonstandard_methods=True,
)
assert r.code == 204
assert r.reason == "No Content"
async def restart_kernel(jp_fetch, kernel_id):
"""Issues request to retart the given kernel"""
with mocked_gateway:
r = await jp_fetch(
"api",
"kernels",
kernel_id,
"restart",
method="POST",
allow_nonstandard_methods=True,
)
assert r.code == 200
model = json.loads(r.body.decode("utf-8"))
restarted_kernel_id = model.get("id")
# ensure its in the running_kernels and name matches.
running_kernel = running_kernels.get(restarted_kernel_id)
assert running_kernel is not None
assert restarted_kernel_id == running_kernel.get("id")
assert model.get("name") == running_kernel.get("name")
async def delete_kernel(jp_fetch, kernel_id):
"""Deletes kernel corresponding to the given kernel id."""
with mocked_gateway:
# Delete the session (and kernel)
r = await jp_fetch("api", "kernels", kernel_id, method="DELETE")
assert r.code == 204
assert r.reason == "No Content"