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

feat(core): support TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX by PadenZach · Pull Request #961 · 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  (4) 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
3 changes: 2 additions & 1 deletion core/testcontainers/core/config.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 @@ -110,11 +110,12 @@ def _render_bool(self, env_name: str, prop_name: str) -> bool:
_docker_auth_config: Optional[str] = field(default_factory=lambda: environ.get("DOCKER_AUTH_CONFIG"))
tc_host_override: Optional[str] = environ.get("TC_HOST", environ.get("TESTCONTAINERS_HOST_OVERRIDE"))
connection_mode_override: Optional[ConnectionMode] = field(default_factory=get_user_overwritten_connection_mode)

"""
https://github.com/testcontainers/testcontainers-go/blob/dd76d1e39c654433a3d80429690d07abcec04424/docker.go#L644
if os env TC_HOST is set, use it
"""
hub_image_name_prefix: str = environ.get("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", "")
""" Prefix to use for hub image names, e.g. for private registries. """

@property
def docker_auth_config(self) -> Optional[str]:
Expand Down
5 changes: 2 additions & 3 deletions core/testcontainers/core/container.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 @@ -3,7 +3,7 @@
import pathlib
import sys
import tarfile
from os import PathLike
from os import PathLike, getenv
from socket import socket
from types import TracebackType
from typing import TYPE_CHECKING, Any, Optional, TypedDict, Union, cast
Expand Down Expand Up @@ -89,8 +89,7 @@ def __init__(
self.with_volume_mapping(*vol)

self.tmpfs: dict[str, str] = {}

self.image = image
self.image = c.hub_image_name_prefix + image
self._docker = DockerClient(**(docker_client_kw or {}))
self._container: Optional[Container] = None
self._command: Optional[Union[str, list[str]]] = command
Expand Down
9 changes: 8 additions & 1 deletion core/tests/test_config.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 @@ -27,7 +27,14 @@ def test_read_tc_properties(monkeypatch: MonkeyPatch) -> None:
config = TCC()
assert config.tc_properties == {"tc.host": "some_value"}


def test_hub_image_name_prefix(monkeypatch: MonkeyPatch) -> None:
"""
Ensure that the hub_image_name_prefix configuration variable can be read from the environment
"""
monkeypatch.setenv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", "myregistry.local/")
config = TCC()
assert config.hub_image_name_prefix == "myregistry.local/"

def test_set_tc_properties(monkeypatch: MonkeyPatch) -> None:
"""
Ensure the configuration file variables can be read if no environment variable is set
Expand Down
23 changes: 23 additions & 0 deletions core/tests/test_container.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 @@ -5,6 +5,7 @@
from testcontainers.core.container import DockerContainer
from testcontainers.core.docker_client import DockerClient
from testcontainers.core.config import ConnectionMode
from testcontainers.core.config import testcontainers_config

FAKE_ID = "ABC123"

Expand Down Expand Up @@ -103,6 +104,28 @@ def test_attribute(init_attr, init_value, class_attr, stored_value):
assert getattr(container, class_attr) == stored_value


def test_image_prefix_applied(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that the hub_image_name_prefix is properly applied to the image name."""

# Set a prefix
test_prefix = "myregistry.example.com/"
monkeypatch.setattr(testcontainers_config, "hub_image_name_prefix", test_prefix)

# Create a container and verify the prefix is applied
container = DockerContainer("nginx:latest")
assert container.image == "myregistry.example.com/nginx:latest"


def test_image_no_prefix_applied_when_empty(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that when hub_image_name_prefix is empty, no prefix is applied."""
# Set an empty prefix
monkeypatch.setattr(testcontainers_config, "hub_image_name_prefix", "")

# Create a container and verify no prefix is applied
container = DockerContainer("nginx:latest")
assert container.image == "nginx:latest"


def test_container_info():
"""Test get_container_info functionality with a real container."""
with DockerContainer("alpine:latest").with_command("sleep 30") as container:
Expand Down
Loading

Back | FazBrowse Home | New Git URL