Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more gateway tests #1078

Merged
merged 2 commits into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jupyter_server/gateway/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class GatewayWebSocketClient(LoggingConfigurable):
"""Proxy web socket connection to a kernel/enterprise gateway."""

def __init__(self, **kwargs):
super().__init__(**kwargs)
super().__init__()
self.kernel_id = None
self.ws = None
self.ws_future: Future = Future()
Expand Down
20 changes: 18 additions & 2 deletions tests/test_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ async def mock_gateway_request(url, **kwargs):
else:
raise HTTPError(404, message="Kernelspec does not exist: %s" % requested_kernelspec)

# Fetch kernelspec asset
if endpoint.rfind("/kernelspecs/") >= 0 and method == "GET":
response_buf = BytesIO(b"foo")
response = await ensure_async(HTTPResponse(request, 200, buffer=response_buf))
return response

# Create kernel
if endpoint.endswith("/api/kernels") and method == "POST":
json_body = json.loads(kwargs["body"])
Expand Down Expand Up @@ -393,7 +399,7 @@ async def test_gateway_class_mappings(init_gateway, jp_serverapp):
assert jp_serverapp.kernel_spec_manager_class.__name__ == "GatewayKernelSpecManager"


async def test_gateway_get_kernelspecs(init_gateway, jp_fetch):
async def test_gateway_get_kernelspecs(init_gateway, jp_fetch, jp_serverapp):
# Validate that kernelspecs come from gateway.
with mocked_gateway:
r = await jp_fetch("api", "kernelspecs", method="GET")
Expand All @@ -412,6 +418,10 @@ async def test_gateway_get_named_kernelspec(init_gateway, jp_fetch):
kspec_foo = json.loads(r.body.decode("utf-8"))
assert kspec_foo.get("name") == "kspec_foo"

r = await jp_fetch("kernelspecs", "kspec_foo", "hi", method="GET")
assert r.code == 200
assert r.body == b"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)
Expand Down Expand Up @@ -443,7 +453,7 @@ async def test_gateway_session_lifecycle(init_gateway, jp_root_dir, jp_fetch):
assert await is_kernel_running(jp_fetch, kernel_id) is False


async def test_gateway_kernel_lifecycle(init_gateway, jp_fetch):
async def test_gateway_kernel_lifecycle(init_gateway, jp_serverapp, jp_ws_fetch, jp_fetch):
# Validate kernel lifecycle functions; create, interrupt, restart and delete.

# create
Expand All @@ -452,6 +462,12 @@ async def test_gateway_kernel_lifecycle(init_gateway, jp_fetch):
# ensure kernel still considered running
assert await is_kernel_running(jp_fetch, kernel_id) is True

ws = await jp_ws_fetch("api", "kernels", kernel_id, "channels")
ws.ping()
ws.write_message(b"hi")
ws.on_message(b"hi")
ws.close()

# interrupt
await interrupt_kernel(jp_fetch, kernel_id)

Expand Down