pytest_container
pytest_container copied to clipboard
use podman port & docker port for port binding?
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
I experimented the auto port forwarding (letting podman to choose the host port) and found the below issues:
-
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)
-
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
-
We ran the test case
test_multiple_open_portsin the suitetest_port_forwardingand 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