selftests: Move test output to diagnostic lines
keescook at chromium.org (Kees Cook)
cover selftests: Move test output to diagnostic lines
This refactors the selftest Makefiles to extract the test running logic to be reused between "run_tests" and "emit_tests", while also fixing up the test output to be TAP version 13 compliant:
- added "plan" line
- fixed result line syntax
- moved all test output to be "# "-prefixed as TAP "diagnostic" lines
The prefixing code includes a fallback mode for limited execution environments.
-Kees
Kees Cook (6):
selftests: Extract single-test shell logic from lib.mk
selftests: Use runner.sh for emit targets
selftests: Extract logic for multiple test runs selftests/runner: Add plan line and fix result line syntax selftests/runner: Distinguish between missing and non-executable
selftests: Move test output to diagnostic lines
tools/testing/selftests/.gitignore | 1 - tools/testing/selftests/Makefile | 18 +++-- tools/testing/selftests/kselftest/prefix.pl | 23 ++++++ tools/testing/selftests/kselftest/runner.sh | 80 +++++++++++++++++++++ tools/testing/selftests/lib.mk | 61 +++------------- 5 files changed, 119 insertions(+), 64 deletions(-) create mode 100755 tools/testing/selftests/kselftest/prefix.pl create mode 100644 tools/testing/selftests/kselftest/runner.sh
Signature · 1 line
2.17.1
keescook at chromium.org (Kees Cook)
PATCH 1/6 selftests: Extract single-test shell logic from lib.mk
In order to improve the reusability of the kselftest test running logic, this extracts the single-test logic from lib.mk into kselftest/runner.sh which lib.mk can call directly. No changes in output.
As part of the change, this removes the unused "summary" Makefile variable (and tests). However, future merging with the "emit_tests" target needs to be able to redirect output, so a new "logfile" variable is introduced.
--- tools/testing/selftests/.gitignore | 1 - tools/testing/selftests/kselftest/runner.sh | 31 +++++++++++++++++++ tools/testing/selftests/lib.mk | 33 ++------------------- 3 files changed, 34 insertions(+), 31 deletions(-) create mode 100644 tools/testing/selftests/kselftest/runner.sh
tools/testing/selftests/.gitignore +0 -1
@@ -1,4 +1,3 @@ -kselftest gpiogpio-event-mon gpiogpio-hammer gpioinclude/
tools/testing/selftests/kselftest/runner.sh new file +31 -0
@@ -0,0 +1,31 @@ +#!/bin/sh +# +# Runs a set of tests in a given subdirectory. +export skip_rc=4 +export logfile=/dev/stdout + +run_one() +{ + TEST="$1" + NUM="$2" + + BASENAME_TEST=$(basename $TEST) + + TEST_HDR_MSG="selftests: "`basename $PWD`:" $BASENAME_TEST" + echo "$TEST_HDR_MSG" + echo "========================================" + if [ ! -x "$TEST" ]; then + echo "$TEST_HDR_MSG: Warning: file $TEST is not executable, correct this." + echo "not ok 1..$test_num $TEST_HDR_MSG [FAIL]" + else + cd `dirname $TEST` > /dev/null + (./$BASENAME_TEST >> "$logfile" 2>&1 && + echo "ok 1..$test_num $TEST_HDR_MSG [PASS]") || + (if [ $? -eq $skip_rc ]; then \ + echo "not ok 1..$test_num $TEST_HDR_MSG [SKIP]" + else + echo "not ok 1..$test_num $TEST_HDR_MSG [FAIL]" + fi) + cd - >/dev/null + fi +}
tools/testing/selftests/lib.mk +3 -30
@@ -5,6 +5,7 @@ CC := $(CROSS_COMPILE)gcc ifeq (0,$(MAKELEVEL)) OUTPUT := $(shell pwd) endif +selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST)))) # The following are built by lib.mk common compile rules. # TEST_CUSTOM_PROGS should be used by tests that require
@@ -31,43 +32,15 @@ all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) endif .ONESHELL: -define RUN_TEST_PRINT_RESULT - TEST_HDR_MSG="selftests: "`basename $$PWD`:" $$BASENAME_TEST"; \ - echo $$TEST_HDR_MSG; \ - echo "========================================"; \ - if [ ! -x $$TEST ]; then \ - echo "$$TEST_HDR_MSG: Warning: file $$BASENAME_TEST is not executable, correct this.";\ - echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]"; \ - else \ - cd `dirname $$TEST` > /dev/null; \ - if [ "X$(summary)" != "X" ]; then \ - (./$$BASENAME_TEST > /tmp/$$BASENAME_TEST 2>&1 && \ - echo "ok 1..$$test_num $$TEST_HDR_MSG [PASS]") || \ - (if [ $$? -eq $$skip ]; then \ - echo "not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]"; \ - else echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]"; \ - fi;) \ - else \ - (./$$BASENAME_TEST && \ - echo "ok 1..$$test_num $$TEST_HDR_MSG [PASS]") || \ - (if [ $$? -eq $$skip ]; then \ - echo "not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]"; \ - else echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]"; \ - fi;) \ - fi; \ - cd - > /dev/null; \ - fi; -endef - define RUN_TESTS @export KSFT_TAP_LEVEL=`echo 1`; \ test_num=`echo 0`; \ - skip=`echo 4`; \ + . $(selfdir)/kselftest/runner.sh; \ echo "TAP version 13"; \ for TEST in $(1); do \ BASENAME_TEST=`basename $$TEST`; \ test_num=`echo $$test_num+1 | bc`; \ - $(call RUN_TEST_PRINT_RESULT,$(TEST),$(BASENAME_TEST),$(test_num),$(skip)) \ + run_one "$$BASENAME_TEST" "$$test_num"; \ done; endef
Signature · 1 line
2.17.1
shuah at kernel.org (shuah)
PATCH 1/6 selftests: Extract single-test shell logic from lib.mk
Hi Kees,
Thanks for the patch.
Quoted 7 lines · Kees Cook
In order to improve the reusability of the kselftest test running logic, this extracts the single-test logic from lib.mk into
kselftest/runner.shwhich lib.mk can call directly. No changes in output.As part of the change, this removes the unused "summary"
Makefilevariable (and tests). However, future merging with the "emit_tests" target needs to be able to redirect output, so a new "logfile" variable is introduced.
Shouldn't the selftests/Makefile need update for "summary" removal??
Quoted 14 lines
Signed-off-by Kees Cook <keescook at chromium.org>--- tools/testing/selftests/.gitignore | 1 - tools/testing/selftests/kselftest/runner.sh | 31 +++++++++++++++++++ tools/testing/selftests/lib.mk | 33 ++------------------- 3 files changed, 34 insertions(+), 31 deletions(-)create mode 100644
tools/testing/selftests/kselftest/runner.shtools/testing/selftests/.gitignore +0
-0
tools/testing/selftests/.gitignore line 3
Quoted 4 lines
tools/testing/selftests/.gitignore +0
-1@@ -1,4 +1,3 @@ -kselftest gpiogpio-event-mon gpiogpio-hammer gpioinclude/
Please don't include this .gitignore change here. These are generated in tools/gpio and this .gitignore isn't the right place for them.
Quoted 86 lines
tools/testing/selftests/kselftest/runner.sh new file +31
-0@@ -0,0 +1,31 @@ +#!/bin/sh +# +# Runs a set of tests in a given subdirectory. +export skip_rc=4 +export logfile=/dev/stdout + +run_one() +{ + TEST="$1" + NUM="$2" + + BASENAME_TEST=$(basename $TEST) + + TEST_HDR_MSG="selftests: "`basename $PWD`:" $BASENAME_TEST" + echo "$TEST_HDR_MSG" + echo "========================================" + if [ ! -x "$TEST" ]; then + echo "$TEST_HDR_MSG: Warning: file $TEST is not executable, correct this." + echo "not ok 1..$test_num $TEST_HDR_MSG [FAIL]" + else + cd `dirname $TEST` > /dev/null + (./$BASENAME_TEST >> "$logfile" 2>&1 && + echo "ok 1..$test_num $TEST_HDR_MSG [PASS]") || + (if [ $? -eq $skip_rc ]; then \ + echo "not ok 1..$test_num $TEST_HDR_MSG [SKIP]" + else + echo "not ok 1..$test_num $TEST_HDR_MSG [FAIL]" + fi) + cd - >/dev/null + fi +}tools/testing/selftests/lib.mk +1
-29@@ -5,6 +5,7 @@ CC := $(CROSS_COMPILE)gcc ifeq (0,$(MAKELEVEL)) OUTPUT := $(shell pwd) endif +selfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST)))) # The following are built by lib.mk common compile rules. # TEST_CUSTOM_PROGS should be used by tests that require@@ -31,43 +32,15 @@ all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) endif .ONESHELL: -define RUN_TEST_PRINT_RESULT - TEST_HDR_MSG="selftests: "`basename $$PWD`:" $$BASENAME_TEST"; \ - echo $$TEST_HDR_MSG; \ - echo "========================================"; \ - if [ ! -x $$TEST ]; then \ - echo "$$TEST_HDR_MSG: Warning: file $$BASENAME_TEST is not executable, correct this.";\ - echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]"; \ - else \ - cd `dirname $$TEST` > /dev/null; \ - if [ "X$(summary)" != "X" ]; then \ - (./$$BASENAME_TEST > /tmp/$$BASENAME_TEST 2>&1 && \ - echo "ok 1..$$test_num $$TEST_HDR_MSG [PASS]") || \ - (if [ $$? -eq $$skip ]; then \ - echo "not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]"; \ - else echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]"; \ - fi;) \ - else \ - (./$$BASENAME_TEST && \ - echo "ok 1..$$test_num $$TEST_HDR_MSG [PASS]") || \ - (if [ $$? -eq $$skip ]; then \ - echo "not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]"; \ - else echo "not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]"; \ - fi;) \ - fi; \ - cd - > /dev/null; \ - fi; -endef - define RUN_TESTS @export KSFT_TAP_LEVEL=`echo 1`; \ test_num=`echo 0`; \ - skip=`echo 4`; \
tools/testing/selftests/lib.mk all: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) · line 46
Quoted 10 lines
tools/testing/selftests/lib.mk +2
-1+ . $(selfdir)/kselftest/runner.sh; \ echo "TAP version 13"; \ for TEST in $(1); do \ BASENAME_TEST=`basename $$TEST`; \ test_num=`echo $$test_num+1 | bc`; \ - $(call RUN_TEST_PRINT_RESULT,$(TEST),$(BASENAME_TEST),$(test_num),$(skip)) \ + run_one "$$BASENAME_TEST" "$$test_num"; \ done; endef
thanks,
-- Shuah
keescook at chromium.org (Kees Cook)
PATCH 1/6 selftests: Extract single-test shell logic from lib.mk
Quoted 10 lines · shuah
Hi Kees,
Thanks for the patch.
Quoted 4 lines · Kees Cook
In order to improve the reusability of the kselftest test running logic, this extracts the single-test logic from lib.mk into
kselftest/runner.shwhich lib.mk can call directly. No changes in output.
Quoted 6 lines
Quoted 3 lines
As part of the change, this removes the unused "summary"
Makefilevariable (and tests). However, future merging with the "emit_tests" target needs to be able to redirect output, so a new "logfile" variable is introduced.Shouldn't the selftests/Makefile need update for "summary" removal??
I didn't see anything using "summary" except as a --summary argument to the run_kselftests.sh script. Maybe I missed it?
Quoted 9 lines
Quoted 9 lines
Signed-off-by Kees Cook <keescook at chromium.org>--- tools/testing/selftests/.gitignore | 1 - tools/testing/selftests/kselftest/runner.sh | 31 +++++++++++++++++++ tools/testing/selftests/lib.mk | 33 ++------------------- 3 files changed, 34 insertions(+), 31 deletions(-)create mode 100644
tools/testing/selftests/kselftest/runner.sh
Quoted 12 lines
tools/testing/selftests/.gitignore line 3
Quoted 9 lines
tools/testing/selftests/.gitignore +0
-1@@ -1,4 +1,3 @@ -kselftest gpiogpio-event-mon gpiogpio-hammer gpioinclude/Please don't include this .gitignore change here. These are generated in
tools/gpioand this .gitignore isn't the right place for them.
This change is only removing the "kselftest" entry, for which the target is long gone. Since I was adding a directory by that name, I needed to remove it from the .gitignore file. I have nothing to do with the gpio stuff. :)
Thanks for looking this over!
Signature · 1 line
Kees Cook
shuah at kernel.org (shuah)
PATCH 1/6 selftests: Extract single-test shell logic from lib.mk
Quoted 15 lines · Kees Cook
Quoted 14 lines · shuah
Hi Kees,
Thanks for the patch.
Quoted 7 lines · Kees Cook
In order to improve the reusability of the kselftest test running logic, this extracts the single-test logic from lib.mk into
kselftest/runner.shwhich lib.mk can call directly. No changes in output.As part of the change, this removes the unused "summary"
Makefilevariable (and tests). However, future merging with the "emit_tests" target needs to be able to redirect output, so a new "logfile" variable is introduced.
Quoted 6 lines
Quoted 2 lines
Shouldn't the selftests/Makefile need update for "summary" removal??
I didn't see anything using "summary" except as a --summary argument to the
run_kselftests.shscript. Maybe I missed it?
It is in the selftests/Makefile install target.
Quoted 22 lines
Quoted 9 lines
Quoted 9 lines
Signed-off-by Kees Cook <keescook at chromium.org>--- tools/testing/selftests/.gitignore | 1 - tools/testing/selftests/kselftest/runner.sh | 31 +++++++++++++++++++ tools/testing/selftests/lib.mk | 33 ++------------------- 3 files changed, 34 insertions(+), 31 deletions(-)create mode 100644
tools/testing/selftests/kselftest/runner.shQuoted 12 lines
tools/testing/selftests/.gitignore line 3
Quoted 9 lines
tools/testing/selftests/.gitignore +0
-1@@ -1,4 +1,3 @@ -kselftest gpiogpio-event-mon gpiogpio-hammer gpioinclude/Please don't include this .gitignore change here. These are generated in
tools/gpioand this .gitignore isn't the right place for them.
Quoted 5 lines
This change is only removing the "kselftest" entry, for which the target is long gone. Since I was adding a directory by that name, I needed to remove it from the .gitignore file. I have nothing to do with the gpio stuff. :)
Yeah my bad! :)
Quoted 2 lines
Thanks for looking this over!
thanks,
-- Shuah
keescook at chromium.org (Kees Cook)
PATCH 1/6 selftests: Extract single-test shell logic from lib.mk
Quoted 17 lines · shuah
Quoted 15 lines · Kees Cook
Quoted 14 lines · shuah
Hi Kees,
Thanks for the patch.
Quoted 7 lines · Kees Cook
In order to improve the reusability of the kselftest test running logic, this extracts the single-test logic from lib.mk into
kselftest/runner.shwhich lib.mk can call directly. No changes in output.As part of the change, this removes the unused "summary"
Makefilevariable (and tests). However, future merging with the "emit_tests" target needs to be able to redirect output, so a new "logfile" variable is introduced.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Shouldn't the selftests/Makefile need update for "summary" removal??
I didn't see anything using "summary" except as a --summary argument to the
run_kselftests.shscript. Maybe I missed it?It is in the selftests/Makefile install target.
Right: it's used only by the run_kselftest.sh script:
ALL_SCRIPT := $(INSTALL_PATH)/run_kselftest.sh
install:
...
echo "if [ \"\$$1\" = \"--summary\" ]; then" >> $(ALL_SCRIPT)
So, I think this entire series can land. Is there other feedback I should incorporate? I'd like to see it get some -next testing...
Thanks!
Signature · 1 line
Kees Cook
shuah at kernel.org (shuah)
PATCH 1/6 selftests: Extract single-test shell logic from lib.mk
Quoted 33 lines · Kees Cook
Quoted 17 lines · shuah
Quoted 15 lines · Kees Cook
Quoted 14 lines · shuah
Hi Kees,
Thanks for the patch.
Quoted 7 lines · Kees Cook
In order to improve the reusability of the kselftest test running logic, this extracts the single-test logic from lib.mk into
kselftest/runner.shwhich lib.mk can call directly. No changes in output.As part of the change, this removes the unused "summary"
Makefilevariable (and tests). However, future merging with the "emit_tests" target needs to be able to redirect output, so a new "logfile" variable is introduced.Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Shouldn't the selftests/Makefile need update for "summary" removal??
I didn't see anything using "summary" except as a --summary argument to the
run_kselftests.shscript. Maybe I missed it?It is in the selftests/Makefile install target.
Right: it's used only by the
run_kselftest.shscript:
ALL_SCRIPT:= $(INSTALL_PATH)/run_kselftest.shinstall:
...
Quoted 8 lines
echo "if [ \"\$$1\" = \"--summary\" ]; then" >> $(ALL_SCRIPT)So, I think this entire series can land. Is there other feedback I should incorporate? I'd like to see it get some -next testing...
Thanks!
Kees,
Yes I was planning to get this into next and did some testing. I found that the first patch breaks the summary option.
You can see it by running
make summary=1 TARGETS="breakpoints" kselftest
with and without your first patch. Ca you fix the regression and send me revised patches. Sorry, I didn't get back to you sooner. A few bugs I found kept me busy.
While I was testing your patch series, I found test build failures due to circular dependency that needed fixing. In addition, the same headers_install dependency, also broke O= and KBUILD_OUTPUT builds and fixed those as well. Long story short, these fixes might conflict with your work.
thanks,
-- Shuah
keescook at chromium.org (Kees Cook)
PATCH 1/6 selftests: Extract single-test shell logic from lib.mk
Quoted 3 lines · shuah
You can see it by running
make summary=1 TARGETS="breakpoints" kselftest
Okay, thanks for this! I'll give it a spin.
Quoted 9 lines
with and without your first patch. Ca you fix the regression and send me revised patches. Sorry, I didn't get back to you sooner. A few bugs I found kept me busy.
While I was testing your patch series, I found test build failures due to circular dependency that needed fixing. In addition, the same
headers_installdependency, also broke O= andKBUILD_OUTPUTbuilds and fixed those as well. Long story short, these fixes might conflict with your work.
I can rebase the series -- which tree should I look at? (Is it all in -next already?)
Thanks!
Signature · 1 line
Kees Cook
shuah at kernel.org (shuah)
PATCH 1/6 selftests: Extract single-test shell logic from lib.mk
Quoted 11 lines · Kees Cook
Quoted 3 lines · shuah
You can see it by running
make summary=1 TARGETS="breakpoints" kselftest
Okay, thanks for this! I'll give it a spin.
Quoted 4 lines
with and without your first patch. Ca you fix the regression and send me revised patches. Sorry, I didn't get back to you sooner. A few bugs I found kept me busy.
Quoted 9 lines
Quoted 5 lines
While I was testing your patch series, I found test build failures due to circular dependency that needed fixing. In addition, the same
headers_installdependency, also broke O= andKBUILD_OUTPUTbuilds and fixed those as well. Long story short, these fixes might conflict with your work.I can rebase the series -- which tree should I look at? (Is it all in -next already?)
Yes - the two patches are in linux-kselftest next
thanks,
-- Shuah
keescook at chromium.org (Kees Cook)
PATCH 2/6 selftests: Use runner.sh for emit targets
In reply to keescook at chromium.org (Kees Cook)This reuses the new runner.sh for the emit targets instead of manually running each test via run_kselftest.sh.
--- tools/testing/selftests/Makefile | 11 +++++------ tools/testing/selftests/lib.mk | 15 ++------------- 2 files changed, 7 insertions(+), 19 deletions(-)
tools/testing/selftests/Makefile +5 -6
@@ -131,7 +131,8 @@ ALL_SCRIPT := $(INSTALL_PATH)/run_kselftest.sh install: ifdef INSTALL_PATH @# Ask all targets to install their files - mkdir -p $(INSTALL_PATH) + mkdir -p $(INSTALL_PATH)/kselftest + install -m 744 kselftest/runner.sh $(INSTALL_PATH)/kselftest/ @for TARGET in $(TARGETS); do \ BUILD_TARGET=$$BUILD/$$TARGET; \ make OUTPUT=$$BUILD_TARGET -C $$TARGET INSTALL_PATH=$(INSTALL_PATH)/$$TARGET install; \
@@ -141,15 +142,13 @@ ifdef INSTALL_PATH echo "#!/bin/sh" > $(ALL_SCRIPT) echo "BASE_DIR=\$$(realpath \$$(dirname \$$0))" >> $(ALL_SCRIPT) echo "cd \$$BASE_DIR" >> $(ALL_SCRIPT) + echo ". ./kselftest/runner.sh" >> $(ALL_SCRIPT) echo "ROOT=\$$PWD" >> $(ALL_SCRIPT) echo "if [ \"\$$1\" = \"--summary\" ]; then" >> $(ALL_SCRIPT) - echo " OUTPUT=\$$BASE_DIR/output.log" >> $(ALL_SCRIPT) - echo " cat /dev/null > \$$OUTPUT" >> $(ALL_SCRIPT) - echo "else" >> $(ALL_SCRIPT) - echo " OUTPUT=/dev/stdout" >> $(ALL_SCRIPT) + echo " logfile=\$$BASE_DIR/output.log" >> $(ALL_SCRIPT) + echo " cat /dev/null > \$$logfile" >> $(ALL_SCRIPT) echo "fi" >> $(ALL_SCRIPT) echo "export KSFT_TAP_LEVEL=1" >> $(ALL_SCRIPT) - echo "export skip=4" >> $(ALL_SCRIPT) for TARGET in $(TARGETS); do \ BUILD_TARGET=$$BUILD/$$TARGET; \
tools/testing/selftests/lib.mk +2 -13
@@ -78,24 +78,13 @@ else $(error Error: set INSTALL_PATH to use install) endif -define EMIT_TESTS +emit_tests: @test_num=`echo 0`; \ for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \ BASENAME_TEST=`basename $$TEST`; \ test_num=`echo $$test_num+1 | bc`; \ - TEST_HDR_MSG="selftests: "`basename $$PWD`:" $$BASENAME_TEST"; \ - echo "echo $$TEST_HDR_MSG"; \ - if [ ! -x $$TEST ]; then \ - echo "echo \"$$TEST_HDR_MSG: Warning: file $$BASENAME_TEST is not executable, correct this.\""; \ - echo "echo \"not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]\""; \ - else - echo "(./$$BASENAME_TEST >> \$$OUTPUT 2>&1 && echo \"ok 1..$$test_num $$TEST_HDR_MSG [PASS]\") || (if [ \$$? -eq \$$skip ]; then echo \"not ok 1..$$test_num $$TEST_HDR_MSG [SKIP]\"; else echo \"not ok 1..$$test_num $$TEST_HDR_MSG [FAIL]\"; fi;)"; \ - fi; \ + echo "run_one \"$$BASENAME_TEST\" \"$$test_num\""; \ done; -endef - -emit_tests: - $(EMIT_TESTS) # define if isn't already. It is undefined in make O= case. ifeq ($(RM),)
Signature · 1 line
2.17.1
keescook at chromium.org (Kees Cook)
PATCH 3/6 selftests: Extract logic for multiple test runs
In reply to keescook at chromium.org (Kees Cook)This moves the logic for running multiple tests into a single "run_many" function of runner.sh. Both "run_tests" and "emit_tests" are modified to use it.
--- tools/testing/selftests/Makefile | 6 ++---- tools/testing/selftests/kselftest/runner.sh | 19 ++++++++++++++++--- tools/testing/selftests/lib.mk | 16 ++++------------ 3 files changed, 22 insertions(+), 19 deletions(-)
tools/testing/selftests/Makefile +2 -4
@@ -148,16 +148,14 @@ ifdef INSTALL_PATH echo " logfile=\$$BASE_DIR/output.log" >> $(ALL_SCRIPT) echo " cat /dev/null > \$$logfile" >> $(ALL_SCRIPT) echo "fi" >> $(ALL_SCRIPT) - echo "export KSFT_TAP_LEVEL=1" >> $(ALL_SCRIPT) for TARGET in $(TARGETS); do \ BUILD_TARGET=$$BUILD/$$TARGET; \ - echo "echo ; echo TAP version 13" >> $(ALL_SCRIPT); \ - echo "echo Running tests in $$TARGET" >> $(ALL_SCRIPT); \ - echo "echo ========================================" >> $(ALL_SCRIPT); \ echo "[ -w /dev/kmsg ] && echo \"kselftest: Running tests in $$TARGET\" >> /dev/kmsg" >> $(ALL_SCRIPT); \ echo "cd $$TARGET" >> $(ALL_SCRIPT); \ + echo -n "run_many" >> $(ALL_SCRIPT); \ make -s --no-print-directory OUTPUT=$$BUILD_TARGET -C $$TARGET emit_tests >> $(ALL_SCRIPT); \ + echo "" >> $(ALL_SCRIPT); \ echo "cd \$$ROOT" >> $(ALL_SCRIPT); \ done;
tools/testing/selftests/kselftest/runner.sh +16 -3
@@ -1,17 +1,19 @@ #!/bin/sh # # Runs a set of tests in a given subdirectory. +export KSFT_TAP_LEVEL=1 export skip_rc=4 export logfile=/dev/stdout run_one() { - TEST="$1" - NUM="$2" + DIR="$1" + TEST="$2" + NUM="$3" BASENAME_TEST=$(basename $TEST) - TEST_HDR_MSG="selftests: "`basename $PWD`:" $BASENAME_TEST" + TEST_HDR_MSG="selftests: $DIR: $BASENAME_TEST" echo "$TEST_HDR_MSG" echo "========================================" if [ ! -x "$TEST" ]; then
@@ -29,3 +31,14 @@ run_one() cd - >/dev/null fi } + +run_many() +{ + echo "TAP version 13" + DIR=$(basename "$PWD") + test_num=0 + for TEST in "$@"; do + test_num=$(( test_num + 1 )) + run_one "$DIR" "$TEST" "$test_num" + done +}
tools/testing/selftests/lib.mk +4 -12
@@ -33,15 +33,8 @@ endif .ONESHELL: define RUN_TESTS - @export KSFT_TAP_LEVEL=`echo 1`; \ - test_num=`echo 0`; \ . $(selfdir)/kselftest/runner.sh; \ - echo "TAP version 13"; \ - for TEST in $(1); do \ - BASENAME_TEST=`basename $$TEST`; \ - test_num=`echo $$test_num+1 | bc`; \ - run_one "$$BASENAME_TEST" "$$test_num"; \ - done; + run_many $(1) endef run_tests: all
@@ -79,12 +72,11 @@ else endif emit_tests: - @test_num=`echo 0`; \ for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \ BASENAME_TEST=`basename $$TEST`; \ - test_num=`echo $$test_num+1 | bc`; \ - echo "run_one \"$$BASENAME_TEST\" \"$$test_num\""; \ - done; + echo " \\"; \ + echo -n " \"$$BASENAME_TEST\""; \ + done; \ # define if isn't already. It is undefined in make O= case. ifeq ($(RM),)
Signature · 1 line
2.17.1
keescook at chromium.org (Kees Cook)
PATCH 4/6 selftests/runner: Add plan line and fix result line syntax
In reply to keescook at chromium.org (Kees Cook)The TAP version 13 spec requires a "plan" line, which has been missing. Since we always know how many tests we're going to run, emit the count on the plan line. This also fixes the result lines to remove the "1.." prefix which is against spec, and to mark skips with the correct "# SKIP" suffix.
--- tools/testing/selftests/kselftest/runner.sh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
tools/testing/selftests/kselftest/runner.sh +6 -4
@@ -18,15 +18,15 @@ run_one() echo "========================================" if [ ! -x "$TEST" ]; then echo "$TEST_HDR_MSG: Warning: file $TEST is not executable, correct this." - echo "not ok 1..$test_num $TEST_HDR_MSG [FAIL]" + echo "not ok $test_num $TEST_HDR_MSG" else cd `dirname $TEST` > /dev/null (./$BASENAME_TEST >> "$logfile" 2>&1 && - echo "ok 1..$test_num $TEST_HDR_MSG [PASS]") || + echo "ok $test_num $TEST_HDR_MSG") || (if [ $? -eq $skip_rc ]; then \ - echo "not ok 1..$test_num $TEST_HDR_MSG [SKIP]" + echo "not ok $test_num $TEST_HDR_MSG # SKIP" else - echo "not ok 1..$test_num $TEST_HDR_MSG [FAIL]" + echo "not ok $test_num $TEST_HDR_MSG" fi) cd - >/dev/null fi
@@ -37,6 +37,8 @@ run_many() echo "TAP version 13" DIR=$(basename "$PWD") test_num=0 + total=$(echo "$@" | wc -w) + echo "1..$total" for TEST in "$@"; do test_num=$(( test_num + 1 )) run_one "$DIR" "$TEST" "$test_num"
Signature · 1 line
2.17.1
keescook at chromium.org (Kees Cook)
PATCH 5/6 selftests/runner: Distinguish between missing and non-executable
In reply to keescook at chromium.org (Kees Cook)If a test was missing (e.g. wrong architecture, etc), the test runner would incorrectly claim the test was non-executable. This adds an existence check to report correctly.
--- tools/testing/selftests/kselftest/runner.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
tools/testing/selftests/kselftest/runner.sh +6 -1
@@ -17,7 +17,12 @@ run_one() echo "$TEST_HDR_MSG" echo "========================================" if [ ! -x "$TEST" ]; then - echo "$TEST_HDR_MSG: Warning: file $TEST is not executable, correct this." + echo -n "$TEST_HDR_MSG: Warning: file $TEST is " + if [ ! -e "$TEST" ]; then + echo "missing!" + else + echo "not executable, correct this." + fi echo "not ok $test_num $TEST_HDR_MSG" else cd `dirname $TEST` > /dev/null
Signature · 1 line
2.17.1
keescook at chromium.org (Kees Cook)
PATCH 6/6 selftests: Move test output to diagnostic lines
In reply to keescook at chromium.org (Kees Cook)This changes the selftest output so that each test's output is prefixed with "# " as a TAP "diagnostic line".
This creates a bit of a kernel-specific TAP dialect where the diagnostics precede the results. The TAP spec isn't entirely clear about this, though, so I think it's the correct solution so as to keep interactive runs making sense. If the output _followed_ the result line in the spec-suggested YAML form, each test would dump all of its output at once instead of as it went, making debugging harder.
This does, however, solve the recursive TAP output problem, as sub-tests will simply be prefixed by "# ". Parsing sub-tests becomes a simple problem of just removing the first two characters of a given top-level test's diagnostic output, and parsing the results.
Note that the shell construct needed to both get an exit code from the first command in a pipe and still filter the pipe (to add the "# " prefix) uses a POSIX solution rather than the bash "pipefail" option which is not supported by dash.
Since some test environments may have a very minimal set of utilities available, the new prefixing code will fall back to doing line-at-a-time prefixing if perl and/or stdbuf are not available.
--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/kselftest/prefix.pl | 23 +++++++++++++ tools/testing/selftests/kselftest/runner.sh | 37 ++++++++++++++++++--- tools/testing/selftests/lib.mk | 1 + 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100755 tools/testing/selftests/kselftest/prefix.pl
tools/testing/selftests/Makefile +1 -0
@@ -133,6 +133,7 @@ ifdef INSTALL_PATH @# Ask all targets to install their files mkdir -p $(INSTALL_PATH)/kselftest install -m 744 kselftest/runner.sh $(INSTALL_PATH)/kselftest/ + install -m 744 kselftest/prefix.pl $(INSTALL_PATH)/kselftest/ @for TARGET in $(TARGETS); do \ BUILD_TARGET=$$BUILD/$$TARGET; \ make OUTPUT=$$BUILD_TARGET -C $$TARGET INSTALL_PATH=$(INSTALL_PATH)/$$TARGET install; \
tools/testing/selftests/kselftest/prefix.pl new file +23 -0
@@ -0,0 +1,23 @@ +#!/usr/bin/perl +# SPDX-License-Identifier: GPL-2.0 +# Prefix all lines with "# ", unbuffered. Command being piped in may need +# to have unbuffering forced with "stdbuf -i0 -o0 -e0 $cmd". +use strict; + +binmode STDIN; +binmode STDOUT; + +STDOUT->autoflush(1); + +my $needed = 1; +while (1) { + my $char; + my $bytes = sysread(STDIN, $char, 1); + exit 0 if ($bytes == 0); + if ($needed) { + print "# "; + $needed = 0; + } + print $char; + $needed = 1 if ($char eq "\n"); +}
tools/testing/selftests/kselftest/runner.sh +33 -4
@@ -5,6 +5,34 @@ export KSFT_TAP_LEVEL=1 export skip_rc=4 export logfile=/dev/stdout +# There isn't a shell-agnostic way to find the path of a sourced file, +# so we must rely on BASE_DIR being set to find other tools. +if [ -z "$BASE_DIR" ]; then + echo "Error: BASE_DIR must be set before sourcing." >&2 + exit 1 +fi + +# If Perl is unavailable, we must fall back to line-at-a-time prefixing +# with sed instead of unbuffered output. +tap_prefix() +{ + if [ ! -x /usr/bin/perl ]; then + sed -e 's/^/# /' + else + "$BASE_DIR"/kselftest/prefix.pl + fi +} + +# If stdbuf is unavailable, we must fall back to line-at-a-time piping. +tap_unbuffer() +{ + if ! which stdbuf >/dev/null ; then + "$@" + else + stdbuf -i0 -o0 -e0 "$@" + fi +} + run_one() { DIR="$1"
@@ -14,10 +42,9 @@ run_one() BASENAME_TEST=$(basename $TEST) TEST_HDR_MSG="selftests: $DIR: $BASENAME_TEST" - echo "$TEST_HDR_MSG" - echo "========================================" + echo "# $TEST_HDR_MSG" if [ ! -x "$TEST" ]; then - echo -n "$TEST_HDR_MSG: Warning: file $TEST is " + echo -n "# Warning: file $TEST is " if [ ! -e "$TEST" ]; then echo "missing!" else
@@ -26,7 +53,9 @@ run_one() echo "not ok $test_num $TEST_HDR_MSG" else cd `dirname $TEST` > /dev/null - (./$BASENAME_TEST >> "$logfile" 2>&1 && + (((((tap_unbuffer ./$BASENAME_TEST 2>&1; echo $? >&3) | + tap_prefix >&4) 3>&1) | + (read xs; exit $xs)) 4>>"$logfile" && echo "ok $test_num $TEST_HDR_MSG") || (if [ $? -eq $skip_rc ]; then \ echo "not ok $test_num $TEST_HDR_MSG # SKIP"
tools/testing/selftests/lib.mk +1 -0
@@ -33,6 +33,7 @@ endif .ONESHELL: define RUN_TESTS + BASE_DIR="$(selfdir)"; \ . $(selfdir)/kselftest/runner.sh; \ run_many $(1) endef
Signature · 1 line
2.17.1