pytest_container icon indicating copy to clipboard operation
pytest_container copied to clipboard

use podman port & docker port for port binding?

Open dcermak opened this issue 1 year ago • 1 comments

Currently we rely on the function pytest_container.container.create_host_port_port_forward to find free ports on the host: https://github.com/dcermak/pytest_container/blob/92c1a636021c64ceb5d82f3ba356ca800f08cd8d/pytest_container/container.py#L88

However, both podman & docker support the subcommand docker port $ctr and podman port $ctr which outputs the port binding of a container. This could be leveraged to let the container runtime pick a random port for us and then later on obtain that port via this command (or via inspect).

We'd probably still need to lock the container launch though to prevent port binding races in the container runtime and ensure that the API also works when we bind to specific IP addresses as we check here: https://github.com/dcermak/pytest_container/blob/92c1a636021c64ceb5d82f3ba356ca800f08cd8d/tests/test_port_forwarding.py#L214

dcermak avatar Aug 30 '24 10:08 dcermak

I experimented the auto port forwarding (letting podman to choose the host port) and found the below issues:

  1. Sometimes, podman run command fails with exit status 126 which is basically a permission issue, but not sure why this happens this way (we are port forwarding ~1000 ports in a test case)

  2. Sometimes, the previous step passes but then the ngnix webserver we run in container not exposing the port properly, the curl command fails as the page is not reachable

  3. We ran the test case test_multiple_open_ports in the suite test_port_forwarding and obtained the results mentioned in the previous points. We try to run the test case on 10 containers. A very few times, the test case passes on any one of those 10 containers

rcmadhankumar avatar Sep 04 '24 07:09 rcmadhankumar