diff options
author | Paul Holzinger <pholzing@redhat.com> | 2022-06-24 15:21:27 +0200 |
---|---|---|
committer | Paul Holzinger <pholzing@redhat.com> | 2022-06-27 14:25:24 +0200 |
commit | 345778ece0eac49fe49e68a2dd2bb301c975c5af (patch) | |
tree | b21809bf3bd4c5b8bfd59f732605b52058c4cea2 /test/system/helpers.bash | |
parent | 3176b3fe011aa3323ba192e90efe516d0701e46e (diff) | |
download | podman-345778ece0eac49fe49e68a2dd2bb301c975c5af.tar.gz podman-345778ece0eac49fe49e68a2dd2bb301c975c5af.tar.bz2 podman-345778ece0eac49fe49e68a2dd2bb301c975c5af.zip |
Fix "podman run port forward range" flake
The test must ensure that all ports in the range are free not just
the first. This flakes often because port 5355 is always in use by
systemd-resolved on fedora.
Fixes #14716
Signed-off-by: Paul Holzinger <pholzing@redhat.com>
Diffstat (limited to 'test/system/helpers.bash')
-rw-r--r-- | test/system/helpers.bash | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/test/system/helpers.bash b/test/system/helpers.bash index 74b5ddc4b..273e8d2f5 100644 --- a/test/system/helpers.bash +++ b/test/system/helpers.bash @@ -284,7 +284,7 @@ function random_free_port() { local port for port in $(shuf -i ${range}); do - if ! { exec {unused_fd}<> /dev/tcp/127.0.0.1/$port; } &>/dev/null; then + if port_is_free $port; then echo $port return fi @@ -293,6 +293,35 @@ function random_free_port() { die "Could not find open port in range $range" } +function random_free_port_range() { + local size=${1?Usage: random_free_port_range SIZE (as in, number of ports)} + + local maxtries=10 + while [[ $maxtries -gt 0 ]]; do + local firstport=$(random_free_port) + local all_ports_free=1 + for i in $(seq 2 $size); do + if ! port_is_free $((firstport + $i)); then + all_ports_free= + break + fi + done + if [[ -n "$all_ports_free" ]]; then + echo "$firstport-$((firstport + $size - 1))" + return + fi + + maxtries=$((maxtries - 1)) + done + + die "Could not find free port range with size $size" +} + +function port_is_free() { + local port=${1?Usage: port_is_free PORT} + ! { exec {unused_fd}<> /dev/tcp/127.0.0.1/$port; } &>/dev/null +} + ################### # wait_for_port # Returns once port is available on host ################### |