Files
docker-stacks/tests/base-notebook/test_healthcheck.py
Muhammad Aji Muharrom bcd323f794 Healthcheck url from json (#1868)
* Get healthcheck URL from JSON file

Obtain Jupyter server's full URL from
/home/$NB_USER/.local/share/jupyter/runtime/*.json
and use it for healthcheck.

* Expand tests for healthcheck

* Update pre-commit config

* Remove workdir from tests

* Wait for container start with 0.1s interval

* Quote NB_USER variable on base-notebook/Dockerfile

* Fix style and make test actually work

* Ooops, GitHub didn't show last line

* Fix mypy

* Use bash with pipefail option for healthcheck

* Create python script for healthcheck

* Don't verify SSL certificate on healthcheck

* Update healthcheck test

- Add "user" parameter
- Add tests when container should not be healthy

* Fix typo

* Update test_healthcheck.py

---------

Co-authored-by: Ayaz Salikhov <mathbunnyru@users.noreply.github.com>
2023-02-28 14:07:52 +04:00

113 lines
3.3 KiB
Python

# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import logging
import time
from typing import Optional
import pytest # type: ignore
from tests.conftest import TrackedContainer, get_health
LOGGER = logging.getLogger(__name__)
@pytest.mark.parametrize(
"env,cmd,user",
[
(None, None, None),
(["DOCKER_STACKS_JUPYTER_CMD=lab"], None, None),
(["DOCKER_STACKS_JUPYTER_CMD=notebook"], None, None),
(["DOCKER_STACKS_JUPYTER_CMD=server"], None, None),
(["DOCKER_STACKS_JUPYTER_CMD=nbclassic"], None, None),
(["RESTARTABLE=yes"], None, None),
(["JUPYTER_PORT=8171"], None, None),
(["JUPYTER_PORT=8117", "DOCKER_STACKS_JUPYTER_CMD=notebook"], None, None),
(None, ["start-notebook.sh", "--NotebookApp.base_url=/test"], None),
(None, ["start-notebook.sh", "--NotebookApp.base_url=/test/"], None),
(["GEN_CERT=1"], ["start-notebook.sh", "--NotebookApp.base_url=/test"], None),
(
["GEN_CERT=1", "JUPYTER_PORT=7891"],
["start-notebook.sh", "--NotebookApp.base_url=/test"],
None,
),
(["NB_USER=testuser", "CHOWN_HOME=1"], None, "root"),
(
["NB_USER=testuser", "CHOWN_HOME=1"],
["start-notebook.sh", "--NotebookApp.base_url=/test"],
"root",
),
(
["NB_USER=testuser", "CHOWN_HOME=1", "JUPYTER_PORT=8123"],
["start-notebook.sh", "--NotebookApp.base_url=/test"],
"root",
),
],
)
def test_health(
container: TrackedContainer,
env: Optional[list[str]],
cmd: Optional[list[str]],
user: Optional[str],
) -> None:
running_container = container.run_detached(
tty=True,
environment=env,
command=cmd,
user=user,
)
# sleeping some time to let the server start
time_spent = 0.0
wait_time = 0.1
time_limit = 15
while time_spent < time_limit:
time.sleep(wait_time)
time_spent += wait_time
if get_health(running_container) == "healthy":
return
assert get_health(running_container) == "healthy"
@pytest.mark.parametrize(
"env,cmd,user",
[
(["NB_USER=testuser", "CHOWN_HOME=1"], None, None),
(
["NB_USER=testuser", "CHOWN_HOME=1"],
["start-notebook.sh", "--NotebookApp.base_url=/test"],
None,
),
(
["NB_USER=testuser", "CHOWN_HOME=1", "JUPYTER_PORT=8123"],
["start-notebook.sh", "--NotebookApp.base_url=/test"],
None,
),
],
)
def test_not_healthy(
container: TrackedContainer,
env: Optional[list[str]],
cmd: Optional[list[str]],
user: Optional[str],
) -> None:
running_container = container.run_detached(
tty=True,
environment=env,
command=cmd,
user=user,
)
# sleeping some time to let the server start
time_spent = 0.0
wait_time = 0.1
time_limit = 15
while time_spent < time_limit:
time.sleep(wait_time)
time_spent += wait_time
if get_health(running_container) == "healthy":
raise RuntimeError("Container should not be healthy for these testcases.")
assert get_health(running_container) != "healthy"