Skip to content

Commit

Permalink
Reverted function container
Browse files Browse the repository at this point in the history
container_context now uses container()
  • Loading branch information
ckunki committed Mar 22, 2024
1 parent cdc39de commit 5072c85
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 32 deletions.
19 changes: 16 additions & 3 deletions test/docker/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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],
Expand Down
39 changes: 17 additions & 22 deletions test/integration/test_create_dss_docker_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
pull as pull_docker_image,
)
from test.docker.container import (
container,
container_context,
DOCKER_SOCKET_CONTAINER,
sanitize_container_name,
Expand All @@ -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', }, },
Expand All @@ -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

Expand Down Expand Up @@ -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()
Expand All @@ -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")
Expand Down
13 changes: 6 additions & 7 deletions test/notebook_test_runner/test_notebooks_in_dss_docker_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 5072c85

Please sign in to comment.