[PATCH] selftests/livepatch: support BusyBox diff

From: Joe Lawrence
Date: Wed Jun 24 2020 - 15:34:04 EST


BusyBox diff doesn't support the GNU diff '--LTYPE-line-format' options
that were used in the selftests to filter older kernel log messages from
dmesg output.

Bite the bullet and code this into the selftest:

- test start: remember the last dmesg entry (has unique [timestamp]
prefix) in $SAVED_DMESG
- test end: awk-filter dmesg output up to and including $SAVED_DMESG

Account for kernel log overflow in case awk doesn't find $SAVED_DMESG,
exit and fail the test with a recommendation that the user expand the
kernel log buffer.

Reported-by: Naresh Kamboju <naresh.kamboju@xxxxxxxxxx>
Signed-off-by: Joe Lawrence <joe.lawrence@xxxxxxxxxx>
---
tools/testing/selftests/livepatch/functions.sh | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/tools/testing/selftests/livepatch/functions.sh b/tools/testing/selftests/livepatch/functions.sh
index 36648ca367c2..365ce4110265 100644
--- a/tools/testing/selftests/livepatch/functions.sh
+++ b/tools/testing/selftests/livepatch/functions.sh
@@ -41,15 +41,16 @@ function die() {
exit 1
}

-# save existing dmesg so we can detect new content
+# save last dmesg entry so we can detect new content
function save_dmesg() {
- SAVED_DMESG=$(mktemp --tmpdir -t klp-dmesg-XXXXXX)
- dmesg > "$SAVED_DMESG"
+ SAVED_DMESG="$(dmesg | tail -n1)"
}

-# cleanup temporary dmesg file from save_dmesg()
-function cleanup_dmesg_file() {
- rm -f "$SAVED_DMESG"
+# filter all dmesg content before and including $SAVED_DMESG
+function new_dmesg() {
+ if ! dmesg | awk -v last="$SAVED_DMESG" 'p; $0 == last{p=1} END {exit !p}'; then
+ die "dmesg overflow, try increasing kernel log buffer size"
+ fi
}

function push_config() {
@@ -81,7 +82,6 @@ function set_ftrace_enabled() {

function cleanup() {
pop_config
- cleanup_dmesg_file
}

# setup_config - save the current config and set a script exit trap that
@@ -277,7 +277,7 @@ function check_result {
# help differentiate repeated testing runs. Remove them with a
# post-comparison sed filter.

- result=$(dmesg | diff --changed-group-format='%>' --unchanged-group-format='' "$SAVED_DMESG" - | \
+ result=$(new_dmesg | \
grep -e 'livepatch:' -e 'test_klp' | \
grep -v '\(tainting\|taints\) kernel' | \
sed 's/^\[[ 0-9.]*\] //')
@@ -288,6 +288,4 @@ function check_result {
echo -e "not ok\n\n$(diff -upr --label expected --label result <(echo "$expect") <(echo "$result"))\n"
die "livepatch kselftest(s) failed"
fi
-
- cleanup_dmesg_file
}
--
2.21.3