port forward range test: fix many oopses

Wrong variable. And, wrong index range. And, wrong bash
syntax for extracting end_port. And, add explicit check
for valid range, because die() inside 'foo=$(...)' will not
actually die. And, refactor some confusing code. And,
reformat/clean up a confusing and too-wide comment.

Fixes: #14854

Signed-off-by: Ed Santiago <santiago@redhat.com>
This commit is contained in:
Ed Santiago
2022-07-07 08:02:53 -06:00
parent dd0418a5fe
commit e8d2d70ee2
2 changed files with 16 additions and 10 deletions

View File

@ -677,16 +677,20 @@ EOF
@test "podman run port forward range" { @test "podman run port forward range" {
for netmode in bridge slirp4netns:port_handler=slirp4netns slirp4netns:port_handler=rootlesskit; do for netmode in bridge slirp4netns:port_handler=slirp4netns slirp4netns:port_handler=rootlesskit; do
local range=$(random_free_port_range 3) local range=$(random_free_port_range 3)
local port="${test%-*}" # die() inside $(...) does not actually stop us.
local end_port="${test#-*}" assert "$range" != "" "Could not find free port range"
local port="${range%-*}"
local end_port="${range#*-}"
local random=$(random_string) local random=$(random_string)
run_podman run --network $netmode -p "$range:$range" -d $IMAGE sleep inf run_podman run --network $netmode -p "$range:$range" -d $IMAGE sleep inf
cid="$output" cid="$output"
for port in $(seq $port $end_port); do for port in $(seq $port $end_port); do
run_podman exec -d $cid nc -l -p $port -e /bin/cat run_podman exec -d $cid nc -l -p $port -e /bin/cat
# -w 1 adds a 1 second timeout, for some reason ubuntus ncat doesn't close the connection on EOF, # -w 1 adds a 1 second timeout. For some reason, ubuntu's ncat
# other options to change this are not portable across distros but -w seems to work # doesn't close the connection on EOF, and other options to
# change this are not portable across distros. -w seems to work.
run nc -w 1 127.0.0.1 $port <<<$random run nc -w 1 127.0.0.1 $port <<<$random
is "$output" "$random" "ncat got data back (netmode=$netmode port=$port)" is "$output" "$random" "ncat got data back (netmode=$netmode port=$port)"
done done

View File

@ -299,15 +299,17 @@ function random_free_port_range() {
local maxtries=10 local maxtries=10
while [[ $maxtries -gt 0 ]]; do while [[ $maxtries -gt 0 ]]; do
local firstport=$(random_free_port) local firstport=$(random_free_port)
local all_ports_free=1 local lastport=
for i in $(seq 2 $size); do for i in $(seq 1 $((size - 1))); do
if ! port_is_free $((firstport + $i)); then lastport=$((firstport + i))
all_ports_free= if ! port_is_free $lastport; then
echo "# port $lastport is in use; trying another." >&3
lastport=
break break
fi fi
done done
if [[ -n "$all_ports_free" ]]; then if [[ -n "$lastport" ]]; then
echo "$firstport-$((firstport + $size - 1))" echo "$firstport-$lastport"
return return
fi fi