summaryrefslogtreecommitdiff
path: root/test/system/helpers.bash
diff options
context:
space:
mode:
authoropenshift-ci[bot] <75433959+openshift-ci[bot]@users.noreply.github.com>2022-06-27 13:42:58 +0000
committerGitHub <noreply@github.com>2022-06-27 13:42:58 +0000
commit8806606ca2e2060750b3f86c6d31bb50125309de (patch)
tree1810bd984b11c50802170ae712da41827d6cf7ad /test/system/helpers.bash
parent1022ea36dd36cf57a2556f5eac99b30500513e08 (diff)
parent345778ece0eac49fe49e68a2dd2bb301c975c5af (diff)
downloadpodman-8806606ca2e2060750b3f86c6d31bb50125309de.tar.gz
podman-8806606ca2e2060750b3f86c6d31bb50125309de.tar.bz2
podman-8806606ca2e2060750b3f86c6d31bb50125309de.zip
Merge pull request #14725 from Luap99/port-range-flake
Fix "podman run port forward range" flake
Diffstat (limited to 'test/system/helpers.bash')
-rw-r--r--test/system/helpers.bash31
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
###################