Skip to content

Commit

Permalink
Merge pull request #11486 from edsantiago/bats
Browse files Browse the repository at this point in the history
system tests: new random_free_port helper
  • Loading branch information
openshift-merge-robot authored Sep 8, 2021
2 parents 400799b + 1ff797e commit 26c8549
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 32 deletions.
7 changes: 1 addition & 6 deletions test/system/150-login.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 3 additions & 15 deletions test/system/200-pod.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions test/system/271-tcp-cors-server.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 5 additions & 7 deletions test/system/500-networking.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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\..*"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion test/system/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
###################
Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion test/system/helpers.t
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 26c8549

Please sign in to comment.