Skip to content

Commit d68ff8a

Browse files
Guillaume NaultPaolo Abeni
authored andcommitted
selftests/net: l2_tos_ttl_inherit.sh: Ensure environment cleanup on failure.
Use 'set -e' and an exit handler to stop the script if a command fails and ensure the test environment is cleaned up in any case. Also, handle the case where the script is interrupted by SIGINT. The only command that's expected to fail is 'wait $ping_pid', since it's killed by the script. Handle this case with '|| true' to make it play well with 'set -e'. Finally, return the Kselftest SKIP code (4) when the script breaks because of an environment problem or a command line failure. The 0 and 1 return codes should now reliably indicate that all tests have been run (0: all tests run and passed, 1: all tests run but at least one failed, 4: test script didn't run completely). Fixes: b690842 ("selftests/net: test l2 tunnel TOS/TTL inheriting") Reported-by: Mirsad Goran Todorovac <[email protected]> Tested-by: Mirsad Goran Todorovac <[email protected]> Signed-off-by: Guillaume Nault <[email protected]> Signed-off-by: Paolo Abeni <[email protected]>
1 parent c53cb00 commit d68ff8a

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

tools/testing/selftests/net/l2_tos_ttl_inherit.sh

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
# In addition this script also checks if forcing a specific field in the
1313
# outer header is working.
1414

15+
# Return 4 by default (Kselftest SKIP code)
16+
ERR=4
17+
1518
if [ "$(id -u)" != "0" ]; then
1619
echo "Please run as root."
17-
exit 0
20+
exit $ERR
1821
fi
1922
if ! which tcpdump > /dev/null 2>&1; then
2023
echo "No tcpdump found. Required for this test."
21-
exit 0
24+
exit $ERR
2225
fi
2326

2427
expected_tos="0x00"
@@ -340,7 +343,7 @@ verify() {
340343
fi
341344
fi
342345
kill -9 $ping_pid
343-
wait $ping_pid 2>/dev/null
346+
wait $ping_pid 2>/dev/null || true
344347
result="FAIL"
345348
if [ "$outer" = "4" ]; then
346349
captured_ttl="$(get_field "ttl" "$out")"
@@ -380,6 +383,31 @@ cleanup() {
380383
ip netns del "${NS1}" 2>/dev/null
381384
}
382385

386+
exit_handler() {
387+
# Don't exit immediately if one of the intermediate commands fails.
388+
# We might be called at the end of the script, when the network
389+
# namespaces have already been deleted. So cleanup() may fail, but we
390+
# still need to run until 'exit $ERR' or the script won't return the
391+
# correct error code.
392+
set +e
393+
394+
cleanup
395+
396+
exit $ERR
397+
}
398+
399+
# Restore the default SIGINT handler (just in case) and exit.
400+
# The exit handler will take care of cleaning everything up.
401+
interrupted() {
402+
trap - INT
403+
404+
exit $ERR
405+
}
406+
407+
set -e
408+
trap exit_handler EXIT
409+
trap interrupted INT
410+
383411
printf "┌────────┬───────┬───────┬──────────────┬"
384412
printf "──────────────┬───────┬────────┐\n"
385413
for type in gre vxlan geneve; do
@@ -409,6 +437,10 @@ done
409437
printf "└────────┴───────┴───────┴──────────────┴"
410438
printf "──────────────┴───────┴────────┘\n"
411439

440+
# All tests done.
441+
# Set ERR appropriately: it will be returned by the exit handler.
412442
if $failed; then
413-
exit 1
443+
ERR=1
444+
else
445+
ERR=0
414446
fi

0 commit comments

Comments
 (0)