|
| 1 | +# sourced from: |
| 2 | +# https://github.com/jupyter/docker-stacks |
| 3 | +# |
| 4 | +# Copyright (c) Jupyter Development Team. |
| 5 | +# Distributed under the terms of the Modified BSD License. |
| 6 | +# |
| 7 | +import os |
| 8 | +import logging |
| 9 | + |
| 10 | +import docker |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +import requests |
| 15 | + |
| 16 | +from requests.packages.urllib3.util.retry import Retry |
| 17 | +from requests.adapters import HTTPAdapter |
| 18 | + |
| 19 | +from typing import Container |
| 20 | + |
| 21 | + |
| 22 | +LOGGER = logging.getLogger(__name__) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(scope="session") |
| 26 | +def http_client() -> requests.Session: |
| 27 | + """Requests session with retries and backoff.""" |
| 28 | + s = requests.Session() |
| 29 | + retries = Retry(total=5, backoff_factor=1) |
| 30 | + s.mount("http://", HTTPAdapter(max_retries=retries)) |
| 31 | + s.mount("https://", HTTPAdapter(max_retries=retries)) |
| 32 | + return s |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture(scope="session") |
| 36 | +def docker_client() -> docker.client: |
| 37 | + """Docker client configured based on the host environment""" |
| 38 | + return docker.from_env() |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture(scope="session") |
| 42 | +def image_name() -> str: |
| 43 | + """Image name to test""" |
| 44 | + return os.getenv("TEST_IMAGE", "illumidesk/base-notebook") |
| 45 | + |
| 46 | + |
| 47 | +class TrackedContainer: |
| 48 | + """Wrapper that collects docker container configuration and delays |
| 49 | + container creation/execution. |
| 50 | +
|
| 51 | + Parameters |
| 52 | + ---------- |
| 53 | + docker_client: docker.DockerClient |
| 54 | + Docker client instance |
| 55 | + image_name: str |
| 56 | + Name of the docker image to launch |
| 57 | + **kwargs: dict, optional |
| 58 | + Default keyword arguments to pass to docker.DockerClient.containers.run |
| 59 | + """ |
| 60 | + |
| 61 | + def __init__( |
| 62 | + self, docker_client: docker.client, image_name: str, **kwargs: str |
| 63 | + ) -> None: |
| 64 | + self.container = None |
| 65 | + self.docker_client = docker_client |
| 66 | + self.image_name = image_name |
| 67 | + self.kwargs = kwargs |
| 68 | + |
| 69 | + def run(self, **kwargs: str) -> Container: |
| 70 | + """Runs a docker container using the preconfigured image name |
| 71 | + and a mix of the preconfigured container options and those passed |
| 72 | + to this method. |
| 73 | +
|
| 74 | + Keeps track of the docker.Container instance spawned to kill it |
| 75 | + later. |
| 76 | +
|
| 77 | + Parameters |
| 78 | + ---------- |
| 79 | + **kwargs: dict, optional |
| 80 | + Keyword arguments to pass to docker.DockerClient.containers.run |
| 81 | + extending and/or overriding key/value pairs passed to the constructor |
| 82 | +
|
| 83 | + Returns |
| 84 | + ------- |
| 85 | + docker.Container |
| 86 | + """ |
| 87 | + all_kwargs = {} |
| 88 | + all_kwargs.update(self.kwargs) |
| 89 | + all_kwargs.update(kwargs) |
| 90 | + LOGGER.info(f"Running {self.image_name} with args {all_kwargs} ...") |
| 91 | + self.container = self.docker_client.containers.run( |
| 92 | + self.image_name, **all_kwargs |
| 93 | + ) |
| 94 | + return self.container |
| 95 | + |
| 96 | + def remove(self) -> None: |
| 97 | + """Kills and removes the tracked docker container.""" |
| 98 | + if self.container: |
| 99 | + self.container.remove(force=True) |
| 100 | + |
| 101 | + |
| 102 | +@pytest.fixture(scope="function") |
| 103 | +def container(docker_client, image_name) -> None: |
| 104 | + """Notebook container with initial configuration appropriate for testing |
| 105 | + (e.g., HTTP port exposed to the host for HTTP calls). |
| 106 | +
|
| 107 | + Yields the container instance and kills it when the caller is done with it. |
| 108 | + """ |
| 109 | + container = TrackedContainer( |
| 110 | + docker_client, image_name, detach=True, ports={"8888/tcp": 8888} |
| 111 | + ) |
| 112 | + yield container |
0 commit comments