Make the test suite runners exit 77 when requiring root and running as regular user, exit 99 for internal errors (unrelated to test cases) and exit 1 (or any free non-zero value) to indicate test failures. Signed-off-by: Phil Sutter --- tests/monitor/run-tests.sh | 11 ++++------- tests/py/nft-test.py | 12 +++++++----- tests/shell/run-tests.sh | 2 +- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/tests/monitor/run-tests.sh b/tests/monitor/run-tests.sh index 67d3e618cee07..969afe249201b 100755 --- a/tests/monitor/run-tests.sh +++ b/tests/monitor/run-tests.sh @@ -12,18 +12,15 @@ err() { echo "$*" >&2 } -die() { - err "$*" - exit 1 -} - if [ "$(id -u)" != "0" ] ; then - die "this requires root!" + err "this requires root!" + exit 77 fi testdir=$(mktemp -d) if [ ! -d $testdir ]; then - die "Failed to create test directory" + err "Failed to create test directory" + exit 99 fi trap 'rm -rf $testdir; $nft flush ruleset' EXIT diff --git a/tests/py/nft-test.py b/tests/py/nft-test.py index 984f2b937a077..78f3fa9b27df7 100755 --- a/tests/py/nft-test.py +++ b/tests/py/nft-test.py @@ -1519,7 +1519,7 @@ def set_delete_elements(set_element, set_name, table, filename=None, if os.getuid() != 0: print("You need to be root to run this, sorry") - return + return 77 if not args.no_netns and not spawn_netns(): print_warning("cannot run in own namespace, connectivity might break") @@ -1538,11 +1538,11 @@ def set_delete_elements(set_element, set_name, table, filename=None, if check_lib_path and not os.path.exists(args.library): print("The nftables library at '%s' does not exist. " "You need to build the project." % args.library) - return + return 99 if args.enable_schema and not args.enable_json: print_error("Option --schema requires option --json") - return + return 99 global nftables nftables = Nftables(sofile = args.library) @@ -1555,7 +1555,7 @@ def set_delete_elements(set_element, set_name, table, filename=None, print_info("Log will be available at %s" % LOGFILE) except IOError: print_error("Cannot open log file %s" % LOGFILE) - return + return 99 file_list = [] if args.filenames: @@ -1601,5 +1601,7 @@ def set_delete_elements(set_element, set_name, table, filename=None, print("%d test files, %d files passed, %d unit tests, " % (test_files, files_ok, tests)) print("%d error, %d warning" % (errors, warnings)) + return errors != 0 + if __name__ == '__main__': - main() + sys.exit(main()) diff --git a/tests/shell/run-tests.sh b/tests/shell/run-tests.sh index 2d2e0ad146c80..46f523b962b13 100755 --- a/tests/shell/run-tests.sh +++ b/tests/shell/run-tests.sh @@ -96,7 +96,7 @@ _msg() { printf '%s\n' "$level: $*" fi if [ "$level" = E ] ; then - exit 1 + exit 99 fi } -- 2.49.0 Detect RUN_FULL_TESTSUITE env variable set by automake and do an "unattended" full testrun. Signed-off-by: Phil Sutter --- tests/monitor/run-tests.sh | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/tests/monitor/run-tests.sh b/tests/monitor/run-tests.sh index 969afe249201b..b8589344a9732 100755 --- a/tests/monitor/run-tests.sh +++ b/tests/monitor/run-tests.sh @@ -58,7 +58,7 @@ json_output_filter() { # (filename) monitor_run_test() { monitor_output=$(mktemp -p $testdir) monitor_args="" - $test_json && monitor_args="vm json" + $json_mode && monitor_args="vm json" local rc=0 $nft -nn monitor $monitor_args >$monitor_output & @@ -77,7 +77,7 @@ monitor_run_test() { sleep 0.5 kill $monitor_pid wait >/dev/null 2>&1 - $test_json && json_output_filter $monitor_output + $json_mode && json_output_filter $monitor_output mydiff -q $monitor_output $output_file >/dev/null 2>&1 if [[ $rc == 0 && $? != 0 ]]; then err "monitor output differs!" @@ -156,20 +156,29 @@ while [ -n "$1" ]; do esac done -if $test_json; then - variants="monitor" +if [[ $RUN_FULL_TESTSUITE == 1 ]]; then + variants="monitor_json monitor echo" +elif $test_json; then + variants="monitor_json" else variants="monitor echo" fi rc=0 for variant in $variants; do + orig_variant=$variant + if [[ $variant =~ .*_json ]]; then + variant=${variant%_json} + json_mode=true + else + json_mode=false + fi run_test=${variant}_run_test output_append=${variant}_output_append for testcase in ${testcases:-testcases/*.t}; do filename=$(basename $testcase) - echo "$variant: running tests from file $filename" + echo "$orig_variant: running tests from file $filename" rc_start=$rc # files are like this: @@ -194,11 +203,11 @@ for variant in $variants; do ;; O) input_complete=true - $test_json || $output_append "$line" + $json_mode || $output_append "$line" ;; J) input_complete=true - $test_json && $output_append "$line" + $json_mode && $output_append "$line" ;; '#'|'') # ignore comments and empty lines -- 2.49.0 Automake is supposed to set this for a full testrun. Signed-off-by: Phil Sutter --- tests/py/nft-test.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/py/nft-test.py b/tests/py/nft-test.py index 78f3fa9b27df7..52be394c1975a 100755 --- a/tests/py/nft-test.py +++ b/tests/py/nft-test.py @@ -1517,6 +1517,13 @@ def set_delete_elements(set_element, set_name, table, filename=None, signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) + try: + if os.environ["RUN_FULL_TESTSUITE"] != 0: + enable_json_option = True + enable_json_schema = True + except KeyError: + pass + if os.getuid() != 0: print("You need to be root to run this, sorry") return 77 -- 2.49.0 Signed-off-by: Phil Sutter --- tests/json_echo/run-test.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/json_echo/run-test.py b/tests/json_echo/run-test.py index a6bdfc61afd7b..a3085b35ade6b 100755 --- a/tests/json_echo/run-test.py +++ b/tests/json_echo/run-test.py @@ -6,6 +6,10 @@ import os import json import argparse +if os.getuid() != 0: + print("You need to be root to run this, sorry") + sys.exit(77) + TESTS_PATH = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(TESTS_PATH, '../../py/')) -- 2.49.0 The script relies upon a call to modprobe which does not work in fake root environments. Signed-off-by: Phil Sutter --- tests/shell/testcases/packetpath/nat_ftp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/shell/testcases/packetpath/nat_ftp b/tests/shell/testcases/packetpath/nat_ftp index c2fb3a1c8ebcd..d0faf2ef59c57 100755 --- a/tests/shell/testcases/packetpath/nat_ftp +++ b/tests/shell/testcases/packetpath/nat_ftp @@ -4,6 +4,9 @@ # NFT_TEST_REQUIRES(NFT_TEST_HAVE_curl) # NFT_TEST_REQUIRES(NFT_TEST_HAVE_vsftpd) +# modprobe does not work in fake root env +[ "$NFT_TEST_HAS_REALROOT" != y ] && exit 77 + . $NFT_TEST_LIBRARY_FILE cleanup() -- 2.49.0 Add the various testsuite runners to TESTS variable and have make call them with RUN_FULL_TESTSUITE=1 env var. Signed-off-by: Phil Sutter --- Makefile.am | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Makefile.am b/Makefile.am index ba09e7f0953d5..4fb75b85a5d59 100644 --- a/Makefile.am +++ b/Makefile.am @@ -409,5 +409,11 @@ EXTRA_DIST += \ tests \ $(NULL) +AM_TESTS_ENVIRONMENT = RUN_FULL_TESTSUITE=1; export RUN_FULL_TESTSUITE; +TESTS = tests/json_echo/run-test.py \ + tests/monitor/run-tests.sh \ + tests/py/nft-test.py \ + tests/shell/run-tests.sh + pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libnftables.pc -- 2.49.0