Skip to content

Commit be9a0a6

Browse files
feat(core): support TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX (#961)
fixes #808 partially resolve #562, there may be some additional features requested there that this commit does not resolve. Additionally added test cases to check that it's picked up from env into config, and that the config changes the resolved image in DockerContainer Co-authored-by: David Ankin <daveankin@gmail.com>
1 parent 73aeb43 commit be9a0a6

4 files changed

Lines changed: 35 additions & 5 deletions

File tree

core/testcontainers/core/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@ def _render_bool(self, env_name: str, prop_name: str) -> bool:
110110
_docker_auth_config: Optional[str] = field(default_factory=lambda: environ.get("DOCKER_AUTH_CONFIG"))
111111
tc_host_override: Optional[str] = environ.get("TC_HOST", environ.get("TESTCONTAINERS_HOST_OVERRIDE"))
112112
connection_mode_override: Optional[ConnectionMode] = field(default_factory=get_user_overwritten_connection_mode)
113-
114113
"""
115114
https://github.com/testcontainers/testcontainers-go/blob/dd76d1e39c654433a3d80429690d07abcec04424/docker.go#L644
116115
if os env TC_HOST is set, use it
117116
"""
117+
hub_image_name_prefix: str = environ.get("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", "")
118+
""" Prefix to use for hub image names, e.g. for private registries. """
118119

119120
@property
120121
def docker_auth_config(self) -> Optional[str]:

core/testcontainers/core/container.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pathlib
44
import sys
55
import tarfile
6-
from os import PathLike
6+
from os import PathLike, getenv
77
from socket import socket
88
from types import TracebackType
99
from typing import TYPE_CHECKING, Any, Optional, TypedDict, Union, cast
@@ -89,8 +89,7 @@ def __init__(
8989
self.with_volume_mapping(*vol)
9090

9191
self.tmpfs: dict[str, str] = {}
92-
93-
self.image = image
92+
self.image = c.hub_image_name_prefix + image
9493
self._docker = DockerClient(**(docker_client_kw or {}))
9594
self._container: Optional[Container] = None
9695
self._command: Optional[Union[str, list[str]]] = command

core/tests/test_config.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ def test_read_tc_properties(monkeypatch: MonkeyPatch) -> None:
2727
config = TCC()
2828
assert config.tc_properties == {"tc.host": "some_value"}
2929

30-
30+
def test_hub_image_name_prefix(monkeypatch: MonkeyPatch) -> None:
31+
"""
32+
Ensure that the hub_image_name_prefix configuration variable can be read from the environment
33+
"""
34+
monkeypatch.setenv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX", "myregistry.local/")
35+
config = TCC()
36+
assert config.hub_image_name_prefix == "myregistry.local/"
37+
3138
def test_set_tc_properties(monkeypatch: MonkeyPatch) -> None:
3239
"""
3340
Ensure the configuration file variables can be read if no environment variable is set

core/tests/test_container.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from testcontainers.core.container import DockerContainer
66
from testcontainers.core.docker_client import DockerClient
77
from testcontainers.core.config import ConnectionMode
8+
from testcontainers.core.config import testcontainers_config
89

910
FAKE_ID = "ABC123"
1011

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

105106

107+
def test_image_prefix_applied(monkeypatch: pytest.MonkeyPatch) -> None:
108+
"""Test that the hub_image_name_prefix is properly applied to the image name."""
109+
110+
# Set a prefix
111+
test_prefix = "myregistry.example.com/"
112+
monkeypatch.setattr(testcontainers_config, "hub_image_name_prefix", test_prefix)
113+
114+
# Create a container and verify the prefix is applied
115+
container = DockerContainer("nginx:latest")
116+
assert container.image == "myregistry.example.com/nginx:latest"
117+
118+
119+
def test_image_no_prefix_applied_when_empty(monkeypatch: pytest.MonkeyPatch) -> None:
120+
"""Test that when hub_image_name_prefix is empty, no prefix is applied."""
121+
# Set an empty prefix
122+
monkeypatch.setattr(testcontainers_config, "hub_image_name_prefix", "")
123+
124+
# Create a container and verify no prefix is applied
125+
container = DockerContainer("nginx:latest")
126+
assert container.image == "nginx:latest"
127+
128+
106129
def test_container_info():
107130
"""Test get_container_info functionality with a real container."""
108131
with DockerContainer("alpine:latest").with_command("sleep 30") as container:

0 commit comments

Comments
 (0)