Skip to content

Commit ed3854f

Browse files
committed
Merge tag 'ktest-v5.9' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-ktest
Pull ktest updates from Steven Rostedt: - Have config-bisect save the good/bad configs at each step. - Show log file location even on success - Add PRE_TEST_DIE to kill test if the PRE_TEST fails - Add a NOT operator for conditionals in config file - Add the log output of the last test when emailing on failure. - Other minor clean ups and small fixes. * tag 'ktest-v5.9' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-ktest: ktest.pl: Fix spelling mistake "Cant" -> "Can't" ktest.pl: Change the logic to control the size of the log file emailed ktest.pl: Add MAIL_MAX_SIZE to limit the amount of log emailed ktest.pl: Add the log of last test in email on failure ktest.pl: Turn off buffering to the log file ktest.pl: Just open up the log file once ktest.pl: Add a NOT operator ktest.pl: Define PRE_TEST_DIE to kill the test if the PRE_TEST fails ktest.pl: Always show log file location if defined even on success ktest.pl: Have config-bisect save each config used in the bisect
2 parents 97d052e + ff131ef commit ed3854f

File tree

2 files changed

+105
-16
lines changed

2 files changed

+105
-16
lines changed

tools/testing/ktest/ktest.pl

Lines changed: 87 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use File::Copy qw(cp);
1212
use FileHandle;
1313
use FindBin;
14+
use IO::Handle;
1415

1516
my $VERSION = "0.2";
1617

@@ -81,6 +82,8 @@
8182
"IGNORE_UNUSED" => 0,
8283
);
8384

85+
my $test_log_start = 0;
86+
8487
my $ktest_config = "ktest.conf";
8588
my $version;
8689
my $have_version = 0;
@@ -98,6 +101,7 @@
98101
my $pre_ktest;
99102
my $post_ktest;
100103
my $pre_test;
104+
my $pre_test_die;
101105
my $post_test;
102106
my $pre_build;
103107
my $post_build;
@@ -223,6 +227,7 @@
223227
my $mailto;
224228
my $mailer;
225229
my $mail_path;
230+
my $mail_max_size;
226231
my $mail_command;
227232
my $email_on_error;
228233
my $email_when_finished;
@@ -259,6 +264,7 @@
259264
"MAILTO" => \$mailto,
260265
"MAILER" => \$mailer,
261266
"MAIL_PATH" => \$mail_path,
267+
"MAIL_MAX_SIZE" => \$mail_max_size,
262268
"MAIL_COMMAND" => \$mail_command,
263269
"EMAIL_ON_ERROR" => \$email_on_error,
264270
"EMAIL_WHEN_FINISHED" => \$email_when_finished,
@@ -273,6 +279,7 @@
273279
"PRE_KTEST" => \$pre_ktest,
274280
"POST_KTEST" => \$post_ktest,
275281
"PRE_TEST" => \$pre_test,
282+
"PRE_TEST_DIE" => \$pre_test_die,
276283
"POST_TEST" => \$post_test,
277284
"BUILD_TYPE" => \$build_type,
278285
"BUILD_OPTIONS" => \$build_options,
@@ -507,9 +514,7 @@
507514

508515
sub _logit {
509516
if (defined($opt{"LOG_FILE"})) {
510-
open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
511-
print OUT @_;
512-
close(OUT);
517+
print LOG @_;
513518
}
514519
}
515520

@@ -909,6 +914,12 @@ sub process_expression {
909914
}
910915
}
911916

