Skip to content

Commit 1ba3a11

Browse files
committed
Add network context manager
1 parent 928af5a commit 1ba3a11

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

core/testcontainers/core/network.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3+
# not use this file except in compliance with the License. You may obtain
4+
# a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
from typing import Optional
14+
15+
from testcontainers.core.docker_client import DockerClient
16+
17+
18+
class Network(object):
19+
"""
20+
Network context manager to conveniently connect containers.
21+
"""
22+
23+
def __init__(self, name, docker_client_kw: Optional[dict] = None, **kwargs) -> None:
24+
self.name = name
25+
self._docker = DockerClient(**(docker_client_kw or {}))
26+
self._kwargs = kwargs
27+
28+
def remove(self):
29+
self._network.remove()
30+
31+
def __enter__(self):
32+
self._network = self._docker.client.networks.create(self.name, **self._kwargs)
33+
self.id = self._network.id
34+
return self
35+
36+
def __exit__(self, exc_type, exc_val, exc_tb):
37+
self.remove()
38+
39+
def __del__(self):
40+
if self._network is not None:
41+
try:
42+
self.remove()
43+
except: # noqa: E722
44+
pass

core/tests/test_network.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from testcontainers.core.container import DockerContainer
2+
from testcontainers.core.docker_client import DockerClient
3+
from testcontainers.core.network import Network
4+
5+
6+
def test_network_gets_created_and_cleaned_up():
7+
with Network("test-network") as network:
8+
docker = DockerClient()
9+
networks_list = docker.client.networks.list("test-network")
10+
assert networks_list[0].name == "test-network"
11+
assert networks_list[0].id == network.id
12+
assert not docker.client.networks.list("test-network")
13+
14+
15+
def test_containers_can_communicate_over_network():
16+
with Network("network") as network:
17+
with DockerContainer("nginx:alpine-slim").with_name(
18+
"alpine1").with_kwargs(network=network.name) as alpine1:
19+
with DockerContainer("nginx:alpine-slim").with_name(
20+
"alpine2").with_kwargs(network=network.name) as alpine2:
21+
status, output = alpine1.exec("ping -c 1 alpine2")
22+
assert status == 0
23+
assert "64 bytes" in str(output)
24+
25+
status, output = alpine2.exec("ping -c 1 alpine1")
26+
assert status == 0
27+
assert "64 bytes" in str(output)

0 commit comments

Comments
 (0)