예제 #1
0
def test_secured_server(container: TrackedContainer,
                        http_client: requests.Session) -> None:
    """Notebook server should eventually request user login."""
    host_port = find_free_port()
    container.run_detached(ports={"8888/tcp": host_port})
    resp = http_client.get(f"http://localhost:{host_port}")
    resp.raise_for_status()
    assert "login_submit" in resp.text, "User login not requested"
def test_matplotlib(container: TrackedContainer, test_file: str,
                    expected_file: str, description: str) -> None:
    """Various tests performed on matplotlib

    - Test that matplotlib is able to plot a graph and write it as an image
    - Test matplotlib latex fonts, which depend on the cm-super package
    """
    host_data_dir = THIS_DIR / "data/matplotlib"
    cont_data_dir = "/home/jovyan/data"
    output_dir = "/tmp"
    LOGGER.info(description)
    command = "sleep infinity"
    running_container = container.run_detached(
        volumes={str(host_data_dir): {
                     "bind": cont_data_dir,
                     "mode": "ro"
                 }},
        tty=True,
        command=["start.sh", "bash", "-c", command],
    )
    command = f"python {cont_data_dir}/{test_file}"
    cmd = running_container.exec_run(command)
    LOGGER.debug(cmd.output.decode("utf-8"))
    assert cmd.exit_code == 0, f"Command {command} failed"
    # Checking if the file is generated
    # https://stackoverflow.com/a/15895594/4413446
    command = f"test -s {output_dir}/{expected_file}"
    cmd = running_container.exec_run(command)
    LOGGER.debug(cmd.output.decode("utf-8"))
    assert cmd.exit_code == 0, f"Command {command} failed"
예제 #3
0
 def start_container(container: TrackedContainer) -> Container:
     """Start the TrackedContainer and return an instance of a running container"""
     LOGGER.info(f"Starting container {container.image_name} ...")
     return container.run_detached(
         tty=True,
         command=["start.sh", "bash", "-c", "sleep infinity"],
     )
def test_julia(container: TrackedContainer) -> None:
    """Basic julia test"""
    LOGGER.info("Test that julia is correctly installed ...")
    running_container = container.run_detached(
        tty=True,
        command=["start.sh", "bash", "-c", "sleep infinity"],
    )
    command = "julia --version"
    cmd = running_container.exec_run(command)
    output = cmd.output.decode("utf-8")
    LOGGER.debug(output)
    assert cmd.exit_code == 0, f"Command {command} failed {output}"
def test_nb_user_change(container: TrackedContainer) -> None:
    """Container should change the user name (`NB_USER`) of the default user."""
    nb_user = "******"
    running_container = container.run_detached(
        tty=True,
        user="******",
        environment=[f"NB_USER={nb_user}", "CHOWN_HOME=yes"],
        command=["start.sh", "bash", "-c", "sleep infinity"],
    )

    # Give the chown time to complete. Use sleep, not wait, because the
    # container sleeps forever.
    time.sleep(10)
    LOGGER.info(
        f"Checking if the user is changed to {nb_user} by the start script ..."
    )
    output = running_container.logs().decode("utf-8")
    assert "ERROR" not in output
    assert "WARNING" not in output
    assert (f"username: jovyan       -> {nb_user}"
            in output), f"User is not changed to {nb_user}"

    LOGGER.info(f"Checking {nb_user} id ...")
    command = "id"
    expected_output = f"uid=1000({nb_user}) gid=100(users) groups=100(users)"
    cmd = running_container.exec_run(command,
                                     user=nb_user,
                                     workdir=f"/home/{nb_user}")
    output = cmd.output.decode("utf-8").strip("\n")
    assert output == expected_output, f"Bad user {output}, expected {expected_output}"

    LOGGER.info(f"Checking if {nb_user} owns his home folder ...")
    command = f'stat -c "%U %G" /home/{nb_user}/'
    expected_output = f"{nb_user} users"
    cmd = running_container.exec_run(command, workdir=f"/home/{nb_user}")
    output = cmd.output.decode("utf-8").strip("\n")
    assert (
        output == expected_output
    ), f"Bad owner for the {nb_user} home folder {output}, expected {expected_output}"

    LOGGER.info(
        f"Checking if home folder of {nb_user} contains the hidden '.jupyter' folder with appropriate permissions ..."
    )
    command = f'stat -c "%F %U %G" /home/{nb_user}/.jupyter'
    expected_output = f"directory {nb_user} users"
    cmd = running_container.exec_run(command, workdir=f"/home/{nb_user}")
    output = cmd.output.decode("utf-8").strip("\n")
    assert (
        output == expected_output
    ), f"Hidden folder .jupyter was not copied properly to {nb_user} home folder. stat: {output}, expected {expected_output}"
def test_cli_args(container: TrackedContainer, http_client: requests.Session) -> None:
    """Container should respect notebook server command line args
    (e.g., disabling token security)"""
    host_port = find_free_port()
    running_container = container.run_detached(
        command=["start-notebook.sh", "--NotebookApp.token=''"],
        ports={"8888/tcp": host_port},
    )
    resp = http_client.get(f"http://localhost:{host_port}")
    resp.raise_for_status()
    logs = running_container.logs().decode("utf-8")
    LOGGER.debug(logs)
    assert "ERROR" not in logs
    warnings = TrackedContainer.get_warnings(logs)
    assert not warnings
    assert "login_submit" not in resp.text
예제 #7
0
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} ...")
    running_container = container.run_detached(
        tty=True,
        command=["start.sh"],
    )
    # Select the PID 1 and get the corresponding command
    cmd = running_container.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}"
def test_unsigned_ssl(container: TrackedContainer,
                      http_client: requests.Session) -> None:
    """Container should generate a self-signed SSL certificate
    and notebook server should use it to enable HTTPS.
    """
    host_port = find_free_port()
    running_container = container.run_detached(
        environment=["GEN_CERT=yes"],
        ports={"8888/tcp": host_port},
    )
    # NOTE: The requests.Session backing the http_client fixture does not retry
    # properly while the server is booting up. An SSL handshake error seems to
    # abort the retry logic. Forcing a long sleep for the moment until I have
    # time to dig more.
    time.sleep(5)
    resp = http_client.get(f"https://localhost:{host_port}", verify=False)
    resp.raise_for_status()
    assert "login_submit" in resp.text
    logs = running_container.logs().decode("utf-8")
    assert "ERROR" not in logs
    warnings = TrackedContainer.get_warnings(logs)
    assert not warnings
예제 #9
0
def test_start_notebook(
    container: TrackedContainer,
    http_client: requests.Session,
    env: Optional[list[str]],
    expected_command: str,
    expected_start: bool,
    expected_warnings: list[str],
) -> None:
    """Test the notebook start-notebook script"""
    LOGGER.info(
        f"Test that the start-notebook launches the {expected_command} server from the env {env} ..."
    )
    host_port = find_free_port()
    running_container = container.run_detached(
        tty=True,
        environment=env,
        command=["start-notebook.sh"],
        ports={"8888/tcp": host_port},
    )
    # sleeping some time to let the server start
    time.sleep(3)
    logs = running_container.logs().decode("utf-8")
    LOGGER.debug(logs)
    # checking that the expected command is launched
    assert (
        f"Executing the command: {expected_command}"
        in logs), f"Not the expected command ({expected_command}) was launched"
    # checking errors and warnings in 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 = TrackedContainer.get_warnings(logs)
    assert len(expected_warnings) == len(warnings)
    # checking if the server is listening
    if expected_start:
        resp = http_client.get(f"http://localhost:{host_port}")
        assert resp.status_code == 200, "Server is not listening"