3 # Copywrite 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
4 # Licensed under the terms of the GNU GPL License version 2
9 use Fcntl
qw(F_GETFL F_SETFL O_NONBLOCK);
10 use File
::Path
qw(mkpath);
11 use File
::Copy
qw(cp);
24 $default{"NUM_TESTS"} = 1;
25 $default{"REBOOT_TYPE"} = "grub";
26 $default{"TEST_TYPE"} = "test";
27 $default{"BUILD_TYPE"} = "randconfig";
28 $default{"MAKE_CMD"} = "make";
29 $default{"TIMEOUT"} = 120;
30 $default{"TMP_DIR"} = "/tmp/ktest";
31 $default{"SLEEP_TIME"} = 60; # sleep time between tests
32 $default{"BUILD_NOCLEAN"} = 0;
33 $default{"REBOOT_ON_ERROR"} = 0;
34 $default{"POWEROFF_ON_ERROR"} = 0;
35 $default{"REBOOT_ON_SUCCESS"} = 1;
36 $default{"POWEROFF_ON_SUCCESS"} = 0;
37 $default{"BUILD_OPTIONS"} = "";
38 $default{"BISECT_SLEEP_TIME"} = 60; # sleep time between bisects
39 $default{"CLEAR_LOG"} = 0;
40 $default{"SUCCESS_LINE"} = "login:";
41 $default{"BOOTED_TIMEOUT"} = 1;
42 $default{"DIE_ON_FAILURE"} = 1;
43 $default{"SSH_EXEC"} = "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND";
44 $default{"SCP_TO_TARGET"} = "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE";
45 $default{"REBOOT"} = "ssh \$SSH_USER\@\$MACHINE reboot";
46 $default{"STOP_AFTER_SUCCESS"} = 10;
47 $default{"STOP_AFTER_FAILURE"} = 60;
48 $default{"LOCALVERSION"} = "-test";
66 my $poweroff_on_error;
68 my $powercycle_after_reboot;
69 my $poweroff_after_halt;
84 my $in_patchcheck = 0;
93 my $bisect_sleep_time;
99 my $stop_after_success;
100 my $stop_after_failure;
110 $config_help{"MACHINE"} = << "EOF"
111 The machine hostname that you will test
.
114 $config_help{"SSH_USER"} = << "EOF"
115 The box is expected to have ssh on normal bootup
, provide the user
116 (most likely root
, since you need privileged operations
)
119 $config_help{"BUILD_DIR"} = << "EOF"
120 The directory that contains the Linux source code
(full path
).
123 $config_help{"OUTPUT_DIR"} = << "EOF"
124 The directory that the objects will be built
(full path
).
125 (can
not be same as BUILD_DIR
)
128 $config_help{"BUILD_TARGET"} = << "EOF"
129 The location of the compiled file to copy to the target
.
130 (relative to OUTPUT_DIR
)
133 $config_help{"TARGET_IMAGE"} = << "EOF"
134 The place to put your image on the test machine
.
137 $config_help{"POWER_CYCLE"} = << "EOF"
138 A script
or command to reboot the box
.
140 Here is a digital loggers power switch example
141 POWER_CYCLE
= wget
--no-proxy
-O
/dev/null
-q
--auth
-no-challenge
'http://admin:admin\@power/outlet?5=CCL'
143 Here is an example to reboot a virtual box on the current host
144 with the name
"Guest".
145 POWER_CYCLE
= virsh destroy Guest
; sleep 5; virsh start Guest
148 $config_help{"CONSOLE"} = << "EOF"
149 The script
or command that reads the console
151 If you
use ttywatch server
, something like the following would work
.
152 CONSOLE
= nc
-d localhost
3001
154 For a virtual machine with guest name
"Guest".
155 CONSOLE
= virsh console Guest
158 $config_help{"LOCALVERSION"} = << "EOF"
159 Required version ending to differentiate the test
160 from other linux builds on the
system.
163 $config_help{"REBOOT_TYPE"} = << "EOF"
164 Way to reboot the box to the test kernel
.
165 Only valid options so far are
"grub" and "script".
167 If you specify grub
, it will assume grub version
1
168 and will search
in /boot/grub
/menu
.lst
for the title \
$GRUB_MENU
169 and select that target to reboot to the kernel
. If this is
not
170 your setup
, then specify
"script" and have a command
or script
171 specified
in REBOOT_SCRIPT to boot to the target
.
173 The entry
in /boot/grub
/menu
.lst must be entered
in manually
.
174 The test will
not modify that file
.
177 $config_help{"GRUB_MENU"} = << "EOF"
178 The grub title name
for the test kernel to boot
179 (Only mandatory
if REBOOT_TYPE
= grub
)
181 Note
, ktest
.pl will
not update the grub menu
.lst
, you need to
182 manually add an option
for the test
. ktest
.pl will search
183 the grub menu
.lst
for this option to find what kernel to
186 For example
, if in the
/boot/grub
/menu
.lst the test kernel title has
:
189 GRUB_MENU
= Test Kernel
192 $config_help{"REBOOT_SCRIPT"} = << "EOF"
193 A script to reboot the target into the test kernel
194 (Only mandatory
if REBOOT_TYPE
= script
)
199 sub get_ktest_config
{
202 return if (defined($opt{$config}));
204 if (defined($config_help{$config})) {
206 print $config_help{$config};
211 if (defined($default{$config})) {
212 print "\[$default{$config}\] ";
214 $entered_configs{$config} = <STDIN
>;
215 $entered_configs{$config} =~ s/^\s*(.*\S)\s*$/$1/;
216 if ($entered_configs{$config} =~ /^\s*$/) {
217 if ($default{$config}) {
218 $entered_configs{$config} = $default{$config};
220 print "Your answer can not be blank\n";
228 sub get_ktest_configs
{
229 get_ktest_config
("MACHINE");
230 get_ktest_config
("SSH_USER");
231 get_ktest_config
("BUILD_DIR");
232 get_ktest_config
("OUTPUT_DIR");
233 get_ktest_config
("BUILD_TARGET");
234 get_ktest_config
("TARGET_IMAGE");
235 get_ktest_config
("POWER_CYCLE");
236 get_ktest_config
("CONSOLE");
237 get_ktest_config
("LOCALVERSION");
239 my $rtype = $opt{"REBOOT_TYPE"};
241 if (!defined($rtype)) {
242 if (!defined($opt{"GRUB_MENU"})) {
243 get_ktest_config
("REBOOT_TYPE");
244 $rtype = $entered_configs{"REBOOT_TYPE"};
250 if ($rtype eq "grub") {
251 get_ktest_config
("GRUB_MENU");
253 get_ktest_config
("REBOOT_SCRIPT");
258 my ($lvalue, $rvalue) = @_;
260 if (defined($opt{$lvalue})) {
261 die "Error: Option $lvalue defined more than once!\n";
263 if ($rvalue =~ /^\s*$/) {
264 delete $opt{$lvalue};
266 $opt{$lvalue} = $rvalue;
273 open(IN
, $config) || die "can't read file $config";
276 $name =~ s
,.*/(.*),$1,;
281 my $num_tests_set = 0;
287 # ignore blank lines and comments
288 next if (/^\s*$/ || /\s*\#/);
290 if (/^\s*TEST_START(.*)/) {
294 if ($num_tests_set) {
295 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
298 my $old_test_num = $test_num;
299 my $old_repeat = $repeat;
301 $test_num += $repeat;
305 if ($rest =~ /\s+SKIP(.*)/) {
312 if ($rest =~ /\s+ITERATE\s+(\d+)(.*)$/) {
315 $repeat_tests{"$test_num"} = $repeat;
318 if ($rest =~ /\s+SKIP(.*)/) {
323 if ($rest !~ /^\s*$/) {
324 die "$name: $.: Gargbage found after TEST_START\n$_";
328 $test_num = $old_test_num;
329 $repeat = $old_repeat;
332 } elsif (/^\s*DEFAULTS(.*)$/) {
337 if ($rest =~ /\s+SKIP(.*)/) {
344 if ($rest !~ /^\s*$/) {
345 die "$name: $.: Gargbage found after DEFAULTS\n$_";
348 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
356 ($lvalue eq "NUM_TESTS" ||
357 $lvalue eq "LOG_FILE" ||
358 $lvalue eq "CLEAR_LOG")) {
359 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
362 if ($lvalue eq "NUM_TESTS") {
364 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
367 die "$name: $.: NUM_TESTS must be set in default section\n";
372 if ($default || $lvalue =~ /\[\d+\]$/) {
373 set_value
($lvalue, $rvalue);
375 my $val = "$lvalue\[$test_num\]";
376 set_value
($val, $rvalue);
379 $repeats{$val} = $repeat;
383 die "$name: $.: Garbage found in config\n$_";
390 $test_num += $repeat - 1;
391 $opt{"NUM_TESTS"} = $test_num;
394 # make sure we have all mandatory configs
399 foreach my $default (keys %default) {
400 if (!defined($opt{$default})) {
401 $opt{$default} = $default{$default};
407 if (defined($opt{"LOG_FILE"})) {
408 open(OUT
, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
415 if (defined($opt{"LOG_FILE"})) {
430 # try to reboot normally
431 if (run_command
$reboot) {
432 if (defined($powercycle_after_reboot)) {
433 sleep $powercycle_after_reboot;
434 run_command
"$power_cycle";
437 # nope? power cycle it.
438 run_command
"$power_cycle";
445 return $test_type eq "build" ||
446 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
447 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
451 doprint
"CRITICAL FAILURE... ", @_, "\n";
455 if ($reboot_on_error && !do_not_reboot
) {
457 doprint
"REBOOTING\n";
460 } elsif ($poweroff_on_error && defined($power_off)) {
461 doprint
"POWERING OFF\n";
465 if (defined($opt{"LOG_FILE"})) {
466 print " See $opt{LOG_FILE} for more info.\n";
477 my $pid = open($fp, "$console|") or
478 dodie
"Can't open console $console";
480 $flags = fcntl($fp, F_GETFL
, 0) or
481 dodie
"Can't get flags for the socket: $!";
482 $flags = fcntl($fp, F_SETFL
, $flags | O_NONBLOCK
) or
483 dodie
"Can't set flags for the socket: $!";
491 doprint
"kill child process $pid\n";
499 if ($monitor_cnt++) {
502 $monitor_fp = \
*MONFD
;
503 $monitor_pid = open_console
$monitor_fp;
507 open(MONFD
, "Stop perl from warning about single use of MONFD");
511 if (--$monitor_cnt) {
514 close_console
($monitor_fp, $monitor_pid);
517 sub wait_for_monitor
{
521 doprint
"** Wait for monitor to settle down **\n";
523 # read the monitor and wait for the system to calm down
525 $line = wait_for_input
($monitor_fp, $time);
526 print "$line" if (defined($line));
527 } while (defined($line));
528 print "** Monitor flushed **\n";
533 if ($die_on_failure) {
541 # no need to reboot for just building.
542 if (!do_not_reboot
) {
543 doprint
"REBOOTING\n";
546 wait_for_monitor
$sleep_time;
550 doprint
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
551 doprint
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
552 doprint
"KTEST RESULT: TEST $i Failed: ", @_, "\n";
553 doprint
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
554 doprint
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
556 return 1 if (!defined($store_failures));
559 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
560 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
562 my $type = $build_type;
563 if ($type =~ /useconfig/) {
567 my $dir = "$machine-$test_type-$type-fail-$date";
568 my $faildir = "$store_failures/$dir";
572 die "can't create $faildir";
574 if (-f
"$output_config") {
575 cp
"$output_config", "$faildir/config" or
576 die "failed to copy .config";
579 cp
$buildlog, "$faildir/buildlog" or
580 die "failed to move $buildlog";
583 cp
$dmesg, "$faildir/dmesg" or
584 die "failed to move $dmesg";
587 doprint
"*** Saved info to $faildir ***\n";
598 $command =~ s/\$SSH_USER/$ssh_user/g;
599 $command =~ s/\$MACHINE/$machine/g;
601 doprint
("$command ... ");
603 $pid = open(CMD
, "$command 2>&1 |") or
604 (fail
"unable to exec $command" and return 0);
606 if (defined($opt{"LOG_FILE"})) {
607 open(LOG
, ">>$opt{LOG_FILE}") or
608 dodie
"failed to write to log";
612 if (defined($redirect)) {
613 open (RD
, ">$redirect") or
614 dodie
"failed to write to redirect $redirect";
619 print LOG
if ($dolog);
627 close(LOG
) if ($dolog);
628 close(RD
) if ($dord);
641 my $cp_exec = $ssh_exec;
643 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
644 return run_command
"$cp_exec";
648 my ($src, $dst) = @_;
649 my $cp_scp = $scp_to_target;
651 $cp_scp =~ s/\$SRC_FILE/$src/g;
652 $cp_scp =~ s/\$DST_FILE/$dst/g;
654 return run_command
"$cp_scp";
659 if ($reboot_type ne "grub") {
662 return if (defined($grub_number));
664 doprint
"Find grub menu ... ";
667 my $ssh_grub = $ssh_exec;
668 $ssh_grub =~ s
,\
$SSH_COMMAND,cat
/boot/grub
/menu
.lst
,g
;
670 open(IN
, "$ssh_grub |")
671 or die "unable to get menu.lst";
674 if (/^\s*title\s+$grub_menu\s*$/) {
677 } elsif (/^\s*title\s/) {
683 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
684 if ($grub_number < 0);
685 doprint
"$grub_number\n";
690 my ($fp, $time) = @_;
696 if (!defined($time)) {
701 vec($rin, fileno($fp), 1) = 1;
702 $ready = select($rin, undef, undef, $time);
706 # try to read one char at a time
707 while (sysread $fp, $ch, 1) {
709 last if ($ch eq "\n");
712 if (!length($line)) {
720 if ($reboot_type eq "grub") {
721 run_ssh
"'(echo \"savedefault --default=$grub_number --once\" | grub --batch; reboot)'";
725 run_command
"$reboot_script";
731 doprint
"git rev-list --max-count=1 $commit ... ";
732 my $sha1 = `git rev-list --max-count=1 $commit`;
739 dodie
"Failed to get git $commit";
752 my $skip_call_trace = 0;
760 open(DMESG
, "> $dmesg") or
761 die "unable to write to $dmesg";
771 $line = wait_for_input
($monitor_fp, $booted_timeout);
773 $line = wait_for_input
($monitor_fp);
776 last if (!defined($line));
781 # we are not guaranteed to get a full line
784 if ($full_line =~ /$success_line/) {
786 $success_start = time;
789 if ($booted && defined($stop_after_success) &&
790 $stop_after_success >= 0) {
792 if ($now - $success_start >= $stop_after_success) {
793 doprint
"Test forced to stop after $stop_after_success seconds after success\n";
798 if ($full_line =~ /\[ backtrace testing \]/) {
799 $skip_call_trace = 1;
802 if ($full_line =~ /call trace:/i) {
803 if (!$skip_call_trace) {
805 $failure_start = time;
809 if ($bug && defined($stop_after_failure) &&
810 $stop_after_failure >= 0) {
812 if ($now - $failure_start >= $stop_after_failure) {
813 doprint
"Test forced to stop after $stop_after_failure seconds after failure\n";
818 if ($full_line =~ /\[ end of backtrace testing \]/) {
819 $skip_call_trace = 0;
822 if ($full_line =~ /Kernel panic -/) {
834 return 0 if ($in_bisect);
835 fail
"failed - got a bug report" and return 0;
839 return 0 if ($in_bisect);
840 fail
"failed - never got a boot prompt." and return 0;
848 run_scp
"$outputdir/$build_target", "$target_image" or
849 dodie
"failed to copy image";
851 my $install_mods = 0;
853 # should we process modules?
855 open(IN
, "$output_config") or dodie
("Can't read config file");
857 if (/CONFIG_MODULES(=y)?/) {
858 $install_mods = 1 if (defined($1));
864 if (!$install_mods) {
865 doprint
"No modules needed\n";
869 run_command
"$make INSTALL_MOD_PATH=$tmpdir modules_install" or
870 dodie
"Failed to install modules";
872 my $modlib = "/lib/modules/$version";
873 my $modtar = "ktest-mods.tar.bz2";
875 run_ssh
"rm -rf $modlib" or
876 dodie
"failed to remove old mods: $modlib";
878 # would be nice if scp -r did not follow symbolic links
879 run_command
"cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
880 dodie
"making tarball";
882 run_scp
"$tmpdir/$modtar", "/tmp" or
883 dodie
"failed to copy modules";
885 unlink "$tmpdir/$modtar";
887 run_ssh
"'(cd / && tar xf /tmp/$modtar)'" or
888 dodie
"failed to tar modules";
890 run_ssh
"rm -f /tmp/$modtar";
892 return if (!defined($post_install));
894 my $cp_post_install = $post_install;
895 $cp_post_install = s/\$KERNEL_VERSION/$version/g;
896 run_command
"$cp_post_install" or
897 dodie
"Failed to run post install";
903 my @files = `git show $patch | diffstat -l`;
905 open(IN
, "git show $patch |") or
906 dodie
"failed to show $patch";
908 if (m
,^--- a
/(.*),) {
910 $files[$#files] = $1;
915 open(IN
, $buildlog) or dodie
"Can't open $buildlog";
917 if (/^\s*(.*?):.*(warning|error)/) {
919 foreach my $file (@files) {
920 my $fullpath = "$builddir/$file";
921 if ($file eq $err || $fullpath eq $err) {
922 fail
"$file built with warnings" and return 0;
938 if ($type =~ /^useconfig:(.*)/) {
939 run_command
"cp $1 $output_config" or
940 dodie
"could not copy $1 to .config";
945 # old config can ask questions
946 if ($type eq "oldconfig") {
947 $type = "oldnoconfig";
949 # allow for empty configs
950 run_command
"touch $output_config";
952 run_command
"mv $output_config $outputdir/config_temp" or
953 dodie
"moving .config";
955 if (!$noclean && !run_command
"$make mrproper") {
956 dodie
"make mrproper";
959 run_command
"mv $outputdir/config_temp $output_config" or
960 dodie
"moving config_temp";
962 } elsif (!$noclean) {
963 unlink "$output_config";
964 run_command
"$make mrproper" or
965 dodie
"make mrproper";
968 # add something to distinguish this build
969 open(OUT
, "> $outputdir/localversion") or dodie
("Can't make localversion file");
970 print OUT
"$localversion\n";
973 if (defined($minconfig)) {
974 $defconfig = "KCONFIG_ALLCONFIG=$minconfig";
977 run_command
"$defconfig $make $type" or
978 dodie
"failed make config";
980 $redirect = "$buildlog";
981 if (!run_command
"$make $build_options") {
983 # bisect may need this to pass
984 return 0 if ($in_bisect);
985 fail
"failed build" and return 0;
993 if (!run_ssh
"halt" or defined($power_off)) {
994 if (defined($poweroff_after_halt)) {
995 sleep $poweroff_after_halt;
996 run_command
"$power_off";
1000 run_command
"$power_off";
1009 doprint
"\n\n*******************************************\n";
1010 doprint
"*******************************************\n";
1011 doprint
"KTEST RESULT: TEST $i SUCCESS!!!! **\n";
1012 doprint
"*******************************************\n";
1013 doprint
"*******************************************\n";
1015 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot
) {
1016 doprint
"Reboot and wait $sleep_time seconds\n";
1019 wait_for_monitor
$sleep_time;
1025 # get the release name
1026 doprint
"$make kernelrelease ... ";
1027 $version = `$make kernelrelease | tail -1`;
1029 doprint
"$version\n";
1032 sub child_run_test
{
1035 # child should have no power
1036 $reboot_on_error = 0;
1037 $poweroff_on_error = 0;
1038 $die_on_failure = 1;
1040 run_command
$run_test or $failed = 1;
1046 sub child_finished
{
1059 doprint
"run test $run_test\n";
1063 $SIG{CHLD
} = qw(child_finished);
1067 child_run_test
if (!$child_pid);
1072 $line = wait_for_input
($monitor_fp, 1);
1073 if (defined($line)) {
1075 # we are not guaranteed to get a full line
1076 $full_line .= $line;
1078 if ($full_line =~ /call trace:/i) {
1082 if ($full_line =~ /Kernel panic -/) {
1086 if ($line =~ /\n/) {
1090 } while (!$child_done && !$bug);
1093 doprint
"Detected kernel crash!\n";
1094 # kill the child with extreme prejudice
1098 waitpid $child_pid, 0;
1101 if ($bug || $child_exit) {
1102 return 0 if $in_bisect;
1103 fail
"test failed" and return 0;
1108 sub run_git_bisect
{
1111 doprint
"$command ... ";
1113 my $output = `$command 2>&1`;
1120 dodie
"Failed to git bisect";
1123 doprint
"SUCCESS\n";
1124 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
1125 doprint
"$1 [$2]\n";
1126 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
1128 doprint
"Found bad commit... $1\n";
1131 # we already logged it, just print it now.
1138 # returns 1 on success, 0 on failure
1139 sub run_bisect_test
{
1140 my ($type, $buildtype) = @_;
1149 build
$buildtype or $failed = 1;
1151 if ($type ne "build") {
1152 dodie
"Failed on build" if $failed;
1160 monitor
or $failed = 1;
1162 if ($type ne "boot") {
1163 dodie
"Failed on boot" if $failed;
1165 do_run_test
or $failed = 1;
1173 # reboot the box to a good kernel
1174 if ($type ne "build") {
1175 doprint
"Reboot and sleep $bisect_sleep_time seconds\n";
1178 wait_for_monitor
$bisect_sleep_time;
1191 my $buildtype = "oldconfig";
1193 # We should have a minconfig to use?
1194 if (defined($minconfig)) {
1195 $buildtype = "useconfig:$minconfig";
1198 my $ret = run_bisect_test
$type, $buildtype;
1201 # Are we looking for where it worked, not failed?
1202 if ($reverse_bisect) {
1218 die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
1219 die "BISECT_BAD[$i] not defined\n" if (!defined($opt{"BISECT_BAD[$i]"}));
1220 die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
1222 my $good = $opt{"BISECT_GOOD[$i]"};
1223 my $bad = $opt{"BISECT_BAD[$i]"};
1224 my $type = $opt{"BISECT_TYPE[$i]"};
1225 my $start = $opt{"BISECT_START[$i]"};
1226 my $replay = $opt{"BISECT_REPLAY[$i]"};
1228 # convert to true sha1's
1229 $good = get_sha1
($good);
1230 $bad = get_sha1
($bad);
1232 if (defined($opt{"BISECT_REVERSE[$i]"}) &&
1233 $opt{"BISECT_REVERSE[$i]"} == 1) {
1234 doprint
"Performing a reverse bisect (bad is good, good is bad!)\n";
1235 $reverse_bisect = 1;
1237 $reverse_bisect = 0;
1240 # Can't have a test without having a test to run
1241 if ($type eq "test" && !defined($run_test)) {
1245 my $check = $opt{"BISECT_CHECK[$i]"};
1246 if (defined($check) && $check ne "0") {
1249 my $head = get_sha1
("HEAD");
1251 if ($check ne "good") {
1252 doprint
"TESTING BISECT BAD [$bad]\n";
1253 run_command
"git checkout $bad" or
1254 die "Failed to checkout $bad";
1256 $result = run_bisect
$type;
1258 if ($result ne "bad") {
1259 fail
"Tested BISECT_BAD [$bad] and it succeeded" and return 0;
1263 if ($check ne "bad") {
1264 doprint
"TESTING BISECT GOOD [$good]\n";
1265 run_command
"git checkout $good" or
1266 die "Failed to checkout $good";
1268 $result = run_bisect
$type;
1270 if ($result ne "good") {
1271 fail
"Tested BISECT_GOOD [$good] and it failed" and return 0;
1275 # checkout where we started
1276 run_command
"git checkout $head" or
1277 die "Failed to checkout $head";
1280 run_command
"git bisect start" or
1281 dodie
"could not start bisect";
1283 run_command
"git bisect good $good" or
1284 dodie
"could not set bisect good to $good";
1286 run_git_bisect
"git bisect bad $bad" or
1287 dodie
"could not set bisect bad to $bad";
1289 if (defined($replay)) {
1290 run_command
"git bisect replay $replay" or
1291 dodie
"failed to run replay";
1294 if (defined($start)) {
1295 run_command
"git checkout $start" or
1296 dodie
"failed to checkout $start";
1301 $result = run_bisect
$type;
1302 $test = run_git_bisect
"git bisect $result";
1305 run_command
"git bisect log" or
1306 dodie
"could not capture git bisect log";
1308 run_command
"git bisect reset" or
1309 dodie
"could not reset git bisect";
1311 doprint
"Bad commit was [$bisect_bad]\n";
1324 sub process_config_ignore
{
1328 or dodie
"Failed to read $config";
1331 if (/^(.*?(CONFIG\S*)(=.*| is not set))/) {
1332 $config_ignore{$2} = $1;
1339 sub read_current_config
{
1340 my ($config_ref) = @_;
1342 %{$config_ref} = ();
1343 undef %{$config_ref};
1345 my @key = keys %{$config_ref};
1347 print "did not delete!\n";
1350 open (IN
, "$output_config");
1353 if (/^(CONFIG\S+)=(.*)/) {
1354 ${$config_ref}{$1} = $2;
1360 sub get_dependencies
{
1363 my $arr = $dependency{$config};
1364 if (!defined($arr)) {
1370 foreach my $dep (@
{$arr}) {
1371 print "ADD DEP $dep\n";
1372 @deps = (@deps, get_dependencies
$dep);
1381 open(OUT
, ">$output_config") or dodie
"Can not write to $output_config";
1383 foreach my $config (@configs) {
1384 print OUT
"$config_set{$config}\n";
1385 my @deps = get_dependencies
$config;
1386 foreach my $dep (@deps) {
1387 print OUT
"$config_set{$dep}\n";
1391 foreach my $config (keys %config_ignore) {
1392 print OUT
"$config_ignore{$config}\n";
1397 run_command
"$make oldnoconfig" or
1398 dodie
"failed make config oldconfig";
1402 sub compare_configs
{
1405 foreach my $item (keys %a) {
1406 if (!defined($b{$item})) {
1407 print "diff $item\n";
1415 print "diff2 $keys[0]\n";
1417 return -1 if ($#keys >= 0);
1422 sub run_config_bisect_test
{
1425 return run_bisect_test
$type, "oldconfig";
1428 sub process_passed
{
1431 doprint
"These configs had no failure: (Enabling them for further compiles)\n";
1432 # Passed! All these configs are part of a good compile.
1433 # Add them to the min options.
1434 foreach my $config (keys %configs) {
1435 if (defined($config_list{$config})) {
1436 doprint
" removing $config\n";
1437 $config_ignore{$config} = $config_list{$config};
1438 delete $config_list{$config};
1441 doprint
"config copied to $outputdir/config_good\n";
1442 run_command
"cp -f $output_config $outputdir/config_good";
1445 sub process_failed
{
1448 doprint
"\n\n***************************************\n";
1449 doprint
"Found bad config: $config\n";
1450 doprint
"***************************************\n\n";
1453 sub run_config_bisect
{
1455 my @start_list = keys %config_list;
1457 if ($#start_list < 0) {
1458 doprint
"No more configs to test!!!\n";
1462 doprint
"***** RUN TEST ***\n";
1463 my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
1467 my $count = $#start_list + 1;
1468 doprint
" $count configs to test\n";
1470 my $half = int($#start_list / 2);
1473 my @tophalf = @start_list[0 .. $half];
1475 create_config
@tophalf;
1476 read_current_config \
%current_config;
1478 $count = $#tophalf + 1;
1479 doprint
"Testing $count configs\n";
1481 # make sure we test something
1482 foreach my $config (@tophalf) {
1483 if (defined($current_config{$config})) {
1489 # try the other half
1490 doprint
"Top half produced no set configs, trying bottom half\n";
1491 @tophalf = @start_list[$half .. $#start_list];
1492 create_config
@tophalf;
1493 read_current_config \
%current_config;
1494 foreach my $config (@tophalf) {
1495 if (defined($current_config{$config})) {
1501 doprint
"Failed: Can't make new config with current configs\n";
1502 foreach my $config (@start_list) {
1503 doprint
" CONFIG: $config\n";
1507 $count = $#tophalf + 1;
1508 doprint
"Testing $count configs\n";
1511 $ret = run_config_bisect_test
$type;
1514 process_passed
%current_config;
1518 doprint
"This config had a failure.\n";
1519 doprint
"Removing these configs that were not set in this config:\n";
1520 doprint
"config copied to $outputdir/config_bad\n";
1521 run_command
"cp -f $output_config $outputdir/config_bad";
1523 # A config exists in this group that was bad.
1524 foreach my $config (keys %config_list) {
1525 if (!defined($current_config{$config})) {
1526 doprint
" removing $config\n";
1527 delete $config_list{$config};
1531 @start_list = @tophalf;
1533 if ($#start_list == 0) {
1534 process_failed
$start_list[0];
1538 # remove half the configs we are looking at and see if
1540 $half = int($#start_list / 2);
1541 } while ($half > 0);
1543 # we found a single config, try it again
1544 my @tophalf = @start_list[0 .. 0];
1546 $ret = run_config_bisect_test
$type;
1548 process_passed
%current_config;
1552 process_failed
$start_list[0];
1559 my $start_config = $opt{"CONFIG_BISECT[$i]"};
1561 my $tmpconfig = "$tmpdir/use_config";
1563 # Make the file with the bad config and the min config
1564 if (defined($minconfig)) {
1565 # read the min config for things to ignore
1566 run_command
"cp $minconfig $tmpconfig" or
1567 dodie
"failed to copy $minconfig to $tmpconfig";
1573 if (defined($addconfig)) {
1574 run_command
"cat $addconfig >> $tmpconfig" or
1575 dodie
"failed to append $addconfig";
1579 if (-f
$tmpconfig) {
1580 $defconfig = "KCONFIG_ALLCONFIG=$tmpconfig";
1581 process_config_ignore
$tmpconfig;
1584 # now process the start config
1585 run_command
"cp $start_config $output_config" or
1586 dodie
"failed to copy $start_config to $output_config";
1588 # read directly what we want to check
1590 open (IN
, $output_config)
1591 or dodie
"faied to open $output_config";
1594 if (/^((CONFIG\S*)=.*)/) {
1595 $config_check{$2} = $1;
1600 # Now run oldconfig with the minconfig (and addconfigs)
1601 run_command
"$defconfig $make oldnoconfig" or
1602 dodie
"failed make config oldconfig";
1604 # check to see what we lost (or gained)
1605 open (IN
, $output_config)
1606 or dodie
"Failed to read $start_config";
1608 my %removed_configs;
1612 if (/^((CONFIG\S*)=.*)/) {
1613 # save off all options
1614 $config_set{$2} = $1;
1615 if (defined($config_check{$2})) {
1616 if (defined($config_ignore{$2})) {
1617 $removed_configs{$2} = $1;
1619 $config_list{$2} = $1;
1621 } elsif (!defined($config_ignore{$2})) {
1622 $added_configs{$2} = $1;
1623 $config_list{$2} = $1;
1629 my @confs = keys %removed_configs;
1631 doprint
"Configs overridden by default configs and removed from check:\n";
1632 foreach my $config (@confs) {
1633 doprint
" $config\n";
1636 @confs = keys %added_configs;
1638 doprint
"Configs appearing in make oldconfig and added:\n";
1639 foreach my $config (@confs) {
1640 doprint
" $config\n";
1647 # Sometimes kconfig does weird things. We must make sure
1648 # that the config we autocreate has everything we need
1649 # to test, otherwise we may miss testing configs, or
1650 # may not be able to create a new config.
1651 # Here we create a config with everything set.
1652 create_config
(keys %config_list);
1653 read_current_config \
%config_test;
1654 foreach my $config (keys %config_list) {
1655 if (!defined($config_test{$config})) {
1658 doprint
"Configs not produced by kconfig (will not be checked):\n";
1660 doprint
" $config\n";
1661 delete $config_list{$config};
1666 $ret = run_config_bisect
;
1669 return $ret if ($ret < 0);
1677 die "PATCHCHECK_START[$i] not defined\n"
1678 if (!defined($opt{"PATCHCHECK_START[$i]"}));
1679 die "PATCHCHECK_TYPE[$i] not defined\n"
1680 if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
1682 my $start = $opt{"PATCHCHECK_START[$i]"};
1685 if (defined($opt{"PATCHCHECK_END[$i]"})) {
1686 $end = $opt{"PATCHCHECK_END[$i]"};
1689 # Get the true sha1's since we can use things like HEAD~3
1690 $start = get_sha1
($start);
1691 $end = get_sha1
($end);
1693 my $type = $opt{"PATCHCHECK_TYPE[$i]"};
1695 # Can't have a test without having a test to run
1696 if ($type eq "test" && !defined($run_test)) {
1700 open (IN
, "git log --pretty=oneline $end|") or
1701 dodie
"could not get git list";
1707 $list[$#list+1] = $_;
1708 last if (/^$start/);
1712 if ($list[$#list] !~ /^$start/) {
1713 fail
"SHA1 $start not found";
1716 # go backwards in the list
1717 @list = reverse @list;
1719 my $save_clean = $noclean;
1722 foreach my $item (@list) {
1724 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
1726 doprint
"\nProcessing commit $item\n\n";
1728 run_command
"git checkout $sha1" or
1729 die "Failed to checkout $sha1";
1731 # only clean on the first and last patch
1732 if ($item eq $list[0] ||
1733 $item eq $list[$#list]) {
1734 $noclean = $save_clean;
1739 if (defined($minconfig)) {
1740 build
"useconfig:$minconfig" or return 0;
1742 # ?? no config to use?
1743 build
"oldconfig" or return 0;
1746 check_buildlog
$sha1 or return 0;
1748 next if ($type eq "build");
1757 monitor
or $failed = 1;
1759 if (!$failed && $type ne "boot"){
1760 do_run_test
or $failed = 1;
1763 return 0 if ($failed);
1772 $#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
1775 $ktest_config = $ARGV[0];
1776 if (! -f
$ktest_config) {
1777 print "$ktest_config does not exist.\n";
1780 print "Create it? [Y/n] ";
1783 if ($ans =~ /^\s*$/) {
1786 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
1787 print "Please answer either 'y' or 'n'.\n";
1789 if ($ans !~ /^y$/i) {
1794 $ktest_config = "ktest.conf";
1797 if (! -f
$ktest_config) {
1798 open(OUT
, ">$ktest_config") or die "Can not create $ktest_config";
1800 # Generated by ktest.pl
1802 # Define each test with TEST_START
1803 # The config options below it will override the defaults
1811 read_config
$ktest_config;
1813 # Append any configs entered in manually to the config file.
1814 my @new_configs = keys %entered_configs;
1815 if ($#new_configs >= 0) {
1816 print "\nAppending entered in configs to $ktest_config\n";
1817 open(OUT
, ">>$ktest_config") or die "Can not append to $ktest_config";
1818 foreach my $config (@new_configs) {
1819 print OUT
"$config = $entered_configs{$config}\n";
1820 $opt{$config} = $entered_configs{$config};
1824 if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
1825 unlink $opt{"LOG_FILE"};
1828 doprint
"\n\nSTARTING AUTOMATED TESTS\n\n";
1830 for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
1833 doprint
"DEFAULT OPTIONS:\n";
1835 doprint
"\nTEST $i OPTIONS";
1836 if (defined($repeat_tests{$i})) {
1837 $repeat = $repeat_tests{$i};
1838 doprint
" ITERATE $repeat";
1843 foreach my $option (sort keys %opt) {
1845 if ($option =~ /\[(\d+)\]$/) {
1851 doprint
"$option = $opt{$option}\n";
1855 sub set_test_option
{
1856 my ($name, $i) = @_;
1858 my $option = "$name\[$i\]";
1860 if (defined($opt{$option})) {
1861 return $opt{$option};
1864 foreach my $test (keys %repeat_tests) {
1866 $i < $test + $repeat_tests{$test}) {
1867 $option = "$name\[$test\]";
1868 if (defined($opt{$option})) {
1869 return $opt{$option};
1874 if (defined($opt{$name})) {
1881 # First we need to do is the builds
1882 for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
1886 my $makecmd = set_test_option
("MAKE_CMD", $i);
1888 $machine = set_test_option
("MACHINE", $i);
1889 $ssh_user = set_test_option
("SSH_USER", $i);
1890 $tmpdir = set_test_option
("TMP_DIR", $i);
1891 $outputdir = set_test_option
("OUTPUT_DIR", $i);
1892 $builddir = set_test_option
("BUILD_DIR", $i);
1893 $test_type = set_test_option
("TEST_TYPE", $i);
1894 $build_type = set_test_option
("BUILD_TYPE", $i);
1895 $build_options = set_test_option
("BUILD_OPTIONS", $i);
1896 $power_cycle = set_test_option
("POWER_CYCLE", $i);
1897 $reboot = set_test_option
("REBOOT", $i);
1898 $noclean = set_test_option
("BUILD_NOCLEAN", $i);
1899 $minconfig = set_test_option
("MIN_CONFIG", $i);
1900 $run_test = set_test_option
("TEST", $i);
1901 $addconfig = set_test_option
("ADD_CONFIG", $i);
1902 $reboot_type = set_test_option
("REBOOT_TYPE", $i);
1903 $grub_menu = set_test_option
("GRUB_MENU", $i);
1904 $post_install = set_test_option
("POST_INSTALL", $i);
1905 $reboot_script = set_test_option
("REBOOT_SCRIPT", $i);
1906 $reboot_on_error = set_test_option
("REBOOT_ON_ERROR", $i);
1907 $poweroff_on_error = set_test_option
("POWEROFF_ON_ERROR", $i);
1908 $die_on_failure = set_test_option
("DIE_ON_FAILURE", $i);
1909 $power_off = set_test_option
("POWER_OFF", $i);
1910 $powercycle_after_reboot = set_test_option
("POWERCYCLE_AFTER_REBOOT", $i);
1911 $poweroff_after_halt = set_test_option
("POWEROFF_AFTER_HALT", $i);
1912 $sleep_time = set_test_option
("SLEEP_TIME", $i);
1913 $bisect_sleep_time = set_test_option
("BISECT_SLEEP_TIME", $i);
1914 $store_failures = set_test_option
("STORE_FAILURES", $i);
1915 $timeout = set_test_option
("TIMEOUT", $i);
1916 $booted_timeout = set_test_option
("BOOTED_TIMEOUT", $i);
1917 $console = set_test_option
("CONSOLE", $i);
1918 $success_line = set_test_option
("SUCCESS_LINE", $i);
1919 $stop_after_success = set_test_option
("STOP_AFTER_SUCCESS", $i);
1920 $stop_after_failure = set_test_option
("STOP_AFTER_FAILURE", $i);
1921 $build_target = set_test_option
("BUILD_TARGET", $i);
1922 $ssh_exec = set_test_option
("SSH_EXEC", $i);
1923 $scp_to_target = set_test_option
("SCP_TO_TARGET", $i);
1924 $target_image = set_test_option
("TARGET_IMAGE", $i);
1925 $localversion = set_test_option
("LOCALVERSION", $i);
1927 chdir $builddir || die "can't change directory to $builddir";
1931 die "can't create $tmpdir";
1934 $ENV{"SSH_USER"} = $ssh_user;
1935 $ENV{"MACHINE"} = $machine;
1937 $target = "$ssh_user\@$machine";
1939 $buildlog = "$tmpdir/buildlog-$machine";
1940 $dmesg = "$tmpdir/dmesg-$machine";
1941 $make = "$makecmd O=$outputdir";
1942 $output_config = "$outputdir/.config";
1944 if ($reboot_type eq "grub") {
1945 dodie
"GRUB_MENU not defined" if (!defined($grub_menu));
1946 } elsif (!defined($reboot_script)) {
1947 dodie
"REBOOT_SCRIPT not defined"
1950 my $run_type = $build_type;
1951 if ($test_type eq "patchcheck") {
1952 $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
1953 } elsif ($test_type eq "bisect") {
1954 $run_type = $opt{"BISECT_TYPE[$i]"};
1955 } elsif ($test_type eq "config_bisect") {
1956 $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
1959 # mistake in config file?
1960 if (!defined($run_type)) {
1961 $run_type = "ERROR";
1965 doprint
"RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type\n\n";
1970 if (!defined($minconfig)) {
1971 $minconfig = $addconfig;
1973 } elsif (defined($addconfig)) {
1974 run_command
"cat $addconfig $minconfig > $tmpdir/add_config" or
1975 dodie
"Failed to create temp config";
1976 $minconfig = "$tmpdir/add_config";
1979 my $checkout = $opt{"CHECKOUT[$i]"};
1980 if (defined($checkout)) {
1981 run_command
"git checkout $checkout" or
1982 die "failed to checkout $checkout";
1985 if ($test_type eq "bisect") {
1988 } elsif ($test_type eq "config_bisect") {
1991 } elsif ($test_type eq "patchcheck") {
1996 if ($build_type ne "nobuild") {
1997 build
$build_type or next;
2000 if ($test_type ne "build") {
2007 monitor
or $failed = 1;;
2009 if (!$failed && $test_type ne "boot" && defined($run_test)) {
2010 do_run_test
or $failed = 1;
2019 if ($opt{"POWEROFF_ON_SUCCESS"}) {
2021 } elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot
) {
2025 doprint
"\n $successes of $opt{NUM_TESTS} tests were successful\n\n";