Skip to content

Add the name of the running container to the environment #189

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 1 commit 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
21 changes: 21 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 @@ -155,6 +168,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 +181,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 +249,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
Loading