Skip to content

Commit

Permalink
Merge tag 'ktest-v4.11' of git://git.kernel.org/pub/scm/linux/kernel/…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
Linus Torvalds committed Feb 28, 2017
2 parents 08e32dc + 5739438 commit 1ac884f
Showing 1 changed file with 76 additions and 35 deletions.
111 changes: 76 additions & 35 deletions tools/testing/ktest/ktest.pl
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
my $iteration = 0;
my $successes = 0;
my $stty_orig;
my $run_command_status = 0;

my $bisect_good;
my $bisect_bad;
Expand Down Expand Up @@ -1325,26 +1326,44 @@ sub eval_option {

sub reboot {
my ($time) = @_;
my $powercycle = 0;

# Make sure everything has been written to disk
run_ssh("sync");
# test if the machine can be connected to within 5 seconds
my $stat = run_ssh("echo check machine status", 5);
if (!$stat) {
doprint("power cycle\n");
$powercycle = 1;
}

if ($powercycle) {
run_command "$power_cycle";

if (defined($time)) {
start_monitor;
# flush out current monitor
# May contain the reboot success line
wait_for_monitor 1;
}

# try to reboot normally
if (run_command $reboot) {
if (defined($powercycle_after_reboot)) {
sleep $powercycle_after_reboot;
} else {
# Make sure everything has been written to disk
run_ssh("sync");

if (defined($time)) {
start_monitor;
# flush out current monitor
# May contain the reboot success line
wait_for_monitor 1;
}

# try to reboot normally
if (run_command $reboot) {
if (defined($powercycle_after_reboot)) {
sleep $powercycle_after_reboot;
run_command "$power_cycle";
}
} else {
# nope? power cycle it.
run_command "$power_cycle";
}
} else {
# nope? power cycle it.
run_command "$power_cycle";
}

if (defined($time)) {
Expand Down Expand Up @@ -1412,6 +1431,10 @@ sub dodie {
system("stty $stty_orig");
}

if (defined($post_test)) {
run_command $post_test;
}

die @_, "\n";
}

Expand Down Expand Up @@ -1624,10 +1647,6 @@ sub save_logs {

sub fail {

if (defined($post_test)) {
run_command $post_test;
}

if ($die_on_failure) {
dodie @_;
}
Expand Down Expand Up @@ -1660,23 +1679,26 @@ sub fail {
save_logs "fail", $store_failures;
}

if (defined($post_test)) {
run_command $post_test;
}

return 1;
}

sub run_command {
my ($command, $redirect) = @_;
my ($command, $redirect, $timeout) = @_;
my $start_time;
my $end_time;
my $dolog = 0;
my $dord = 0;
my $pid;

$start_time = time;

$command =~ s/\$SSH_USER/$ssh_user/g;
$command =~ s/\$MACHINE/$machine/g;

doprint("$command ... ");
$start_time = time;

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

while (<CMD>) {
print LOG if ($dolog);
print RD if ($dord);
my $hit_timeout = 0;

while (1) {
my $fp = \*CMD;
if (defined($timeout)) {
doprint "timeout = $timeout\n";
}
my $line = wait_for_input($fp, $timeout);
if (!defined($line)) {
my $now = time;
if (defined($timeout) && (($now - $start_time) >= $timeout)) {
doprint "Hit timeout of $timeout, killing process\n";
$hit_timeout = 1;
kill 9, $pid;
}
last;
}
print LOG $line if ($dolog);
print RD $line if ($dord);
}

waitpid($pid, 0);
my $failed = $?;
# shift 8 for real exit status
$run_command_status = $? >> 8;

close(CMD);
close(LOG) if ($dolog);
Expand All @@ -1714,21 +1753,25 @@ sub run_command {
doprint "[$delta seconds] ";
}

if ($failed) {
if ($hit_timeout) {
$run_command_status = 1;
}

if ($run_command_status) {
doprint "FAILED!\n";
} else {
doprint "SUCCESS\n";
}

return !$failed;
return !$run_command_status;
}

sub run_ssh {
my ($cmd) = @_;
my ($cmd, $timeout) = @_;
my $cp_exec = $ssh_exec;

$cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
return run_command "$cp_exec";
return run_command "$cp_exec", undef , $timeout;
}

sub run_scp {
Expand Down Expand Up @@ -2489,10 +2532,6 @@ sub halt {
sub success {
my ($i) = @_;

if (defined($post_test)) {
run_command $post_test;
}

$successes++;

my $name = "";
Expand All @@ -2517,6 +2556,10 @@ sub success {
doprint "Reboot and wait $sleep_time seconds\n";
reboot_to_good $sleep_time;
}

if (defined($post_test)) {
run_command $post_test;
}
}

sub answer_bisect {
Expand All @@ -2537,16 +2580,15 @@ sub answer_bisect {
}

sub child_run_test {
my $failed = 0;

# child should have no power
$reboot_on_error = 0;
$poweroff_on_error = 0;
$die_on_failure = 1;

run_command $run_test, $testlog or $failed = 1;
run_command $run_test, $testlog;

exit $failed;
exit $run_command_status;
}

my $child_done;
Expand Down Expand Up @@ -2629,7 +2671,7 @@ sub do_run_test {
}

waitpid $child_pid, 0;
$child_exit = $?;
$child_exit = $? >> 8;

my $end_time = time;
$test_time = $end_time - $start_time;
Expand Down Expand Up @@ -3330,7 +3372,6 @@ sub config_bisect {
save_config \%good_configs, $good_config;
save_config \%bad_configs, $bad_config;


if (defined($config_bisect_check) && $config_bisect_check ne "0") {
if ($config_bisect_check ne "good") {
doprint "Testing bad config\n";
Expand Down

0 comments on commit 1ac884f

Please sign in to comment.