a7a74f015ef36f5dc834cd1a02149738b7363b90
[pandora-kernel.git] / tools / testing / ktest / ktest.pl
1 #!/usr/bin/perl -w
2 #
3 # Copywrite 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
4 # Licensed under the terms of the GNU GPL License version 2
5 #
6
7 use strict;
8 use IPC::Open2;
9 use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
10 use File::Path qw(mkpath);
11 use File::Copy qw(cp);
12 use FileHandle;
13
14 my $VERSION = "0.2";
15
16 $#ARGV >= 0 || die "ktest.pl version: $VERSION\n   usage: ktest.pl config-file\n";
17
18 $| = 1;
19
20 my %opt;
21 my %repeat_tests;
22 my %repeats;
23 my %default;
24
25 #default opts
26 $default{"NUM_TESTS"}           = 1;
27 $default{"REBOOT_TYPE"}         = "grub";
28 $default{"TEST_TYPE"}           = "test";
29 $default{"BUILD_TYPE"}          = "randconfig";
30 $default{"MAKE_CMD"}            = "make";
31 $default{"TIMEOUT"}             = 120;
32 $default{"TMP_DIR"}             = "/tmp/ktest";
33 $default{"SLEEP_TIME"}          = 60;   # sleep time between tests
34 $default{"BUILD_NOCLEAN"}       = 0;
35 $default{"REBOOT_ON_ERROR"}     = 0;
36 $default{"POWEROFF_ON_ERROR"}   = 0;
37 $default{"REBOOT_ON_SUCCESS"}   = 1;
38 $default{"POWEROFF_ON_SUCCESS"} = 0;
39 $default{"BUILD_OPTIONS"}       = "";
40 $default{"BISECT_SLEEP_TIME"}   = 60;   # sleep time between bisects
41 $default{"CLEAR_LOG"}           = 0;
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
49 my $version;
50 my $machine;
51 my $ssh_user;
52 my $tmpdir;
53 my $builddir;
54 my $outputdir;
55 my $output_config;
56 my $test_type;
57 my $build_type;
58 my $build_options;
59 my $reboot_type;
60 my $reboot_script;
61 my $power_cycle;
62 my $reboot;
63 my $reboot_on_error;
64 my $poweroff_on_error;
65 my $die_on_failure;
66 my $powercycle_after_reboot;
67 my $poweroff_after_halt;
68 my $ssh_exec;
69 my $scp_to_target;
70 my $power_off;
71 my $grub_menu;
72 my $grub_number;
73 my $target;
74 my $make;
75 my $post_install;
76 my $noclean;
77 my $minconfig;
78 my $addconfig;
79 my $in_bisect = 0;
80 my $bisect_bad = "";
81 my $reverse_bisect;
82 my $in_patchcheck = 0;
83 my $run_test;
84 my $redirect;
85 my $buildlog;
86 my $dmesg;
87 my $monitor_fp;
88 my $monitor_pid;
89 my $monitor_cnt = 0;
90 my $sleep_time;
91 my $bisect_sleep_time;
92 my $store_failures;
93 my $timeout;
94 my $booted_timeout;
95 my $console;
96 my $success_line;
97 my $build_target;
98 my $target_image;
99 my $localversion;
100 my $iteration = 0;
101 my $successes = 0;
102
103 sub set_value {
104     my ($lvalue, $rvalue) = @_;
105
106     if (defined($opt{$lvalue})) {
107         die "Error: Option $lvalue defined more than once!\n";
108     }
109     if ($rvalue =~ /^\s*$/) {
110         delete $opt{$lvalue};
111     } else {
112         $opt{$lvalue} = $rvalue;
113     }
114 }
115
116 sub read_config {
117     my ($config) = @_;
118
119     open(IN, $config) || die "can't read file $config";
120
121     my $name = $config;
122     $name =~ s,.*/(.*),$1,;
123
124     my $test_num = 0;
125     my $default = 1;
126     my $repeat = 1;
127     my $num_tests_set = 0;
128     my $skip = 0;
129     my $rest;
130
131     while (<IN>) {
132
133         # ignore blank lines and comments
134         next if (/^\s*$/ || /\s*\#/);
135
136         if (/^\s*TEST_START(.*)/) {
137
138             $rest = $1;
139
140             if ($num_tests_set) {
141                 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
142             }
143
144             my $old_test_num = $test_num;
145             my $old_repeat = $repeat;
146
147             $test_num += $repeat;
148             $default = 0;
149             $repeat = 1;
150
151             if ($rest =~ /\s+SKIP(.*)/) {
152                 $rest = $1;
153                 $skip = 1;
154             } else {
155                 $skip = 0;
156             }
157
158             if ($rest =~ /\s+ITERATE\s+(\d+)(.*)$/) {
159                 $repeat = $1;
160                 $rest = $2;
161                 $repeat_tests{"$test_num"} = $repeat;
162             }
163
164             if ($rest =~ /\s+SKIP(.*)/) {
165                 $rest = $1;
166                 $skip = 1;
167             }
168
169             if ($rest !~ /^\s*$/) {
170                 die "$name: $.: Gargbage found after TEST_START\n$_";
171             }
172
173             if ($skip) {
174                 $test_num = $old_test_num;
175                 $repeat = $old_repeat;
176             }
177
178         } elsif (/^\s*DEFAULTS(.*)$/) {
179             $default = 1;
180
181             $rest = $1;
182
183             if ($rest =~ /\s+SKIP(.*)/) {
184                 $rest = $1;
185                 $skip = 1;
186             } else {
187                 $skip = 0;
188             }
189
190             if ($rest !~ /^\s*$/) {
191                 die "$name: $.: Gargbage found after DEFAULTS\n$_";
192             }
193
194         } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
195
196             next if ($skip);
197
198             my $lvalue = $1;
199             my $rvalue = $2;
200
201             if (!$default &&
202                 ($lvalue eq "NUM_TESTS" ||
203                  $lvalue eq "LOG_FILE" ||
204                  $lvalue eq "CLEAR_LOG")) {
205                 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
206             }
207
208             if ($lvalue eq "NUM_TESTS") {
209                 if ($test_num) {
210                     die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
211                 }
212                 if (!$default) {
213                     die "$name: $.: NUM_TESTS must be set in default section\n";
214                 }
215                 $num_tests_set = 1;
216             }
217
218             if ($default || $lvalue =~ /\[\d+\]$/) {
219                 set_value($lvalue, $rvalue);
220             } else {
221                 my $val = "$lvalue\[$test_num\]";
222                 set_value($val, $rvalue);
223
224                 if ($repeat > 1) {
225                     $repeats{$val} = $repeat;
226                 }
227             }
228         } else {
229             die "$name: $.: Garbage found in config\n$_";
230         }
231     }
232
233     close(IN);
234
235     if ($test_num) {
236         $test_num += $repeat - 1;
237         $opt{"NUM_TESTS"} = $test_num;
238     }
239
240     # set any defaults
241
242     foreach my $default (keys %default) {
243         if (!defined($opt{$default})) {
244             $opt{$default} = $default{$default};
245         }
246     }
247 }
248
249 sub _logit {
250     if (defined($opt{"LOG_FILE"})) {
251         open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
252         print OUT @_;
253         close(OUT);
254     }
255 }
256
257 sub logit {
258     if (defined($opt{"LOG_FILE"})) {
259         _logit @_;
260     } else {
261         print @_;
262     }
263 }
264
265 sub doprint {
266     print @_;
267     _logit @_;
268 }
269
270 sub run_command;
271
272 sub reboot {
273     # try to reboot normally
274     if (run_command $reboot) {
275         if (defined($powercycle_after_reboot)) {
276             sleep $powercycle_after_reboot;
277             run_command "$power_cycle";
278         }
279     } else {
280         # nope? power cycle it.
281         run_command "$power_cycle";
282     }
283 }
284
285 sub do_not_reboot {
286     my $i = $iteration;
287
288     return $test_type eq "build" ||
289         ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
290         ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
291 }
292
293 sub dodie {
294     doprint "CRITICAL FAILURE... ", @_, "\n";
295
296     my $i = $iteration;
297
298     if ($reboot_on_error && !do_not_reboot) {
299
300         doprint "REBOOTING\n";
301         reboot;
302
303     } elsif ($poweroff_on_error && defined($power_off)) {
304         doprint "POWERING OFF\n";
305         `$power_off`;
306     }
307
308     die @_, "\n";
309 }
310
311 sub open_console {
312     my ($fp) = @_;
313
314     my $flags;
315
316     my $pid = open($fp, "$console|") or
317         dodie "Can't open console $console";
318
319     $flags = fcntl($fp, F_GETFL, 0) or
320         dodie "Can't get flags for the socket: $!";
321     $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
322         dodie "Can't set flags for the socket: $!";
323
324     return $pid;
325 }
326
327 sub close_console {
328     my ($fp, $pid) = @_;
329
330     doprint "kill child process $pid\n";
331     kill 2, $pid;
332
333     print "closing!\n";
334     close($fp);
335 }
336
337 sub start_monitor {
338     if ($monitor_cnt++) {
339         return;
340     }
341     $monitor_fp = \*MONFD;
342     $monitor_pid = open_console $monitor_fp;
343
344     return;
345
346     open(MONFD, "Stop perl from warning about single use of MONFD");
347 }
348
349 sub end_monitor {
350     if (--$monitor_cnt) {
351         return;
352     }
353     close_console($monitor_fp, $monitor_pid);
354 }
355
356 sub wait_for_monitor {
357     my ($time) = @_;
358     my $line;
359
360     doprint "** Wait for monitor to settle down **\n";
361
362     # read the monitor and wait for the system to calm down
363     do {
364         $line = wait_for_input($monitor_fp, $time);
365         print "$line" if (defined($line));
366     } while (defined($line));
367     print "** Monitor flushed **\n";
368 }
369
370 sub fail {
371
372         if ($die_on_failure) {
373                 dodie @_;
374         }
375
376         doprint "FAILED\n";
377
378         my $i = $iteration;
379
380         # no need to reboot for just building.
381         if (!do_not_reboot) {
382             doprint "REBOOTING\n";
383             reboot;
384             start_monitor;
385             wait_for_monitor $sleep_time;
386             end_monitor;
387         }
388
389         doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
390         doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
391         doprint "KTEST RESULT: TEST $i Failed: ", @_, "\n";
392         doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
393         doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
394
395         return 1 if (!defined($store_failures));
396
397         my @t = localtime;
398         my $date = sprintf "%04d%02d%02d%02d%02d%02d",
399                 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
400
401         my $dir = "$machine-$test_type-$build_type-fail-$date";
402         my $faildir = "$store_failures/$dir";
403
404         if (!-d $faildir) {
405             mkpath($faildir) or
406                 die "can't create $faildir";
407         }
408         if (-f "$output_config") {
409             cp "$output_config", "$faildir/config" or
410                 die "failed to copy .config";
411         }
412         if (-f $buildlog) {
413             cp $buildlog, "$faildir/buildlog" or
414                 die "failed to move $buildlog";
415         }
416         if (-f $dmesg) {
417             cp $dmesg, "$faildir/dmesg" or
418                 die "failed to move $dmesg";
419         }
420
421         doprint "*** Saved info to $faildir ***\n";
422
423         return 1;
424 }
425
426 sub run_command {
427     my ($command) = @_;
428     my $dolog = 0;
429     my $dord = 0;
430     my $pid;
431
432     $command =~ s/\$SSH_USER/$ssh_user/g;
433     $command =~ s/\$MACHINE/$machine/g;
434
435     doprint("$command ... ");
436
437     $pid = open(CMD, "$command 2>&1 |") or
438         (fail "unable to exec $command" and return 0);
439
440     if (defined($opt{"LOG_FILE"})) {
441         open(LOG, ">>$opt{LOG_FILE}") or
442             dodie "failed to write to log";
443         $dolog = 1;
444     }
445
446     if (defined($redirect)) {
447         open (RD, ">$redirect") or
448             dodie "failed to write to redirect $redirect";
449         $dord = 1;
450     }
451
452     while (<CMD>) {
453         print LOG if ($dolog);
454         print RD  if ($dord);
455     }
456
457     waitpid($pid, 0);
458     my $failed = $?;
459
460     close(CMD);
461     close(LOG) if ($dolog);
462     close(RD)  if ($dord);
463
464     if ($failed) {
465         doprint "FAILED!\n";
466     } else {
467         doprint "SUCCESS\n";
468     }
469
470     return !$failed;
471 }
472
473 sub run_ssh {
474     my ($cmd) = @_;
475     my $cp_exec = $ssh_exec;
476
477     $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
478     return run_command "$cp_exec";
479 }
480
481 sub run_scp {
482     my ($src, $dst) = @_;
483     my $cp_scp = $scp_to_target;
484
485     $cp_scp =~ s/\$SRC_FILE/$src/g;
486     $cp_scp =~ s/\$DST_FILE/$dst/g;
487
488     return run_command "$cp_scp";
489 }
490
491 sub get_grub_index {
492
493     if ($reboot_type ne "grub") {
494         return;
495     }
496     return if (defined($grub_number));
497
498     doprint "Find grub menu ... ";
499     $grub_number = -1;
500
501     my $ssh_grub = $ssh_exec;
502     $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
503
504     open(IN, "$ssh_grub |")
505         or die "unable to get menu.lst";
506
507     while (<IN>) {
508         if (/^\s*title\s+$grub_menu\s*$/) {
509             $grub_number++;
510             last;
511         } elsif (/^\s*title\s/) {
512             $grub_number++;
513         }
514     }
515     close(IN);
516
517     die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
518         if ($grub_number < 0);
519     doprint "$grub_number\n";
520 }
521
522 sub wait_for_input
523 {
524     my ($fp, $time) = @_;
525     my $rin;
526     my $ready;
527     my $line;
528     my $ch;
529
530     if (!defined($time)) {
531         $time = $timeout;
532     }
533
534     $rin = '';
535     vec($rin, fileno($fp), 1) = 1;
536     $ready = select($rin, undef, undef, $time);
537
538     $line = "";
539
540     # try to read one char at a time
541     while (sysread $fp, $ch, 1) {
542         $line .= $ch;
543         last if ($ch eq "\n");
544     }
545
546     if (!length($line)) {
547         return undef;
548     }
549
550     return $line;
551 }
552
553 sub reboot_to {
554     if ($reboot_type eq "grub") {
555         run_command "$ssh_exec '(echo \"savedefault --default=$grub_number --once\" | grub --batch; reboot)'";
556         return;
557     }
558
559     run_command "$reboot_script";
560 }
561
562 sub get_sha1 {
563     my ($commit) = @_;
564
565     doprint "git rev-list --max-count=1 $commit ... ";
566     my $sha1 = `git rev-list --max-count=1 $commit`;
567     my $ret = $?;
568
569     logit $sha1;
570
571     if ($ret) {
572         doprint "FAILED\n";
573         dodie "Failed to get git $commit";
574     }
575
576     print "SUCCESS\n";
577
578     chomp $sha1;
579
580     return $sha1;
581 }
582
583 sub monitor {
584     my $booted = 0;
585     my $bug = 0;
586     my $skip_call_trace = 0;
587     my $loops;
588
589     wait_for_monitor 5;
590
591     my $line;
592     my $full_line = "";
593
594     open(DMESG, "> $dmesg") or
595         die "unable to write to $dmesg";
596
597     reboot_to;
598
599     for (;;) {
600
601         if ($booted) {
602             $line = wait_for_input($monitor_fp, $booted_timeout);
603         } else {
604             $line = wait_for_input($monitor_fp);
605         }
606
607         last if (!defined($line));
608
609         doprint $line;
610         print DMESG $line;
611
612         # we are not guaranteed to get a full line
613         $full_line .= $line;
614
615         if ($full_line =~ /$success_line/) {
616             $booted = 1;
617         }
618
619         if ($full_line =~ /\[ backtrace testing \]/) {
620             $skip_call_trace = 1;
621         }
622
623         if ($full_line =~ /call trace:/i) {
624             $bug = 1 if (!$skip_call_trace);
625         }
626
627         if ($full_line =~ /\[ end of backtrace testing \]/) {
628             $skip_call_trace = 0;
629         }
630
631         if ($full_line =~ /Kernel panic -/) {
632             $bug = 1;
633         }
634
635         if ($line =~ /\n/) {
636             $full_line = "";
637         }
638     }
639
640     close(DMESG);
641
642     if ($bug) {
643         return 0 if ($in_bisect);
644         fail "failed - got a bug report" and return 0;
645     }
646
647     if (!$booted) {
648         return 0 if ($in_bisect);
649         fail "failed - never got a boot prompt." and return 0;
650     }
651
652     return 1;
653 }
654
655 sub install {
656
657     run_scp "$outputdir/$build_target", "$target_image" or
658         dodie "failed to copy image";
659
660     my $install_mods = 0;
661
662     # should we process modules?
663     $install_mods = 0;
664     open(IN, "$output_config") or dodie("Can't read config file");
665     while (<IN>) {
666         if (/CONFIG_MODULES(=y)?/) {
667             $install_mods = 1 if (defined($1));
668             last;
669         }
670     }
671     close(IN);
672
673     if (!$install_mods) {
674         doprint "No modules needed\n";
675         return;
676     }
677
678     run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
679         dodie "Failed to install modules";
680
681     my $modlib = "/lib/modules/$version";
682     my $modtar = "ktest-mods.tar.bz2";
683
684     run_ssh "rm -rf $modlib" or
685         dodie "failed to remove old mods: $modlib";
686
687     # would be nice if scp -r did not follow symbolic links
688     run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
689         dodie "making tarball";
690
691     run_scp "$tmpdir/$modtar", "/tmp" or
692         dodie "failed to copy modules";
693
694     unlink "$tmpdir/$modtar";
695
696     run_ssh "'(cd / && tar xf /tmp/$modtar)'" or
697         dodie "failed to tar modules";
698
699     run_ssh "rm -f /tmp/$modtar";
700
701     return if (!defined($post_install));
702
703     my $cp_post_install = $post_install;
704     $cp_post_install = s/\$KERNEL_VERSION/$version/g;
705     run_command "$cp_post_install" or
706         dodie "Failed to run post install";
707 }
708
709 sub check_buildlog {
710     my ($patch) = @_;
711
712     my @files = `git show $patch | diffstat -l`;
713
714     open(IN, "git show $patch |") or
715         dodie "failed to show $patch";
716     while (<IN>) {
717         if (m,^--- a/(.*),) {
718             chomp $1;
719             $files[$#files] = $1;
720         }
721     }
722     close(IN);
723
724     open(IN, $buildlog) or dodie "Can't open $buildlog";
725     while (<IN>) {
726         if (/^\s*(.*?):.*(warning|error)/) {
727             my $err = $1;
728             foreach my $file (@files) {
729                 my $fullpath = "$builddir/$file";
730                 if ($file eq $err || $fullpath eq $err) {
731                     fail "$file built with warnings" and return 0;
732                 }
733             }
734         }
735     }
736     close(IN);
737
738     return 1;
739 }
740
741 sub build {
742     my ($type) = @_;
743     my $defconfig = "";
744
745     unlink $buildlog;
746
747     if ($type =~ /^useconfig:(.*)/) {
748         run_command "cp $1 $output_config" or
749             dodie "could not copy $1 to .config";
750
751         $type = "oldconfig";
752     }
753
754     # old config can ask questions
755     if ($type eq "oldconfig") {
756         $type = "oldnoconfig";
757
758         # allow for empty configs
759         run_command "touch $output_config";
760
761         run_command "mv $output_config $outputdir/config_temp" or
762             dodie "moving .config";
763
764         if (!$noclean && !run_command "$make mrproper") {
765             dodie "make mrproper";
766         }
767
768         run_command "mv $outputdir/config_temp $output_config" or
769             dodie "moving config_temp";
770
771     } elsif (!$noclean) {
772         unlink "$output_config";
773         run_command "$make mrproper" or
774             dodie "make mrproper";
775     }
776
777     # add something to distinguish this build
778     open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
779     print OUT "$localversion\n";
780     close(OUT);
781
782     if (defined($minconfig)) {
783         $defconfig = "KCONFIG_ALLCONFIG=$minconfig";
784     }
785
786     run_command "$defconfig $make $type" or
787         dodie "failed make config";
788
789     $redirect = "$buildlog";
790     if (!run_command "$make $build_options") {
791         undef $redirect;
792         # bisect may need this to pass
793         return 0 if ($in_bisect);
794         fail "failed build" and return 0;
795     }
796     undef $redirect;
797
798     return 1;
799 }
800
801 sub halt {
802     if (!run_ssh "halt" or defined($power_off)) {
803         if (defined($poweroff_after_halt)) {
804             sleep $poweroff_after_halt;
805             run_command "$power_off";
806         }
807     } else {
808         # nope? the zap it!
809         run_command "$power_off";
810     }
811 }
812
813 sub success {
814     my ($i) = @_;
815
816     $successes++;
817
818     doprint "\n\n*******************************************\n";
819     doprint     "*******************************************\n";
820     doprint     "KTEST RESULT: TEST $i SUCCESS!!!!         **\n";
821     doprint     "*******************************************\n";
822     doprint     "*******************************************\n";
823
824     if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
825         doprint "Reboot and wait $sleep_time seconds\n";
826         reboot;
827         start_monitor;
828         wait_for_monitor $sleep_time;
829         end_monitor;
830     }
831 }
832
833 sub get_version {
834     # get the release name
835     doprint "$make kernelrelease ... ";
836     $version = `$make kernelrelease | tail -1`;
837     chomp($version);
838     doprint "$version\n";
839 }
840
841 sub child_run_test {
842     my $failed = 0;
843
844     # child should have no power
845     $reboot_on_error = 0;
846     $poweroff_on_error = 0;
847     $die_on_failure = 1;
848
849     run_command $run_test or $failed = 1;
850     exit $failed;
851 }
852
853 my $child_done;
854
855 sub child_finished {
856     $child_done = 1;
857 }
858
859 sub do_run_test {
860     my $child_pid;
861     my $child_exit;
862     my $line;
863     my $full_line;
864     my $bug = 0;
865
866     wait_for_monitor 1;
867
868     doprint "run test $run_test\n";
869
870     $child_done = 0;
871
872     $SIG{CHLD} = qw(child_finished);
873
874     $child_pid = fork;
875
876     child_run_test if (!$child_pid);
877
878     $full_line = "";
879
880     do {
881         $line = wait_for_input($monitor_fp, 1);
882         if (defined($line)) {
883
884             # we are not guaranteed to get a full line
885             $full_line .= $line;
886
887             if ($full_line =~ /call trace:/i) {
888                 $bug = 1;
889             }
890
891             if ($full_line =~ /Kernel panic -/) {
892                 $bug = 1;
893             }
894
895             if ($line =~ /\n/) {
896                 $full_line = "";
897             }
898         }
899     } while (!$child_done && !$bug);
900
901     if ($bug) {
902         doprint "Detected kernel crash!\n";
903         # kill the child with extreme prejudice
904         kill 9, $child_pid;
905     }
906
907     waitpid $child_pid, 0;
908     $child_exit = $?;
909
910     if ($bug || $child_exit) {
911         return 0 if $in_bisect;
912         fail "test failed" and return 0;
913     }
914     return 1;
915 }
916
917 sub run_git_bisect {
918     my ($command) = @_;
919
920     doprint "$command ... ";
921
922     my $output = `$command 2>&1`;
923     my $ret = $?;
924
925     logit $output;
926
927     if ($ret) {
928         doprint "FAILED\n";
929         dodie "Failed to git bisect";
930     }
931
932     doprint "SUCCESS\n";
933     if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
934         doprint "$1 [$2]\n";
935     } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
936         $bisect_bad = $1;
937         doprint "Found bad commit... $1\n";
938         return 0;
939     } else {
940         # we already logged it, just print it now.
941         print $output;
942     }
943
944     return 1;
945 }
946
947 # returns 1 on success, 0 on failure
948 sub run_bisect_test {
949     my ($type, $buildtype) = @_;
950
951     my $failed = 0;
952     my $result;
953     my $output;
954     my $ret;
955
956     $in_bisect = 1;
957
958     build $buildtype or $failed = 1;
959
960     if ($type ne "build") {
961         dodie "Failed on build" if $failed;
962
963         # Now boot the box
964         get_grub_index;
965         get_version;
966         install;
967
968         start_monitor;
969         monitor or $failed = 1;
970
971         if ($type ne "boot") {
972             dodie "Failed on boot" if $failed;
973
974             do_run_test or $failed = 1;
975         }
976         end_monitor;
977     }
978
979     if ($failed) {
980         $result = 0;
981
982         # reboot the box to a good kernel
983         if ($type ne "build") {
984             doprint "Reboot and sleep $bisect_sleep_time seconds\n";
985             reboot;
986             start_monitor;
987             wait_for_monitor $bisect_sleep_time;
988             end_monitor;
989         }
990     } else {
991         $result = 1;
992     }
993     $in_bisect = 0;
994
995     return $result;
996 }
997
998 sub run_bisect {
999     my ($type) = @_;
1000     my $buildtype = "oldconfig";
1001
1002     # We should have a minconfig to use?
1003     if (defined($minconfig)) {
1004         $buildtype = "useconfig:$minconfig";
1005     }
1006
1007     my $ret = run_bisect_test $type, $buildtype;
1008
1009
1010     # Are we looking for where it worked, not failed?
1011     if ($reverse_bisect) {
1012         $ret = !$ret;
1013     }
1014
1015     if ($ret) {
1016         return "good";
1017     } else {
1018         return  "bad";
1019     }
1020 }
1021
1022 sub bisect {
1023     my ($i) = @_;
1024
1025     my $result;
1026
1027     die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
1028     die "BISECT_BAD[$i] not defined\n"  if (!defined($opt{"BISECT_BAD[$i]"}));
1029     die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
1030
1031     my $good = $opt{"BISECT_GOOD[$i]"};
1032     my $bad = $opt{"BISECT_BAD[$i]"};
1033     my $type = $opt{"BISECT_TYPE[$i]"};
1034     my $start = $opt{"BISECT_START[$i]"};
1035     my $replay = $opt{"BISECT_REPLAY[$i]"};
1036
1037     # convert to true sha1's
1038     $good = get_sha1($good);
1039     $bad = get_sha1($bad);
1040
1041     if (defined($opt{"BISECT_REVERSE[$i]"}) &&
1042         $opt{"BISECT_REVERSE[$i]"} == 1) {
1043         doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
1044         $reverse_bisect = 1;
1045     } else {
1046         $reverse_bisect = 0;
1047     }
1048
1049     # Can't have a test without having a test to run
1050     if ($type eq "test" && !defined($run_test)) {
1051         $type = "boot";
1052     }
1053
1054     my $check = $opt{"BISECT_CHECK[$i]"};
1055     if (defined($check) && $check ne "0") {
1056
1057         # get current HEAD
1058         my $head = get_sha1("HEAD");
1059
1060         if ($check ne "good") {
1061             doprint "TESTING BISECT BAD [$bad]\n";
1062             run_command "git checkout $bad" or
1063                 die "Failed to checkout $bad";
1064
1065             $result = run_bisect $type;
1066
1067             if ($result ne "bad") {
1068                 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
1069             }
1070         }
1071
1072         if ($check ne "bad") {
1073             doprint "TESTING BISECT GOOD [$good]\n";
1074             run_command "git checkout $good" or
1075                 die "Failed to checkout $good";
1076
1077             $result = run_bisect $type;
1078
1079             if ($result ne "good") {
1080                 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
1081             }
1082         }
1083
1084         # checkout where we started
1085         run_command "git checkout $head" or
1086             die "Failed to checkout $head";
1087     }
1088
1089     run_command "git bisect start" or
1090         dodie "could not start bisect";
1091
1092     run_command "git bisect good $good" or
1093         dodie "could not set bisect good to $good";
1094
1095     run_git_bisect "git bisect bad $bad" or
1096         dodie "could not set bisect bad to $bad";
1097
1098     if (defined($replay)) {
1099         run_command "git bisect replay $replay" or
1100             dodie "failed to run replay";
1101     }
1102
1103     if (defined($start)) {
1104         run_command "git checkout $start" or
1105             dodie "failed to checkout $start";
1106     }
1107
1108     my $test;
1109     do {
1110         $result = run_bisect $type;
1111         $test = run_git_bisect "git bisect $result";
1112     } while ($test);
1113
1114     run_command "git bisect log" or
1115         dodie "could not capture git bisect log";
1116
1117     run_command "git bisect reset" or
1118         dodie "could not reset git bisect";
1119
1120     doprint "Bad commit was [$bisect_bad]\n";
1121
1122     success $i;
1123 }
1124
1125 my %config_ignore;
1126 my %config_set;
1127
1128 my %config_list;
1129 my %null_config;
1130
1131 my %dependency;
1132
1133 sub process_config_ignore {
1134     my ($config) = @_;
1135
1136     open (IN, $config)
1137         or dodie "Failed to read $config";
1138
1139     while (<IN>) {
1140         if (/^(.*?(CONFIG\S*)(=.*| is not set))/) {
1141             $config_ignore{$2} = $1;
1142         }
1143     }
1144
1145     close(IN);
1146 }
1147
1148 sub read_current_config {
1149     my ($config_ref) = @_;
1150
1151     %{$config_ref} = ();
1152     undef %{$config_ref};
1153
1154     my @key = keys %{$config_ref};
1155     if ($#key >= 0) {
1156         print "did not delete!\n";
1157         exit;
1158     }
1159     open (IN, "$output_config");
1160
1161     while (<IN>) {
1162         if (/^(CONFIG\S+)=(.*)/) {
1163             ${$config_ref}{$1} = $2;
1164         }
1165     }
1166     close(IN);
1167 }
1168
1169 sub get_dependencies {
1170     my ($config) = @_;
1171
1172     my $arr = $dependency{$config};
1173     if (!defined($arr)) {
1174         return ();
1175     }
1176
1177     my @deps = @{$arr};
1178
1179     foreach my $dep (@{$arr}) {
1180         print "ADD DEP $dep\n";
1181         @deps = (@deps, get_dependencies $dep);
1182     }
1183
1184     return @deps;
1185 }
1186
1187 sub create_config {
1188     my @configs = @_;
1189
1190     open(OUT, ">$output_config") or dodie "Can not write to $output_config";
1191
1192     foreach my $config (@configs) {
1193         print OUT "$config_set{$config}\n";
1194         my @deps = get_dependencies $config;
1195         foreach my $dep (@deps) {
1196             print OUT "$config_set{$dep}\n";
1197         }
1198     }
1199
1200     foreach my $config (keys %config_ignore) {
1201         print OUT "$config_ignore{$config}\n";
1202     }
1203     close(OUT);
1204
1205 #    exit;
1206     run_command "$make oldnoconfig" or
1207         dodie "failed make config oldconfig";
1208
1209 }
1210
1211 sub compare_configs {
1212     my (%a, %b) = @_;
1213
1214     foreach my $item (keys %a) {
1215         if (!defined($b{$item})) {
1216             print "diff $item\n";
1217             return 1;
1218         }
1219         delete $b{$item};
1220     }
1221
1222     my @keys = keys %b;
1223     if ($#keys) {
1224         print "diff2 $keys[0]\n";
1225     }
1226     return -1 if ($#keys >= 0);
1227
1228     return 0;
1229 }
1230
1231 sub run_config_bisect_test {
1232     my ($type) = @_;
1233
1234     return run_bisect_test $type, "oldconfig";
1235 }
1236
1237 sub process_passed {
1238     my (%configs) = @_;
1239
1240     doprint "These configs had no failure: (Enabling them for further compiles)\n";
1241     # Passed! All these configs are part of a good compile.
1242     # Add them to the min options.
1243     foreach my $config (keys %configs) {
1244         if (defined($config_list{$config})) {
1245             doprint " removing $config\n";
1246             $config_ignore{$config} = $config_list{$config};
1247             delete $config_list{$config};
1248         }
1249     }
1250 }
1251
1252 sub process_failed {
1253     my ($config) = @_;
1254
1255     doprint "\n\n***************************************\n";
1256     doprint "Found bad config: $config\n";
1257     doprint "***************************************\n\n";
1258 }
1259
1260 sub run_config_bisect {
1261
1262     my @start_list = keys %config_list;
1263
1264     if ($#start_list < 0) {
1265         doprint "No more configs to test!!!\n";
1266         return -1;
1267     }
1268
1269     doprint "***** RUN TEST ***\n";
1270     my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
1271     my $ret;
1272     my %current_config;
1273
1274     my $count = $#start_list + 1;
1275     doprint "  $count configs to test\n";
1276
1277     my $half = int($#start_list / 2);
1278
1279     do {
1280         my @tophalf = @start_list[0 .. $half];
1281
1282         create_config @tophalf;
1283         read_current_config \%current_config;
1284
1285         $count = $#tophalf + 1;
1286         doprint "Testing $count configs\n";
1287         my $found = 0;
1288         # make sure we test something
1289         foreach my $config (@tophalf) {
1290             if (defined($current_config{$config})) {
1291                 logit " $config\n";
1292                 $found = 1;
1293             }
1294         }
1295         if (!$found) {
1296             # try the other half
1297             doprint "Top half produced no set configs, trying bottom half\n";
1298             @tophalf = @start_list[$half .. $#start_list];
1299             create_config @tophalf;
1300             read_current_config \%current_config;
1301             foreach my $config (@tophalf) {
1302                 if (defined($current_config{$config})) {
1303                     logit " $config\n";
1304                     $found = 1;
1305                 }
1306             }
1307             if (!$found) {
1308                 doprint "Failed: Can't make new config with current configs\n";
1309                 foreach my $config (@start_list) {
1310                     doprint "  CONFIG: $config\n";
1311                 }
1312                 return -1;
1313             }
1314             $count = $#tophalf + 1;
1315             doprint "Testing $count configs\n";
1316         }
1317
1318         $ret = run_config_bisect_test $type;
1319
1320         if ($ret) {
1321             process_passed %current_config;
1322             return 0;
1323         }
1324
1325         doprint "This config had a failure.\n";
1326         doprint "Removing these configs that were not set in this config:\n";
1327
1328         # A config exists in this group that was bad.
1329         foreach my $config (keys %config_list) {
1330             if (!defined($current_config{$config})) {
1331                 doprint " removing $config\n";
1332                 delete $config_list{$config};
1333             }
1334         }
1335
1336         @start_list = @tophalf;
1337
1338         if ($#start_list == 0) {
1339             process_failed $start_list[0];
1340             return 1;
1341         }
1342
1343         # remove half the configs we are looking at and see if
1344         # they are good.
1345         $half = int($#start_list / 2);
1346     } while ($half > 0);
1347
1348     # we found a single config, try it again
1349     my @tophalf = @start_list[0 .. 0];
1350
1351     $ret = run_config_bisect_test $type;
1352     if ($ret) {
1353         process_passed %current_config;
1354         return 0;
1355     }
1356
1357     process_failed $start_list[0];
1358     return 1;
1359 }
1360
1361 sub config_bisect {
1362     my ($i) = @_;
1363
1364     my $start_config = $opt{"CONFIG_BISECT[$i]"};
1365
1366     my $tmpconfig = "$tmpdir/use_config";
1367
1368     # Make the file with the bad config and the min config
1369     if (defined($minconfig)) {
1370         # read the min config for things to ignore
1371         run_command "cp $minconfig $tmpconfig" or
1372             dodie "failed to copy $minconfig to $tmpconfig";
1373     } else {
1374         unlink $tmpconfig;
1375     }
1376
1377     # Add other configs
1378     if (defined($addconfig)) {
1379         run_command "cat $addconfig >> $tmpconfig" or
1380             dodie "failed to append $addconfig";
1381     }
1382
1383     my $defconfig = "";
1384     if (-f $tmpconfig) {
1385         $defconfig = "KCONFIG_ALLCONFIG=$tmpconfig";
1386         process_config_ignore $tmpconfig;
1387     }
1388
1389     # now process the start config
1390     run_command "cp $start_config $output_config" or
1391         dodie "failed to copy $start_config to $output_config";
1392
1393     # read directly what we want to check
1394     my %config_check;
1395     open (IN, $output_config)
1396         or dodie "faied to open $output_config";
1397
1398     while (<IN>) {
1399         if (/^((CONFIG\S*)=.*)/) {
1400             $config_check{$2} = $1;
1401         }
1402     }
1403     close(IN);
1404
1405     # Now run oldconfig with the minconfig (and addconfigs)
1406     run_command "$defconfig $make oldnoconfig" or
1407         dodie "failed make config oldconfig";
1408
1409     # check to see what we lost (or gained)
1410     open (IN, $output_config)
1411         or dodie "Failed to read $start_config";
1412
1413     my %removed_configs;
1414     my %added_configs;
1415
1416     while (<IN>) {
1417         if (/^((CONFIG\S*)=.*)/) {
1418             # save off all options
1419             $config_set{$2} = $1;
1420             if (defined($config_check{$2})) {
1421                 if (defined($config_ignore{$2})) {
1422                     $removed_configs{$2} = $1;
1423                 } else {
1424                     $config_list{$2} = $1;
1425                 }
1426             } elsif (!defined($config_ignore{$2})) {
1427                 $added_configs{$2} = $1;
1428                 $config_list{$2} = $1;
1429             }
1430         }
1431     }
1432     close(IN);
1433
1434     my @confs = keys %removed_configs;
1435     if ($#confs >= 0) {
1436         doprint "Configs overridden by default configs and removed from check:\n";
1437         foreach my $config (@confs) {
1438             doprint " $config\n";
1439         }
1440     }
1441     @confs = keys %added_configs;
1442     if ($#confs >= 0) {
1443         doprint "Configs appearing in make oldconfig and added:\n";
1444         foreach my $config (@confs) {
1445             doprint " $config\n";
1446         }
1447     }
1448
1449     my %config_test;
1450     my $once = 0;
1451
1452     # Sometimes kconfig does weird things. We must make sure
1453     # that the config we autocreate has everything we need
1454     # to test, otherwise we may miss testing configs, or
1455     # may not be able to create a new config.
1456     # Here we create a config with everything set.
1457     create_config (keys %config_list);
1458     read_current_config \%config_test;
1459     foreach my $config (keys %config_list) {
1460         if (!defined($config_test{$config})) {
1461             if (!$once) {
1462                 $once = 1;
1463                 doprint "Configs not produced by kconfig (will not be checked):\n";
1464             }
1465             doprint "  $config\n";
1466             delete $config_list{$config};
1467         }
1468     }
1469     my $ret;
1470     do {
1471         $ret = run_config_bisect;
1472     } while (!$ret);
1473
1474     return $ret if ($ret < 0);
1475
1476     success $i;
1477 }
1478
1479 sub patchcheck {
1480     my ($i) = @_;
1481
1482     die "PATCHCHECK_START[$i] not defined\n"
1483         if (!defined($opt{"PATCHCHECK_START[$i]"}));
1484     die "PATCHCHECK_TYPE[$i] not defined\n"
1485         if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
1486
1487     my $start = $opt{"PATCHCHECK_START[$i]"};
1488
1489     my $end = "HEAD";
1490     if (defined($opt{"PATCHCHECK_END[$i]"})) {
1491         $end = $opt{"PATCHCHECK_END[$i]"};
1492     }
1493
1494     # Get the true sha1's since we can use things like HEAD~3
1495     $start = get_sha1($start);
1496     $end = get_sha1($end);
1497
1498     my $type = $opt{"PATCHCHECK_TYPE[$i]"};
1499
1500     # Can't have a test without having a test to run
1501     if ($type eq "test" && !defined($run_test)) {
1502         $type = "boot";
1503     }
1504
1505     open (IN, "git log --pretty=oneline $end|") or
1506         dodie "could not get git list";
1507
1508     my @list;
1509
1510     while (<IN>) {
1511         chomp;
1512         $list[$#list+1] = $_;
1513         last if (/^$start/);
1514     }
1515     close(IN);
1516
1517     if ($list[$#list] !~ /^$start/) {
1518         fail "SHA1 $start not found";
1519     }
1520
1521     # go backwards in the list
1522     @list = reverse @list;
1523
1524     my $save_clean = $noclean;
1525
1526     $in_patchcheck = 1;
1527     foreach my $item (@list) {
1528         my $sha1 = $item;
1529         $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
1530
1531         doprint "\nProcessing commit $item\n\n";
1532
1533         run_command "git checkout $sha1" or
1534             die "Failed to checkout $sha1";
1535
1536         # only clean on the first and last patch
1537         if ($item eq $list[0] ||
1538             $item eq $list[$#list]) {
1539             $noclean = $save_clean;
1540         } else {
1541             $noclean = 1;
1542         }
1543
1544         if (defined($minconfig)) {
1545             build "useconfig:$minconfig" or return 0;
1546         } else {
1547             # ?? no config to use?
1548             build "oldconfig" or return 0;
1549         }
1550
1551         check_buildlog $sha1 or return 0;
1552
1553         next if ($type eq "build");
1554
1555         get_grub_index;
1556         get_version;
1557         install;
1558
1559         my $failed = 0;
1560
1561         start_monitor;
1562         monitor or $failed = 1;
1563
1564         if (!$failed && $type ne "boot"){
1565             do_run_test or $failed = 1;
1566         }
1567         end_monitor;
1568         return 0 if ($failed);
1569
1570     }
1571     $in_patchcheck = 0;
1572     success $i;
1573
1574     return 1;
1575 }
1576
1577 read_config $ARGV[0];
1578
1579 # mandatory configs
1580 die "MACHINE not defined\n"             if (!defined($opt{"MACHINE"}));
1581 die "SSH_USER not defined\n"            if (!defined($opt{"SSH_USER"}));
1582 die "BUILD_DIR not defined\n"           if (!defined($opt{"BUILD_DIR"}));
1583 die "OUTPUT_DIR not defined\n"          if (!defined($opt{"OUTPUT_DIR"}));
1584 die "BUILD_TARGET not defined\n"        if (!defined($opt{"BUILD_TARGET"}));
1585 die "TARGET_IMAGE not defined\n"        if (!defined($opt{"TARGET_IMAGE"}));
1586 die "POWER_CYCLE not defined\n"         if (!defined($opt{"POWER_CYCLE"}));
1587 die "CONSOLE not defined\n"             if (!defined($opt{"CONSOLE"}));
1588 die "LOCALVERSION not defined\n"        if (!defined($opt{"LOCALVERSION"}));
1589
1590 if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
1591     unlink $opt{"LOG_FILE"};
1592 }
1593
1594 doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
1595
1596 for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
1597
1598     if (!$i) {
1599         doprint "DEFAULT OPTIONS:\n";
1600     } else {
1601         doprint "\nTEST $i OPTIONS";
1602         if (defined($repeat_tests{$i})) {
1603             $repeat = $repeat_tests{$i};
1604             doprint " ITERATE $repeat";
1605         }
1606         doprint "\n";
1607     }
1608
1609     foreach my $option (sort keys %opt) {
1610
1611         if ($option =~ /\[(\d+)\]$/) {
1612             next if ($i != $1);
1613         } else {
1614             next if ($i);
1615         }
1616
1617         doprint "$option = $opt{$option}\n";
1618     }
1619 }
1620
1621 sub set_test_option {
1622     my ($name, $i) = @_;
1623
1624     my $option = "$name\[$i\]";
1625
1626     if (defined($opt{$option})) {
1627         return $opt{$option};
1628     }
1629
1630     foreach my $test (keys %repeat_tests) {
1631         if ($i >= $test &&
1632             $i < $test + $repeat_tests{$test}) {
1633             $option = "$name\[$test\]";
1634             if (defined($opt{$option})) {
1635                 return $opt{$option};
1636             }
1637         }
1638     }
1639
1640     if (defined($opt{$name})) {
1641         return $opt{$name};
1642     }
1643
1644     return undef;
1645 }
1646
1647 # First we need to do is the builds
1648 for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
1649
1650     $iteration = $i;
1651
1652     my $makecmd = set_test_option("MAKE_CMD", $i);
1653
1654     $machine = set_test_option("MACHINE", $i);
1655     $ssh_user = set_test_option("SSH_USER", $i);
1656     $tmpdir = set_test_option("TMP_DIR", $i);
1657     $outputdir = set_test_option("OUTPUT_DIR", $i);
1658     $builddir = set_test_option("BUILD_DIR", $i);
1659     $test_type = set_test_option("TEST_TYPE", $i);
1660     $build_type = set_test_option("BUILD_TYPE", $i);
1661     $build_options = set_test_option("BUILD_OPTIONS", $i);
1662     $power_cycle = set_test_option("POWER_CYCLE", $i);
1663     $reboot = set_test_option("REBOOT", $i);
1664     $noclean = set_test_option("BUILD_NOCLEAN", $i);
1665     $minconfig = set_test_option("MIN_CONFIG", $i);
1666     $run_test = set_test_option("TEST", $i);
1667     $addconfig = set_test_option("ADD_CONFIG", $i);
1668     $reboot_type = set_test_option("REBOOT_TYPE", $i);
1669     $grub_menu = set_test_option("GRUB_MENU", $i);
1670     $post_install = set_test_option("POST_INSTALL", $i);
1671     $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
1672     $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
1673     $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
1674     $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
1675     $power_off = set_test_option("POWER_OFF", $i);
1676     $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
1677     $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
1678     $sleep_time = set_test_option("SLEEP_TIME", $i);
1679     $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
1680     $store_failures = set_test_option("STORE_FAILURES", $i);
1681     $timeout = set_test_option("TIMEOUT", $i);
1682     $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
1683     $console = set_test_option("CONSOLE", $i);
1684     $success_line = set_test_option("SUCCESS_LINE", $i);
1685     $build_target = set_test_option("BUILD_TARGET", $i);
1686     $ssh_exec = set_test_option("SSH_EXEC", $i);
1687     $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
1688     $target_image = set_test_option("TARGET_IMAGE", $i);
1689     $localversion = set_test_option("LOCALVERSION", $i);
1690
1691     chdir $builddir || die "can't change directory to $builddir";
1692
1693     if (!-d $tmpdir) {
1694         mkpath($tmpdir) or
1695             die "can't create $tmpdir";
1696     }
1697
1698     $ENV{"SSH_USER"} = $ssh_user;
1699     $ENV{"MACHINE"} = $machine;
1700
1701     $target = "$ssh_user\@$machine";
1702
1703     $buildlog = "$tmpdir/buildlog-$machine";
1704     $dmesg = "$tmpdir/dmesg-$machine";
1705     $make = "$makecmd O=$outputdir";
1706     $output_config = "$outputdir/.config";
1707
1708     if ($reboot_type eq "grub") {
1709         dodie "GRUB_MENU not defined" if (!defined($grub_menu));
1710     } elsif (!defined($reboot_script)) {
1711         dodie "REBOOT_SCRIPT not defined"
1712     }
1713
1714     my $run_type = $build_type;
1715     if ($test_type eq "patchcheck") {
1716         $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
1717     } elsif ($test_type eq "bisect") {
1718         $run_type = $opt{"BISECT_TYPE[$i]"};
1719     } elsif ($test_type eq "config_bisect") {
1720         $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
1721     }
1722
1723     # mistake in config file?
1724     if (!defined($run_type)) {
1725         $run_type = "ERROR";
1726     }
1727
1728     doprint "\n\n";
1729     doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type\n\n";
1730
1731     unlink $dmesg;
1732     unlink $buildlog;
1733
1734     if (!defined($minconfig)) {
1735         $minconfig = $addconfig;
1736
1737     } elsif (defined($addconfig)) {
1738         run_command "cat $addconfig $minconfig > $tmpdir/add_config" or
1739             dodie "Failed to create temp config";
1740         $minconfig = "$tmpdir/add_config";
1741     }
1742
1743     my $checkout = $opt{"CHECKOUT[$i]"};
1744     if (defined($checkout)) {
1745         run_command "git checkout $checkout" or
1746             die "failed to checkout $checkout";
1747     }
1748
1749     if ($test_type eq "bisect") {
1750         bisect $i;
1751         next;
1752     } elsif ($test_type eq "config_bisect") {
1753         config_bisect $i;
1754         next;
1755     } elsif ($test_type eq "patchcheck") {
1756         patchcheck $i;
1757         next;
1758     }
1759
1760     if ($build_type ne "nobuild") {
1761         build $build_type or next;
1762     }
1763
1764     if ($test_type ne "build") {
1765         get_grub_index;
1766         get_version;
1767         install;
1768
1769         my $failed = 0;
1770         start_monitor;
1771         monitor or $failed = 1;;
1772
1773         if (!$failed && $test_type ne "boot" && defined($run_test)) {
1774             do_run_test or $failed = 1;
1775         }
1776         end_monitor;
1777         next if ($failed);
1778     }
1779
1780     success $i;
1781 }
1782
1783 if ($opt{"POWEROFF_ON_SUCCESS"}) {
1784     halt;
1785 } elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
1786     reboot;
1787 }
1788
1789 doprint "\n    $successes of $opt{NUM_TESTS} tests were successful\n\n";
1790
1791 exit 0;