diff --git a/data/helpers.d/network b/data/helpers.d/network index 702757534..2011d502b 100644 --- a/data/helpers.d/network +++ b/data/helpers.d/network @@ -20,7 +20,7 @@ ynh_find_port () { test -n "$port" || ynh_die --message="The argument of ynh_find_port must be a valid port." while ! ynh_port_available --port=$port do - port=$((port+1)) # Else, pass to next port + port=$((port+1)) done echo $port } @@ -42,7 +42,12 @@ ynh_port_available () { # Manage arguments with getopts ynh_handle_getopts_args "$@" - if ss --numeric --listening --tcp --udp | awk '{print$5}' | grep --quiet --extended-regexp ":$port$" # Check if the port is free + # Check if the port is free + if ss --numeric --listening --tcp --udp | awk '{print$5}' | grep --quiet --extended-regexp ":$port$" + then + return 1 + # This is to cover (most) case where an app is using a port yet ain't currently using it for some reason (typically service ain't up) + elif grep -q "port: '$port'" /etc/yunohost/apps/*/settings.yml then return 1 else diff --git a/tests/test_helpers.d/ynhtest_network.sh b/tests/test_helpers.d/ynhtest_network.sh new file mode 100644 index 000000000..c1644fc15 --- /dev/null +++ b/tests/test_helpers.d/ynhtest_network.sh @@ -0,0 +1,22 @@ +ynhtest_port_80_aint_available() { + ! ynh_port_available 80 +} + +ynhtest_port_12345_is_available() { + ynh_port_available 12345 +} + +ynhtest_port_12345_is_booked_by_other_app() { + + ynh_port_available 12345 + ynh_port_available 12346 + + mkdir -p /etc/yunohost/apps/block_port/ + echo "port: '12345'" > /etc/yunohost/apps/block_port/settings.yml + ! ynh_port_available 12345 + + echo "other_port: '12346'" > /etc/yunohost/apps/block_port/settings.yml + ! ynh_port_available 12346 + + rm -rf /etc/yunohost/apps/block_port +}