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: 2d0c1d27ea4e ("selftests: mptcp: add mptcp_lib_check_output helper")
Cc: [email protected]
Reviewed-by: Mat Martineau <[email protected]>
Signed-off-by: Matthieu Baerts (NGI0) <[email protected]>
---
Cc: Shuah Khan <[email protected]>
Cc: [email protected]
---
 tools/testing/selftests/net/mptcp/mptcp_lib.sh  | 16 ++++++++++------
 tools/testing/selftests/net/mptcp/pm_netlink.sh | 10 ++++++----
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/tools/testing/selftests/net/mptcp/mptcp_lib.sh 
b/tools/testing/selftests/net/mptcp/mptcp_lib.sh
index 5fea7e7df628..989a5975dcea 100644
--- a/tools/testing/selftests/net/mptcp/mptcp_lib.sh
+++ b/tools/testing/selftests/net/mptcp/mptcp_lib.sh
@@ -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"
-               cat "${err}"
+       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
diff --git a/tools/testing/selftests/net/mptcp/pm_netlink.sh 
b/tools/testing/selftests/net/mptcp/pm_netlink.sh
index 123d9d7a0278..b69f30fcb91e 100755
--- a/tools/testing/selftests/net/mptcp/pm_netlink.sh
+++ b/tools/testing/selftests/net/mptcp/pm_netlink.sh
@@ -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

-- 
2.53.0


Reply via email to