|
2 | 2 | # Distributed under the terms of the Modified BSD License. |
3 | 3 |
|
4 | 4 | import logging |
| 5 | +from typing import Optional |
5 | 6 | import pytest |
6 | 7 | import requests |
| 8 | +import re |
| 9 | +import time |
7 | 10 |
|
8 | 11 | from conftest import TrackedContainer |
9 | 12 |
|
10 | 13 | LOGGER = logging.getLogger(__name__) |
11 | 14 |
|
12 | 15 |
|
13 | 16 | @pytest.mark.parametrize( |
14 | | - "env,expected_server", |
| 17 | + "env,expected_command,expected_start,expected_warnings", |
15 | 18 | [ |
16 | | - (["JUPYTER_ENABLE_LAB=yes"], "lab"), |
17 | | - (None, "notebook"), |
| 19 | + ( |
| 20 | + ["JUPYTER_ENABLE_LAB=yes"], |
| 21 | + "jupyter lab", |
| 22 | + True, |
| 23 | + ["WARNING: JUPYTER_ENABLE_LAB is ignored"], |
| 24 | + ), |
| 25 | + (None, "jupyter lab", True, []), |
| 26 | + (["DOCKER_STACKS_JUPYTER_CMD=lab"], "jupyter lab", True, []), |
| 27 | + (["RESTARTABLE=yes"], "run-one-constantly jupyter lab", True, []), |
| 28 | + (["DOCKER_STACKS_JUPYTER_CMD=notebook"], "jupyter notebook", True, []), |
| 29 | + (["DOCKER_STACKS_JUPYTER_CMD=server"], "jupyter server", True, []), |
| 30 | + (["DOCKER_STACKS_JUPYTER_CMD=nbclassic"], "jupyter nbclassic", True, []), |
| 31 | + ( |
| 32 | + ["JUPYTERHUB_API_TOKEN=my_token"], |
| 33 | + "jupyterhub-singleuser", |
| 34 | + False, |
| 35 | + ["WARNING: using start-singleuser.sh"], |
| 36 | + ), |
18 | 37 | ], |
19 | 38 | ) |
20 | 39 | def test_start_notebook( |
21 | 40 | container: TrackedContainer, |
22 | 41 | http_client: requests.Session, |
23 | | - env, |
24 | | - expected_server: str, |
| 42 | + env: Optional[list[str]], |
| 43 | + expected_command: str, |
| 44 | + expected_start: bool, |
| 45 | + expected_warnings: list[str], |
25 | 46 | ) -> None: |
26 | 47 | """Test the notebook start-notebook script""" |
27 | 48 | LOGGER.info( |
28 | | - f"Test that the start-notebook launches the {expected_server} server from the env {env} ..." |
| 49 | + f"Test that the start-notebook launches the {expected_command} server from the env {env} ..." |
29 | 50 | ) |
30 | 51 | c = container.run( |
31 | 52 | tty=True, |
32 | 53 | environment=env, |
33 | 54 | command=["start-notebook.sh"], |
34 | 55 | ) |
35 | | - resp = http_client.get("http://localhost:8888") |
| 56 | + # sleeping some time to let the server start |
| 57 | + time.sleep(3) |
36 | 58 | logs = c.logs(stdout=True).decode("utf-8") |
37 | 59 | LOGGER.debug(logs) |
38 | | - assert "ERROR" not in logs |
39 | | - if expected_server != "notebook": |
40 | | - assert "WARNING" not in logs |
41 | | - else: |
42 | | - warnings = [ |
43 | | - warning for warning in logs.split("\n") if warning.startswith("WARNING") |
44 | | - ] |
45 | | - assert len(warnings) == 1 |
46 | | - assert warnings[0].startswith("WARNING: Jupyter Notebook deprecation notice") |
47 | | - assert resp.status_code == 200, "Server is not listening" |
| 60 | + # checking that the expected command is launched |
48 | 61 | assert ( |
49 | | - f"Executing the command: jupyter {expected_server}" in logs |
50 | | - ), f"Not the expected command (jupyter {expected_server}) was launched" |
51 | | - # Checking warning messages |
52 | | - if not env: |
53 | | - msg = "WARNING: Jupyter Notebook deprecation notice" |
54 | | - assert msg in logs, f"Expected warning message {msg} not printed" |
| 62 | + f"Executing the command: {expected_command}" in logs |
| 63 | + ), f"Not the expected command ({expected_command}) was launched" |
| 64 | + # checking errors and warnings in logs |
| 65 | + assert "ERROR" not in logs, "ERROR(s) found in logs" |
| 66 | + for exp_warning in expected_warnings: |
| 67 | + assert exp_warning in logs, f"Expected warning {exp_warning} not found in logs" |
| 68 | + warnings = re.findall(r"^WARNING", logs, flags=re.MULTILINE) |
| 69 | + assert len(expected_warnings) == len( |
| 70 | + warnings |
| 71 | + ), "Not found the number of expected warnings in logs" |
| 72 | + # checking if the server is listening |
| 73 | + if expected_start: |
| 74 | + resp = http_client.get("http://localhost:8888") |
| 75 | + assert resp.status_code == 200, "Server is not listening" |
55 | 76 |
|
56 | 77 |
|
57 | 78 | def test_tini_entrypoint( |
|
0 commit comments