-
Notifications
You must be signed in to change notification settings - Fork 351
feat(core): Protocol support for container port bind and expose #690
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
alexanderankin
merged 17 commits into
testcontainers:main
from
Tranquility2:ports_refactor
May 4, 2025
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a10a934
feat(core): Protocol support for container port bind and expose
Tranquility2 bd0e1e7
Merge branch 'main' into ports_refactor
Tranquility2 0f36b37
fix doctest
Tranquility2 f28b07d
fix doctest
Tranquility2 1a7ac70
CR fix
Tranquility2 c9f81c9
Merge branch 'main' into ports_refactor
Tranquility2 08c7908
Merge branch 'main' into ports_refactor
Tranquility2 9bbb514
Merge branch 'main' into ports_refactor
Tranquility2 5a591bd
Merge branch 'main' into ports_refactor
Tranquility2 83950fa
Merge branch 'main' into ports_refactor
Tranquility2 6c10870
Merge branch 'main' into ports_refactor
Tranquility2 9252691
Merge branch 'main' into ports_refactor
Tranquility2 fb2645d
Update core/testcontainers/core/container.py
Tranquility2 26e30ff
Update core/testcontainers/core/container.py
Tranquility2 dec26d7
Update core/testcontainers/core/container.py
Tranquility2 fc5f459
Merge branch 'main' into ports_refactor
Tranquility2 ff8b217
fix typing issue
alexanderankin 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
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
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,99 @@ | ||
| import pytest | ||
| from typing import Union, Optional | ||
| from testcontainers.core.container import DockerContainer | ||
|
|
||
| from docker.errors import APIError | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "container_port, host_port", | ||
| [ | ||
| ("8080", "8080"), | ||
| ("8125/udp", "8125/udp"), | ||
| ("8092/udp", "8092/udp"), | ||
| ("9000/tcp", "9000/tcp"), | ||
| ("8080", "8080/udp"), | ||
| (8080, 8080), | ||
| (9000, None), | ||
| ("9009", None), | ||
| ("9000", ""), | ||
| ("9000/udp", ""), | ||
| ], | ||
| ) | ||
| def test_docker_container_with_bind_ports(container_port: Union[str, int], host_port: Optional[Union[str, int]]): | ||
| container = DockerContainer("alpine:latest") | ||
| container.with_bind_ports(container_port, host_port) | ||
| container.start() | ||
|
|
||
| # prepare to inspect container | ||
| container_id = container._container.id | ||
| client = container._container.client | ||
|
|
||
| # assemble expected output to compare to container API | ||
| container_port = str(container_port) | ||
| host_port = str(host_port or "") | ||
|
|
||
| # if the port protocol is not specified, it will default to tcp | ||
| if "/" not in container_port: | ||
| container_port += "/tcp" | ||
|
|
||
| expected = {container_port: [{"HostIp": "", "HostPort": host_port}]} | ||
|
|
||
| # compare PortBindings to expected output | ||
| assert client.containers.get(container_id).attrs["HostConfig"]["PortBindings"] == expected | ||
| container.stop() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "container_port, host_port", | ||
| [ | ||
| ("0", "8080"), | ||
| ("8080", "abc"), | ||
| (0, 0), | ||
| (-1, 8080), | ||
| (None, 8080), | ||
| ], | ||
| ) | ||
| def test_error_docker_container_with_bind_ports(container_port: Union[str, int], host_port: Optional[Union[str, int]]): | ||
| with pytest.raises(APIError): | ||
| container = DockerContainer("alpine:latest") | ||
| container.with_bind_ports(container_port, host_port) | ||
| container.start() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "ports, expected", | ||
| [ | ||
| (("8125/udp",), {"8125/udp": {}}), | ||
| (("8092/udp", "9000/tcp"), {"8092/udp": {}, "9000/tcp": {}}), | ||
| (("8080", "8080/udp"), {"8080/tcp": {}, "8080/udp": {}}), | ||
| ((9000,), {"9000/tcp": {}}), | ||
| ((8080, 8080), {"8080/tcp": {}}), | ||
| (("9001", 9002), {"9001/tcp": {}, "9002/tcp": {}}), | ||
| (("9001", 9002, "9003/udp", 9004), {"9001/tcp": {}, "9002/tcp": {}, "9003/udp": {}, "9004/tcp": {}}), | ||
| ], | ||
| ) | ||
| def test_docker_container_with_exposed_ports(ports: tuple[Union[str, int], ...], expected: dict): | ||
| container = DockerContainer("alpine:latest") | ||
| container.with_exposed_ports(*ports) | ||
| container.start() | ||
|
|
||
| container_id = container._container.id | ||
| client = container._container.client | ||
| assert client.containers.get(container_id).attrs["Config"]["ExposedPorts"] == expected | ||
| container.stop() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "ports", | ||
| [ | ||
| ((9000, None)), | ||
| (("", 9000)), | ||
| ("tcp", ""), | ||
| ], | ||
| ) | ||
| def test_error_docker_container_with_exposed_ports(ports: tuple[Union[str, int], ...]): | ||
| with pytest.raises(APIError): | ||
| container = DockerContainer("alpine:latest") | ||
| container.with_exposed_ports(*ports) | ||
| container.start() |
Oops, something went wrong.
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.