ktest: Use oldnoconfig instead of yes command
[deliverable/linux.git] / tools / testing / ktest / ktest.pl
CommitLineData
2545eb61 1#!/usr/bin/perl -w
d6ce2a0b
SR
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#
2545eb61
SR
6
7use strict;
8use IPC::Open2;
9use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
7faafbd6
SR
10use File::Path qw(mkpath);
11use File::Copy qw(cp);
2545eb61
SR
12use FileHandle;
13
a57419b3 14$#ARGV >= 0 || die "usage: ktest.pl config-file\n";
2545eb61
SR
15
16$| = 1;
17
18my %opt;
a57419b3
SR
19my %repeat_tests;
20my %repeats;
a75fecec 21my %default;
2545eb61
SR
22
23#default opts
a57419b3 24$default{"NUM_TESTS"} = 1;
a75fecec
SR
25$default{"REBOOT_TYPE"} = "grub";
26$default{"TEST_TYPE"} = "test";
27$default{"BUILD_TYPE"} = "randconfig";
28$default{"MAKE_CMD"} = "make";
29$default{"TIMEOUT"} = 120;
a57419b3 30$default{"TMP_DIR"} = "/tmp/ktest";
a75fecec
SR
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;
2545eb61
SR
43
44my $version;
a75fecec
SR
45my $machine;
46my $tmpdir;
47my $builddir;
48my $outputdir;
49my $test_type;
7faafbd6 50my $build_type;
a75fecec
SR
51my $build_options;
52my $reboot_type;
53my $reboot_script;
54my $power_cycle;
55my $reboot_on_error;
56my $poweroff_on_error;
57my $die_on_failure;
576f627c
SR
58my $powercycle_after_reboot;
59my $poweroff_after_halt;
a75fecec
SR
60my $power_off;
61my $grub_menu;
2545eb61
SR
62my $grub_number;
63my $target;
64my $make;
8b37ca8c 65my $post_install;
5c42fc5b 66my $noclean;
5f9b6ced 67my $minconfig;
2b7d9b21 68my $addconfig;
5f9b6ced
SR
69my $in_bisect = 0;
70my $bisect_bad = "";
d6ce2a0b 71my $reverse_bisect;
6c5ee0be 72my $in_patchcheck = 0;
5a391fbf 73my $run_test;
6c5ee0be 74my $redirect;
7faafbd6
SR
75my $buildlog;
76my $dmesg;
77my $monitor_fp;
78my $monitor_pid;
79my $monitor_cnt = 0;
a75fecec
SR
80my $sleep_time;
81my $bisect_sleep_time;
82my $store_failures;
83my $timeout;
84my $booted_timeout;
85my $console;
86my $success_line;
87my $build_target;
88my $target_image;
89my $localversion;
576f627c 90my $iteration = 0;
2545eb61 91
a57419b3
SR
92sub set_value {
93 my ($lvalue, $rvalue) = @_;
94
95 if (defined($opt{$lvalue})) {
96 die "Error: Option $lvalue defined more than once!\n";
97 }
98 $opt{$lvalue} = $rvalue;
99}
100
2545eb61
SR
101sub read_config {
102 my ($config) = @_;
103
104 open(IN, $config) || die "can't read file $config";
105
a57419b3
SR
106 my $name = $config;
107 $name =~ s,.*/(.*),$1,;
108
109 my $test_num = 0;
110 my $default = 1;
111 my $repeat = 1;
112 my $num_tests_set = 0;
113 my $skip = 0;
114 my $rest;
115
2545eb61
SR
116 while (<IN>) {
117
118 # ignore blank lines and comments
119 next if (/^\s*$/ || /\s*\#/);
120
a57419b3
SR
121 if (/^\s*TEST_START(.*)/) {
122
123 $rest = $1;
124
125 if ($num_tests_set) {
126 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
127 }
128
129 my $old_test_num = $test_num;
130
131 $test_num += $repeat;
132 $default = 0;
133 $repeat = 1;
134
135 if ($rest =~ /\s+SKIP(.*)/) {
136 $rest = $1;
137 $skip = 1;
138 } else {
139 $skip = 0;
140 }
141
142 if ($rest =~ /\s+ITERATE\s+(\d+)(.*)$/) {
143 $repeat = $1;
144 $rest = $2;
145 $repeat_tests{"$test_num"} = $repeat;
146 }
147
148 if ($rest =~ /\s+SKIP(.*)/) {
149 $rest = $1;
150 $skip = 1;
151 }
152
153 if ($rest !~ /^\s*$/) {
154 die "$name: $.: Gargbage found after TEST_START\n$_";
155 }
156
157 if ($skip) {
158 $test_num = $old_test_num;
159 $repeat = 1;
160 }
161
162 } elsif (/^\s*DEFAULTS(.*)$/) {
163 $default = 1;
164
165 $rest = $1;
166
167 if ($rest =~ /\s+SKIP(.*)/) {
168 $rest = $1;
169 $skip = 1;
170 } else {
171 $skip = 0;
172 }
173
174 if ($rest !~ /^\s*$/) {
175 die "$name: $.: Gargbage found after DEFAULTS\n$_";
176 }
177
178 } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
179
180 next if ($skip);
181
2545eb61
SR
182 my $lvalue = $1;
183 my $rvalue = $2;
184
a57419b3
SR
185 if (!$default &&
186 ($lvalue eq "NUM_TESTS" ||
187 $lvalue eq "LOG_FILE" ||
188 $lvalue eq "CLEAR_LOG")) {
189 die "$name: $.: $lvalue must be set in DEFAULTS section\n";
190 }
191
192 if ($lvalue eq "NUM_TESTS") {
193 if ($test_num) {
194 die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
195 }
196 if (!$default) {
197 die "$name: $.: NUM_TESTS must be set in default section\n";
198 }
199 $num_tests_set = 1;
200 }
201
202 if ($default || $lvalue =~ /\[\d+\]$/) {
203 set_value($lvalue, $rvalue);
204 } else {
205 my $val = "$lvalue\[$test_num\]";
206 set_value($val, $rvalue);
207
208 if ($repeat > 1) {
209 $repeats{$val} = $repeat;
210 }
a75fecec 211 }
a57419b3
SR
212 } else {
213 die "$name: $.: Garbage found in config\n$_";
2545eb61
SR
214 }
215 }
216
217 close(IN);
a75fecec 218
a57419b3
SR
219 if ($test_num) {
220 $test_num += $repeat - 1;
221 $opt{"NUM_TESTS"} = $test_num;
222 }
223
a75fecec
SR
224 # set any defaults
225
226 foreach my $default (keys %default) {
227 if (!defined($opt{$default})) {
228 $opt{$default} = $default{$default};
229 }
230 }
2545eb61
SR
231}
232
5f9b6ced 233sub logit {
2545eb61
SR
234 if (defined($opt{"LOG_FILE"})) {
235 open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
236 print OUT @_;
237 close(OUT);
238 }
239}
240
5f9b6ced
SR
241sub doprint {
242 print @_;
243 logit @_;
244}
245
7faafbd6
SR
246sub run_command;
247
248sub reboot {
249 # try to reboot normally
576f627c
SR
250 if (run_command "ssh $target reboot") {
251 if (defined($powercycle_after_reboot)) {
252 sleep $powercycle_after_reboot;
253 run_command "$power_cycle";
254 }
255 } else {
7faafbd6 256 # nope? power cycle it.
a75fecec 257 run_command "$power_cycle";
7faafbd6
SR
258 }
259}
260
576f627c
SR
261sub do_not_reboot {
262 my $i = $iteration;
263
264 return $test_type eq "build" ||
265 ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
266 ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
267}
268
5c42fc5b 269sub dodie {
5a391fbf 270 doprint "CRITICAL FAILURE... ", @_, "\n";
5c42fc5b 271
576f627c
SR
272 my $i = $iteration;
273
274 if ($reboot_on_error && !do_not_reboot) {
275
75c3fda7 276 doprint "REBOOTING\n";
7faafbd6 277 reboot;
75c3fda7 278
a75fecec 279 } elsif ($poweroff_on_error && defined($power_off)) {
5c42fc5b 280 doprint "POWERING OFF\n";
a75fecec 281 `$power_off`;
5c42fc5b 282 }
75c3fda7 283
576f627c 284 die @_, "\n";
5c42fc5b
SR
285}
286
7faafbd6
SR
287sub open_console {
288 my ($fp) = @_;
289
290 my $flags;
291
a75fecec
SR
292 my $pid = open($fp, "$console|") or
293 dodie "Can't open console $console";
7faafbd6
SR
294
295 $flags = fcntl($fp, F_GETFL, 0) or
576f627c 296 dodie "Can't get flags for the socket: $!";
7faafbd6 297 $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
576f627c 298 dodie "Can't set flags for the socket: $!";
7faafbd6
SR
299
300 return $pid;
301}
302
303sub close_console {
304 my ($fp, $pid) = @_;
305
306 doprint "kill child process $pid\n";
307 kill 2, $pid;
308
309 print "closing!\n";
310 close($fp);
311}
312
313sub start_monitor {
314 if ($monitor_cnt++) {
315 return;
316 }
317 $monitor_fp = \*MONFD;
318 $monitor_pid = open_console $monitor_fp;
a75fecec
SR
319
320 return;
321
322 open(MONFD, "Stop perl from warning about single use of MONFD");
7faafbd6
SR
323}
324
325sub end_monitor {
326 if (--$monitor_cnt) {
327 return;
328 }
329 close_console($monitor_fp, $monitor_pid);
330}
331
332sub wait_for_monitor {
333 my ($time) = @_;
334 my $line;
335
a75fecec 336 doprint "** Wait for monitor to settle down **\n";
7faafbd6
SR
337
338 # read the monitor and wait for the system to calm down
339 do {
340 $line = wait_for_input($monitor_fp, $time);
a75fecec 341 print "$line" if (defined($line));
7faafbd6 342 } while (defined($line));
a75fecec 343 print "** Monitor flushed **\n";
7faafbd6
SR
344}
345
2b7d9b21
SR
346sub fail {
347
a75fecec 348 if ($die_on_failure) {
2b7d9b21
SR
349 dodie @_;
350 }
351
a75fecec 352 doprint "FAILED\n";
7faafbd6 353
576f627c
SR
354 my $i = $iteration;
355
a75fecec 356 # no need to reboot for just building.
576f627c 357 if (!do_not_reboot) {
a75fecec
SR
358 doprint "REBOOTING\n";
359 reboot;
360 start_monitor;
361 wait_for_monitor $sleep_time;
362 end_monitor;
363 }
7faafbd6 364
576f627c
SR
365 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
366 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
a75fecec 367 doprint "**** Failed: ", @_, " ****\n";
576f627c
SR
368 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
369 doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
a75fecec
SR
370
371 return 1 if (!defined($store_failures));
7faafbd6
SR
372
373 my @t = localtime;
374 my $date = sprintf "%04d%02d%02d%02d%02d%02d",
375 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
376
a75fecec
SR
377 my $dir = "$machine-$test_type-$build_type-fail-$date";
378 my $faildir = "$store_failures/$dir";
7faafbd6
SR
379
380 if (!-d $faildir) {
381 mkpath($faildir) or
a75fecec 382 die "can't create $faildir";
7faafbd6 383 }
a75fecec
SR
384 if (-f "$outputdir/.config") {
385 cp "$outputdir/.config", "$faildir/config" or
7faafbd6
SR
386 die "failed to copy .config";
387 }
388 if (-f $buildlog) {
389 cp $buildlog, "$faildir/buildlog" or
390 die "failed to move $buildlog";
391 }
392 if (-f $dmesg) {
393 cp $dmesg, "$faildir/dmesg" or
394 die "failed to move $dmesg";
395 }
396
397 doprint "*** Saved info to $faildir ***\n";
398
2b7d9b21
SR
399 return 1;
400}
401
2545eb61
SR
402sub run_command {
403 my ($command) = @_;
d6ce2a0b
SR
404 my $dolog = 0;
405 my $dord = 0;
406 my $pid;
407
408 doprint("$command ... ");
409
410 $pid = open(CMD, "$command 2>&1 |") or
2b7d9b21 411 (fail "unable to exec $command" and return 0);
2545eb61
SR
412
413 if (defined($opt{"LOG_FILE"})) {
d6ce2a0b
SR
414 open(LOG, ">>$opt{LOG_FILE}") or
415 dodie "failed to write to log";
416 $dolog = 1;
6c5ee0be
SR
417 }
418
419 if (defined($redirect)) {
d6ce2a0b
SR
420 open (RD, ">$redirect") or
421 dodie "failed to write to redirect $redirect";
422 $dord = 1;
2545eb61
SR
423 }
424
d6ce2a0b
SR
425 while (<CMD>) {
426 print LOG if ($dolog);
427 print RD if ($dord);
428 }
2545eb61 429
d6ce2a0b 430 waitpid($pid, 0);
2545eb61
SR
431 my $failed = $?;
432
d6ce2a0b
SR
433 close(CMD);
434 close(LOG) if ($dolog);
435 close(RD) if ($dord);
436
2545eb61
SR
437 if ($failed) {
438 doprint "FAILED!\n";
439 } else {
440 doprint "SUCCESS\n";
441 }
442
5f9b6ced
SR
443 return !$failed;
444}
445
446sub get_grub_index {
447
a75fecec
SR
448 if ($reboot_type ne "grub") {
449 return;
450 }
5a391fbf 451 return if (defined($grub_number));
5f9b6ced
SR
452
453 doprint "Find grub menu ... ";
454 $grub_number = -1;
455 open(IN, "ssh $target cat /boot/grub/menu.lst |")
456 or die "unable to get menu.lst";
457 while (<IN>) {
a75fecec 458 if (/^\s*title\s+$grub_menu\s*$/) {
5f9b6ced
SR
459 $grub_number++;
460 last;
461 } elsif (/^\s*title\s/) {
462 $grub_number++;
463 }
464 }
465 close(IN);
466
a75fecec 467 die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
5f9b6ced
SR
468 if ($grub_number < 0);
469 doprint "$grub_number\n";
2545eb61
SR
470}
471
2545eb61
SR
472sub wait_for_input
473{
474 my ($fp, $time) = @_;
475 my $rin;
476 my $ready;
477 my $line;
478 my $ch;
479
480 if (!defined($time)) {
481 $time = $timeout;
482 }
483
484 $rin = '';
485 vec($rin, fileno($fp), 1) = 1;
486 $ready = select($rin, undef, undef, $time);
487
488 $line = "";
489
490 # try to read one char at a time
491 while (sysread $fp, $ch, 1) {
492 $line .= $ch;
493 last if ($ch eq "\n");
494 }
495
496 if (!length($line)) {
497 return undef;
498 }
499
500 return $line;
501}
502
75c3fda7 503sub reboot_to {
a75fecec
SR
504 if ($reboot_type eq "grub") {
505 run_command "ssh $target '(echo \"savedefault --default=$grub_number --once\" | grub --batch; reboot)'";
506 return;
507 }
508
509 run_command "$reboot_script";
2545eb61
SR
510}
511
a57419b3
SR
512sub get_sha1 {
513 my ($commit) = @_;
514
515 doprint "git rev-list --max-count=1 $commit ... ";
516 my $sha1 = `git rev-list --max-count=1 $commit`;
517 my $ret = $?;
518
519 logit $sha1;
520
521 if ($ret) {
522 doprint "FAILED\n";
523 dodie "Failed to get git $commit";
524 }
525
526 print "SUCCESS\n";
527
528 chomp $sha1;
529
530 return $sha1;
531}
532
5a391fbf 533sub monitor {
2545eb61
SR
534 my $booted = 0;
535 my $bug = 0;
5c42fc5b 536 my $skip_call_trace = 0;
2b7d9b21 537 my $loops;
2545eb61 538
7faafbd6 539 wait_for_monitor 5;
2545eb61
SR
540
541 my $line;
542 my $full_line = "";
543
7faafbd6
SR
544 open(DMESG, "> $dmesg") or
545 die "unable to write to $dmesg";
2545eb61 546
75c3fda7 547 reboot_to;
2545eb61
SR
548
549 for (;;) {
550
2b7d9b21 551 if ($booted) {
a75fecec 552 $line = wait_for_input($monitor_fp, $booted_timeout);
2b7d9b21 553 } else {
7faafbd6 554 $line = wait_for_input($monitor_fp);
2b7d9b21 555 }
2545eb61
SR
556
557 last if (!defined($line));
558
559 doprint $line;
7faafbd6 560 print DMESG $line;
2545eb61
SR
561
562 # we are not guaranteed to get a full line
563 $full_line .= $line;
564
a75fecec 565 if ($full_line =~ /$success_line/) {
2545eb61
SR
566 $booted = 1;
567 }
568
5c42fc5b
SR
569 if ($full_line =~ /\[ backtrace testing \]/) {
570 $skip_call_trace = 1;
571 }
572
2545eb61 573 if ($full_line =~ /call trace:/i) {
5c42fc5b
SR
574 $bug = 1 if (!$skip_call_trace);
575 }
576
577 if ($full_line =~ /\[ end of backtrace testing \]/) {
578 $skip_call_trace = 0;
579 }
580
581 if ($full_line =~ /Kernel panic -/) {
2545eb61
SR
582 $bug = 1;
583 }
584
585 if ($line =~ /\n/) {
586 $full_line = "";
587 }
588 }
589
7faafbd6 590 close(DMESG);
2545eb61 591
a75fecec 592 if ($bug) {
2b7d9b21 593 return 0 if ($in_bisect);
576f627c 594 fail "failed - got a bug report" and return 0;
2545eb61
SR
595 }
596
a75fecec 597 if (!$booted) {
2b7d9b21 598 return 0 if ($in_bisect);
576f627c 599 fail "failed - never got a boot prompt." and return 0;
2545eb61 600 }
5f9b6ced 601
2b7d9b21 602 return 1;
2545eb61
SR
603}
604
605sub install {
606
a75fecec 607 run_command "scp $outputdir/$build_target $target:$target_image" or
5c42fc5b 608 dodie "failed to copy image";
2545eb61 609
5f9b6ced 610 my $install_mods = 0;
2545eb61 611
5f9b6ced
SR
612 # should we process modules?
613 $install_mods = 0;
a75fecec 614 open(IN, "$outputdir/.config") or dodie("Can't read config file");
5f9b6ced
SR
615 while (<IN>) {
616 if (/CONFIG_MODULES(=y)?/) {
617 $install_mods = 1 if (defined($1));
618 last;
5c42fc5b 619 }
5f9b6ced
SR
620 }
621 close(IN);
5c42fc5b 622
5f9b6ced
SR
623 if (!$install_mods) {
624 doprint "No modules needed\n";
625 return;
626 }
2545eb61 627
a75fecec 628 run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
5f9b6ced 629 dodie "Failed to install modules";
5c42fc5b 630
5f9b6ced 631 my $modlib = "/lib/modules/$version";
a57419b3 632 my $modtar = "ktest-mods.tar.bz2";
5c42fc5b 633
5f9b6ced
SR
634 run_command "ssh $target rm -rf $modlib" or
635 dodie "failed to remove old mods: $modlib";
5c42fc5b 636
5f9b6ced 637 # would be nice if scp -r did not follow symbolic links
a75fecec 638 run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
5f9b6ced
SR
639 dodie "making tarball";
640
a75fecec 641 run_command "scp $tmpdir/$modtar $target:/tmp" or
5f9b6ced
SR
642 dodie "failed to copy modules";
643
a75fecec 644 unlink "$tmpdir/$modtar";
5f9b6ced
SR
645
646 run_command "ssh $target '(cd / && tar xf /tmp/$modtar)'" or
647 dodie "failed to tar modules";
2545eb61 648
5f9b6ced 649 run_command "ssh $target rm -f /tmp/$modtar";
8b37ca8c
SR
650
651 return if (!defined($post_install));
652
653 my $save_env = $ENV{KERNEL_VERSION};
654
655 $ENV{KERNEL_VERSION} = $version;
576f627c
SR
656 run_command "$post_install" or
657 dodie "Failed to run post install";
8b37ca8c
SR
658
659 $ENV{KERNEL_VERSION} = $save_env;
2545eb61
SR
660}
661
6c5ee0be
SR
662sub check_buildlog {
663 my ($patch) = @_;
664
6c5ee0be
SR
665 my @files = `git show $patch | diffstat -l`;
666
667 open(IN, "git show $patch |") or
668 dodie "failed to show $patch";
669 while (<IN>) {
670 if (m,^--- a/(.*),) {
671 chomp $1;
672 $files[$#files] = $1;
673 }
674 }
675 close(IN);
676
677 open(IN, $buildlog) or dodie "Can't open $buildlog";
678 while (<IN>) {
679 if (/^\s*(.*?):.*(warning|error)/) {
680 my $err = $1;
681 foreach my $file (@files) {
a75fecec 682 my $fullpath = "$builddir/$file";
6c5ee0be 683 if ($file eq $err || $fullpath eq $err) {
2b7d9b21 684 fail "$file built with warnings" and return 0;
6c5ee0be
SR
685 }
686 }
687 }
688 }
689 close(IN);
2b7d9b21
SR
690
691 return 1;
6c5ee0be
SR
692}
693
2545eb61
SR
694sub build {
695 my ($type) = @_;
5c42fc5b 696 my $defconfig = "";
5c42fc5b 697
7faafbd6
SR
698 unlink $buildlog;
699
75c3fda7 700 if ($type =~ /^useconfig:(.*)/) {
a75fecec 701 run_command "cp $1 $outputdir/.config" or
75c3fda7 702 dodie "could not copy $1 to .config";
5f9b6ced 703
75c3fda7
SR
704 $type = "oldconfig";
705 }
706
5c42fc5b
SR
707 # old config can ask questions
708 if ($type eq "oldconfig") {
9386c6ab 709 $type = "oldnoconfig";
75c3fda7
SR
710
711 # allow for empty configs
a75fecec 712 run_command "touch $outputdir/.config";
75c3fda7 713
a75fecec 714 run_command "mv $outputdir/.config $outputdir/config_temp" or
5c42fc5b 715 dodie "moving .config";
2545eb61 716
5f9b6ced 717 if (!$noclean && !run_command "$make mrproper") {
5c42fc5b
SR
718 dodie "make mrproper";
719 }
2545eb61 720
a75fecec 721 run_command "mv $outputdir/config_temp $outputdir/.config" or
5c42fc5b 722 dodie "moving config_temp";
5c42fc5b
SR
723
724 } elsif (!$noclean) {
a75fecec 725 unlink "$outputdir/.config";
5f9b6ced 726 run_command "$make mrproper" or
5c42fc5b 727 dodie "make mrproper";
5c42fc5b 728 }
2545eb61
SR
729
730 # add something to distinguish this build
a75fecec
SR
731 open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
732 print OUT "$localversion\n";
2545eb61
SR
733 close(OUT);
734
5f9b6ced
SR
735 if (defined($minconfig)) {
736 $defconfig = "KCONFIG_ALLCONFIG=$minconfig";
2545eb61
SR
737 }
738
9386c6ab 739 run_command "$defconfig $make $type" or
5c42fc5b 740 dodie "failed make config";
2545eb61 741
a75fecec
SR
742 $redirect = "$buildlog";
743 if (!run_command "$make $build_options") {
6c5ee0be 744 undef $redirect;
5f9b6ced 745 # bisect may need this to pass
2b7d9b21
SR
746 return 0 if ($in_bisect);
747 fail "failed build" and return 0;
2545eb61 748 }
6c5ee0be 749 undef $redirect;
5f9b6ced 750
2b7d9b21 751 return 1;
2545eb61
SR
752}
753
75c3fda7 754sub halt {
a75fecec 755 if (!run_command "ssh $target halt" or defined($power_off)) {
576f627c
SR
756 if (defined($poweroff_after_halt)) {
757 sleep $poweroff_after_halt;
758 run_command "$power_off";
759 }
760 } else {
75c3fda7 761 # nope? the zap it!
a75fecec 762 run_command "$power_off";
75c3fda7
SR
763 }
764}
765
5f9b6ced
SR
766sub success {
767 my ($i) = @_;
768
769 doprint "\n\n*******************************************\n";
770 doprint "*******************************************\n";
a75fecec 771 doprint "** TEST $i SUCCESS!!!! **\n";
5f9b6ced
SR
772 doprint "*******************************************\n";
773 doprint "*******************************************\n";
774
576f627c 775 if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
a75fecec 776 doprint "Reboot and wait $sleep_time seconds\n";
5f9b6ced 777 reboot;
7faafbd6 778 start_monitor;
a75fecec 779 wait_for_monitor $sleep_time;
7faafbd6 780 end_monitor;
5f9b6ced
SR
781 }
782}
783
784sub get_version {
785 # get the release name
786 doprint "$make kernelrelease ... ";
787 $version = `$make kernelrelease | tail -1`;
788 chomp($version);
789 doprint "$version\n";
790}
791
5a391fbf 792sub child_run_test {
7faafbd6 793 my $failed = 0;
5a391fbf 794
7faafbd6 795 # child should have no power
a75fecec
SR
796 $reboot_on_error = 0;
797 $poweroff_on_error = 0;
798 $die_on_failure = 1;
7faafbd6
SR
799
800 run_command $run_test or $failed = 1;
5a391fbf
SR
801 exit $failed;
802}
803
804my $child_done;
805
806sub child_finished {
807 $child_done = 1;
808}
809
810sub do_run_test {
811 my $child_pid;
812 my $child_exit;
5a391fbf
SR
813 my $line;
814 my $full_line;
815 my $bug = 0;
5a391fbf 816
7faafbd6 817 wait_for_monitor 1;
5a391fbf 818
7faafbd6 819 doprint "run test $run_test\n";
5a391fbf
SR
820
821 $child_done = 0;
822
823 $SIG{CHLD} = qw(child_finished);
824
825 $child_pid = fork;
826
827 child_run_test if (!$child_pid);
828
829 $full_line = "";
830
831 do {
7faafbd6 832 $line = wait_for_input($monitor_fp, 1);
5a391fbf
SR
833 if (defined($line)) {
834
835 # we are not guaranteed to get a full line
836 $full_line .= $line;
837
838 if ($full_line =~ /call trace:/i) {
839 $bug = 1;
840 }
841
842 if ($full_line =~ /Kernel panic -/) {
843 $bug = 1;
844 }
845
846 if ($line =~ /\n/) {
847 $full_line = "";
848 }
849 }
850 } while (!$child_done && !$bug);
851
852 if ($bug) {
853 doprint "Detected kernel crash!\n";
854 # kill the child with extreme prejudice
855 kill 9, $child_pid;
856 }
857
858 waitpid $child_pid, 0;
859 $child_exit = $?;
860
5a391fbf 861 if ($bug || $child_exit) {
2b7d9b21
SR
862 return 0 if $in_bisect;
863 fail "test failed" and return 0;
5a391fbf 864 }
2b7d9b21 865 return 1;
5a391fbf
SR
866}
867
a75fecec
SR
868sub run_git_bisect {
869 my ($command) = @_;
870
871 doprint "$command ... ";
872
873 my $output = `$command 2>&1`;
874 my $ret = $?;
875
876 logit $output;
877
878 if ($ret) {
879 doprint "FAILED\n";
880 dodie "Failed to git bisect";
881 }
882
883 doprint "SUCCESS\n";
884 if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
885 doprint "$1 [$2]\n";
886 } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
887 $bisect_bad = $1;
888 doprint "Found bad commit... $1\n";
889 return 0;
890 } else {
891 # we already logged it, just print it now.
892 print $output;
893 }
894
895 return 1;
896}
897
5f9b6ced
SR
898sub run_bisect {
899 my ($type) = @_;
900
2b7d9b21 901 my $failed = 0;
5f9b6ced
SR
902 my $result;
903 my $output;
904 my $ret;
905
5f9b6ced 906 if (defined($minconfig)) {
2b7d9b21 907 build "useconfig:$minconfig" or $failed = 1;
5f9b6ced
SR
908 } else {
909 # ?? no config to use?
2b7d9b21 910 build "oldconfig" or $failed = 1;
5f9b6ced
SR
911 }
912
913 if ($type ne "build") {
7faafbd6 914 dodie "Failed on build" if $failed;
5f9b6ced
SR
915
916 # Now boot the box
917 get_grub_index;
918 get_version;
919 install;
7faafbd6
SR
920
921 start_monitor;
2b7d9b21 922 monitor or $failed = 1;
5f9b6ced
SR
923
924 if ($type ne "boot") {
7faafbd6 925 dodie "Failed on boot" if $failed;
5a391fbf 926
2b7d9b21 927 do_run_test or $failed = 1;
5f9b6ced 928 }
7faafbd6 929 end_monitor;
5f9b6ced
SR
930 }
931
932 if ($failed) {
933 $result = "bad";
5a391fbf
SR
934
935 # reboot the box to a good kernel
a75fecec
SR
936 if ($type ne "build") {
937 doprint "Reboot and sleep $bisect_sleep_time seconds\n";
5a391fbf 938 reboot;
7faafbd6 939 start_monitor;
a75fecec 940 wait_for_monitor $bisect_sleep_time;
7faafbd6 941 end_monitor;
5a391fbf 942 }
5f9b6ced
SR
943 } else {
944 $result = "good";
945 }
946
d6ce2a0b
SR
947 # Are we looking for where it worked, not failed?
948 if ($reverse_bisect) {
949 if ($failed) {
950 $result = "good";
951 } else {
952 $result = "bad";
953 }
954 }
955
a75fecec 956 return $result;
5f9b6ced
SR
957}
958
959sub bisect {
960 my ($i) = @_;
961
962 my $result;
963
964 die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
965 die "BISECT_BAD[$i] not defined\n" if (!defined($opt{"BISECT_BAD[$i]"}));
966 die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
967
968 my $good = $opt{"BISECT_GOOD[$i]"};
969 my $bad = $opt{"BISECT_BAD[$i]"};
970 my $type = $opt{"BISECT_TYPE[$i]"};
a75fecec
SR
971 my $start = $opt{"BISECT_START[$i]"};
972 my $replay = $opt{"BISECT_REPLAY[$i]"};
5f9b6ced 973
a57419b3
SR
974 # convert to true sha1's
975 $good = get_sha1($good);
976 $bad = get_sha1($bad);
977
d6ce2a0b
SR
978 if (defined($opt{"BISECT_REVERSE[$i]"}) &&
979 $opt{"BISECT_REVERSE[$i]"} == 1) {
980 doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
981 $reverse_bisect = 1;
982 } else {
983 $reverse_bisect = 0;
984 }
985
5f9b6ced
SR
986 $in_bisect = 1;
987
a75fecec
SR
988 # Can't have a test without having a test to run
989 if ($type eq "test" && !defined($run_test)) {
990 $type = "boot";
991 }
992
993 my $check = $opt{"BISECT_CHECK[$i]"};
994 if (defined($check) && $check ne "0") {
995
996 # get current HEAD
a57419b3 997 my $head = get_sha1("HEAD");
a75fecec
SR
998
999 if ($check ne "good") {
1000 doprint "TESTING BISECT BAD [$bad]\n";
1001 run_command "git checkout $bad" or
1002 die "Failed to checkout $bad";
1003
1004 $result = run_bisect $type;
1005
1006 if ($result ne "bad") {
1007 fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
1008 }
1009 }
1010
1011 if ($check ne "bad") {
1012 doprint "TESTING BISECT GOOD [$good]\n";
1013 run_command "git checkout $good" or
1014 die "Failed to checkout $good";
1015
1016 $result = run_bisect $type;
1017
1018 if ($result ne "good") {
1019 fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
1020 }
1021 }
1022
1023 # checkout where we started
1024 run_command "git checkout $head" or
1025 die "Failed to checkout $head";
1026 }
1027
5f9b6ced 1028 run_command "git bisect start" or
a75fecec 1029 dodie "could not start bisect";
5f9b6ced
SR
1030
1031 run_command "git bisect good $good" or
a75fecec 1032 dodie "could not set bisect good to $good";
5f9b6ced 1033
a75fecec
SR
1034 run_git_bisect "git bisect bad $bad" or
1035 dodie "could not set bisect bad to $bad";
5f9b6ced 1036
a75fecec
SR
1037 if (defined($replay)) {
1038 run_command "git bisect replay $replay" or
1039 dodie "failed to run replay";
5a391fbf
SR
1040 }
1041
a75fecec
SR
1042 if (defined($start)) {
1043 run_command "git checkout $start" or
1044 dodie "failed to checkout $start";
1045 }
1046
1047 my $test;
5f9b6ced
SR
1048 do {
1049 $result = run_bisect $type;
a75fecec
SR
1050 $test = run_git_bisect "git bisect $result";
1051 } while ($test);
5f9b6ced
SR
1052
1053 run_command "git bisect log" or
1054 dodie "could not capture git bisect log";
1055
1056 run_command "git bisect reset" or
1057 dodie "could not reset git bisect";
1058
1059 doprint "Bad commit was [$bisect_bad]\n";
1060
1061 $in_bisect = 0;
1062
1063 success $i;
1064}
1065
6c5ee0be
SR
1066sub patchcheck {
1067 my ($i) = @_;
1068
1069 die "PATCHCHECK_START[$i] not defined\n"
1070 if (!defined($opt{"PATCHCHECK_START[$i]"}));
1071 die "PATCHCHECK_TYPE[$i] not defined\n"
1072 if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
1073
1074 my $start = $opt{"PATCHCHECK_START[$i]"};
1075
1076 my $end = "HEAD";
1077 if (defined($opt{"PATCHCHECK_END[$i]"})) {
1078 $end = $opt{"PATCHCHECK_END[$i]"};
1079 }
1080
a57419b3
SR
1081 # Get the true sha1's since we can use things like HEAD~3
1082 $start = get_sha1($start);
1083 $end = get_sha1($end);
1084
6c5ee0be
SR
1085 my $type = $opt{"PATCHCHECK_TYPE[$i]"};
1086
1087 # Can't have a test without having a test to run
1088 if ($type eq "test" && !defined($run_test)) {
1089 $type = "boot";
1090 }
1091
1092 open (IN, "git log --pretty=oneline $end|") or
1093 dodie "could not get git list";
1094
1095 my @list;
1096
1097 while (<IN>) {
1098 chomp;
1099 $list[$#list+1] = $_;
1100 last if (/^$start/);
1101 }
1102 close(IN);
1103
1104 if ($list[$#list] !~ /^$start/) {
2b7d9b21 1105 fail "SHA1 $start not found";
6c5ee0be
SR
1106 }
1107
1108 # go backwards in the list
1109 @list = reverse @list;
1110
1111 my $save_clean = $noclean;
1112
1113 $in_patchcheck = 1;
1114 foreach my $item (@list) {
1115 my $sha1 = $item;
1116 $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
1117
1118 doprint "\nProcessing commit $item\n\n";
1119
1120 run_command "git checkout $sha1" or
1121 die "Failed to checkout $sha1";
1122
1123 # only clean on the first and last patch
1124 if ($item eq $list[0] ||
1125 $item eq $list[$#list]) {
1126 $noclean = $save_clean;
1127 } else {
1128 $noclean = 1;
1129 }
1130
1131 if (defined($minconfig)) {
2b7d9b21 1132 build "useconfig:$minconfig" or return 0;
6c5ee0be
SR
1133 } else {
1134 # ?? no config to use?
2b7d9b21 1135 build "oldconfig" or return 0;
6c5ee0be
SR
1136 }
1137
2b7d9b21 1138 check_buildlog $sha1 or return 0;
6c5ee0be
SR
1139
1140 next if ($type eq "build");
1141
1142 get_grub_index;
1143 get_version;
1144 install;
6c5ee0be 1145
7faafbd6
SR
1146 my $failed = 0;
1147
1148 start_monitor;
1149 monitor or $failed = 1;
1150
1151 if (!$failed && $type ne "boot"){
1152 do_run_test or $failed = 1;
1153 }
1154 end_monitor;
1155 return 0 if ($failed);
1156
6c5ee0be
SR
1157 }
1158 $in_patchcheck = 0;
1159 success $i;
2b7d9b21
SR
1160
1161 return 1;
6c5ee0be
SR
1162}
1163
2545eb61
SR
1164read_config $ARGV[0];
1165
1166# mandatory configs
1167die "MACHINE not defined\n" if (!defined($opt{"MACHINE"}));
1168die "SSH_USER not defined\n" if (!defined($opt{"SSH_USER"}));
1169die "BUILD_DIR not defined\n" if (!defined($opt{"BUILD_DIR"}));
1170die "OUTPUT_DIR not defined\n" if (!defined($opt{"OUTPUT_DIR"}));
1171die "BUILD_TARGET not defined\n" if (!defined($opt{"BUILD_TARGET"}));
75c3fda7 1172die "TARGET_IMAGE not defined\n" if (!defined($opt{"TARGET_IMAGE"}));
2545eb61
SR
1173die "POWER_CYCLE not defined\n" if (!defined($opt{"POWER_CYCLE"}));
1174die "CONSOLE not defined\n" if (!defined($opt{"CONSOLE"}));
1175die "LOCALVERSION not defined\n" if (!defined($opt{"LOCALVERSION"}));
2545eb61 1176
2b7d9b21
SR
1177if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
1178 unlink $opt{"LOG_FILE"};
1179}
2545eb61 1180
2b7d9b21
SR
1181doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
1182
a57419b3
SR
1183for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
1184
1185 if (!$i) {
1186 doprint "DEFAULT OPTIONS:\n";
1187 } else {
1188 doprint "\nTEST $i OPTIONS";
1189 if (defined($repeat_tests{$i})) {
1190 $repeat = $repeat_tests{$i};
1191 doprint " ITERATE $repeat";
1192 }
1193 doprint "\n";
1194 }
1195
1196 foreach my $option (sort keys %opt) {
1197
1198 if ($option =~ /\[(\d+)\]$/) {
1199 next if ($i != $1);
1200 } else {
1201 next if ($i);
1202 }
1203
1204 doprint "$option = $opt{$option}\n";
1205 }
2b7d9b21 1206}
2545eb61 1207
a75fecec 1208sub set_test_option {
5a391fbf 1209 my ($name, $i) = @_;
2545eb61 1210
5a391fbf 1211 my $option = "$name\[$i\]";
5c42fc5b 1212
5a391fbf
SR
1213 if (defined($opt{$option})) {
1214 return $opt{$option};
5f9b6ced
SR
1215 }
1216
a57419b3
SR
1217 foreach my $test (keys %repeat_tests) {
1218 if ($i >= $test &&
1219 $i < $test + $repeat_tests{$test}) {
1220 $option = "$name\[$test\]";
1221 if (defined($opt{$option})) {
1222 return $opt{$option};
1223 }
1224 }
1225 }
1226
5a391fbf
SR
1227 if (defined($opt{$name})) {
1228 return $opt{$name};
2545eb61
SR
1229 }
1230
5a391fbf
SR
1231 return undef;
1232}
1233
1234# First we need to do is the builds
a75fecec
SR
1235for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
1236
576f627c
SR
1237 $iteration = $i;
1238
a75fecec
SR
1239 my $ssh_user = set_test_option("SSH_USER", $i);
1240 my $makecmd = set_test_option("MAKE_CMD", $i);
1241
1242 $machine = set_test_option("MACHINE", $i);
1243 $tmpdir = set_test_option("TMP_DIR", $i);
1244 $outputdir = set_test_option("OUTPUT_DIR", $i);
1245 $builddir = set_test_option("BUILD_DIR", $i);
1246 $test_type = set_test_option("TEST_TYPE", $i);
1247 $build_type = set_test_option("BUILD_TYPE", $i);
1248 $build_options = set_test_option("BUILD_OPTIONS", $i);
1249 $power_cycle = set_test_option("POWER_CYCLE", $i);
1250 $noclean = set_test_option("BUILD_NOCLEAN", $i);
1251 $minconfig = set_test_option("MIN_CONFIG", $i);
1252 $run_test = set_test_option("TEST", $i);
1253 $addconfig = set_test_option("ADD_CONFIG", $i);
1254 $reboot_type = set_test_option("REBOOT_TYPE", $i);
1255 $grub_menu = set_test_option("GRUB_MENU", $i);
8b37ca8c 1256 $post_install = set_test_option("POST_INSTALL", $i);
a75fecec
SR
1257 $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
1258 $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
1259 $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
1260 $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
1261 $power_off = set_test_option("POWER_OFF", $i);
576f627c
SR
1262 $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
1263 $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
a75fecec
SR
1264 $sleep_time = set_test_option("SLEEP_TIME", $i);
1265 $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
1266 $store_failures = set_test_option("STORE_FAILURES", $i);
1267 $timeout = set_test_option("TIMEOUT", $i);
1268 $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
1269 $console = set_test_option("CONSOLE", $i);
1270 $success_line = set_test_option("SUCCESS_LINE", $i);
1271 $build_target = set_test_option("BUILD_TARGET", $i);
1272 $target_image = set_test_option("TARGET_IMAGE", $i);
1273 $localversion = set_test_option("LOCALVERSION", $i);
1274
1275 chdir $builddir || die "can't change directory to $builddir";
1276
1277 if (!-d $tmpdir) {
1278 mkpath($tmpdir) or
1279 die "can't create $tmpdir";
1280 }
1a5cfce3 1281
a75fecec
SR
1282 $target = "$ssh_user\@$machine";
1283
1284 $buildlog = "$tmpdir/buildlog-$machine";
1285 $dmesg = "$tmpdir/dmesg-$machine";
1286 $make = "$makecmd O=$outputdir";
1287
1288 if ($reboot_type eq "grub") {
576f627c 1289 dodie "GRUB_MENU not defined" if (!defined($grub_menu));
a75fecec 1290 } elsif (!defined($reboot_script)) {
576f627c 1291 dodie "REBOOT_SCRIPT not defined"
a75fecec
SR
1292 }
1293
1294 my $run_type = $build_type;
1295 if ($test_type eq "patchcheck") {
1296 $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
1297 } elsif ($test_type eq "bisect") {
1298 $run_type = $opt{"BISECT_TYPE[$i]"};
1299 }
1300
1301 # mistake in config file?
1302 if (!defined($run_type)) {
1303 $run_type = "ERROR";
1304 }
5a391fbf 1305
2545eb61 1306 doprint "\n\n";
a75fecec 1307 doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type\n\n";
7faafbd6
SR
1308
1309 unlink $dmesg;
1310 unlink $buildlog;
2545eb61 1311
2b7d9b21
SR
1312 if (!defined($minconfig)) {
1313 $minconfig = $addconfig;
1314
1315 } elsif (defined($addconfig)) {
a75fecec 1316 run_command "cat $addconfig $minconfig > $tmpdir/use_config" or
2b7d9b21 1317 dodie "Failed to create temp config";
a75fecec 1318 $minconfig = "$tmpdir/use_config";
2b7d9b21
SR
1319 }
1320
6c5ee0be
SR
1321 my $checkout = $opt{"CHECKOUT[$i]"};
1322 if (defined($checkout)) {
1323 run_command "git checkout $checkout" or
1324 die "failed to checkout $checkout";
1325 }
1326
a75fecec 1327 if ($test_type eq "bisect") {
5f9b6ced
SR
1328 bisect $i;
1329 next;
a75fecec 1330 } elsif ($test_type eq "patchcheck") {
6c5ee0be
SR
1331 patchcheck $i;
1332 next;
2545eb61 1333 }
2545eb61 1334
7faafbd6
SR
1335 if ($build_type ne "nobuild") {
1336 build $build_type or next;
2545eb61
SR
1337 }
1338
a75fecec
SR
1339 if ($test_type ne "build") {
1340 get_grub_index;
1341 get_version;
1342 install;
5a391fbf 1343
a75fecec
SR
1344 my $failed = 0;
1345 start_monitor;
1346 monitor or $failed = 1;;
1347
1348 if (!$failed && $test_type ne "boot" && defined($run_test)) {
1349 do_run_test or $failed = 1;
1350 }
1351 end_monitor;
1352 next if ($failed);
5a391fbf
SR
1353 }
1354
5f9b6ced 1355 success $i;
2545eb61
SR
1356}
1357
5c42fc5b 1358if ($opt{"POWEROFF_ON_SUCCESS"}) {
75c3fda7 1359 halt;
576f627c 1360} elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
75c3fda7 1361 reboot;
5c42fc5b 1362}
75c3fda7 1363
2545eb61 1364exit 0;
This page took 0.124902 seconds and 5 git commands to generate.