Skip to content

Create missing rw volumes #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tox_docker/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ def __repr__(self) -> str:
return repr(str(self))


class ContainerVar:
def __init__(self, config_line: str) -> None:
if not ENV_VAR.match(config_line):
raise ValueError(f"{config_line!r} is not a valid environment variable")
self.container_var = config_line

def __str__(self) -> str:
return self.container_var

def __repr__(self) -> str:
return repr(str(self))


class Link:
def __init__(self, config_line: str) -> None:
target, sep, alias = config_line.partition(":")
Expand Down Expand Up @@ -128,6 +141,8 @@ def __init__(self, config_line: str) -> None:
volume_type, mode, outside, inside = parts
if not os.path.isabs(outside):
raise ValueError(f"Volume source {outside!r} must be an absolute path")
elif not os.path.exists(outside) and parts[1] == "rw":
os.makedirs(outside, exist_ok=True)
if not os.path.isabs(inside):
raise ValueError(f"Mount point {inside!r} must be an absolute path")

Expand Down Expand Up @@ -155,6 +170,7 @@ def __init__(
healthcheck_retries: Optional[int] = None,
expose: Optional[Collection[ExposedPort]] = None,
host_var: Optional[HostVar] = None,
container_var: Optional[ContainerVar] = None,
links: Optional[Collection[Link]] = None,
volumes: Optional[Collection[Volume]] = None,
) -> None:
Expand All @@ -167,6 +183,7 @@ def __init__(
self.environment: Mapping[str, str] = environment or {}
self.expose: Collection[ExposedPort] = expose or []
self.host_var = str(host_var) if host_var else ""
self.container_var = str(container_var) if container_var else ""
self.links: Collection[Link] = links or []
self.mounts: Collection[Mount] = [v.docker_mount for v in volumes or ()]

Expand Down Expand Up @@ -234,6 +251,12 @@ def register_config(self) -> None:
default=None,
desc="environment variable to pass hostname or IP of container to testenv",
)
self.add_config(
keys=["container_var"],
of_type=Optional[ContainerVar],
default=None,
desc="environment variable to pass the name of the container to testenv",
)
self.add_config(
keys=["links"],
of_type=List[Link],
Expand Down
10 changes: 9 additions & 1 deletion tox_docker/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ def get_host_env_var(container_config: ContainerConfig) -> str:
return escape_env_var(f"{container_config.name}_HOST")


def get_container_env_var(container_config: ContainerConfig) -> str:
if container_config.container_var:
return container_config.container_var

return escape_env_var(f"{container_config.name}_CONTAINER")


def get_env_vars(
container_config: ContainerConfig, container: Container
) -> Mapping[str, str]:
Expand All @@ -102,7 +109,8 @@ def get_env_vars(
gateway_ip = get_gateway_ip(container)
env_var = get_host_env_var(container_config)
env[env_var] = gateway_ip

env_var = get_container_env_var(container_config)
env[env_var] = container_config.runas_name
return env


Expand Down
15 changes: 15 additions & 0 deletions tox_docker/tests/test_volumes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import os
import tempfile

from tox_docker.config import Volume


def test_the_image_is_healthy() -> None:
Expand All @@ -7,3 +10,15 @@ def test_the_image_is_healthy() -> None:
# and thus the bind mount worked as expected
volume = os.environ["VOLUME_DIR"]
assert "healthy" in os.listdir(volume)


def test_volume_creation() -> None:
with tempfile.NamedTemporaryFile() as f:
source = f"{f.name}/test"
assert not os.path.exists(source)
volume = Volume(f"bind:ro:{source}:/tmp/test")
assert volume.docker_mount.source == source
assert volume.docker_mount.target == "/tmp/test"
assert volume.docker_mount.type == "bind"
assert volume.docker_mount.readonly is True
assert os.path.isfile(source)
Loading