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 serverapp tests #1079

Merged
merged 14 commits into from
Nov 21, 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
12 changes: 4 additions & 8 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ jobs:
uses: actions/checkout@v3
- name: Base Setup
uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
- name: Install the Python dependencies
run: |
pip install -e ".[test]"
pip install pytest-github-actions-annotate-failures
- name: List installed packages
run: |
pip freeze
pip check
- name: Run the tests
run: |
hatch run cov:integration
- name: Coverage
run: |
pip install codecov
codecov

integration_check: # This job does nothing and is only used for the branch protection
if: always()
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ exclude_lines = [
"def __repr__",
"if self.debug:",
"if settings.DEBUG",
"if TYPE_CHECKING:",
"raise AssertionError",
"raise NotImplementedError",
"if 0:",
Expand Down
38 changes: 37 additions & 1 deletion tests/test_serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import pathlib
import warnings
from unittest.mock import patch

import pytest
Expand All @@ -10,7 +11,13 @@
from traitlets.tests.utils import check_help_all_output

from jupyter_server.auth.security import passwd_check
from jupyter_server.serverapp import JupyterPasswordApp, ServerApp, list_running_servers
from jupyter_server.serverapp import (
JupyterPasswordApp,
ServerApp,
ServerWebApplication,
list_running_servers,
random_ports,
)


def test_help_output():
Expand Down Expand Up @@ -401,3 +408,32 @@ def test_observed_root_dir_does_not_update_preferred_dir(tmp_path, jp_configurab
app = jp_configurable_serverapp(root_dir=path, preferred_dir=path)
app.root_dir = new_path
assert app.preferred_dir == path


def test_random_ports():
ports = list(random_ports(500, 50))
assert len(ports) == 50


def test_server_web_application(jp_serverapp):
server: ServerApp = jp_serverapp
server.default_url = "/foo"
with warnings.catch_warnings():
warnings.simplefilter("ignore")
app = ServerWebApplication(
server,
[],
server.kernel_manager,
server.contents_manager,
server.session_manager,
server.kernel_manager,
server.config_manager,
server.event_logger,
["jupyter_server.gateway.handlers"],
server.log,
server.base_url,
server.default_url,
{},
{},
)
app.init_handlers([], app.settings)
56 changes: 54 additions & 2 deletions tests/unix_sockets/test_serverapp_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

import pytest

from jupyter_server.serverapp import list_running_servers, shutdown_server
from jupyter_server.serverapp import (
JupyterServerListApp,
JupyterServerStopApp,
list_running_servers,
shutdown_server,
)
from jupyter_server.utils import urlencode_unix_socket, urlencode_unix_socket_path

# Skip this module if on Windows. Unix sockets are not available on Windows.
Expand Down Expand Up @@ -58,6 +63,13 @@ def test_shutdown_sock_server_integration(jp_unix_socket_file):

assert encoded_sock_path.encode() in subprocess.check_output(["jupyter-server", "list"])

# Fake out stopping the server.
app = JupyterServerStopApp(sock=str(jp_unix_socket_file))
app.initialize([])
app.shutdown_server = lambda _: True # type:ignore
app._maybe_remove_unix_socket = lambda _: _ # type: ignore
app.start()

subprocess.check_output(["jupyter-server", "stop", jp_unix_socket_file])

assert encoded_sock_path.encode() not in subprocess.check_output(["jupyter-server", "list"])
Expand Down Expand Up @@ -195,4 +207,44 @@ def test_shutdown_server(jp_environ):
break
except ConnectionRefusedError:
time.sleep(0.1)
p.wait()
_cleanup_process(p)


@pytest.mark.integration_test
def test_jupyter_server_apps(jp_environ):

# Start a server in another process
# Stop that server
import subprocess

from jupyter_client.connect import LocalPortCache

port = LocalPortCache().find_available_port("localhost")
p = subprocess.Popen(["jupyter-server", f"--port={port}"])
servers = []
while 1:
servers = list(list_running_servers())
if len(servers):
break
time.sleep(0.1)

app = JupyterServerListApp()
app.initialize([])
app.jsonlist = True
app.start()
app.jsonlist = False
app.json = True
app.start()
app.json = False
app.start()

app = JupyterServerStopApp()
app.initialize([])
app.port = port
while 1:
try:
app.start()
break
except ConnectionRefusedError:
time.sleep(0.1)
_cleanup_process(p)