FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(core): Add kwargs to image build by Tranquility2 · Pull Request #708 · testcontainers/testcontainers-python · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
9 changes: 6 additions & 3 deletions core/testcontainers/core/image.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ class DockerImage:
>>> with DockerImage(path="./core/tests/image_fixtures/sample/", tag="test-image") as image:
... logs = image.get_logs()

:param tag: Tag for the image to be built (default: None)
:param path: Path to the build context
:param docker_client_kw: Keyword arguments to pass to the DockerClient
:param tag: Tag for the image to be built (default: None)
:param clean_up: Remove the image after exiting the context (default: True)
:param dockerfile_path: Path to the Dockerfile within the build context path (default: Dockerfile)
:param no_cache: Bypass build cache; CLI's --no-cache
:param kwargs: Additional keyword arguments to pass to the underlying docker-py
"""

def __init__(
Expand All @@ -49,11 +52,11 @@ def __init__(
self._dockerfile_path = dockerfile_path
self._no_cache = no_cache

def build(self, **kwargs) -> Self:
def build(self) -> Self:
logger.info(f"Building image from {self.path}")
docker_client = self.get_docker_client()
self._image, self._logs = docker_client.build(
path=str(self.path), tag=self.tag, dockerfile=self._dockerfile_path, nocache=self._no_cache, **kwargs
path=str(self.path), tag=self.tag, dockerfile=self._dockerfile_path, nocache=self._no_cache, **self._kwargs
)
logger.info(f"Built image {self.short_id} with tag {self.tag}")
return self
Expand Down
21 changes: 21 additions & 0 deletions core/tests/test_image.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,24 @@ 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_image_with_kwargs():
with tempfile.TemporaryDirectory() as temp_directory:
with open(f"{temp_directory}/Dockerfile", "w") as f:
f.write(
f"""
FROM alpine:latest
ARG TEST_ARG
ENV TEST_ARG $TEST_ARG
CMD echo $TEST_ARG
"""
)
with DockerImage(
path=temp_directory, tag="test", clean_up=True, no_cache=True, buildargs={"TEST_ARG": "new_arg"}
) as image:
image_short_id = image.short_id
assert image.get_wrapped_image() is not None
with DockerContainer(str(image)) as container:
assert container._container.image.short_id.endswith(image_short_id), "Image ID mismatch"
assert container.get_logs() == (("new_arg\n").encode(), b""), "Container logs mismatch"

Back | FazBrowse Home | New Git URL