Skip to content

Commit 94564d5

Browse files
committed
feat(core): support TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX
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
1 parent 44dd40b commit 94564d5

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

core/testcontainers/core/config.py

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

117118
@property
118119
def docker_auth_config(self) -> Optional[str]:

core/testcontainers/core/container.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import contextlib
22
import sys
3-
from os import PathLike
3+
from os import PathLike, getenv
44
from socket import socket
55
from types import TracebackType
66
from typing import TYPE_CHECKING, Any, Optional, TypedDict, Union, cast
@@ -82,7 +82,7 @@ def __init__(
8282
for vol in volumes:
8383
self.with_volume_mapping(*vol)
8484

85-
self.image = image
85+
self.image = c.hub_image_name_prefix + image
8686
self._docker = DockerClient(**(docker_client_kw or {}))
8787
self._container: Optional[Container] = None
8888
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
@@ -3,6 +3,7 @@
33
from testcontainers.core.container import DockerContainer
44
from testcontainers.core.docker_client import DockerClient
55
from testcontainers.core.config import ConnectionMode
6+
from testcontainers.core.config import testcontainers_config
67

78
FAKE_ID = "ABC123"
89

@@ -96,3 +97,25 @@ def test_attribute(init_attr, init_value, class_attr, stored_value):
9697
"""Test that the attributes set through the __init__ function are properly stored."""
9798
with DockerContainer("ubuntu", **{init_attr: init_value}) as container:
9899
assert getattr(container, class_attr) == stored_value
100+
101+
102+
def test_image_prefix_applied(monkeypatch: pytest.MonkeyPatch) -> None:
103+
"""Test that the hub_image_name_prefix is properly applied to the image name."""
104+
105+
# Set a prefix
106+
test_prefix = "myregistry.example.com/"
107+
monkeypatch.setattr(testcontainers_config, "hub_image_name_prefix", test_prefix)
108+
109+
# Create a container and verify the prefix is applied
110+
container = DockerContainer("nginx:latest")
111+
assert container.image == "myregistry.example.com/nginx:latest"
112+
113+
114+
def test_image_no_prefix_applied_when_empty(monkeypatch: pytest.MonkeyPatch) -> None:
115+
"""Test that when hub_image_name_prefix is empty, no prefix is applied."""
116+
# Set an empty prefix
117+
monkeypatch.setattr(testcontainers_config, "hub_image_name_prefix", "")
118+
119+
# Create a container and verify no prefix is applied
120+
container = DockerContainer("nginx:latest")
121+
assert container.image == "nginx:latest"

0 commit comments

Comments
 (0)