|  | 
|  | 1 | +import pytest | 
|  | 2 | +from typing import Union, Optional | 
|  | 3 | +from testcontainers.core.container import DockerContainer | 
|  | 4 | + | 
|  | 5 | +from docker.errors import APIError | 
|  | 6 | + | 
|  | 7 | + | 
|  | 8 | +@pytest.mark.parametrize( | 
|  | 9 | +    "container_port, host_port", | 
|  | 10 | +    [ | 
|  | 11 | +        ("8080", "8080"), | 
|  | 12 | +        ("8125/udp", "8125/udp"), | 
|  | 13 | +        ("8092/udp", "8092/udp"), | 
|  | 14 | +        ("9000/tcp", "9000/tcp"), | 
|  | 15 | +        ("8080", "8080/udp"), | 
|  | 16 | +        (8080, 8080), | 
|  | 17 | +        (9000, None), | 
|  | 18 | +        ("9009", None), | 
|  | 19 | +        ("9000", ""), | 
|  | 20 | +        ("9000/udp", ""), | 
|  | 21 | +    ], | 
|  | 22 | +) | 
|  | 23 | +def test_docker_container_with_bind_ports(container_port: Union[str, int], host_port: Optional[Union[str, int]]): | 
|  | 24 | +    container = DockerContainer("alpine:latest") | 
|  | 25 | +    container.with_bind_ports(container_port, host_port) | 
|  | 26 | +    container.start() | 
|  | 27 | + | 
|  | 28 | +    # prepare to inspect container | 
|  | 29 | +    container_id = container._container.id | 
|  | 30 | +    client = container._container.client | 
|  | 31 | + | 
|  | 32 | +    # assemble expected output to compare to container API | 
|  | 33 | +    container_port = str(container_port) | 
|  | 34 | +    host_port = str(host_port or "") | 
|  | 35 | + | 
|  | 36 | +    # if the port protocol is not specified, it will default to tcp | 
|  | 37 | +    if "/" not in container_port: | 
|  | 38 | +        container_port += "/tcp" | 
|  | 39 | + | 
|  | 40 | +    expected = {container_port: [{"HostIp": "", "HostPort": host_port}]} | 
|  | 41 | + | 
|  | 42 | +    # compare PortBindings to expected output | 
|  | 43 | +    assert client.containers.get(container_id).attrs["HostConfig"]["PortBindings"] == expected | 
|  | 44 | +    container.stop() | 
|  | 45 | + | 
|  | 46 | + | 
|  | 47 | +@pytest.mark.parametrize( | 
|  | 48 | +    "container_port, host_port", | 
|  | 49 | +    [ | 
|  | 50 | +        ("0", "8080"), | 
|  | 51 | +        ("8080", "abc"), | 
|  | 52 | +        (0, 0), | 
|  | 53 | +        (-1, 8080), | 
|  | 54 | +        (None, 8080), | 
|  | 55 | +    ], | 
|  | 56 | +) | 
|  | 57 | +def test_error_docker_container_with_bind_ports(container_port: Union[str, int], host_port: Optional[Union[str, int]]): | 
|  | 58 | +    with pytest.raises(APIError): | 
|  | 59 | +        container = DockerContainer("alpine:latest") | 
|  | 60 | +        container.with_bind_ports(container_port, host_port) | 
|  | 61 | +        container.start() | 
|  | 62 | + | 
|  | 63 | + | 
|  | 64 | +@pytest.mark.parametrize( | 
|  | 65 | +    "ports, expected", | 
|  | 66 | +    [ | 
|  | 67 | +        (("8125/udp",), {"8125/udp": {}}), | 
|  | 68 | +        (("8092/udp", "9000/tcp"), {"8092/udp": {}, "9000/tcp": {}}), | 
|  | 69 | +        (("8080", "8080/udp"), {"8080/tcp": {}, "8080/udp": {}}), | 
|  | 70 | +        ((9000,), {"9000/tcp": {}}), | 
|  | 71 | +        ((8080, 8080), {"8080/tcp": {}}), | 
|  | 72 | +        (("9001", 9002), {"9001/tcp": {}, "9002/tcp": {}}), | 
|  | 73 | +        (("9001", 9002, "9003/udp", 9004), {"9001/tcp": {}, "9002/tcp": {}, "9003/udp": {}, "9004/tcp": {}}), | 
|  | 74 | +    ], | 
|  | 75 | +) | 
|  | 76 | +def test_docker_container_with_exposed_ports(ports: tuple[Union[str, int], ...], expected: dict): | 
|  | 77 | +    container = DockerContainer("alpine:latest") | 
|  | 78 | +    container.with_exposed_ports(*ports) | 
|  | 79 | +    container.start() | 
|  | 80 | + | 
|  | 81 | +    container_id = container._container.id | 
|  | 82 | +    client = container._container.client | 
|  | 83 | +    assert client.containers.get(container_id).attrs["Config"]["ExposedPorts"] == expected | 
|  | 84 | +    container.stop() | 
|  | 85 | + | 
|  | 86 | + | 
|  | 87 | +@pytest.mark.parametrize( | 
|  | 88 | +    "ports", | 
|  | 89 | +    [ | 
|  | 90 | +        ((9000, None)), | 
|  | 91 | +        (("", 9000)), | 
|  | 92 | +        ("tcp", ""), | 
|  | 93 | +    ], | 
|  | 94 | +) | 
|  | 95 | +def test_error_docker_container_with_exposed_ports(ports: tuple[Union[str, int], ...]): | 
|  | 96 | +    with pytest.raises(APIError): | 
|  | 97 | +        container = DockerContainer("alpine:latest") | 
|  | 98 | +        container.with_exposed_ports(*ports) | 
|  | 99 | +        container.start() | 
0 commit comments