3 # Copyright 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{"BISECT_MANUAL"} = 0;
41 $default{"BISECT_SKIP"} = 1;
42 $default{"SUCCESS_LINE"} = "login:";
43 $default{"BOOTED_TIMEOUT"} = 1;
44 $default{"DIE_ON_FAILURE"} = 1;
45 $default{"SSH_EXEC"} = "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND";
46 $default{"SCP_TO_TARGET"} = "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE";
47 $default{"REBOOT"} = "ssh \$SSH_USER\@\$MACHINE reboot";
48 $default{"STOP_AFTER_SUCCESS"} = 10;
49 $default{"STOP_AFTER_FAILURE"} = 60;
50 $default{"STOP_TEST_AFTER"} = 600;
51 $default{"LOCALVERSION"} = "-test";
69 my $poweroff_on_error;
71 my $powercycle_after_reboot;
72 my $poweroff_after_halt;
89 my $in_patchcheck = 0;
98 my $bisect_sleep_time;
104 my $stop_after_success;
105 my $stop_after_failure;
116 $config_help{"MACHINE"} = << "EOF"
117 The machine hostname that you will test.
120 $config_help{"SSH_USER"} = << "EOF"
121 The box is expected to have ssh on normal bootup, provide the user
122 (most likely root, since you need privileged operations)
125 $config_help{"BUILD_DIR"} = << "EOF"
126 The directory that contains the Linux source code (full path).
129 $config_help{"OUTPUT_DIR"} = << "EOF"
130 The directory that the objects will be built (full path).
131 (can not be same as BUILD_DIR)
134 $config_help{"BUILD_TARGET"} = << "EOF"
135 The location of the compiled file to copy to the target.
136 (relative to OUTPUT_DIR)
139 $config_help{"TARGET_IMAGE"} = << "EOF"
140 The place to put your image on the test machine.
143 $config_help{"POWER_CYCLE"} = << "EOF"
144 A script or command to reboot the box.
146 Here is a digital loggers power switch example
147 POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
149 Here is an example to reboot a virtual box on the current host
150 with the name "Guest".
151 POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
154 $config_help{"CONSOLE"} = << "EOF"
155 The script or command that reads the console
157 If you use ttywatch server, something like the following would work.
158 CONSOLE = nc -d localhost 3001
160 For a virtual machine with guest name "Guest".
161 CONSOLE = virsh console Guest
164 $config_help{"LOCALVERSION"} = << "EOF"
165 Required version ending to differentiate the test
166 from other linux builds on the system.
169 $config_help{"REBOOT_TYPE"} = << "EOF"
170 Way to reboot the box to the test kernel.
171 Only valid options so far are "grub" and "script".
173 If you specify grub, it will assume grub version 1
174 and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
175 and select that target to reboot to the kernel. If this is not
176 your setup, then specify "script" and have a command or script
177 specified in REBOOT_SCRIPT to boot to the target.
179 The entry in /boot/grub/menu.lst must be entered in manually.
180 The test will not modify that file.
183 $config_help{"GRUB_MENU"} = << "EOF"
184 The grub title name for the test kernel to boot
185 (Only mandatory if REBOOT_TYPE = grub)
187 Note, ktest.pl will not update the grub menu.lst, you need to
188 manually add an option for the test. ktest.pl will search
189 the grub menu.lst for this option to find what kernel to
192 For example, if in the /boot/grub/menu.lst the test kernel title has:
195 GRUB_MENU = Test Kernel
198 $config_help{"REBOOT_SCRIPT"} = << "EOF"
199 A script to reboot the target into the test kernel
200 (Only mandatory if REBOOT_TYPE = script)
205 sub get_ktest_config {
208 return if (defined($opt{$config}));
210 if (defined($config_help{$config})) {
212 print $config_help{$config};
217 if (defined($default{$config})) {
218 print "\[$default{$config}\] ";
220 $entered_configs{$config} = <STDIN>;
221 $entered_configs{$config} =~ s/^\s*(.*\S)\s*$/$1/;
222 if ($entered_configs{$config} =~ /^\s*$/) {
223 if ($default{$config}) {
224 $entered_configs{$config} = $default{$config};
226 print "Your answer can not be blank\n";
234 sub get_ktest_configs {
235 get_ktest_config("MACHINE");
236 get_ktest_config("SSH_USER");
237 get_ktest_config("BUILD_DIR");
238 get_ktest_config("OUTPUT_DIR");
239 get_ktest_config("BUILD_TARGET");
240 get_ktest_config("TARGET_IMAGE");
241 get_ktest_config("POWER_CYCLE");
242 get_ktest_config("CONSOLE");
243 get_ktest_config("LOCALVERSION");
245 my $rtype = $opt{"REBOOT_TYPE"};
247 if (!defined($rtype)) {
248 if (!defined($opt{"GRUB_MENU"})) {
249 get_ktest_config("REBOOT_TYPE");
250 $rtype = $entered_configs{"REBOOT_TYPE"};
256 if ($rtype eq "grub") {
257 get_ktest_config("GRUB_MENU");
259 get_ktest_config("REBOOT_SCRIPT");
264 my ($lvalue, $rvalue) = @_;
266 if (defined($opt{$lvalue})) {
267 die "Error: Option $lvalue defined more than once!\n";
269 if ($rvalue =~ /^\s*$/) {
270 delete $opt{$lvalue};
272 $opt{$lvalue} = $rvalue;
279 open(IN, $config) || die "can't read file $config";
282 $name =~ s,.*/(.*),$1,;
287 my $num_tests_set = 0;
293 # ignore blank lines and comments
294 next if (/^\s*$/ || /\s*\#/);
296 if (/^\s*TEST_START(.*)/) {
300 if ($num_tests_set) {
301 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
304 my $old_test_num = $test_num;
305 my $old_repeat = $repeat;
307 $test_num += $repeat;
311 if ($rest =~ /\s+SKIP(.*)/) {
318 if ($rest =~ /\s+ITERATE\s+(\d+)(.*)$/) {
321 $repeat_tests{"$test_num"} = $repeat;
324 if ($rest =~ /\s+SKIP(.*)/) {
329 if ($rest !~ /^\s*$/) {
330 die "$name: $.: Gargbage found after TEST_START\n$_";
334 $test_num = $old_test_num;
335 $repeat = $old_repeat;
338 } elsif (/^\s*DEFAULTS(.*)$/) {
343 if ($rest =~ /\s+SKIP(.*)/) {
350 if ($rest !~ /^\s*$/) {
351 die "$name: $.: Gargbage found after DEFAULTS\n$_";
354 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
362 ($lvalue eq "NUM_TESTS" ||
363 $lvalue eq "LOG_FILE" ||
364 $lvalue eq "CLEAR_LOG")) {
365 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
368 if ($lvalue eq "NUM_TESTS") {
370 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
373 die "$name: $.: NUM_TESTS must be set in default section\n";
378 if ($default || $lvalue =~ /\[\d+\]$/) {
379 set_value($lvalue, $rvalue);
381 my $val = "$lvalue\[$test_num\]";
382 set_value($val, $rvalue);
385 $repeats{$val} = $repeat;
389 die "$name: $.: Garbage found in config\n$_";
396 $test_num += $repeat - 1;
397 $opt{"NUM_TESTS"} = $test_num;
400 # make sure we have all mandatory configs
405 foreach my $default (keys %default) {
406 if (!defined($opt{$default})) {
407 $opt{$default} = $default{$default};
413 if (defined($opt{"LOG_FILE"})) {
414 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
421 if (defined($opt{"LOG_FILE"})) {
436 # try to reboot normally
437 if (run_command $reboot) {
438 if (defined($powercycle_after_reboot)) {
439 sleep $powercycle_after_reboot;
440 run_command "$power_cycle";
443 # nope? power cycle it.
444 run_command "$power_cycle";
451 return $test_type eq "build" ||
452 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
453 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
457 doprint "CRITICAL FAILURE... ", @_, "\n";
461 if ($reboot_on_error && !do_not_reboot) {
463 doprint "REBOOTING\n";
466 } elsif ($poweroff_on_error && defined($power_off)) {
467 doprint "POWERING OFF\n";
471 if (defined($opt{"LOG_FILE"})) {
472 print " See $opt{LOG_FILE} for more info.\n";
483 my $pid = open($fp, "$console|") or
484 dodie "Can't open console $console";
486 $flags = fcntl($fp, F_GETFL, 0) or
487 dodie "Can't get flags for the socket: $!";
488 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
489 dodie "Can't set flags for the socket: $!";
497 doprint "kill child process $pid\n";
505 if ($monitor_cnt++) {
508 $monitor_fp = \*MONFD;
509 $monitor_pid = open_console $monitor_fp;
513 open(MONFD, "Stop perl from warning about single use of MONFD");
517 if (--$monitor_cnt) {
520 close_console($monitor_fp, $monitor_pid);
523 sub wait_for_monitor {
527 doprint "** Wait for monitor to settle down **\n";
529 # read the monitor and wait for the system to calm down
531 $line = wait_for_input($monitor_fp, $time);
532 print "$line" if (defined($line));
533 } while (defined($line));
534 print "** Monitor flushed **\n";
539 if ($die_on_failure) {
547 # no need to reboot for just building.
548 if (!do_not_reboot) {
549 doprint "REBOOTING\n";
552 wait_for_monitor $sleep_time;
556 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
557 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
558 doprint "KTEST RESULT: TEST $i Failed: ", @_, "\n";
559 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
560 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
562 return 1 if (!defined($store_failures));
565 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
566 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
568 my $type = $build_type;
569 if ($type =~ /useconfig/) {
573 my $dir = "$machine-$test_type-$type-fail-$date";
574 my $faildir = "$store_failures/$dir";
578 die "can't create $faildir";
580 if (-f "$output_config") {
581 cp "$output_config", "$faildir/config" or
582 die "failed to copy .config";
585 cp $buildlog, "$faildir/buildlog" or
586 die "failed to move $buildlog";
589 cp $dmesg, "$faildir/dmesg" or
590 die "failed to move $dmesg";
593 doprint "*** Saved info to $faildir ***\n";
604 $command =~ s/\$SSH_USER/$ssh_user/g;
605 $command =~ s/\$MACHINE/$machine/g;
607 doprint("$command ... ");
609 $pid = open(CMD, "$command 2>&1 |") or
610 (fail "unable to exec $command" and return 0);
612 if (defined($opt{"LOG_FILE"})) {
613 open(LOG, ">>$opt{LOG_FILE}") or
614 dodie "failed to write to log";
618 if (defined($redirect)) {
619 open (RD, ">$redirect") or
620 dodie "failed to write to redirect $redirect";
625 print LOG if ($dolog);
633 close(LOG) if ($dolog);
634 close(RD) if ($dord);
647 my $cp_exec = $ssh_exec;
649 $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
650 return run_command "$cp_exec";
654 my ($src, $dst) = @_;
655 my $cp_scp = $scp_to_target;
657 $cp_scp =~ s/\$SRC_FILE/$src/g;
658 $cp_scp =~ s/\$DST_FILE/$dst/g;
660 return run_command "$cp_scp";
665 if ($reboot_type ne "grub") {
668 return if (defined($grub_number));
670 doprint "Find grub menu ... ";
673 my $ssh_grub = $ssh_exec;
674 $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
676 open(IN, "$ssh_grub |")
677 or die "unable to get menu.lst";
680 if (/^\s*title\s+$grub_menu\s*$/) {
683 } elsif (/^\s*title\s/) {
689 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
690 if ($grub_number < 0);
691 doprint "$grub_number\n";
696 my ($fp, $time) = @_;
702 if (!defined($time)) {
707 vec($rin, fileno($fp), 1) = 1;
708 $ready = select($rin, undef, undef, $time);
712 # try to read one char at a time
713 while (sysread $fp, $ch, 1) {
715 last if ($ch eq "\n");
718 if (!length($line)) {
726 if ($reboot_type eq "grub") {
727 run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch; reboot)'";
731 run_command "$reboot_script";
737 doprint "git rev-list --max-count=1 $commit ... ";
738 my $sha1 = `git rev-list --max-count=1 $commit`;
745 dodie "Failed to get git $commit";
758 my $skip_call_trace = 0;
766 open(DMESG, "> $dmesg") or
767 die "unable to write to $dmesg";
773 my $monitor_start = time;
779 $line = wait_for_input($monitor_fp, $booted_timeout);
781 $line = wait_for_input($monitor_fp);
784 last if (!defined($line));
789 # we are not guaranteed to get a full line
792 if ($full_line =~ /$success_line/) {
794 $success_start = time;
797 if ($booted && defined($stop_after_success) &&
798 $stop_after_success >= 0) {
800 if ($now - $success_start >= $stop_after_success) {
801 doprint "Test forced to stop after $stop_after_success seconds after success\n";
806 if ($full_line =~ /\[ backtrace testing \]/) {
807 $skip_call_trace = 1;
810 if ($full_line =~ /call trace:/i) {
811 if (!$bug && !$skip_call_trace) {
813 $failure_start = time;
817 if ($bug && defined($stop_after_failure) &&
818 $stop_after_failure >= 0) {
820 if ($now - $failure_start >= $stop_after_failure) {
821 doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
826 if ($full_line =~ /\[ end of backtrace testing \]/) {
827 $skip_call_trace = 0;
830 if ($full_line =~ /Kernel panic -/) {
831 $failure_start = time;
839 if ($stop_test_after > 0 && !$booted && !$bug) {
840 if (time - $monitor_start > $stop_test_after) {
841 doprint "STOP_TEST_AFTER ($stop_test_after seconds) timed out\n";
850 return 0 if ($in_bisect);
851 fail "failed - got a bug report" and return 0;
855 return 0 if ($in_bisect);
856 fail "failed - never got a boot prompt." and return 0;
864 run_scp "$outputdir/$build_target", "$target_image" or
865 dodie "failed to copy image";
867 my $install_mods = 0;
869 # should we process modules?
871 open(IN, "$output_config") or dodie("Can't read config file");
873 if (/CONFIG_MODULES(=y)?/) {
874 $install_mods = 1 if (defined($1));
880 if (!$install_mods) {
881 doprint "No modules needed\n";
885 run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
886 dodie "Failed to install modules";
888 my $modlib = "/lib/modules/$version";
889 my $modtar = "ktest-mods.tar.bz2";
891 run_ssh "rm -rf $modlib" or
892 dodie "failed to remove old mods: $modlib";
894 # would be nice if scp -r did not follow symbolic links
895 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
896 dodie "making tarball";
898 run_scp "$tmpdir/$modtar", "/tmp" or
899 dodie "failed to copy modules";
901 unlink "$tmpdir/$modtar";
903 run_ssh "'(cd / && tar xf /tmp/$modtar)'" or
904 dodie "failed to tar modules";
906 run_ssh "rm -f /tmp/$modtar";
908 return if (!defined($post_install));
910 my $cp_post_install = $post_install;
911 $cp_post_install =~ s/\$KERNEL_VERSION/$version/g;
912 run_command "$cp_post_install" or
913 dodie "Failed to run post install";
919 my @files = `git show $patch | diffstat -l`;
921 open(IN, "git show $patch |") or
922 dodie "failed to show $patch";
924 if (m,^--- a/(.*),) {
926 $files[$#files] = $1;
931 open(IN, $buildlog) or dodie "Can't open $buildlog";
933 if (/^\s*(.*?):.*(warning|error)/) {
935 foreach my $file (@files) {
936 my $fullpath = "$builddir/$file";
937 if ($file eq $err || $fullpath eq $err) {
938 fail "$file built with warnings" and return 0;
949 my ($defconfig) = @_;
951 if (!run_command "$defconfig $make oldnoconfig") {
952 # Perhaps oldnoconfig doesn't exist in this version of the kernel
953 # try a yes '' | oldconfig
954 doprint "oldnoconfig failed, trying yes '' | make oldconfig\n";
955 run_command "yes '' | $defconfig $make oldconfig" or
956 dodie "failed make config oldconfig";
966 if ($type =~ /^useconfig:(.*)/) {
967 run_command "cp $1 $output_config" or
968 dodie "could not copy $1 to .config";
973 # old config can ask questions
974 if ($type eq "oldconfig") {
975 $type = "oldnoconfig";
977 # allow for empty configs
978 run_command "touch $output_config";
980 run_command "mv $output_config $outputdir/config_temp" or
981 dodie "moving .config";
983 if (!$noclean && !run_command "$make mrproper") {
984 dodie "make mrproper";
987 run_command "mv $outputdir/config_temp $output_config" or
988 dodie "moving config_temp";
990 } elsif (!$noclean) {
991 unlink "$output_config";
992 run_command "$make mrproper" or
993 dodie "make mrproper";
996 # add something to distinguish this build
997 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
998 print OUT "$localversion\n";
1001 if (defined($minconfig)) {
1002 $defconfig = "KCONFIG_ALLCONFIG=$minconfig";
1005 if ($type eq "oldnoconfig") {
1006 make_oldconfig $defconfig;
1008 run_command "$defconfig $make $type" or
1009 dodie "failed make config";
1012 $redirect = "$buildlog";
1013 if (!run_command "$make $build_options") {
1015 # bisect may need this to pass
1016 return 0 if ($in_bisect);
1017 fail "failed build" and return 0;
1025 if (!run_ssh "halt" or defined($power_off)) {
1026 if (defined($poweroff_after_halt)) {
1027 sleep $poweroff_after_halt;
1028 run_command "$power_off";
1032 run_command "$power_off";
1041 doprint "\n\n*******************************************\n";
1042 doprint "*******************************************\n";
1043 doprint "KTEST RESULT: TEST $i SUCCESS!!!! **\n";
1044 doprint "*******************************************\n";
1045 doprint "*******************************************\n";
1047 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
1048 doprint "Reboot and wait $sleep_time seconds\n";
1051 wait_for_monitor $sleep_time;
1057 # get the release name
1058 doprint "$make kernelrelease ... ";
1059 $version = `$make kernelrelease | tail -1`;
1061 doprint "$version\n";
1066 doprint "Pass or fail? [p/f]";
1069 if ($ans eq "p" || $ans eq "P") {
1071 } elsif ($ans eq "f" || $ans eq "F") {
1074 print "Please answer 'P' or 'F'\n";
1079 sub child_run_test {
1082 # child should have no power
1083 $reboot_on_error = 0;
1084 $poweroff_on_error = 0;
1085 $die_on_failure = 1;
1087 run_command $run_test or $failed = 1;
1093 sub child_finished {
1106 doprint "run test $run_test\n";
1110 $SIG{CHLD} = qw(child_finished);
1114 child_run_test if (!$child_pid);
1119 $line = wait_for_input($monitor_fp, 1);
1120 if (defined($line)) {
1122 # we are not guaranteed to get a full line
1123 $full_line .= $line;
1126 if ($full_line =~ /call trace:/i) {
1130 if ($full_line =~ /Kernel panic -/) {
1134 if ($line =~ /\n/) {
1138 } while (!$child_done && !$bug);
1141 my $failure_start = time;
1144 $line = wait_for_input($monitor_fp, 1);
1145 if (defined($line)) {
1149 if ($now - $failure_start >= $stop_after_failure) {
1152 } while (defined($line));
1154 doprint "Detected kernel crash!\n";
1155 # kill the child with extreme prejudice
1159 waitpid $child_pid, 0;
1162 if ($bug || $child_exit) {
1163 return 0 if $in_bisect;
1164 fail "test failed" and return 0;
1169 sub run_git_bisect {
1172 doprint "$command ... ";
1174 my $output = `$command 2>&1`;
1181 dodie "Failed to git bisect";
1184 doprint "SUCCESS\n";
1185 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
1186 doprint "$1 [$2]\n";
1187 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
1189 doprint "Found bad commit... $1\n";
1192 # we already logged it, just print it now.
1200 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
1203 wait_for_monitor $bisect_sleep_time;
1207 # returns 1 on success, 0 on failure, -1 on skip
1208 sub run_bisect_test {
1209 my ($type, $buildtype) = @_;
1218 build $buildtype or $failed = 1;
1220 if ($type ne "build") {
1221 if ($failed && $bisect_skip) {
1225 dodie "Failed on build" if $failed;
1233 monitor or $failed = 1;
1235 if ($type ne "boot") {
1236 if ($failed && $bisect_skip) {
1242 dodie "Failed on boot" if $failed;
1244 do_run_test or $failed = 1;
1255 # reboot the box to a kernel we can ssh to
1256 if ($type ne "build") {
1266 my $buildtype = "oldconfig";
1268 # We should have a minconfig to use?
1269 if (defined($minconfig)) {
1270 $buildtype = "useconfig:$minconfig";
1273 my $ret = run_bisect_test $type, $buildtype;
1275 if ($bisect_manual) {
1276 $ret = answer_bisect;
1279 # Are we looking for where it worked, not failed?
1280 if ($reverse_bisect) {
1286 } elsif ($ret == 0) {
1288 } elsif ($bisect_skip) {
1289 doprint "HIT A BAD COMMIT ... SKIPPING\n";
1299 die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
1300 die "BISECT_BAD[$i] not defined\n" if (!defined($opt{"BISECT_BAD[$i]"}));
1301 die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
1303 my $good = $opt{"BISECT_GOOD[$i]"};
1304 my $bad = $opt{"BISECT_BAD[$i]"};
1305 my $type = $opt{"BISECT_TYPE[$i]"};
1306 my $start = $opt{"BISECT_START[$i]"};
1307 my $replay = $opt{"BISECT_REPLAY[$i]"};
1308 my $start_files = $opt{"BISECT_FILES[$i]"};
1310 if (defined($start_files)) {
1311 $start_files = " -- " . $start_files;
1316 # convert to true sha1's
1317 $good = get_sha1($good);
1318 $bad = get_sha1($bad);
1320 if (defined($opt{"BISECT_REVERSE[$i]"}) &&
1321 $opt{"BISECT_REVERSE[$i]"} == 1) {
1322 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
1323 $reverse_bisect = 1;
1325 $reverse_bisect = 0;
1328 # Can't have a test without having a test to run
1329 if ($type eq "test" && !defined($run_test)) {
1333 my $check = $opt{"BISECT_CHECK[$i]"};
1334 if (defined($check) && $check ne "0") {
1337 my $head = get_sha1("HEAD");
1339 if ($check ne "good") {
1340 doprint "TESTING BISECT BAD [$bad]\n";
1341 run_command "git checkout $bad" or
1342 die "Failed to checkout $bad";
1344 $result = run_bisect $type;
1346 if ($result ne "bad") {
1347 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
1351 if ($check ne "bad") {
1352 doprint "TESTING BISECT GOOD [$good]\n";
1353 run_command "git checkout $good" or
1354 die "Failed to checkout $good";
1356 $result = run_bisect $type;
1358 if ($result ne "good") {
1359 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
1363 # checkout where we started
1364 run_command "git checkout $head" or
1365 die "Failed to checkout $head";
1368 run_command "git bisect start$start_files" or
1369 dodie "could not start bisect";
1371 run_command "git bisect good $good" or
1372 dodie "could not set bisect good to $good";
1374 run_git_bisect "git bisect bad $bad" or
1375 dodie "could not set bisect bad to $bad";
1377 if (defined($replay)) {
1378 run_command "git bisect replay $replay" or
1379 dodie "failed to run replay";
1382 if (defined($start)) {
1383 run_command "git checkout $start" or
1384 dodie "failed to checkout $start";
1389 $result = run_bisect $type;
1390 $test = run_git_bisect "git bisect $result";
1393 run_command "git bisect log" or
1394 dodie "could not capture git bisect log";
1396 run_command "git bisect reset" or
1397 dodie "could not reset git bisect";
1399 doprint "Bad commit was [$bisect_bad]\n";
1412 sub process_config_ignore {
1416 or dodie "Failed to read $config";
1419 if (/^(.*?(CONFIG\S*)(=.*| is not set))/) {
1420 $config_ignore{$2} = $1;
1427 sub read_current_config {
1428 my ($config_ref) = @_;
1430 %{$config_ref} = ();
1431 undef %{$config_ref};
1433 my @key = keys %{$config_ref};
1435 print "did not delete!\n";
1438 open (IN, "$output_config");
1441 if (/^(CONFIG\S+)=(.*)/) {
1442 ${$config_ref}{$1} = $2;
1448 sub get_dependencies {
1451 my $arr = $dependency{$config};
1452 if (!defined($arr)) {
1458 foreach my $dep (@{$arr}) {
1459 print "ADD DEP $dep\n";
1460 @deps = (@deps, get_dependencies $dep);
1469 open(OUT, ">$output_config") or dodie "Can not write to $output_config";
1471 foreach my $config (@configs) {
1472 print OUT "$config_set{$config}\n";
1473 my @deps = get_dependencies $config;
1474 foreach my $dep (@deps) {
1475 print OUT "$config_set{$dep}\n";
1479 foreach my $config (keys %config_ignore) {
1480 print OUT "$config_ignore{$config}\n";
1488 sub compare_configs {
1491 foreach my $item (keys %a) {
1492 if (!defined($b{$item})) {
1493 print "diff $item\n";
1501 print "diff2 $keys[0]\n";
1503 return -1 if ($#keys >= 0);
1508 sub run_config_bisect_test {
1511 return run_bisect_test $type, "oldconfig";
1514 sub process_passed {
1517 doprint "These configs had no failure: (Enabling them for further compiles)\n";
1518 # Passed! All these configs are part of a good compile.
1519 # Add them to the min options.
1520 foreach my $config (keys %configs) {
1521 if (defined($config_list{$config})) {
1522 doprint " removing $config\n";
1523 $config_ignore{$config} = $config_list{$config};
1524 delete $config_list{$config};
1527 doprint "config copied to $outputdir/config_good\n";
1528 run_command "cp -f $output_config $outputdir/config_good";
1531 sub process_failed {
1534 doprint "\n\n***************************************\n";
1535 doprint "Found bad config: $config\n";
1536 doprint "***************************************\n\n";
1539 sub run_config_bisect {
1541 my @start_list = keys %config_list;
1543 if ($#start_list < 0) {
1544 doprint "No more configs to test!!!\n";
1548 doprint "***** RUN TEST ***\n";
1549 my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
1553 my $count = $#start_list + 1;
1554 doprint " $count configs to test\n";
1556 my $half = int($#start_list / 2);
1559 my @tophalf = @start_list[0 .. $half];
1561 create_config @tophalf;
1562 read_current_config \%current_config;
1564 $count = $#tophalf + 1;
1565 doprint "Testing $count configs\n";
1567 # make sure we test something
1568 foreach my $config (@tophalf) {
1569 if (defined($current_config{$config})) {
1575 # try the other half
1576 doprint "Top half produced no set configs, trying bottom half\n";
1577 @tophalf = @start_list[$half .. $#start_list];
1578 create_config @tophalf;
1579 read_current_config \%current_config;
1580 foreach my $config (@tophalf) {
1581 if (defined($current_config{$config})) {
1587 doprint "Failed: Can't make new config with current configs\n";
1588 foreach my $config (@start_list) {
1589 doprint " CONFIG: $config\n";
1593 $count = $#tophalf + 1;
1594 doprint "Testing $count configs\n";
1597 $ret = run_config_bisect_test $type;
1598 if ($bisect_manual) {
1599 $ret = answer_bisect;
1602 process_passed %current_config;
1606 doprint "This config had a failure.\n";
1607 doprint "Removing these configs that were not set in this config:\n";
1608 doprint "config copied to $outputdir/config_bad\n";
1609 run_command "cp -f $output_config $outputdir/config_bad";
1611 # A config exists in this group that was bad.
1612 foreach my $config (keys %config_list) {
1613 if (!defined($current_config{$config})) {
1614 doprint " removing $config\n";
1615 delete $config_list{$config};
1619 @start_list = @tophalf;
1621 if ($#start_list == 0) {
1622 process_failed $start_list[0];
1626 # remove half the configs we are looking at and see if
1628 $half = int($#start_list / 2);
1629 } while ($half > 0);
1631 # we found a single config, try it again unless we are running manually
1633 if ($bisect_manual) {
1634 process_failed $start_list[0];
1638 my @tophalf = @start_list[0 .. 0];
1640 $ret = run_config_bisect_test $type;
1642 process_passed %current_config;
1646 process_failed $start_list[0];
1653 my $start_config = $opt{"CONFIG_BISECT[$i]"};
1655 my $tmpconfig = "$tmpdir/use_config";
1657 # Make the file with the bad config and the min config
1658 if (defined($minconfig)) {
1659 # read the min config for things to ignore
1660 run_command "cp $minconfig $tmpconfig" or
1661 dodie "failed to copy $minconfig to $tmpconfig";
1667 if (defined($addconfig)) {
1668 run_command "cat $addconfig >> $tmpconfig" or
1669 dodie "failed to append $addconfig";
1673 if (-f $tmpconfig) {
1674 $defconfig = "KCONFIG_ALLCONFIG=$tmpconfig";
1675 process_config_ignore $tmpconfig;
1678 # now process the start config
1679 run_command "cp $start_config $output_config" or
1680 dodie "failed to copy $start_config to $output_config";
1682 # read directly what we want to check
1684 open (IN, $output_config)
1685 or dodie "faied to open $output_config";
1688 if (/^((CONFIG\S*)=.*)/) {
1689 $config_check{$2} = $1;
1694 # Now run oldconfig with the minconfig (and addconfigs)
1695 make_oldconfig $defconfig;
1697 # check to see what we lost (or gained)
1698 open (IN, $output_config)
1699 or dodie "Failed to read $start_config";
1701 my %removed_configs;
1705 if (/^((CONFIG\S*)=.*)/) {
1706 # save off all options
1707 $config_set{$2} = $1;
1708 if (defined($config_check{$2})) {
1709 if (defined($config_ignore{$2})) {
1710 $removed_configs{$2} = $1;
1712 $config_list{$2} = $1;
1714 } elsif (!defined($config_ignore{$2})) {
1715 $added_configs{$2} = $1;
1716 $config_list{$2} = $1;
1722 my @confs = keys %removed_configs;
1724 doprint "Configs overridden by default configs and removed from check:\n";
1725 foreach my $config (@confs) {
1726 doprint " $config\n";
1729 @confs = keys %added_configs;
1731 doprint "Configs appearing in make oldconfig and added:\n";
1732 foreach my $config (@confs) {
1733 doprint " $config\n";
1740 # Sometimes kconfig does weird things. We must make sure
1741 # that the config we autocreate has everything we need
1742 # to test, otherwise we may miss testing configs, or
1743 # may not be able to create a new config.
1744 # Here we create a config with everything set.
1745 create_config (keys %config_list);
1746 read_current_config \%config_test;
1747 foreach my $config (keys %config_list) {
1748 if (!defined($config_test{$config})) {
1751 doprint "Configs not produced by kconfig (will not be checked):\n";
1753 doprint " $config\n";
1754 delete $config_list{$config};
1759 $ret = run_config_bisect;
1762 return $ret if ($ret < 0);
1770 die "PATCHCHECK_START[$i] not defined\n"
1771 if (!defined($opt{"PATCHCHECK_START[$i]"}));
1772 die "PATCHCHECK_TYPE[$i] not defined\n"
1773 if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
1775 my $start = $opt{"PATCHCHECK_START[$i]"};
1778 if (defined($opt{"PATCHCHECK_END[$i]"})) {
1779 $end = $opt{"PATCHCHECK_END[$i]"};
1782 # Get the true sha1's since we can use things like HEAD~3
1783 $start = get_sha1($start);
1784 $end = get_sha1($end);
1786 my $type = $opt{"PATCHCHECK_TYPE[$i]"};
1788 # Can't have a test without having a test to run
1789 if ($type eq "test" && !defined($run_test)) {
1793 open (IN, "git log --pretty=oneline $end|") or
1794 dodie "could not get git list";
1800 $list[$#list+1] = $_;
1801 last if (/^$start/);
1805 if ($list[$#list] !~ /^$start/) {
1806 fail "SHA1 $start not found";
1809 # go backwards in the list
1810 @list = reverse @list;
1812 my $save_clean = $noclean;
1815 foreach my $item (@list) {
1817 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
1819 doprint "\nProcessing commit $item\n\n";
1821 run_command "git checkout $sha1" or
1822 die "Failed to checkout $sha1";
1824 # only clean on the first and last patch
1825 if ($item eq $list[0] ||
1826 $item eq $list[$#list]) {
1827 $noclean = $save_clean;
1832 if (defined($minconfig)) {
1833 build "useconfig:$minconfig" or return 0;
1835 # ?? no config to use?
1836 build "oldconfig" or return 0;
1839 check_buildlog $sha1 or return 0;
1841 next if ($type eq "build");
1850 monitor or $failed = 1;
1852 if (!$failed && $type ne "boot"){
1853 do_run_test or $failed = 1;
1856 return 0 if ($failed);
1865 $#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
1868 $ktest_config = $ARGV[0];
1869 if (! -f $ktest_config) {
1870 print "$ktest_config does not exist.\n";
1873 print "Create it? [Y/n] ";
1876 if ($ans =~ /^\s*$/) {
1879 last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
1880 print "Please answer either 'y' or 'n'.\n";
1882 if ($ans !~ /^y$/i) {
1887 $ktest_config = "ktest.conf";
1890 if (! -f $ktest_config) {
1891 open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
1893 # Generated by ktest.pl
1895 # Define each test with TEST_START
1896 # The config options below it will override the defaults
1904 read_config $ktest_config;
1906 # Append any configs entered in manually to the config file.
1907 my @new_configs = keys %entered_configs;
1908 if ($#new_configs >= 0) {
1909 print "\nAppending entered in configs to $ktest_config\n";
1910 open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
1911 foreach my $config (@new_configs) {
1912 print OUT "$config = $entered_configs{$config}\n";
1913 $opt{$config} = $entered_configs{$config};
1917 if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
1918 unlink $opt{"LOG_FILE"};
1921 doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
1923 for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
1926 doprint "DEFAULT OPTIONS:\n";
1928 doprint "\nTEST $i OPTIONS";
1929 if (defined($repeat_tests{$i})) {
1930 $repeat = $repeat_tests{$i};
1931 doprint " ITERATE $repeat";
1936 foreach my $option (sort keys %opt) {
1938 if ($option =~ /\[(\d+)\]$/) {
1944 doprint "$option = $opt{$option}\n";
1948 sub set_test_option {
1949 my ($name, $i) = @_;
1951 my $option = "$name\[$i\]";
1953 if (defined($opt{$option})) {
1954 return $opt{$option};
1957 foreach my $test (keys %repeat_tests) {
1959 $i < $test + $repeat_tests{$test}) {
1960 $option = "$name\[$test\]";
1961 if (defined($opt{$option})) {
1962 return $opt{$option};
1967 if (defined($opt{$name})) {
1974 # First we need to do is the builds
1975 for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
1979 my $makecmd = set_test_option("MAKE_CMD", $i);
1981 $machine = set_test_option("MACHINE", $i);
1982 $ssh_user = set_test_option("SSH_USER", $i);
1983 $tmpdir = set_test_option("TMP_DIR", $i);
1984 $outputdir = set_test_option("OUTPUT_DIR", $i);
1985 $builddir = set_test_option("BUILD_DIR", $i);
1986 $test_type = set_test_option("TEST_TYPE", $i);
1987 $build_type = set_test_option("BUILD_TYPE", $i);
1988 $build_options = set_test_option("BUILD_OPTIONS", $i);
1989 $power_cycle = set_test_option("POWER_CYCLE", $i);
1990 $reboot = set_test_option("REBOOT", $i);
1991 $noclean = set_test_option("BUILD_NOCLEAN", $i);
1992 $minconfig = set_test_option("MIN_CONFIG", $i);
1993 $run_test = set_test_option("TEST", $i);
1994 $addconfig = set_test_option("ADD_CONFIG", $i);
1995 $reboot_type = set_test_option("REBOOT_TYPE", $i);
1996 $grub_menu = set_test_option("GRUB_MENU", $i);
1997 $post_install = set_test_option("POST_INSTALL", $i);
1998 $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
1999 $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
2000 $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
2001 $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
2002 $power_off = set_test_option("POWER_OFF", $i);
2003 $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
2004 $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
2005 $sleep_time = set_test_option("SLEEP_TIME", $i);
2006 $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
2007 $bisect_manual = set_test_option("BISECT_MANUAL", $i);
2008 $bisect_skip = set_test_option("BISECT_SKIP", $i);
2009 $store_failures = set_test_option("STORE_FAILURES", $i);
2010 $timeout = set_test_option("TIMEOUT", $i);
2011 $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
2012 $console = set_test_option("CONSOLE", $i);
2013 $success_line = set_test_option("SUCCESS_LINE", $i);
2014 $stop_after_success = set_test_option("STOP_AFTER_SUCCESS", $i);
2015 $stop_after_failure = set_test_option("STOP_AFTER_FAILURE", $i);
2016 $stop_test_after = set_test_option("STOP_TEST_AFTER", $i);
2017 $build_target = set_test_option("BUILD_TARGET", $i);
2018 $ssh_exec = set_test_option("SSH_EXEC", $i);
2019 $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
2020 $target_image = set_test_option("TARGET_IMAGE", $i);
2021 $localversion = set_test_option("LOCALVERSION", $i);
2023 chdir $builddir || die "can't change directory to $builddir";
2027 die "can't create $tmpdir";
2030 $ENV{"SSH_USER"} = $ssh_user;
2031 $ENV{"MACHINE"} = $machine;
2033 $target = "$ssh_user\@$machine";
2035 $buildlog = "$tmpdir/buildlog-$machine";
2036 $dmesg = "$tmpdir/dmesg-$machine";
2037 $make = "$makecmd O=$outputdir";
2038 $output_config = "$outputdir/.config";
2040 if ($reboot_type eq "grub") {
2041 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
2042 } elsif (!defined($reboot_script)) {
2043 dodie "REBOOT_SCRIPT not defined"
2046 my $run_type = $build_type;
2047 if ($test_type eq "patchcheck") {
2048 $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
2049 } elsif ($test_type eq "bisect") {
2050 $run_type = $opt{"BISECT_TYPE[$i]"};
2051 } elsif ($test_type eq "config_bisect") {
2052 $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
2055 # mistake in config file?
2056 if (!defined($run_type)) {
2057 $run_type = "ERROR";
2061 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type\n\n";
2066 if (!defined($minconfig)) {
2067 $minconfig = $addconfig;
2069 } elsif (defined($addconfig)) {
2070 run_command "cat $addconfig $minconfig > $tmpdir/add_config" or
2071 dodie "Failed to create temp config";
2072 $minconfig = "$tmpdir/add_config";
2075 my $checkout = $opt{"CHECKOUT[$i]"};
2076 if (defined($checkout)) {
2077 run_command "git checkout $checkout" or
2078 die "failed to checkout $checkout";
2081 if ($test_type eq "bisect") {
2084 } elsif ($test_type eq "config_bisect") {
2087 } elsif ($test_type eq "patchcheck") {
2092 if ($build_type ne "nobuild") {
2093 build $build_type or next;
2096 if ($test_type ne "build") {
2103 monitor or $failed = 1;;
2105 if (!$failed && $test_type ne "boot" && defined($run_test)) {
2106 do_run_test or $failed = 1;
2115 if ($opt{"POWEROFF_ON_SUCCESS"}) {
2117 } elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
2121 doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";