Skip to content

Commit 8278bdf

Browse files
alexanderankinrhoban13
authored andcommitted
DockerContainer initializer to accept its private members as kwargs
1 parent 0ae704a commit 8278bdf

File tree

4 files changed

+400
-37
lines changed

4 files changed

+400
-37
lines changed

.readthedocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ build:
1414
# https://github.com/readthedocs/readthedocs.org/issues/4912#issuecomment-1143587902s
1515
jobs:
1616
post_install:
17-
- pip install poetry==1.7.1 # match version from poetry.lock
17+
- pip install poetry==2.1.2 # match version from poetry.lock
1818
- poetry config virtualenvs.create false
1919
- poetry install --all-extras

core/testcontainers/core/container.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ class DockerContainer:
2828
"""
2929
Basic container object to spin up Docker instances.
3030
31+
Args:
32+
image: The name of the image to start.
33+
docker_client_kw: Dictionary with arguments that will be passed to the
34+
docker.DockerClient init.
35+
command: Optional execution command for the container.
36+
name: Optional name for the container.
37+
ports: Ports to be exposed by the container. The port number will be
38+
automatically assigned on the host, use
39+
:code:`get_exposed_port(PORT)` method to get the port number on the host.
40+
volumes: Volumes to mount into the container. Each entry should be a tuple with
41+
three values: host path, container path and. mode (default 'ro').
42+
3143
.. doctest::
3244
3345
>>> from testcontainers.core.container import DockerContainer
@@ -41,18 +53,40 @@ def __init__(
4153
self,
4254
image: str,
4355
docker_client_kw: Optional[dict] = None,
56+
command: Optional[str] = None,
57+
env: Optional[dict[str, str]] = None,
58+
name: Optional[str] = None,
59+
ports: Optional[list[int]] = None,
60+
volumes: Optional[list[tuple[str, str, str]]] = None,
61+
network: Optional[Network] = None,
62+
network_aliases: Optional[list[str]] = None,
4463
**kwargs,
4564
) -> None:
46-
self.env = {}
65+
self.env = env or {}
66+
4767
self.ports = {}
68+
if ports:
69+
self.with_exposed_ports(*ports)
70+
4871
self.volumes = {}
72+
if volumes:
73+
for vol in volumes:
74+
self.with_volume_mapping(*vol)
75+
4976
self.image = image
5077
self._docker = DockerClient(**(docker_client_kw or {}))
5178
self._container = None
52-
self._command = None
53-
self._name = None
79+
self._command = command
80+
self._name = name
81+
5482
self._network: Optional[Network] = None
83+
if network is not None:
84+
self.with_network(network)
85+
5586
self._network_aliases: Optional[list[str]] = None
87+
if network_aliases:
88+
self.with_network_aliases(*network_aliases)
89+
5690
self._kwargs = kwargs
5791

5892
def with_env(self, key: str, value: str) -> Self:

core/tests/test_container.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,24 @@ def test_get_exposed_port_original(container: DockerContainer, monkeypatch: pyte
7575
monkeypatch.setattr(client, "get_connection_mode", lambda: ConnectionMode.bridge_ip)
7676

7777
assert container.get_exposed_port(8080) == 8080
78+
79+
80+
@pytest.mark.parametrize(
81+
"init_attr,init_value,class_attr,stored_value",
82+
[
83+
("command", "ps", "_command", "ps"),
84+
("env", {"e1": "v1"}, "env", {"e1": "v1"}),
85+
("name", "foo-bar", "_name", "foo-bar"),
86+
("ports", [22, 80], "ports", {22: None, 80: None}),
87+
(
88+
"volumes",
89+
[("/tmp", "/tmp2", "ro")],
90+
"volumes",
91+
{"/tmp": {"bind": "/tmp2", "mode": "ro"}},
92+
),
93+
],
94+
)
95+
def test_attribute(init_attr, init_value, class_attr, stored_value):
96+
"""Test that the attributes set through the __init__ function are properly stored."""
97+
with DockerContainer("ubuntu", **{init_attr: init_value}) as container:
98+
assert getattr(container, class_attr) == stored_value

0 commit comments

Comments
 (0)