-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
9c3ecc8
Add network context manager
mloesch 97ad2b8
Merge branch 'main' into network
mloesch c10223f
Use random names for networks
mloesch 7542743
Merge branch 'main' into network
kiview e4d9c19
Add explicit API DockerContainer.with_network(Network)
mloesch c757142
Add support for network aliases
mloesch b076309
Merge remote-tracking branch 'origin/main' into network
mloesch 8c2f8b9
Merge branch 'main' into network
mloesch b523301
Merge remote-tracking branch 'origin/main' into network
mloesch 9093c6b
Merge remote-tracking branch 'origin/main' into network
mloesch 1c8ebdc
chore: change return type to Self
mloesch cac19d3
chore: further specify type
mloesch b392ca6
chore: clarify Network init signature
mloesch 8c8b6e2
chore: remove __del__hook
mloesch a64625c
chore(codestyle): switch to `ruff` from `black` for code formatting (…
max-pfeiffer a00d8dd
fix(vault): add support for HashiCorp Vault container (#366)
f4z3r 065f707
fix(core): make config editable to avoid monkeypatching.1 (#532)
alexanderankin ba33506
chore(main): release testcontainers 4.3.2 (#530)
github-actions[bot] 9db1080
chore: pin nginx image
mloesch 9426254
fix class style
mloesch 3fc6bcf
Merge remote-tracking branch 'origin/main' into network
mloesch 66e2584
Merge branch 'main' into network
santi 2e50cb3
chore: reformat
mloesch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# | ||
# 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: | ||
""" | ||
Network context manager for programmatically connecting containers. | ||
""" | ||
|
||
def __init__(self, docker_client_kw: Optional[dict] = None, docker_network_kw: Optional[dict] = None) -> None: | ||
self.name = str(uuid.uuid4()) | ||
self._docker = DockerClient(**(docker_client_kw or {})) | ||
self._docker_network_kw = docker_network_kw or {} | ||
|
||
def connect(self, container_id: str, network_aliases: Optional[list] = None): | ||
self._network.connect(container_id, aliases=network_aliases) | ||
|
||
def remove(self) -> None: | ||
self._network.remove() | ||
|
||
def __enter__(self) -> "Network": | ||
self._network = self._docker.client.networks.create(self.name, **self._docker_network_kw) | ||
self.id = self._network.id | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb) -> None: | ||
self.remove() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from testcontainers.core.container import DockerContainer | ||
from testcontainers.core.docker_client import DockerClient | ||
from testcontainers.core.network import Network | ||
|
||
NGINX_ALPINE_SLIM_IMAGE = "nginx:1.25.4-alpine-slim" | ||
|
||
|
||
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_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") | ||
|
||
|
||
def assert_can_ping(container: DockerContainer, remote_name: str): | ||
status, output = container.exec("ping -c 1 %s" % remote_name) | ||
assert status == 0 | ||
assert "64 bytes" in str(output) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.