From e6ed27fc29dabebf42eb3c644e018f96a7290254 Mon Sep 17 00:00:00 2001 From: Roy Moore Date: Thu, 15 Aug 2024 17:18:14 +0000 Subject: [PATCH 1/3] feat(main): Add support to working with env files --- core/testcontainers/core/container.py | 10 +++++++++- core/tests/test_core.py | 26 ++++++++++++++++++++++++++ poetry.lock | 2 +- pyproject.toml | 1 + 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/core/testcontainers/core/container.py b/core/testcontainers/core/container.py index e9415441..d664bb57 100644 --- a/core/testcontainers/core/container.py +++ b/core/testcontainers/core/container.py @@ -1,11 +1,13 @@ import contextlib +from os import PathLike from platform import system from socket import socket -from typing import TYPE_CHECKING, Optional +from typing import TYPE_CHECKING, Optional, Union import docker.errors from docker import version from docker.types import EndpointConfig +from dotenv import dotenv_values from typing_extensions import Self from testcontainers.core.config import testcontainers_config as c @@ -57,6 +59,12 @@ def with_env(self, key: str, value: str) -> Self: self.env[key] = value return self + def with_env_file(self, env_file: Union[str, PathLike]) -> Self: + env_values = dotenv_values(env_file) + for key, value in env_values.items(): + self.with_env(key, value) + return self + def with_bind_ports(self, container: int, host: Optional[int] = None) -> Self: self.ports[container] = host return self diff --git a/core/tests/test_core.py b/core/tests/test_core.py index 8d0c7794..3a592cdc 100644 --- a/core/tests/test_core.py +++ b/core/tests/test_core.py @@ -92,3 +92,29 @@ def test_docker_image_with_custom_dockerfile_path(dockerfile_path: Optional[Path with DockerContainer(str(image)) as container: assert container._container.image.short_id.endswith(image_short_id), "Image ID mismatch" assert container.get_logs() == (("Hello world!\n").encode(), b""), "Container logs mismatch" + + +def test_docker_container_with_env_file(): + """Test that environment variables can be loaded from a file""" + with tempfile.TemporaryDirectory() as temp_directory: + env_file_path = Path(temp_directory) / "env_file" + with open(env_file_path, "w") as f: + f.write( + """ + TEST_ENV_VAR=hello + NUMBER=123 + DOMAIN=example.org + ADMIN_EMAIL=admin@${DOMAIN} + ROOT_URL=${DOMAIN}/app + """ + ) + container = DockerContainer("alpine").with_command("tail -f /dev/null") # Keep the container running + container.with_env_file(env_file_path) # Load the environment variables from the file + with container: + output = container.exec("env").output.decode("utf-8").strip() + assert "TEST_ENV_VAR=hello" in output + assert "NUMBER=123" in output + assert "DOMAIN=example.org" in output + assert "ADMIN_EMAIL=admin@example.org" in output + assert "ROOT_URL=example.org/app" in output + print(output) diff --git a/poetry.lock b/poetry.lock index bf8872cb..bed91043 100644 --- a/poetry.lock +++ b/poetry.lock @@ -4677,4 +4677,4 @@ weaviate = ["weaviate-client"] [metadata] lock-version = "2.0" python-versions = ">=3.9,<4.0" -content-hash = "69d30cc8cd59a8aa0d019c42b1f171e449dabf6959828160d11f3084c5a03f7f" +content-hash = "01fcde96503baf86c6f971042bafb7ccf001ada3800022e1989a70ddaa782070" diff --git a/pyproject.toml b/pyproject.toml index 43552510..5e85862f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -83,6 +83,7 @@ docker = "*" # ">=4.0" urllib3 = "*" # "<2.0" wrapt = "*" # "^1.16.0" typing-extensions = "*" +python-dotenv = "*" # community modules python-arango = { version = "^7.8", optional = true } From a5a6d5e075d6863f4f82c382c3480f823c2ced8e Mon Sep 17 00:00:00 2001 From: Roy Moore Date: Mon, 19 Aug 2024 17:18:25 +0000 Subject: [PATCH 2/3] update lock --- poetry.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/poetry.lock b/poetry.lock index e0d17ea6..3f27397b 100644 --- a/poetry.lock +++ b/poetry.lock @@ -4677,4 +4677,4 @@ weaviate = ["weaviate-client"] [metadata] lock-version = "2.0" python-versions = ">=3.9,<4.0" -content-hash = "7ffcf39257e1ac79d951b7ccb2cdb972beaaef35d0cd1f722d2964c5bdced674" +content-hash = "5c400cc87dc9708588ee8d7d50646de789235732d868b74ebc43f1cf2a403c88" From 53f3e9d9558d2cab5184a6a0f97efcccbec02f6c Mon Sep 17 00:00:00 2001 From: Roy Moore Date: Sat, 14 Sep 2024 21:09:19 +0000 Subject: [PATCH 3/3] fix --- core/tests/test_core.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/tests/test_core.py b/core/tests/test_core.py index 59d7b2a7..9312b0bc 100644 --- a/core/tests/test_core.py +++ b/core/tests/test_core.py @@ -1,3 +1,6 @@ +import tempfile +from pathlib import Path + from testcontainers.core.container import DockerContainer