summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEd Santiago <santiago@redhat.com>2021-09-08 10:57:24 -0600
committerEd Santiago <santiago@redhat.com>2021-09-08 11:25:42 -0600
commit1ff797e3621e7e370f53c4c71c9f40bb6a878936 (patch)
tree0e39a0e3ba4348e05b157253d042b4366f7dc74d
parentd68e429859b497cd31c6e3dfdc64dce58b0b95d5 (diff)
downloadpodman-1ff797e3621e7e370f53c4c71c9f40bb6a878936.tar.gz
podman-1ff797e3621e7e370f53c4c71c9f40bb6a878936.tar.bz2
podman-1ff797e3621e7e370f53c4c71c9f40bb6a878936.zip
system tests: new random_free_port helper
Picks a pseudorandom open port within a range. Refactor existing instances of such code. Signed-off-by: Ed Santiago <santiago@redhat.com>
-rw-r--r--test/system/150-login.bats7
-rw-r--r--test/system/200-pod.bats18
-rw-r--r--test/system/271-tcp-cors-server.bats4
-rw-r--r--test/system/500-networking.bats12
-rw-r--r--test/system/helpers.bash19
-rwxr-xr-xtest/system/helpers.t10
6 files changed, 38 insertions, 32 deletions
diff --git a/test/system/150-login.bats b/test/system/150-login.bats
index b6c04db08..ed925044c 100644
--- a/test/system/150-login.bats
+++ b/test/system/150-login.bats
@@ -22,12 +22,7 @@ fi
# Randomly-assigned port in the 5xxx range
if [ -z "${PODMAN_LOGIN_REGISTRY_PORT}" ]; then
- for port in $(shuf -i 5000-5999);do
- if ! { exec 3<> /dev/tcp/127.0.0.1/$port; } &>/dev/null; then
- export PODMAN_LOGIN_REGISTRY_PORT=$port
- break
- fi
- done
+ export PODMAN_LOGIN_REGISTRY_PORT=$(random_free_port)
fi
# Override any user-set path to an auth file
diff --git a/test/system/200-pod.bats b/test/system/200-pod.bats
index 266f91298..027abf9dc 100644
--- a/test/system/200-pod.bats
+++ b/test/system/200-pod.bats
@@ -76,11 +76,7 @@ function teardown() {
fi
# Randomly-assigned port in the 5xxx range
- for port in $(shuf -i 5000-5999);do
- if ! { exec 3<> /dev/tcp/127.0.0.1/$port; } &>/dev/null; then
- break
- fi
- done
+ port=$(random_free_port)
# Listener. This will exit as soon as it receives a message.
run_podman run -d --pod $podname $IMAGE nc -l -p $port
@@ -183,16 +179,8 @@ function random_ip() {
pod_id_file=${PODMAN_TMPDIR}/pod-id-file
# Randomly-assigned ports in the 5xxx and 6xxx range
- for port_in in $(shuf -i 5000-5999);do
- if ! { exec 3<> /dev/tcp/127.0.0.1/$port_in; } &>/dev/null; then
- break
- fi
- done
- for port_out in $(shuf -i 6000-6999);do
- if ! { exec 3<> /dev/tcp/127.0.0.1/$port_out; } &>/dev/null; then
- break
- fi
- done
+ port_in=$(random_free_port 5000-5999)
+ port_out=$(random_free_port 6000-6999)
# Create a pod with all the desired options
# FIXME: --ip=$ip fails:
diff --git a/test/system/271-tcp-cors-server.bats b/test/system/271-tcp-cors-server.bats
index cdfa82e82..d8e4eb3df 100644
--- a/test/system/271-tcp-cors-server.bats
+++ b/test/system/271-tcp-cors-server.bats
@@ -14,7 +14,7 @@ SOCKET_FILE="$UNIT_DIR/$SERVICE_NAME.socket"
@test "podman system service - tcp CORS" {
skip_if_remote "system service tests are meaningless over remote"
- PORT=$(( ((RANDOM<<15)|RANDOM) % 63001 + 2000 ))
+ PORT=$(random_free_port 63000-64999)
run_podman system service --cors="*" tcp:$SERVICE_TCP_HOST:$PORT -t 20 &
podman_pid="$!"
sleep 5s
@@ -26,7 +26,7 @@ SOCKET_FILE="$UNIT_DIR/$SERVICE_NAME.socket"
@test "podman system service - tcp without CORS" {
skip_if_remote "system service tests are meaningless over remote"
- PORT=$(( ((RANDOM<<15)|RANDOM) % 63001 + 2000 ))
+ PORT=$(random_free_port 63000-64999)
run_podman system service tcp:$SERVICE_TCP_HOST:$PORT -t 20 &
podman_pid="$!"
sleep 5s
diff --git a/test/system/500-networking.bats b/test/system/500-networking.bats
index 3ebe45e63..ad5891dd9 100644
--- a/test/system/500-networking.bats
+++ b/test/system/500-networking.bats
@@ -23,7 +23,7 @@ load helpers
random_1=$(random_string 30)
random_2=$(random_string 30)
- HOST_PORT=8080
+ HOST_PORT=$(random_free_port)
SERVER=http://127.0.0.1:$HOST_PORT
# Create a test file with random content
@@ -114,11 +114,8 @@ load helpers
# Issue #5466 - port-forwarding doesn't work with this option and -d
@test "podman networking: port with --userns=keep-id" {
- # FIXME: randomize port, and create second random host port
- myport=54321
-
for cidr in "" "$(random_rfc1918_subnet).0/24"; do
- myport=$(( myport + 1 ))
+ myport=$(random_free_port 52000-52999)
if [[ -z $cidr ]]; then
# regex to match that we are in 10.X subnet
match="10\..*"
@@ -188,6 +185,7 @@ load helpers
# "network create" now works rootless, with the help of a special container
@test "podman network create" {
+ # Deliberately use a fixed port, not random_open_port, because of #10806
myport=54322
local mynetname=testnet-$(random_string 10)
@@ -244,7 +242,7 @@ load helpers
skip_if_remote "podman network reload does not have remote support"
random_1=$(random_string 30)
- HOST_PORT=12345
+ HOST_PORT=$(random_free_port)
SERVER=http://127.0.0.1:$HOST_PORT
# Create a test file with random content
@@ -396,7 +394,7 @@ load helpers
# Test for https://github.com/containers/podman/issues/10052
@test "podman network connect/disconnect with port forwarding" {
random_1=$(random_string 30)
- HOST_PORT=12345
+ HOST_PORT=$(random_free_port)
SERVER=http://127.0.0.1:$HOST_PORT
# Create a test file with random content
diff --git a/test/system/helpers.bash b/test/system/helpers.bash
index bd9471ace..28ea924bb 100644
--- a/test/system/helpers.bash
+++ b/test/system/helpers.bash
@@ -278,6 +278,23 @@ function wait_for_ready {
wait_for_output 'READY' "$@"
}
+######################
+# random_free_port # Pick an available port within a specified range
+######################
+function random_free_port() {
+ local range=${1:-5000-5999}
+
+ local port
+ for port in $(shuf -i ${range}); do
+ if ! { exec {unused_fd}<> /dev/tcp/127.0.0.1/$port; } &>/dev/null; then
+ echo $port
+ return
+ fi
+ done
+
+ die "Could not find open port in range $range"
+}
+
###################
# wait_for_port # Returns once port is available on host
###################
@@ -288,7 +305,7 @@ function wait_for_port() {
# Wait
while [ $_timeout -gt 0 ]; do
- { exec 5<> /dev/tcp/$host/$port; } &>/dev/null && return
+ { exec {unused_fd}<> /dev/tcp/$host/$port; } &>/dev/null && return
sleep 1
_timeout=$(( $_timeout - 1 ))
done
diff --git a/test/system/helpers.t b/test/system/helpers.t
index 190e8ba35..b83d9a89b 100755
--- a/test/system/helpers.t
+++ b/test/system/helpers.t
@@ -213,8 +213,16 @@ declare -a lines=(
)
check_same_dev "zero-line output"
-
# END remove_same_dev_warning
###############################################################################
+# BEGIN random_free_port
+
+# Assumes that 16700 is open
+found=$(random_free_port 16700-16700)
+
+check_result "$found" "16700" "random_free_port"
+
+# END random_free_port
+###############################################################################
exit $rc