Skip to content

Commit

Permalink
feat: refactor network setup (#678)
Browse files Browse the repository at this point in the history
fixes #645
- network should be attached as the container is started, not as a
post-start action. This will make sure port binding and exposing works
correctly.

---------

Signed-off-by: mgorsk1 <gorskimariusz13@gmail.com>
  • Loading branch information
mgorsk1 authored Aug 13, 2024
1 parent 1e43923 commit d5de0aa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
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

0 comments on commit d5de0aa

Please sign in to comment.