[PATCH 7/9] ktest: Add PRE_KTEST_DIE for PRE_KTEST failures

Ricardo B. Marlière posted 9 patches 1 month ago
[PATCH 7/9] ktest: Add PRE_KTEST_DIE for PRE_KTEST failures
Posted by Ricardo B. Marlière 1 month ago
PRE_KTEST runs before the first test, but its return status is currently
ignored. A failing setup hook can leave the rest of the run executing in a
partially initialized environment.

Add PRE_KTEST_DIE so PRE_KTEST can fail the run in the same way
PRE_BUILD_DIE and PRE_TEST_DIE already can. Keep the default behavior
unchanged when the new option is not set.

Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
---
 tools/testing/ktest/ktest.pl    | 8 +++++++-
 tools/testing/ktest/sample.conf | 6 ++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/tools/testing/ktest/ktest.pl b/tools/testing/ktest/ktest.pl
index bd2e2311884c..b018b937e028 100755
--- a/tools/testing/ktest/ktest.pl
+++ b/tools/testing/ktest/ktest.pl
@@ -101,6 +101,7 @@ my $build_type;
 my $build_options;
 my $final_post_ktest;
 my $pre_ktest;
+my $pre_ktest_die;
 my $post_ktest;
 my $pre_test;
 my $pre_test_die;
@@ -283,6 +284,7 @@ my %option_map = (
     "BUILD_DIR"			=> \$builddir,
     "TEST_TYPE"			=> \$test_type,
     "PRE_KTEST"			=> \$pre_ktest,
+    "PRE_KTEST_DIE"		=> \$pre_ktest_die,
     "POST_KTEST"		=> \$post_ktest,
     "PRE_TEST"			=> \$pre_test,
     "PRE_TEST_DIE"		=> \$pre_test_die,
@@ -4506,7 +4508,11 @@ for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
     if ($i == 1) {
 	if (defined($pre_ktest)) {
 	    doprint "\n";
-	    run_command $pre_ktest;
+	    my $ret = run_command $pre_ktest;
+	    if (!$ret && defined($pre_ktest_die) &&
+		$pre_ktest_die) {
+		dodie "failed to pre_ktest\n";
+	    }
 	}
 	if ($email_when_started) {
 	    my $name = get_test_name;
diff --git a/tools/testing/ktest/sample.conf b/tools/testing/ktest/sample.conf
index 9c4c449a8f3e..b6e439ef511b 100644
--- a/tools/testing/ktest/sample.conf
+++ b/tools/testing/ktest/sample.conf
@@ -494,6 +494,12 @@
 #
 # default (undefined)
 #PRE_KTEST = ${SSH} ~/set_up_test
+#
+# To specify if the test should fail if PRE_KTEST fails,
+# PRE_KTEST_DIE needs to be set to 1. Otherwise the PRE_KTEST
+# result is ignored.
+# (default 0)
+#PRE_KTEST_DIE = 1
 
 # If you want to execute some command after all the tests have
 # completed, you can set this option. Note, it can be set as a

-- 
2.53.0

Re: [PATCH 7/9] ktest: Add PRE_KTEST_DIE for PRE_KTEST failures
Posted by Steven Rostedt 1 month ago
On Sat, 07 Mar 2026 19:08:02 -0300
Ricardo B. Marlière <rbm@suse.com> wrote:

> PRE_KTEST runs before the first test, but its return status is currently
> ignored. A failing setup hook can leave the rest of the run executing in a
> partially initialized environment.
> 
> Add PRE_KTEST_DIE so PRE_KTEST can fail the run in the same way
> PRE_BUILD_DIE and PRE_TEST_DIE already can. Keep the default behavior
> unchanged when the new option is not set.

Thank you for keeping the previous behavior. My PRE_KTEST usually apply
patches that don't always apply, and yes, I depend on it "not dying" on
failure. Which is probably why that was the way it worked, as the reason I
created that option was to apply patches before testing.

-- Steve
Re: [PATCH 7/9] ktest: Add PRE_KTEST_DIE for PRE_KTEST failures
Posted by Ricardo B. Marlière 1 month ago
On Mon Mar 9, 2026 at 12:00 PM -03, Steven Rostedt wrote:
> On Sat, 07 Mar 2026 19:08:02 -0300
> Ricardo B. Marlière <rbm@suse.com> wrote:
>
>> PRE_KTEST runs before the first test, but its return status is currently
>> ignored. A failing setup hook can leave the rest of the run executing in a
>> partially initialized environment.
>> 
>> Add PRE_KTEST_DIE so PRE_KTEST can fail the run in the same way
>> PRE_BUILD_DIE and PRE_TEST_DIE already can. Keep the default behavior
>> unchanged when the new option is not set.
>
> Thank you for keeping the previous behavior. My PRE_KTEST usually apply
> patches that don't always apply, and yes, I depend on it "not dying" on
> failure. Which is probably why that was the way it worked, as the reason I
> created that option was to apply patches before testing.

I figured this behavior had to be there for a reason :)

Thanks for reviewing/testing btw !

>
> -- Steve