917+
if ($val =~ s/^\s*NOT\s+(.*)//) {
918+
my $express = $1;
919+
my $ret = process_expression($name, $express);
920+
return !$ret;
921+
}
922+
912923
if ($val =~ /^\s*0\s*$/) {
913924
return 0;
914925
} elsif ($val =~ /^\s*\d+\s*$/) {
@@ -1485,8 +1496,32 @@ sub dodie {
14851496

14861497
if ($email_on_error) {
14871498
my $name = get_test_name;
1499+
my $log_file;
1500+
1501+
if (defined($opt{"LOG_FILE"})) {
1502+
my $whence = 0; # beginning of file
1503+
my $pos = $test_log_start;
1504+
1505+
if (defined($mail_max_size)) {
1506+
my $log_size = tell LOG;
1507+
$log_size -= $test_log_start;
1508+
if ($log_size > $mail_max_size) {
1509+
$whence = 2; # end of file
1510+
$pos = - $mail_max_size;
1511+
}
1512+
}
1513+
$log_file = "$tmpdir/log";
1514+
open (L, "$opt{LOG_FILE}") or die "Can't open $opt{LOG_FILE} to read)";
1515+
open (O, "> $tmpdir/log") or die "Can't open $tmpdir/log\n";
1516+
seek(L, $pos, $whence);
1517+
while (<L>) {
1518+
print O;
1519+
}
1520+
close O;
1521+
close L;
1522+
}
14881523
send_email("KTEST: critical failure for test $i [$name]",
1489-
"Your test started at $script_start_time has failed with:\n@_\n");
1524+
"Your test started at $script_start_time has failed with:\n@_\n", $log_file);
14901525
}
14911526

14921527
if ($monitor_cnt) {
@@ -1508,7 +1543,7 @@ sub create_pty {
15081543
my $TIOCGPTN = 0x80045430;
15091544

15101545
sysopen($ptm, "/dev/ptmx", O_RDWR | O_NONBLOCK) or
1511-
dodie "Cant open /dev/ptmx";
1546+
dodie "Can't open /dev/ptmx";
15121547

15131548
# unlockpt()
15141549
$tmp = pack("i", 0);
@@ -1772,8 +1807,6 @@ sub run_command {
17721807
(fail "unable to exec $command" and return 0);
17731808

17741809
if (defined($opt{"LOG_FILE"})) {
1775-
open(LOG, ">>$opt{LOG_FILE}") or
1776-
dodie "failed to write to log";
17771810
$dolog = 1;
17781811
}
17791812

@@ -1821,7 +1854,6 @@ sub run_command {
18211854
}
18221855

18231856
close(CMD);
1824-
close(LOG) if ($dolog);
18251857
close(RD) if ($dord);
18261858

18271859
$end_time = time;
@@ -3188,6 +3220,8 @@ sub config_bisect_end {
31883220
doprint "***************************************\n\n";
31893221
}
31903222

3223+
my $pass = 1;
3224+
31913225
sub run_config_bisect {
31923226
my ($good, $bad, $last_result) = @_;
31933227
my $reset = "";
@@ -3210,11 +3244,15 @@ sub run_config_bisect {
32103244

32113245
$ret = run_config_bisect_test $config_bisect_type;
32123246
if ($ret) {
3213-
doprint "NEW GOOD CONFIG\n";
3247+
doprint "NEW GOOD CONFIG ($pass)\n";
3248+
system("cp $output_config $tmpdir/good_config.tmp.$pass");
3249+
$pass++;
32143250
# Return 3 for good config
32153251
return 3;
32163252
} else {
3217-
doprint "NEW BAD CONFIG\n";
3253+
doprint "NEW BAD CONFIG ($pass)\n";
3254+
system("cp $output_config $tmpdir/bad_config.tmp.$pass");
3255+
$pass++;
32183256
# Return 4 for bad config
32193257
return 4;
32203258
}
@@ -4077,8 +4115,12 @@ sub make_warnings_file {
40774115
}
40784116
}
40794117

4080-
if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
4081-
unlink $opt{"LOG_FILE"};
4118+
if (defined($opt{"LOG_FILE"})) {
4119+
if ($opt{"CLEAR_LOG"}) {
4120+
unlink $opt{"LOG_FILE"};
4121+
}
4122+
open(LOG, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
4123+
LOG->autoflush(1);
40824124
}
40834125

40844126
doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
@@ -4171,7 +4213,7 @@ sub find_mailer {
41714213
}
41724214

41734215
sub do_send_mail {
4174-
my ($subject, $message) = @_;
4216+
my ($subject, $message, $file) = @_;
41754217

41764218
if (!defined($mail_path)) {
41774219
# find the mailer
@@ -4181,16 +4223,30 @@ sub do_send_mail {
41814223
}
41824224
}
41834225

4226+
my $header_file = "$tmpdir/header";
4227+
open (HEAD, ">$header_file") or die "Can not create $header_file\n";
4228+
print HEAD "To: $mailto\n";
4229+
print HEAD "Subject: $subject\n\n";
4230+
print HEAD "$message\n";
4231+
close HEAD;
4232+
41844233
if (!defined($mail_command)) {
41854234
if ($mailer eq "mail" || $mailer eq "mailx") {
4186-
$mail_command = "\$MAIL_PATH/\$MAILER -s \'\$SUBJECT\' \$MAILTO <<< \'\$MESSAGE\'";
4235+
$mail_command = "cat \$HEADER_FILE \$BODY_FILE | \$MAIL_PATH/\$MAILER -s \'\$SUBJECT\' \$MAILTO";
41874236
} elsif ($mailer eq "sendmail" ) {
4188-
$mail_command = "echo \'Subject: \$SUBJECT\n\n\$MESSAGE\' | \$MAIL_PATH/\$MAILER -t \$MAILTO";
4237+
$mail_command = "cat \$HEADER_FILE \$BODY_FILE | \$MAIL_PATH/\$MAILER -t \$MAILTO";
41894238
} else {
41904239
die "\nYour mailer: $mailer is not supported.\n";
41914240
}
41924241
}
41934242

4243+
if (defined($file)) {
4244+
$mail_command =~ s/\$BODY_FILE/$file/g;
4245+
} else {
4246+
$mail_command =~ s/\$BODY_FILE//g;
4247+
}
4248+
4249+
$mail_command =~ s/\$HEADER_FILE/$header_file/g;
41944250
$mail_command =~ s/\$MAILER/$mailer/g;
41954251
$mail_command =~ s/\$MAIL_PATH/$mail_path/g;
41964252
$mail_command =~ s/\$MAILTO/$mailto/g;
@@ -4338,10 +4394,19 @@ sub cancel_test {
43384394
}
43394395

43404396
doprint "\n\n";
4397+
4398+
if (defined($opt{"LOG_FILE"})) {
4399+
$test_log_start = tell(LOG);
4400+
}
4401+
43414402
doprint "RUNNING TEST $i of $opt{NUM_TESTS}$name with option $test_type $run_type$installme\n\n";
43424403

43434404
if (defined($pre_test)) {
4344-
run_command $pre_test;
4405+
my $ret = run_command $pre_test;
4406+
if (!$ret && defined($pre_test_die) &&
4407+
$pre_test_die) {
4408+
dodie "failed to pre_test\n";
4409+
}
43454410
}
43464411

43474412
unlink $dmesg;
@@ -4441,4 +4506,10 @@ sub cancel_test {
44414506
send_email("KTEST: Your test has finished!",
44424507
"$successes of $opt{NUM_TESTS} tests started at $script_start_time were successful!");
44434508
}
4509+
4510+
if (defined($opt{"LOG_FILE"})) {
4511+
print "\n See $opt{LOG_FILE} for the record of results.\n\n";
4512+
close LOG;
4513+
}
4514+
44444515
exit 0;

tools/testing/ktest/sample.conf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,19 @@
442442
# Users can cancel the test by Ctrl^C
443443
# (default 0)
444444
#EMAIL_WHEN_CANCELED = 1
445+
#
446+
# If a test ends with an error and EMAIL_ON_ERROR is set as well
447+
# as a LOG_FILE is defined, then the log of the failing test will
448+
# be included in the email that is sent.
449+
# It is possible that the log may be very large, in which case,
450+
# only the last amount of the log should be sent. To limit how
451+
# much of the log is sent, set MAIL_MAX_SIZE. This will be the
452+
# size in bytes of the last portion of the log of the failed
453+
# test file. That is, if this is set to 100000, then only the
454+
# last 100 thousand bytes of the log file will be included in
455+
# the email.
456+
# (default undef)
457+
#MAIL_MAX_SIZE = 1000000
445458

446459
# Start a test setup. If you leave this off, all options
447460
# will be default and the test will run once.
@@ -557,6 +570,11 @@
557570
# default (undefined)
558571
#PRE_TEST = ${SSH} reboot_to_special_kernel
559572

573+
# To kill the entire test if PRE_TEST is defined but fails set this
574+
# to 1.
575+
# (default 0)
576+
#PRE_TEST_DIE = 1
577+
560578
# If there is a command you want to run after the individual test case
561579
# completes, then you can set this option.
562580
#

0 commit comments

Comments
 (0)