Skip to content

Commit 34148b1

Browse files
committed
ktest.pl: Add the log of last test in email on failure
If a failure happens and an email is sent, show the contents of the log of the last test that failed in the email. Link: http://lore.kernel.org/r/[email protected] Reviewed-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Steven Rostedt (VMware) <[email protected]>
1 parent eefb9d2 commit 34148b1

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

tools/testing/ktest/ktest.pl

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
"IGNORE_UNUSED" => 0,
8383
);
8484

85+
my $test_log_start = 0;
86+
8587
my $ktest_config = "ktest.conf";
8688
my $version;
8789
my $have_version = 0;
@@ -1492,8 +1494,21 @@ sub dodie {
14921494

14931495
if ($email_on_error) {
14941496
my $name = get_test_name;
1497+
my $log_file;
1498+
1499+
if (defined($opt{"LOG_FILE"})) {
1500+
$log_file = "$tmpdir/log";
1501+
open (L, "$opt{LOG_FILE}") or die "Can't open $opt{LOG_FILE} to read)";
1502+
open (O, "> $tmpdir/log") or die "Can't open $tmpdir/log\n";
1503+
seek(L, $test_log_start, 0);
1504+
while (<L>) {
1505+
print O;
1506+
}
1507+
close O;
1508+
close L;
1509+
}
14951510
send_email("KTEST: critical failure for test $i [$name]",
1496-
"Your test started at $script_start_time has failed with:\n@_\n");
1511+
"Your test started at $script_start_time has failed with:\n@_\n", $log_file);
14971512
}
14981513

14991514
if ($monitor_cnt) {
@@ -4185,7 +4200,7 @@ sub find_mailer {
41854200
}
41864201

41874202
sub do_send_mail {
4188-
my ($subject, $message) = @_;
4203+
my ($subject, $message, $file) = @_;
41894204

41904205
if (!defined($mail_path)) {
41914206
# find the mailer
@@ -4195,16 +4210,30 @@ sub do_send_mail {
41954210
}
41964211
}
41974212

4213+
my $header_file = "$tmpdir/header";
4214+
open (HEAD, ">$header_file") or die "Can not create $header_file\n";
4215+
print HEAD "To: $mailto\n";
4216+
print HEAD "Subject: $subject\n\n";
4217+
print HEAD "$message\n";
4218+
close HEAD;
4219+
41984220
if (!defined($mail_command)) {
41994221
if ($mailer eq "mail" || $mailer eq "mailx") {
4200-
$mail_command = "\$MAIL_PATH/\$MAILER -s \'\$SUBJECT\' \$MAILTO <<< \'\$MESSAGE\'";
4222+
$mail_command = "cat \$HEADER_FILE \$BODY_FILE | \$MAIL_PATH/\$MAILER -s \'\$SUBJECT\' \$MAILTO";
42014223
} elsif ($mailer eq "sendmail" ) {
4202-
$mail_command = "echo \'Subject: \$SUBJECT\n\n\$MESSAGE\' | \$MAIL_PATH/\$MAILER -t \$MAILTO";
4224+
$mail_command = "cat \$HEADER_FILE \$BODY_FILE | \$MAIL_PATH/\$MAILER -t \$MAILTO";
42034225
} else {
42044226
die "\nYour mailer: $mailer is not supported.\n";
42054227
}
42064228
}
42074229

4230+
if (defined($file)) {
4231+
$mail_command =~ s/\$BODY_FILE/$file/g;
4232+
} else {
4233+
$mail_command =~ s/\$BODY_FILE//g;
4234+
}
4235+
4236+
$mail_command =~ s/\$HEADER_FILE/$header_file/g;
42084237
$mail_command =~ s/\$MAILER/$mailer/g;
42094238
$mail_command =~ s/\$MAIL_PATH/$mail_path/g;
42104239
$mail_command =~ s/\$MAILTO/$mailto/g;
@@ -4352,6 +4381,11 @@ sub cancel_test {
43524381
}
43534382

43544383
doprint "\n\n";
4384+
4385+
if (defined($opt{"LOG_FILE"})) {
4386+
$test_log_start = tell(LOG);
4387+
}
4388+
43554389
doprint "RUNNING TEST $i of $opt{NUM_TESTS}$name with option $test_type $run_type$installme\n\n";
43564390

43574391
if (defined($pre_test)) {

0 commit comments

Comments
 (0)