Files
docker-stacks/base-notebook/test/test_start_container.py
Ayaz Salikhov 39ae7d2202 Fix typing
2022-01-19 23:37:45 +02:00

84 lines
2.7 KiB
Python

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import logging
from typing import Optional
import pytest
import requests
import re
from conftest import TrackedContainer
LOGGER = logging.getLogger(__name__)
@pytest.mark.parametrize(
"env,expected_server,expected_warnings",
[
(
["JUPYTER_ENABLE_LAB=yes"],
"lab",
["WARNING: Jupyter Notebook deprecation notice"],
),
(None, "lab", []),
(["JUPYTER_CMD=lab"], "lab", []),
(["JUPYTER_CMD=notebook"], "notebook", []),
(["JUPYTER_CMD=server"], "server", []),
(["JUPYTER_CMD=nbclassic"], "nbclassic", []),
],
)
def test_start_notebook(
container: TrackedContainer,
http_client: requests.Session,
env: Optional[list[str]],
expected_server: str,
expected_warnings: list[str],
) -> None:
"""Test the notebook start-notebook script"""
LOGGER.info(
f"Test that the start-notebook launches the {expected_server} server from the env {env} ..."
)
c = container.run(
tty=True,
environment=env,
command=["start-notebook.sh"],
)
resp = http_client.get("http://localhost:8888")
# checking errors and warnings in logs
logs = c.logs(stdout=True).decode("utf-8")
LOGGER.debug(logs)
assert "ERROR" not in logs, "ERROR(s) found in logs"
for exp_warning in expected_warnings:
assert exp_warning in logs, f"Expected warning {exp_warning} not found in logs"
warnings = re.findall(r"^WARNING", logs, flags=re.MULTILINE)
assert len(expected_warnings) == len(
warnings
), "Not found the number of expected warnings in logs"
# checking if the server is listening
assert resp.status_code == 200, "Server is not listening"
assert (
f"Executing the command: jupyter {expected_server}" in logs
), f"Not the expected command (jupyter {expected_server}) was launched"
def test_tini_entrypoint(
container: TrackedContainer, pid: int = 1, command: str = "tini"
) -> None:
"""Check that tini is launched as PID 1
Credits to the following answer for the ps options used in the test:
https://superuser.com/questions/632979/if-i-know-the-pid-number-of-a-process-how-can-i-get-its-name
"""
LOGGER.info(f"Test that {command} is launched as PID {pid} ...")
c = container.run(
tty=True,
command=["start.sh"],
)
# Select the PID 1 and get the corresponding command
cmd = c.exec_run(f"ps -p {pid} -o comm=")
output = cmd.output.decode("utf-8").strip("\n")
assert "ERROR" not in output
assert "WARNING" not in output
assert output == command, f"{command} shall be launched as pid {pid}, got {output}"