ktest.pl 28 KB

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