ktest: Run POST_KTEST hooks on failure and cancellation

PRE_KTEST can be useful for setting up the environment and POST_KTEST to
tear it down, however POST_KTEST only runs on the normal end-of-run path.
It is skipped when ktest exits through dodie() or cancel_test(). Final
cleanup hooks are skipped.

Factor the final hook execution into run_post_ktest(), call it from the
normal exit path and from the early exit paths, and guard it so the hook
runs at most once.

Cc: John Hawley <warthog9@eaglescrag.net>
Cc: Andrea Righi <arighi@nvidia.com>
Cc: Marcos Paulo de Souza <mpdesouza@suse.com>
Cc: Matthieu Baerts <matttbe@kernel.org>
Cc: Fernando Fernandez Mancera <fmancera@suse.de>
Cc: Pedro Falcato <pfalcato@suse.de>
Link: https://patch.msgid.link/20260307-ktest-fixes-v1-8-565d412f4925@suse.com
Fixes: 921ed4c720 ("ktest: Add PRE/POST_KTEST and TEST options")
Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
This commit is contained in:
Ricardo B. Marlière
2026-03-07 19:08:03 -03:00
committed by Steven Rostedt
parent 972816d21b
commit bc6e165a45

View File

@@ -100,6 +100,7 @@ my $test_type;
my $build_type;
my $build_options;
my $final_post_ktest;
my $post_ktest_done = 0;
my $pre_ktest;
my $pre_ktest_die;
my $post_ktest;
@@ -1586,6 +1587,24 @@ sub get_test_name() {
return $name;
}
sub run_post_ktest {
my $cmd;
return if ($post_ktest_done);
if (defined($final_post_ktest)) {
$cmd = $final_post_ktest;
} elsif (defined($post_ktest)) {
$cmd = $post_ktest;
} else {
return;
}
my $cp_post_ktest = eval_kernel_version($cmd);
run_command $cp_post_ktest;
$post_ktest_done = 1;
}
sub dodie {
# avoid recursion
return if ($in_die);
@@ -1645,6 +1664,7 @@ sub dodie {
if (defined($post_test)) {
run_command $post_test;
}
run_post_ktest;
die @_, "\n";
}
@@ -4314,6 +4334,7 @@ sub cancel_test {
send_email("KTEST: Your [$name] test was cancelled",
"Your test started at $script_start_time was cancelled: sig int");
}
run_post_ktest;
die "\nCaught Sig Int, test interrupted: $!\n"
}
@@ -4679,11 +4700,7 @@ for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
success $i;
}
if (defined($final_post_ktest)) {
my $cp_final_post_ktest = eval_kernel_version $final_post_ktest;
run_command $cp_final_post_ktest;
}
run_post_ktest;
if ($opt{"POWEROFF_ON_SUCCESS"}) {
halt;