-
Notifications
You must be signed in to change notification settings - Fork 311
feat(network): Add network context manager #367
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
Changes from 4 commits
9c3ecc8
97ad2b8
c10223f
7542743
e4d9c19
c757142
b076309
8c2f8b9
b523301
9093c6b
1c8ebdc
cac19d3
b392ca6
8c8b6e2
a64625c
a00d8dd
065f707
ba33506
9db1080
9426254
3fc6bcf
66e2584
2e50cb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
import uuid | ||
from typing import Optional | ||
|
||
from testcontainers.core.docker_client import DockerClient | ||
|
||
|
||
class Network(object): | ||
""" | ||
Network context manager for programmatically connecting containers. | ||
""" | ||
|
||
def __init__(self, docker_client_kw: Optional[dict] = None, **kwargs) -> None: | ||
self.name = str(uuid.uuid4()) | ||
self._docker = DockerClient(**(docker_client_kw or {})) | ||
self._kwargs = kwargs | ||
|
||
def remove(self) -> None: | ||
self._network.remove() | ||
|
||
def __enter__(self) -> 'Network': | ||
self._network = self._docker.client.networks.create(self.name, **self._kwargs) | ||
self.id = self._network.id | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb) -> None: | ||
self.remove() | ||
|
||
def __del__(self) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've had some bad experiences with external resource cleanup through the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in 8c8b6e2 |
||
if self._network is not None: | ||
try: | ||
self.remove() | ||
except: # noqa: E722 | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from testcontainers.core.container import DockerContainer | ||
from testcontainers.core.docker_client import DockerClient | ||
from testcontainers.core.network import Network | ||
|
||
|
||
def test_network_gets_created_and_cleaned_up(): | ||
with Network() as network: | ||
docker = DockerClient() | ||
networks_list = docker.client.networks.list(network.name) | ||
assert networks_list[0].name == network.name | ||
assert networks_list[0].id == network.id | ||
assert not docker.client.networks.list(network.name) | ||
|
||
|
||
def test_containers_can_communicate_over_network(): | ||
with Network() as network: | ||
with DockerContainer("nginx:alpine-slim").with_name( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
"alpine1").with_kwargs(network=network.name) as alpine1: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 added in e4d9c19 |
||
with DockerContainer("nginx:alpine-slim").with_name( | ||
"alpine2").with_kwargs(network=network.name) as alpine2: | ||
status, output = alpine1.exec("ping -c 1 alpine2") | ||
assert status == 0 | ||
assert "64 bytes" in str(output) | ||
|
||
status, output = alpine2.exec("ping -c 1 alpine1") | ||
assert status == 0 | ||
assert "64 bytes" in str(output) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From an API users' point of view, it is not really clear what the
kwargs
arguments are meant for. Could you update the signature to bedef __init__(self, docker_client_kw: Optional[dict] = None, docker_network_kw: Optional[dict] = None) -> None:
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in b392ca6