Skip to content

Commit

Permalink
Add regression test for the host_port being honored
Browse files Browse the repository at this point in the history
Explicitly test the behavior from
#190
  • Loading branch information
dcermak committed Mar 19, 2024
1 parent a084444 commit 9f20148
Showing 1 changed file with 65 additions and 24 deletions.
89 changes: 65 additions & 24 deletions tests/test_port_forwarding.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@

import pytest
from pytest_container.container import ContainerData
from pytest_container.container import ContainerLauncher
from pytest_container.container import DerivedContainer
from pytest_container.container import lock_host_port_search
from pytest_container.container import PortForwarding
from pytest_container.inspect import NetworkProtocol
from pytest_container.pod import Pod
from pytest_container.pod import PodLauncher
from pytest_container.runtime import LOCALHOST
from pytest_container.runtime import OciRuntimeBase
from pytest_container.runtime import Version

from .images import NGINX_URL
Expand Down Expand Up @@ -219,29 +224,65 @@ def test_bind_to_address(addr: str, container: ContainerData, host) -> None:
assert host.run_expect([7], cmd)


_sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
_sock.bind(("", 0))
_PORT = _sock.getsockname()[1]
_sock.close()
def test_container_bind_to_host_port(
container_runtime: OciRuntimeBase, host, pytestconfig: pytest.Config
) -> None:
with lock_host_port_search(pytestconfig.rootpath):
with socket.socket(
family=socket.AF_INET, type=socket.SOCK_STREAM
) as sock:
sock.bind(("", 0))
PORT = sock.getsockname()[1]

ctr = DerivedContainer(
base=WEB_SERVER,
forwarded_ports=[
PortForwarding(container_port=8000, host_port=PORT)
],
)
with ContainerLauncher(
container=ctr,
container_runtime=container_runtime,
rootdir=pytestconfig.rootpath,
) as launcher:
launcher.launch_container()

assert launcher.container_data.forwarded_ports[0].host_port == PORT
assert (
host.run_expect(
[0], f"{_CURL} http://localhost:{PORT}"
).stdout.strip()
== "Hello Green World!"
)


def test_pod_bind_to_host_port(
container_runtime: OciRuntimeBase, host, pytestconfig: pytest.Config
) -> None:
if not container_runtime.runner_binary.endswith("podman"):
pytest.skip("pods are only supported with podman")

with lock_host_port_search(pytestconfig.rootpath):
with socket.socket(
family=socket.AF_INET, type=socket.SOCK_STREAM
) as sock:
sock.bind(("", 0))
PORT = sock.getsockname()[1]

pod = Pod(
containers=[WEB_SERVER],
forwarded_ports=[
PortForwarding(container_port=8000, host_port=PORT)
],
)

with PodLauncher(pod=pod, rootdir=pytestconfig.rootpath) as launcher:
launcher.launch_pod()

@pytest.mark.parametrize(
"container",
(
DerivedContainer(
base=WEB_SERVER,
forwarded_ports=[
PortForwarding(container_port=8000, host_port=_PORT)
],
),
),
indirect=True,
)
def test_bind_to_host_port(container: ContainerData, host) -> None:
assert container.forwarded_ports[0].host_port == _PORT
assert (
host.run_expect(
[0], f"{_CURL} http://localhost:{_PORT}"
).stdout.strip()
== "Hello Green World!"
)
assert launcher.pod_data.forwarded_ports[0].host_port == PORT
assert (
host.run_expect(
[0], f"{_CURL} http://localhost:{PORT}"
).stdout.strip()
== "Hello Green World!"
)

0 comments on commit 9f20148

Please sign in to comment.