diff --git a/test/docker/container.py b/test/docker/container.py index dc3dc6e3..f18e7b1e 100644 --- a/test/docker/container.py +++ b/test/docker/container.py @@ -23,12 +23,20 @@ def timestamp() -> str: return f'{datetime.now().timestamp():.0f}' -@contextmanager -def container_context(image_name: str, suffix: str = None, start: bool = True, **kwargs): +def container( + request, + image: Union[Image, str], + suffix: str = None, + start: bool = True, + **kwargs, +) -> Generator[Container, None, None]: """ Create a Docker container based on the specified Docker image. """ - container_name = sanitize_container_name(f"{image_name}_{suffix or timestamp()}") + if suffix is not None: + suffix = f"_{suffix}" + image_name = image.id if hasattr(image, "id") else image + container_name = sanitize_container_name(f"{image_name}_{request.node.name}{suffix}") client = docker.from_env() try: container = client.containers.create( @@ -45,6 +53,11 @@ def container_context(image_name: str, suffix: str = None, start: bool = True, * client.close() +@contextmanager +def container_context(request, image_name: str, **kwargs): + yield from container(request, image_name, **kwargs) + + def wait_for( container: Container, log_message: Union[str, Pattern], diff --git a/test/integration/test_create_dss_docker_image.py b/test/integration/test_create_dss_docker_image.py index 3cecd382..35b88f20 100644 --- a/test/integration/test_create_dss_docker_image.py +++ b/test/integration/test_create_dss_docker_image.py @@ -30,6 +30,7 @@ pull as pull_docker_image, ) from test.docker.container import ( + container, container_context, DOCKER_SOCKET_CONTAINER, sanitize_container_name, @@ -41,28 +42,26 @@ DOCKER_SOCKET_HOST = "/var/run/docker.sock" _logger = logging.getLogger(__name__) -_logger.setLevel(logging.DEBUG) @pytest.fixture -def dss_docker_container(dss_docker_image, jupyter_port): +def dss_docker_container(request, dss_docker_image, jupyter_port): mapped_ports = { f'{jupyter_port}/tcp': jupyter_port } - with container_context( - image_name=dss_docker_image.image_name, - ports=mapped_ports, - volumes={DOCKER_SOCKET_HOST: { - 'bind': DOCKER_SOCKET_CONTAINER, - 'mode': 'rw', }, }, - ) as container: - yield container - + yield from container( + request, + image=dss_docker_image.image_name, + ports=mapped_ports, + volumes={DOCKER_SOCKET_HOST: { + 'bind': DOCKER_SOCKET_CONTAINER, + 'mode': 'rw', }, }, + ) @pytest.fixture def dss_container_context(request, dss_docker_image): def context(docker_socket_host: Path): return container_context( + request, image_name=dss_docker_image.image_name, - suffix=request.node.name, volumes={ docker_socket_host: { 'bind': DOCKER_SOCKET_CONTAINER, 'mode': 'rw', }, }, @@ -77,12 +76,12 @@ def ubuntu_container_context(request, docker_auth): pull_docker_image(spec, docker_auth) def context(path_on_host: Path, path_in_container: str): return container_context( - image_name=spec.name, - suffix=request.node.name, - command="sleep infinity", - volumes={ path_on_host: { - 'bind': path_in_container, - 'mode': 'rw', }, }, + request, + image_name=spec.name, + command="sleep infinity", + volumes={ path_on_host: { + 'bind': path_in_container, + 'mode': 'rw', }, }, ) return context @@ -264,9 +263,7 @@ def group_changer(ubuntu_container_context): def test_write_socket_known_gid(socket_inspector, group_changer): with socket_inspector as inspector: gid = inspector.get_gid("ubuntu") - group_changer.chgrp(gid, socket_inspector.socket_on_host) - with socket_inspector as inspector: inspector.assert_jupyter_member_of("ubuntu") inspector.assert_write_to_socket() @@ -275,9 +272,7 @@ def test_write_socket_known_gid(socket_inspector, group_changer): def test_write_socket_unknown_gid(socket_inspector, group_changer): with socket_inspector as inspector: gid = inspector.get_unassigned_gid() - group_changer.chgrp(gid, socket_inspector.socket_on_host) - with socket_inspector as inspector: assert gid == inspector.get_gid("docker") inspector.assert_jupyter_member_of("docker") diff --git a/test/notebook_test_runner/test_notebooks_in_dss_docker_image.py b/test/notebook_test_runner/test_notebooks_in_dss_docker_image.py index faf947bb..4a9f1f0c 100644 --- a/test/notebook_test_runner/test_notebooks_in_dss_docker_image.py +++ b/test/notebook_test_runner/test_notebooks_in_dss_docker_image.py @@ -11,7 +11,7 @@ from test.docker.image import image from test.docker.in_memory_build_context import InMemoryBuildContext from test.docker.container import ( - container_context, + container, wait_for_socket_access, wait_for, ) @@ -55,14 +55,13 @@ def notebook_test_image(request, notebook_test_build_context): @pytest.fixture() def notebook_test_container(request, notebook_test_image): _logger.debug(f'Starting container context for docker image {notebook_test_image.id}') - with container_context( - image_name=notebook_test_image.id, - suffix=request.node.name, - volumes={'/var/run/docker.sock': { + yield from container( + request, + notebook_test_image, + volumes={'/var/run/docker.sock': { 'bind': '/var/run/docker.sock', 'mode': 'rw', }, }, - ) as container: - yield container + ) @pytest.fixture()