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>
This commit is contained in:
Ed Santiago 2021-09-08 10:57:24 -06:00
parent d68e429859
commit 1ff797e362
6 changed files with 38 additions and 32 deletions

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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