selftests: net: lib: Move logging from forwarding/lib.sh here

Many net selftests invent their own logging helpers. These really should be
in a library sourced by these tests. Currently forwarding/lib.sh has a
suite of perfectly fine logging helpers, but sourcing a forwarding/ library
from a higher-level directory smells of layering violation. In this patch,
move the logging helpers to net/lib.sh so that every net test can use them.

Together with the logging helpers, it's also necessary to move
pause_on_fail(), and EXIT_STATUS and RET.

Existing lib.sh users might be using these same names for their functions
or variables. However lib.sh is always sourced near the top of the
file (checked), and whatever new definitions will simply override the ones
provided by lib.sh.

Signed-off-by: Petr Machata <petrm@nvidia.com>
Reviewed-by: Amit Cohen <amcohen@nvidia.com>
Acked-by: Shuah Khan <skhan@linuxfoundation.org>
Link: https://patch.msgid.link/edd3785a3bd72ffbe1409300989e993ee50ae98b.1731589511.git.petrm@nvidia.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Petr Machata 2024-11-14 15:09:55 +01:00 committed by Jakub Kicinski
parent 42575ad5aa
commit b219bcfcc9
2 changed files with 115 additions and 113 deletions

View File

@ -48,7 +48,6 @@ declare -A NETIFS=(
: "${WAIT_TIME:=5}"
# Whether to pause on, respectively, after a failure and before cleanup.
: "${PAUSE_ON_FAIL:=no}"
: "${PAUSE_ON_CLEANUP:=no}"
# Whether to create virtual interfaces, and what netdevice type they should be.
@ -446,22 +445,6 @@ done
##############################################################################
# Helpers
# Exit status to return at the end. Set in case one of the tests fails.
EXIT_STATUS=0
# Per-test return value. Clear at the beginning of each test.
RET=0
ret_set_ksft_status()
{
local ksft_status=$1; shift
local msg=$1; shift
RET=$(ksft_status_merge $RET $ksft_status)
if (( $? )); then
retmsg=$msg
fi
}
# Whether FAILs should be interpreted as XFAILs. Internal.
FAIL_TO_XFAIL=
@ -535,102 +518,6 @@ xfail_on_veth()
fi
}
log_test_result()
{
local test_name=$1; shift
local opt_str=$1; shift
local result=$1; shift
local retmsg=$1; shift
printf "TEST: %-60s [%s]\n" "$test_name $opt_str" "$result"
if [[ $retmsg ]]; then
printf "\t%s\n" "$retmsg"
fi
}
pause_on_fail()
{
if [[ $PAUSE_ON_FAIL == yes ]]; then
echo "Hit enter to continue, 'q' to quit"
read a
[[ $a == q ]] && exit 1
fi
}
handle_test_result_pass()
{
local test_name=$1; shift
local opt_str=$1; shift
log_test_result "$test_name" "$opt_str" " OK "
}
handle_test_result_fail()
{
local test_name=$1; shift
local opt_str=$1; shift
log_test_result "$test_name" "$opt_str" FAIL "$retmsg"
pause_on_fail
}
handle_test_result_xfail()
{
local test_name=$1; shift
local opt_str=$1; shift
log_test_result "$test_name" "$opt_str" XFAIL "$retmsg"
pause_on_fail
}
handle_test_result_skip()
{
local test_name=$1; shift
local opt_str=$1; shift
log_test_result "$test_name" "$opt_str" SKIP "$retmsg"
}
log_test()
{
local test_name=$1
local opt_str=$2
if [[ $# -eq 2 ]]; then
opt_str="($opt_str)"
fi
if ((RET == ksft_pass)); then
handle_test_result_pass "$test_name" "$opt_str"
elif ((RET == ksft_xfail)); then
handle_test_result_xfail "$test_name" "$opt_str"
elif ((RET == ksft_skip)); then
handle_test_result_skip "$test_name" "$opt_str"
else
handle_test_result_fail "$test_name" "$opt_str"
fi
EXIT_STATUS=$(ksft_exit_status_merge $EXIT_STATUS $RET)
return $RET
}
log_test_skip()
{
RET=$ksft_skip retmsg= log_test "$@"
}
log_test_xfail()
{
RET=$ksft_xfail retmsg= log_test "$@"
}
log_info()
{
local msg=$1
echo "INFO: $msg"
}
not()
{
"$@"

View File

@ -9,6 +9,9 @@ source "$net_dir/lib/sh/defer.sh"
: "${WAIT_TIMEOUT:=20}"
# Whether to pause on after a failure.
: "${PAUSE_ON_FAIL:=no}"
BUSYWAIT_TIMEOUT=$((WAIT_TIMEOUT * 1000)) # ms
# Kselftest framework constants.
@ -20,6 +23,11 @@ ksft_skip=4
# namespace list created by setup_ns
NS_LIST=()
# Exit status to return at the end. Set in case one of the tests fails.
EXIT_STATUS=0
# Per-test return value. Clear at the beginning of each test.
RET=0
##############################################################################
# Helpers
@ -236,3 +244,110 @@ tc_rule_handle_stats_get()
| jq ".[] | select(.options.handle == $handle) | \
.options.actions[0].stats$selector"
}
ret_set_ksft_status()
{
local ksft_status=$1; shift
local msg=$1; shift
RET=$(ksft_status_merge $RET $ksft_status)
if (( $? )); then
retmsg=$msg
fi
}
log_test_result()
{
local test_name=$1; shift
local opt_str=$1; shift
local result=$1; shift
local retmsg=$1; shift
printf "TEST: %-60s [%s]\n" "$test_name $opt_str" "$result"
if [[ $retmsg ]]; then
printf "\t%s\n" "$retmsg"
fi
}
pause_on_fail()
{
if [[ $PAUSE_ON_FAIL == yes ]]; then
echo "Hit enter to continue, 'q' to quit"
read a
[[ $a == q ]] && exit 1
fi
}
handle_test_result_pass()
{
local test_name=$1; shift
local opt_str=$1; shift
log_test_result "$test_name" "$opt_str" " OK "
}
handle_test_result_fail()
{
local test_name=$1; shift
local opt_str=$1; shift
log_test_result "$test_name" "$opt_str" FAIL "$retmsg"
pause_on_fail
}
handle_test_result_xfail()
{
local test_name=$1; shift
local opt_str=$1; shift
log_test_result "$test_name" "$opt_str" XFAIL "$retmsg"
pause_on_fail
}
handle_test_result_skip()
{
local test_name=$1; shift
local opt_str=$1; shift
log_test_result "$test_name" "$opt_str" SKIP "$retmsg"
}
log_test()
{
local test_name=$1
local opt_str=$2
if [[ $# -eq 2 ]]; then
opt_str="($opt_str)"
fi
if ((RET == ksft_pass)); then
handle_test_result_pass "$test_name" "$opt_str"
elif ((RET == ksft_xfail)); then
handle_test_result_xfail "$test_name" "$opt_str"
elif ((RET == ksft_skip)); then
handle_test_result_skip "$test_name" "$opt_str"
else
handle_test_result_fail "$test_name" "$opt_str"
fi
EXIT_STATUS=$(ksft_exit_status_merge $EXIT_STATUS $RET)
return $RET
}
log_test_skip()
{
RET=$ksft_skip retmsg= log_test "$@"
}
log_test_xfail()
{
RET=$ksft_xfail retmsg= log_test "$@"
}
log_info()
{
local msg=$1
echo "INFO: $msg"
}