Skip to content
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

feat: refactor network setup #678

Merged
Merged
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
18 changes: 16 additions & 2 deletions core/testcontainers/core/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from typing import TYPE_CHECKING, Optional

import docker.errors
from docker import version
from docker.types import EndpointConfig
from typing_extensions import Self

from testcontainers.core.config import testcontainers_config as c
Expand Down Expand Up @@ -88,6 +90,18 @@ def start(self) -> Self:
logger.info("Pulling image %s", self.image)
docker_client = self.get_docker_client()
self._configure()

network_kwargs = (
{
"network": self._network.name,
"networking_config": {
self._network.name: EndpointConfig(version.__version__, aliases=self._network_aliases)
},
}
if self._network
else {}
)

self._container = docker_client.run(
self.image,
command=self._command,
Expand All @@ -96,11 +110,11 @@ def start(self) -> Self:
ports=self.ports,
name=self._name,
volumes=self.volumes,
**network_kwargs,
**self._kwargs,
)

logger.info("Container started: %s", self._container.short_id)
if self._network:
self._network.connect(self._container.id, self._network_aliases)
return self

def stop(self, force=True, delete_volume=True) -> None:
Expand Down
52 changes: 32 additions & 20 deletions core/tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,32 @@ def test_network_create_errors():


def test_containers_can_communicate_over_network():
with Network() as network:
with (
DockerContainer(NGINX_ALPINE_SLIM_IMAGE)
.with_name("alpine1")
.with_network_aliases("alpine1-alias-1", "alpine1-alias-2")
.with_network(network) as alpine1
):
with (
DockerContainer(NGINX_ALPINE_SLIM_IMAGE)
.with_name("alpine2")
.with_network_aliases("alpine2-alias-1", "alpine2-alias-2")
.with_network(network) as alpine2
):
assert_can_ping(alpine1, "alpine2")
assert_can_ping(alpine1, "alpine2-alias-1")
assert_can_ping(alpine1, "alpine2-alias-2")

assert_can_ping(alpine2, "alpine1")
assert_can_ping(alpine2, "alpine1-alias-1")
assert_can_ping(alpine2, "alpine1-alias-2")
with (
Network() as network,
DockerContainer(NGINX_ALPINE_SLIM_IMAGE)
.with_name("alpine1")
.with_network_aliases("alpine1-alias-1", "alpine1-alias-2")
.with_network(network) as alpine1,
DockerContainer(NGINX_ALPINE_SLIM_IMAGE)
.with_name("alpine2")
.with_network_aliases("alpine2-alias-1", "alpine2-alias-2")
.with_network(network) as alpine2,
):
assert_can_ping(alpine1, "alpine2")
assert_can_ping(alpine1, "alpine2-alias-1")
assert_can_ping(alpine1, "alpine2-alias-2")

assert_can_ping(alpine2, "alpine1")
assert_can_ping(alpine2, "alpine1-alias-1")
assert_can_ping(alpine2, "alpine1-alias-2")

assert_can_request(alpine1, "alpine2")
assert_can_request(alpine1, "alpine2-alias-1")
assert_can_request(alpine1, "alpine2-alias-2")

assert_can_request(alpine2, "alpine1")
assert_can_request(alpine2, "alpine1-alias-1")
assert_can_request(alpine2, "alpine1-alias-2")


def assert_can_ping(container: DockerContainer, remote_name: str):
Expand All @@ -73,6 +79,12 @@ def assert_can_ping(container: DockerContainer, remote_name: str):
assert "64 bytes" in str(output)


def assert_can_request(container: DockerContainer, remote_name: str):
status, output = container.exec(f"wget -qO- http://{remote_name}")
assert status == 0
assert "Welcome to nginx!" in output.decode()


def test_network_has_labels():
network = Network()
try:
Expand Down