Skip to content

Add CustomContainer exposing settings through the class #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions core/testcontainers/core/container.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from docker.models.containers import Container
import os
from typing import Iterable, Optional, Tuple
from typing import Iterable, Optional, Tuple, Any, Dict, List

from .waiting_utils import wait_container_is_ready
from .docker_client import DockerClient
Expand All @@ -14,6 +14,18 @@ class DockerContainer:
"""
Basic container object to spin up Docker instances.

Args:
image: The name of the image to start.
docker_client_kw: Dictionary with arguments that will be passed to the
docker.DockerClient init.
command: Optional execution command for the container.
name: Optional name for the container.
ports: Ports to be exposed by the container. The port number will be
automatically assigned on the host, use
:code:`get_exposed_port(PORT)` method to get the port number on the host.
volumes: Volumes to mount into the container. Each entry should be a tuple with
three values: host path, container path and. mode (default 'ro').

.. doctest::

>>> from testcontainers.core.container import DockerContainer
Expand All @@ -22,15 +34,37 @@ class DockerContainer:
>>> with DockerContainer("hello-world") as container:
... delay = wait_for_logs(container, "Hello from Docker!")
"""
def __init__(self, image: str, docker_client_kw: Optional[dict] = None, **kwargs) -> None:
self.env = {}
self.ports = {}
self.volumes = {}
def __init__(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do the same with network and network_aliases? I suspect we need to rebase this PR in order to pickup these attributes.

self,
image: str,
docker_client_kw: Optional[Dict[str, Any]] = None,
command: Optional[str] = None,
env: Optional[Dict[str, str]] = None,
name: Optional[str] = None,
ports: Optional[List[int]] = None,
volumes: Optional[List[Tuple[str, str, str]]] = None,
**kwargs
) -> None:

self.image = image
self._docker = DockerClient(**(docker_client_kw or {}))

self._command = command

self.env = env or {}

self._name = name

self.ports = {}
if ports:
self.with_exposed_ports(*ports)

self.volumes = {}
if volumes:
for vol in volumes:
self.with_volume_mapping(*vol)

self._container = None
self._command = None
self._name = None
self._kwargs = kwargs

def with_env(self, key: str, value: str) -> 'DockerContainer':
Expand Down
21 changes: 21 additions & 0 deletions core/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,24 @@ def test_can_get_logs():
wait_for_logs(container, "Hello from Docker!")
stdout, stderr = container.get_logs()
assert stdout, 'There should be something on stdout'


@pytest.mark.parametrize(
"init_attr,init_value,class_attr,stored_value",
[
("command", "ps", "_command", "ps"),
("env", {"e1": "v1"}, "env", {"e1": "v1"}),
("name", "foo-bar", "_name", "foo-bar"),
("ports", [22, 80], "ports", {22: None, 80: None}),
(
"volumes",
[("/tmp", "/tmp2", "ro")],
"volumes",
{"/tmp": {"bind": "/tmp2", "mode": "ro"}},
),
],
)
def test_attribute(init_attr, init_value, class_attr, stored_value):
"""Test that the attributes set through the __init__ function are properly stored."""
with DockerContainer("ubuntu", **{init_attr: init_value}) as container:
assert getattr(container, class_attr) == stored_value