Skip to content

Commit 1ac884f

Browse files
committed
Merge tag 'ktest-v4.11' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-ktest
Pull ktest updates from Steven Rostedt: "These are various fixes that I have made and never got around to pushing. I've been asked to get the upstream repo back up-to-date" * tag 'ktest-v4.11' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-ktest: ktest: Add variable run_command_status to save status of commands executed ktest.pl: Powercycle the box on reboot if no connection can be made ktest: Add timeout to ssh command ktest: Fix child exit code processing ktest: Have POST_TEST run after the test has totally completed
2 parents 08e32dc + 5739438 commit 1ac884f

File tree

1 file changed

+76
-35
lines changed

1 file changed

+76
-35
lines changed

tools/testing/ktest/ktest.pl

Lines changed: 76 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
my $iteration = 0;
180180
my $successes = 0;
181181
my $stty_orig;
182+
my $run_command_status = 0;
182183

183184
my $bisect_good;
184185
my $bisect_bad;
@@ -1325,26 +1326,44 @@ sub eval_option {
13251326

13261327
sub reboot {
13271328
my ($time) = @_;
1329+
my $powercycle = 0;
13281330

1329-
# Make sure everything has been written to disk
1330-
run_ssh("sync");
1331+
# test if the machine can be connected to within 5 seconds
1332+
my $stat = run_ssh("echo check machine status", 5);
1333+
if (!$stat) {
1334+
doprint("power cycle\n");
1335+
$powercycle = 1;
1336+
}
1337+
1338+
if ($powercycle) {
1339+
run_command "$power_cycle";
13311340

1332-
if (defined($time)) {
13331341
start_monitor;
13341342
# flush out current monitor
13351343
# May contain the reboot success line
13361344
wait_for_monitor 1;
1337-
}
13381345

1339-
# try to reboot normally
1340-
if (run_command $reboot) {
1341-
if (defined($powercycle_after_reboot)) {
1342-
sleep $powercycle_after_reboot;
1346+
} else {
1347+
# Make sure everything has been written to disk
1348+
run_ssh("sync");
1349+
1350+
if (defined($time)) {
1351+
start_monitor;
1352+
# flush out current monitor
1353+
# May contain the reboot success line
1354+
wait_for_monitor 1;
1355+
}
1356+
1357+
# try to reboot normally
1358+
if (run_command $reboot) {
1359+
if (defined($powercycle_after_reboot)) {
1360+
sleep $powercycle_after_reboot;
1361+
run_command "$power_cycle";
1362+
}
1363+
} else {
1364+
# nope? power cycle it.
13431365
run_command "$power_cycle";
13441366
}
1345-
} else {
1346-
# nope? power cycle it.
1347-
run_command "$power_cycle";
13481367
}
13491368

13501369
if (defined($time)) {
@@ -1412,6 +1431,10 @@ sub dodie {
14121431
system("stty $stty_orig");
14131432
}
14141433

1434+
if (defined($post_test)) {
1435+
run_command $post_test;
1436+
}
1437+
14151438
die @_, "\n";
14161439
}
14171440

@@ -1624,10 +1647,6 @@ sub save_logs {
16241647

16251648
sub fail {
16261649

1627-
if (defined($post_test)) {
1628-
run_command $post_test;
1629-
}
1630-
16311650
if ($die_on_failure) {
16321651
dodie @_;
16331652
}
@@ -1660,23 +1679,26 @@ sub fail {
16601679
save_logs "fail", $store_failures;
16611680
}
16621681

1682+
if (defined($post_test)) {
1683+
run_command $post_test;
1684+
}
1685+
16631686
return 1;
16641687
}
16651688

16661689
sub run_command {
1667-
my ($command, $redirect) = @_;
1690+
my ($command, $redirect, $timeout) = @_;
16681691
my $start_time;
16691692
my $end_time;
16701693
my $dolog = 0;
16711694
my $dord = 0;
16721695
my $pid;
16731696

1674-
$start_time = time;
1675-
16761697
$command =~ s/\$SSH_USER/$ssh_user/g;
16771698
$command =~ s/\$MACHINE/$machine/g;
16781699

16791700
doprint("$command ... ");
1701+
$start_time = time;
16801702

16811703
$pid = open(CMD, "$command 2>&1 |") or
16821704
(fail "unable to exec $command" and return 0);
@@ -1693,13 +1715,30 @@ sub run_command {
16931715
$dord = 1;
16941716
}
16951717

1696-
while (<CMD>) {
1697-
print LOG if ($dolog);
1698-
print RD if ($dord);
1718+
my $hit_timeout = 0;
1719+
1720+
while (1) {
1721+
my $fp = \*CMD;
1722+
if (defined($timeout)) {
1723+
doprint "timeout = $timeout\n";
1724+
}
1725+
my $line = wait_for_input($fp, $timeout);
1726+
if (!defined($line)) {
1727+
my $now = time;
1728+
if (defined($timeout) && (($now - $start_time) >= $timeout)) {
1729+
doprint "Hit timeout of $timeout, killing process\n";
1730+
$hit_timeout = 1;
1731+
kill 9, $pid;
1732+
}
1733+
last;
1734+
}
1735+
print LOG $line if ($dolog);
1736+
print RD $line if ($dord);
16991737
}
17001738

17011739
waitpid($pid, 0);
1702-
my $failed = $?;
1740+
# shift 8 for real exit status
1741+
$run_command_status = $? >> 8;
17031742

17041743
close(CMD);
17051744
close(LOG) if ($dolog);
@@ -1714,21 +1753,25 @@ sub run_command {
17141753
doprint "[$delta seconds] ";
17151754
}
17161755

1717-
if ($failed) {
1756+
if ($hit_timeout) {
1757+
$run_command_status = 1;
1758+
}
1759+
1760+
if ($run_command_status) {
17181761
doprint "FAILED!\n";
17191762
} else {
17201763
doprint "SUCCESS\n";
17211764
}
17221765

1723-
return !$failed;
1766+
return !$run_command_status;
17241767
}
17251768

17261769
sub run_ssh {
1727-
my ($cmd) = @_;
1770+
my ($cmd, $timeout) = @_;
17281771
my $cp_exec = $ssh_exec;
17291772

17301773
$cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
1731-
return run_command "$cp_exec";
1774+
return run_command "$cp_exec", undef , $timeout;
17321775
}
17331776

17341777
sub run_scp {
@@ -2489,10 +2532,6 @@ sub halt {
24892532
sub success {
24902533
my ($i) = @_;
24912534

2492-
if (defined($post_test)) {
2493-
run_command $post_test;
2494-
}
2495-
24962535
$successes++;
24972536

24982537
my $name = "";
@@ -2517,6 +2556,10 @@ sub success {
25172556
doprint "Reboot and wait $sleep_time seconds\n";
25182557
reboot_to_good $sleep_time;
25192558
}
2559+
2560+
if (defined($post_test)) {
2561+
run_command $post_test;
2562+
}
25202563
}
25212564

25222565
sub answer_bisect {
@@ -2537,16 +2580,15 @@ sub answer_bisect {
25372580
}
25382581

25392582
sub child_run_test {
2540-
my $failed = 0;
25412583

25422584
# child should have no power
25432585
$reboot_on_error = 0;
25442586
$poweroff_on_error = 0;
25452587
$die_on_failure = 1;
25462588

2547-
run_command $run_test, $testlog or $failed = 1;
2589+
run_command $run_test, $testlog;
25482590

2549-
exit $failed;
2591+
exit $run_command_status;
25502592
}
25512593

25522594
my $child_done;
@@ -2629,7 +2671,7 @@ sub do_run_test {
26292671
}
26302672

26312673
waitpid $child_pid, 0;
2632-
$child_exit = $?;
2674+
$child_exit = $? >> 8;
26332675

26342676
my $end_time = time;
26352677
$test_time = $end_time - $start_time;
@@ -3330,7 +3372,6 @@ sub config_bisect {
33303372
save_config \%good_configs, $good_config;
33313373
save_config \%bad_configs, $bad_config;
33323374

3333-
33343375
if (defined($config_bisect_check) && $config_bisect_check ne "0") {
33353376
if ($config_bisect_check ne "good") {
33363377
doprint "Testing bad config\n";

0 commit comments

Comments
 (0)