ktest.pl 28 KB

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