Commit 65db7b27 authored by Matthieu Baerts (NGI0)'s avatar Matthieu Baerts (NGI0) Committed by Jakub Kicinski
Browse files

selftests: mptcp: check output: catch cmd errors



Using '${?}' inside the if-statement to check the returned value from
the command that was evaluated as part of the if-statement is not
correct: here, '${?}' will be linked to the previous instruction, not
the one that is expected here (${cmd}).

Instead, simply mark the error, except if an error is expected. If
that's the case, 1 can be passed as the 4th argument of this helper.
Three checks from pm_netlink.sh expect an error.

While at it, improve the error message when the command unexpectedly
fails or succeeds.

Note that we could expect a specific returned value, but the checks
currently expecting an error can be used with 'ip mptcp' or 'pm_nl_ctl',
and these two tools don't return the same error code.

Fixes: 2d0c1d27 ("selftests: mptcp: add mptcp_lib_check_output helper")
Cc: stable@vger.kernel.org
Reviewed-by: default avatarMat Martineau <martineau@kernel.org>
Signed-off-by: default avatarMatthieu Baerts (NGI0) <matttbe@kernel.org>
Link: https://patch.msgid.link/20260505-net-mptcp-pm-fixes-7-1-rc3-v1-10-fca8091060a4@kernel.org


Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 166b7834
Loading
Loading
Loading
Loading
+10 −6
Original line number Diff line number Diff line
@@ -474,20 +474,24 @@ mptcp_lib_wait_local_port_listen() {
	wait_local_port_listen "${@}" "tcp"
}

# $1: error file, $2: cmd, $3: expected msg, [$4: expected error]
mptcp_lib_check_output() {
	local err="${1}"
	local cmd="${2}"
	local expected="${3}"
	local exp_error="${4:-0}"
	local cmd_ret=0
	local out

	if ! out=$(${cmd} 2>"${err}"); then
		cmd_ret=${?}
	fi
	out=$(${cmd} 2>"${err}") || cmd_ret=1

	if [ ${cmd_ret} -ne 0 ]; then
		mptcp_lib_pr_fail "command execution '${cmd}' stderr"
	if [ "${cmd_ret}" != "${exp_error}" ]; then
		mptcp_lib_pr_fail "unexpected returned code for '${cmd}', info:"
		if [ "${exp_error}" = 0 ]; then
			cat "${err}"
		else
			echo "${out}"
		fi
		return 2
	elif [ "${out}" = "${expected}" ]; then
		return 0
+6 −4
Original line number Diff line number Diff line
@@ -122,10 +122,12 @@ check()
	local cmd="$1"
	local expected="$2"
	local msg="$3"
	local exp_error="$4"
	local rc=0

	mptcp_lib_print_title "$msg"
	mptcp_lib_check_output "${err}" "${cmd}" "${expected}" || rc=${?}
	mptcp_lib_check_output "${err}" "${cmd}" "${expected}" "${exp_error}" ||
		rc=${?}
	if [ ${rc} -eq 2 ]; then
		mptcp_lib_result_fail "${msg} # error ${rc}"
		ret=${KSFT_FAIL}
@@ -158,13 +160,13 @@ check "show_endpoints" \
			    "3,10.0.1.3,signal backup")" "dump addrs"

del_endpoint 2
check "get_endpoint 2" "" "simple del addr"
check "get_endpoint 2" "" "simple del addr" 1
check "show_endpoints" \
	"$(format_endpoints "1,10.0.1.1" \
			    "3,10.0.1.3,signal backup")" "dump addrs after del"

add_endpoint 10.0.1.3 2>/dev/null
check "get_endpoint 4" "" "duplicate addr"
check "get_endpoint 4" "" "duplicate addr" 1

add_endpoint 10.0.1.4 flags signal
check "get_endpoint 4" "$(format_endpoints "4,10.0.1.4,signal")" "id addr increment"
@@ -173,7 +175,7 @@ for i in $(seq 5 9); do
	add_endpoint "10.0.1.${i}" flags signal >/dev/null 2>&1
done
check "get_endpoint 9" "$(format_endpoints "9,10.0.1.9,signal")" "hard addr limit"
check "get_endpoint 10" "" "above hard addr limit"
check "get_endpoint 10" "" "above hard addr limit" 1

del_endpoint 9
for i in $(seq 10 255); do