ktest.pl 37 KB

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