[PATCH 06/11] selftests/livepatch: Test klp-build with patches which cancel out
From: Song Liu
Date: Fri Oct 16 2026 - 20:26:10 EST
Add a test where a patch is followed by its revert. On their own the
two leave nothing to build, so klp-build fails with "no changes
detected" before building anything. With another patch after them,
only that patch's change is left in the combined patch.
Add expect_error() to functions.sh to check for a failure with a given
error message.
Assisted-by: Claude:claude-opus-5
Signed-off-by: Song Liu <song@xxxxxxxxxx>
---
.../livepatch/test_klp_build/Makefile | 3 +-
.../livepatch/test_klp_build/functions.sh | 12 +++
.../test_klp_build/test-cancelling-patches.sh | 102 ++++++++++++++++++
3 files changed, 116 insertions(+), 1 deletion(-)
create mode 100755 tools/testing/selftests/livepatch/test_klp_build/test-cancelling-patches.sh
diff --git a/tools/testing/selftests/livepatch/test_klp_build/Makefile b/tools/testing/selftests/livepatch/test_klp_build/Makefile
index 536b27767435..b3191b5c1925 100644
--- a/tools/testing/selftests/livepatch/test_klp_build/Makefile
+++ b/tools/testing/selftests/livepatch/test_klp_build/Makefile
@@ -3,7 +3,8 @@
TEST_PROGS := \
test-basic.sh \
test-dependent-patches.sh \
- test-independent-patches.sh
+ test-independent-patches.sh \
+ test-cancelling-patches.sh
TEST_PROGS_EXTENDED := functions.sh
diff --git a/tools/testing/selftests/livepatch/test_klp_build/functions.sh b/tools/testing/selftests/livepatch/test_klp_build/functions.sh
index 4e9d4ccfe300..04538efbee5d 100644
--- a/tools/testing/selftests/livepatch/test_klp_build/functions.sh
+++ b/tools/testing/selftests/livepatch/test_klp_build/functions.sh
@@ -136,3 +136,15 @@ function expect_patch() {
echo "ok"
}
+
+# expect_error(msg) - check that klp-build failed with an error message
+# msg - the message, without the "error: klp-build: " prefix
+function expect_error() {
+ [[ $STATUS -ne 0 ]] || not_ok "klp-build succeeded unexpectedly:" "$WORK_DIR/stdout"
+ [[ -s "$WORK_DIR/stdout" ]] && not_ok "klp-build printed a patch:" "$WORK_DIR/stdout"
+
+ grep -qxF "error: klp-build: $1" "$WORK_DIR/stderr" ||
+ not_ok "klp-build didn't fail with \"$1\":" "$WORK_DIR/stderr"
+
+ echo "ok"
+}
diff --git a/tools/testing/selftests/livepatch/test_klp_build/test-cancelling-patches.sh b/tools/testing/selftests/livepatch/test_klp_build/test-cancelling-patches.sh
new file mode 100755
index 000000000000..270ec8dd892f
--- /dev/null
+++ b/tools/testing/selftests/livepatch/test_klp_build/test-cancelling-patches.sh
@@ -0,0 +1,102 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+. "$(dirname "$0")/functions.sh"
+
+setup_tree
+
+add_file drivers/foo/foo.c <<'EOT'
+int foo(int x)
+{
+ if (x < 0)
+ return -1;
+ return x;
+}
+
+int bar(void)
+{
+ return __LINE__;
+}
+
+int baz(int x)
+{
+ if (x > 100)
+ return 100;
+ return x;
+}
+EOT
+
+add_patch 0001-foo.patch <<'EOT'
+--- a/drivers/foo/foo.c
++++ b/drivers/foo/foo.c
+@@ -1,6 +1,8 @@
+ int foo(int x)
+ {
++ if (!x)
++ return 0;
+ if (x < 0)
+ return -1;
+ return x;
+ }
+EOT
+
+add_patch 0002-revert-foo.patch <<'EOT'
+--- a/drivers/foo/foo.c
++++ b/drivers/foo/foo.c
+@@ -1,8 +1,6 @@
+ int foo(int x)
+ {
+- if (!x)
+- return 0;
+ if (x < 0)
+ return -1;
+ return x;
+ }
+EOT
+
+add_patch 0003-baz.patch <<'EOT'
+--- a/drivers/foo/foo.c
++++ b/drivers/foo/foo.c
+@@ -12,7 +12,7 @@ int bar(void)
+
+ int baz(int x)
+ {
+- if (x > 100)
+- return 100;
++ if (x > 1000)
++ return 1000;
+ return x;
+ }
+EOT
+
+# - a patch and its revert leave nothing to build
+
+start_test "patch and its revert"
+
+run_klp_build 0001-foo.patch 0002-revert-foo.patch
+
+expect_error "no changes detected"
+
+# - with another patch after them, only that patch's change is left
+
+start_test "patch and its revert, then another patch"
+
+run_klp_build 0001-foo.patch 0002-revert-foo.patch 0003-baz.patch
+
+expect_patch <<'EOT'
+--- a/drivers/foo/foo.c
++++ b/drivers/foo/foo.c
+@@ -12,7 +12,8 @@ int bar(void)
+
+ int baz(int x)
+ {
+- if (x > 100)
+- return 100;
++ if (x > 1000)
++ return 1000;
++#line 17
+ return x;
+ }
+EOT
+
+exit 0
--
2.53.0-Meta