home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / TestSmoke.pm < prev    next >
Encoding:
Perl POD Document  |  2003-10-20  |  26.1 KB  |  924 lines

  1. package Apache::TestSmoke;
  2.  
  3. use strict;
  4. use warnings FATAL => 'all';
  5.  
  6. use Apache::Test ();
  7. use Apache::TestConfig ();
  8. use Apache::TestTrace;
  9.  
  10. use Apache::TestHarness ();
  11. use Apache::TestRun (); # for core scan functions
  12.  
  13. use Getopt::Long qw(GetOptions);
  14. use File::Spec::Functions qw(catfile);
  15. use FindBin;
  16. use POSIX ();
  17. use Symbol ();
  18.  
  19. #use constant DEBUG => 1;
  20.  
  21. # how many times to run all tests at the first iteration
  22. use constant DEFAULT_TIMES  => 10;
  23.  
  24. # how many various seeds to try in NONSTOP mode
  25. use constant DEFAULT_ITERATIONS  => 10;
  26.  
  27. # if after this number of tries to reduce the number of tests fails we
  28. # give up on more tries
  29. use constant MAX_REDUCTION_TRIES => 50;
  30.  
  31. my @num_opts  = qw(times iterations);
  32. my @string_opts  = qw(order report);
  33. my @flag_opts = qw(help verbose bug_mode);
  34.  
  35. my %order = map {$_ => 1} qw(random repeat rotate);
  36.  
  37. my %usage = (
  38.    'iterations=N'    => 'number of random iterations to run' .
  39.                         ' (default: ' . DEFAULT_ITERATIONS . ')',
  40.    'times=N'         => 'try to repeat all tests at most N times' .
  41.                         ' (default: ' . DEFAULT_TIMES . ')',
  42.    'order=MODE'      => 'modes: random, repeat, rotate' .
  43.                         ' (default: random)',
  44.    'report=FILENAME' => 'save report in a filename' .
  45.                         ' (default: smoke-report-<date>.txt)',
  46.    'verbose[=1]'     => 'verbose output' .
  47.                         ' (default: 0)',
  48.    'bug_mode'        => 'bug report mode' .
  49.                         ' (default: 0)',
  50. );
  51.  
  52. sub new {
  53.     my($class, @argv) = @_;
  54.  
  55.     my $self = bless {
  56.         seen    => {}, # seen sequences and tried them md5 hash
  57.         results => {}, # final reduced sequences md5 hash
  58.         smoking_completed         => 0,
  59.         tests                     => [],
  60.         total_iterations          => 0,
  61.         total_reduction_attempts  => 0,
  62.         total_reduction_successes => 0,
  63.         total_tests_run           => 0,
  64.     }, ref($class)||$class;
  65.  
  66.     $self->{test_config} = Apache::TestConfig->thaw;
  67.  
  68.     $self->getopts(\@argv);
  69.     my $opts = $self->{opts};
  70.  
  71.     chdir "$FindBin::Bin/..";
  72.  
  73.     $self->{times}   = $opts->{times}   || DEFAULT_TIMES;
  74.     $self->{order}   = $opts->{order}   || 'random';
  75.     $self->{verbose} = $opts->{verbose} || 0;
  76.  
  77.     # it doesn't make sense to run a known sequence more than once
  78.     if ($self->{order} eq 'random') {
  79.         $self->{run_iter} = $opts->{iterations} || DEFAULT_ITERATIONS;
  80.     }
  81.     else {
  82.         error "forcing only one iteration for non-random order";
  83.         $self->{run_iter} = 1;
  84.     }
  85.  
  86.     # this is like 'make test' but produces an output to be used in
  87.     # the bug report
  88.     if ($opts->{bug_mode}) {
  89.         $self->{bug_mode} = 1;
  90.         $self->{run_iter} = 1;
  91.         $self->{times}    = 1;
  92.         $self->{verbose}  = 1;
  93.         $self->{order}    = 'rotate';
  94.         $self->{trace}    = 'debug';
  95.     }
  96.  
  97.     # server is run from under t/
  98.     Apache::TestHarness->chdir_t;
  99.  
  100.     # specific tests end up in $self->{tests} and $self->{subtests};
  101.     # and get removed from $self->{argv}
  102.     $self->Apache::TestRun::split_test_args();
  103.  
  104.     my $test_opts = {
  105.         #verbose  => $self->{verbose},
  106.         tests    => $self->{tests},
  107.         times    => $self->{times},
  108.         order    => $self->{order},
  109.         subtests => $self->{subtests} || [],
  110.     };
  111.  
  112.     @{ $self->{tests} } = $self->get_tests($test_opts);
  113.  
  114.     $self->{base_command} = "$^X $FindBin::Bin/TEST";
  115.  
  116.     # options common to all
  117.     $self->{base_command} .= " -verbose" if $self->{verbose};
  118.  
  119.     # options specific to the startup
  120.     $self->{start_command} = "$self->{base_command} -start";
  121.     $self->{start_command} .= " -trace=" . $self->{trace} if $self->{trace};
  122.  
  123.     # options specific to the run
  124.     $self->{run_command} = "$self->{base_command} -run";
  125.  
  126.     # options specific to the stop
  127.     $self->{stop_command} = "$self->{base_command} -stop";
  128.  
  129.     $self;
  130. }
  131.  
  132. sub getopts {
  133.     my($self, $argv) = @_;
  134.     my %opts;
  135.     local *ARGV = $argv;
  136.  
  137.     # permute      : optional values can come before the options
  138.     # pass_through : all unknown things are to be left in @ARGV
  139.     Getopt::Long::Configure(qw(pass_through permute));
  140.  
  141.     # grab from @ARGV only the options that we expect
  142.     GetOptions(\%opts, @flag_opts,
  143.                (map "$_=s", @string_opts),
  144.                (map "$_=i", @num_opts));
  145.  
  146.     if (exists $opts{order}  && !exists $order{$opts{order}}) {
  147.         error "unknown -order mode: $opts{order}";
  148.         $self->opt_help();
  149.         exit;
  150.     }
  151.  
  152.     if ($opts{help}) {
  153.         $self->opt_help;
  154.         exit;
  155.     }
  156.  
  157.     # min
  158.     $self->{opts} = \%opts;
  159.  
  160.     $self->{argv} = [@ARGV];
  161. }
  162.  
  163. # XXX: need proper sub-classing
  164. # from Apache::TestHarness
  165. sub skip      { Apache::TestHarness::skip(@_); }
  166. sub prune     { Apache::TestHarness::prune(@_); }
  167. sub get_tests { Apache::TestHarness::get_tests(@_);}
  168.  
  169. sub install_sighandlers {
  170.     my $self = shift;
  171.  
  172.     $SIG{INT} = sub {
  173.         # make sure that there the server is down
  174.         $self->kill_proc();
  175.  
  176.         $self->report_finish;
  177.         exit;
  178.     };
  179. }
  180.  
  181. END {
  182.     local $?; # preserve the exit status
  183.     eval {
  184.         Apache::TestRun->new(test_config =>
  185.                              Apache::TestConfig->thaw)->scan_core;
  186.     };
  187. }
  188.  
  189. sub run {
  190.     my($self) = shift;
  191.  
  192.     $self->Apache::TestRun::warn_core();
  193.     local $SIG{INT};
  194.     $self->install_sighandlers;
  195.  
  196.     $self->report_start();
  197.  
  198.     if ($self->{bug_mode}) {
  199.         # 'make test', but useful for bug reports
  200.         $self->run_bug_mode();
  201.     }
  202.     else {
  203.          # normal smoke
  204.         my $iter = 0;
  205.         while ($iter++ < $self->{run_iter}) {
  206.             my $last = $self->run_iter($iter);
  207.             last if $last;
  208.         }
  209.     }
  210.     $self->{smoking_completed} = 1;
  211.     $self->report_finish();
  212.     exit;
  213. }
  214.  
  215. sub sep {
  216.     my($char, $title) = @_;
  217.     my $width = 60;
  218.     if ($title) {
  219.         my $side = int( ($width - length($title) - 2) / 2);
  220.         my $pad  = ($side+1) * 2 + length($title) < $width ? 1 : 0;
  221.         return $char x $side . " $title " . $char x ($side+$pad);
  222.     }
  223.     else {
  224.         return $char x $width;
  225.     }
  226. }
  227.  
  228. my %log_files = ();
  229. use constant FH  => 0;
  230. use constant POS => 1;
  231. sub logs_init {
  232.     my($self, @log_files) = @_;
  233.  
  234.     for my $path (@log_files) {
  235.         my $fh = Symbol::gensym();
  236.         open $fh, "<$path" or die "Can't open $path: $!";
  237.         seek $fh, 0, POSIX::SEEK_END();
  238.         $log_files{$path}[FH]  = $fh;
  239.         $log_files{$path}[POS] = tell $fh;
  240.     }
  241. }
  242.  
  243. sub logs_end {
  244.     for my $path (keys %log_files) {
  245.         close $log_files{$path}[FH];
  246.     }
  247. }
  248.  
  249. sub log_diff {
  250.     my($self, $path) = @_;
  251.  
  252.     my $log = $log_files{$path};
  253.     die "no such log file: $path" unless $log;
  254.  
  255.     my $fh = $log->[FH];
  256.     # no checkpoints were made yet?
  257.     unless (defined $log->[POS]) {
  258.         seek $fh, 0, POSIX::SEEK_END();
  259.         $log->[POS] = tell $fh;
  260.         return '';
  261.     }
  262.  
  263.     seek $fh, $log->[POS], POSIX::SEEK_SET(); # not really needed
  264.     local $/; # slurp mode
  265.     my $diff = <$fh>;
  266.     seek $fh, 0, POSIX::SEEK_END(); # not really needed
  267.     $log->[POS] = tell $fh;
  268.  
  269.     return $diff || '';
  270. }
  271.  
  272. # this is a special mode, which really just runs 't/TEST -start;
  273. # t/TEST -run; t/TEST -stop;' but it runs '-run' separately for each
  274. # test, and checks whether anything bad has happened after the run 
  275. # of each test (i.e. either a test has failed, or a test may be successful,
  276. # but server may have dumped a core file, we detect that).
  277. sub run_bug_mode {
  278.     my($self) = @_;
  279.  
  280.     my $iter = 0;
  281.  
  282.     warning "running t/TEST in the bug report mode";
  283.  
  284.     my $reduce_iter = 0;
  285.     my @good = ();
  286.  
  287.     # first time run all tests, or all specified tests
  288.     my @tests = @{ $self->{tests} }; # copy
  289.     my $bad = $self->run_test($iter, $reduce_iter, \@tests, \@good);
  290.     $self->{total_iterations}++;
  291.  
  292. }
  293.  
  294.  
  295. # returns true if for some reason no more iterations should be made
  296. sub run_iter {
  297.     my($self, $iter) = @_;
  298.  
  299.     my $stop_now = 0;
  300.     my $reduce_iter = 0;
  301.     my @good = ();
  302.     warning "\n" . sep("-");
  303.     warning sprintf "[%03d-%02d-%02d] trying all tests $self->{times} times",
  304.         $iter, $reduce_iter, 0;
  305.  
  306.     # first time run all tests, or all specified tests
  307.     my @tests = @{ $self->{tests} }; # copy 
  308.     my $bad = $self->run_test($iter, $reduce_iter, \@tests, \@good);
  309.     unless ($bad) {
  310.         $self->{total_iterations}++;
  311.         return $stop_now;
  312.     }
  313.     error "recorded a positive failure ('$bad'), " .
  314.         "will try to minimize the input now";
  315.  
  316.     my $command = $self->{base_command};
  317.  
  318.     # does the test fail on its own
  319.     {
  320.         $reduce_iter++;
  321.         warning sprintf "[%03d-%02d-%02d] trying '$bad' on its own",
  322.             $iter, $reduce_iter, 1;
  323.         my @good = ();
  324.         my @tests = ($bad);
  325.         my $bad = $self->run_test($iter, $reduce_iter, \@tests, \@good);
  326.         # if a test is failing on its own there is no point to
  327.         # continue looking for other sequences
  328.         if ($bad) {
  329.             $stop_now = 1;
  330.             $self->{total_iterations}++;
  331.             unless ($self->sequence_seen($self->{results}, [@good, $bad])) {
  332.                 $self->report_success($iter, $reduce_iter, "$command $bad", 1);
  333.             }
  334.             return $stop_now;
  335.         }
  336.     }
  337.  
  338.     # positive failure
  339.     my $ok_tests = @good;
  340.     my $reduction_success = 0;
  341.     my $done = 0;
  342.     while (@good > 1) {
  343.         my $tries = 0;
  344.         my $reduce_sub = $self->reduce_stream(\@good);
  345.         $reduce_iter++;
  346.         while ($tries++ < MAX_REDUCTION_TRIES) {
  347.             $self->{total_reduction_attempts}++;
  348.             my @try = @{ $reduce_sub->() };
  349.  
  350.             # reduction stream is empty (tried all?)
  351.             unless (@try) {
  352.                 $done = 1;
  353.                 last;
  354.             }
  355.  
  356.             warning sprintf "\n[%03d-%02d-%02d] trying %d tests",
  357.                 $iter, $reduce_iter, $tries, scalar(@try);
  358.             my @ok = ();
  359.             my @tests = (@try, $bad);
  360.             my $new_bad = $self->run_test($iter, $reduce_iter, \@tests, \@ok);
  361.             if ($new_bad) {
  362.                 # successful reduction
  363.                 $reduction_success++;
  364.                 @good = @ok;
  365.                 $tries = 0;
  366.                 my $num = @ok;
  367.                 error "*** reduction $reduce_iter succeeded ($num tests) ***";
  368.                 $self->{total_reduction_successes}++;
  369.                 last;
  370.             }
  371.         }
  372.  
  373.         # last round of reducing has failed, so we give up
  374.         if ($done || $tries >= MAX_REDUCTION_TRIES){
  375.             error "no further reductions were made";
  376.             $done = 1;
  377.             last;
  378.         }
  379.  
  380.     }
  381.  
  382.     # we have a minimal failure sequence at this point (to the extend
  383.     # of success of our attempts to reduce)
  384.  
  385.     # report the sequence if we didn't see such one yet in the
  386.     # previous iterations
  387.     unless ($self->sequence_seen($self->{results}, [@good, $bad])) {
  388.         # if no reduction succeeded, it's 0
  389.         $reduce_iter = 0 unless $reduction_success;
  390.         $self->report_success($iter, $reduce_iter,
  391.                               "$command @good $bad", @good + 1);
  392.     }
  393.  
  394.     $self->{total_iterations}++;
  395.  
  396.     return $stop_now;
  397. }
  398.  
  399. # my $sub = $self->reduce_stream(\@items);
  400. sub reduce_stream {
  401.     my($self) = shift;
  402.     my @items = @{+shift};
  403.  
  404.     my $items = @items;
  405.     my $odd   = $items % 2 ? 1 : 0;
  406.     my $middle = int($items/2) - 1;
  407.     my $c = 0;
  408.  
  409.     return sub {
  410.         $c++; # remember stream's state 
  411.  
  412.         # a single item is not reduce-able
  413.         return \@items if $items == 1;
  414.  
  415.         my @try = ();
  416.         my $max_repeat_tries = 50; # avoid seen sequences
  417.         my $repeat = 0;
  418.         while ($repeat++ <= $max_repeat_tries) {
  419.  
  420.             # try to use a binary search
  421.             if ($c == 1) {
  422.                 # right half
  423.                 @try = @items[($middle+1)..($items-1)];
  424.             }
  425.             elsif ($c == 2) {
  426.                 # left half
  427.                 @try = @items[0..$middle];
  428.             }
  429.  
  430.             # try to use a random window size alg
  431.             else {
  432.                 my $left = int rand($items);
  433.                 $left = $items - 1 if $left == $items - 1;
  434.                 my $right = $left + int rand($items - $left);
  435.                 $right = $items - 1 if $right >= $items;
  436.                 @try = @items[$left..$right];
  437.             }
  438.  
  439.             if ($self->sequence_seen($self->{seen}, \@try)) {
  440.                 @try = ();
  441.             }
  442.             else {
  443.                 last; # found an unseen sequence
  444.             }
  445.         }
  446.         return \@try;
  447.     }
  448. }
  449.  
  450. sub sequence_seen {
  451.     my ($self, $rh_store, $ra_tests) = @_;
  452.  
  453.     require Digest::MD5;
  454.     my $digest = Digest::MD5::md5_hex(join '', @$ra_tests);
  455.     #error $self->{seen};
  456.     return $rh_store->{$digest}++ ? 1 : 0
  457.  
  458. }
  459.  
  460. sub run_test {
  461.     require IPC::Run3;
  462.     my($self, $iter, $count, $tests, $ra_ok) = @_;
  463.     my $bad = '';
  464.     my $ra_nok = [];
  465.  
  466.     #warning "$self->{base_command} @$tests";
  467.  
  468.     #$SIG{PIPE} = 'IGNORE';
  469.     $SIG{PIPE} = sub { die "pipe broke" };
  470.  
  471.     # start server
  472.     {
  473.         my $command = $self->{start_command};
  474.         my $log = '';
  475.         IPC::Run3::run3($command, undef, \$log, \$log);
  476.         my $started_ok = ($log =~ /started/) ? 1 : 0;
  477.         unless ($started_ok) {
  478.             error "failed to start server\n $log";
  479.             exit 1;
  480.         }
  481.     }
  482.  
  483.     my $t_logs  = $self->{test_config}->{vars}->{t_logs};
  484.     my @log_files = map { catfile $t_logs, $_ } qw(error_log access_log);
  485.     $self->logs_init(@log_files);
  486.  
  487.     # run tests
  488.     {
  489.         my $command = $self->{run_command};
  490.  
  491.         my $max_len = 1;
  492.         for my $test (@$tests) {
  493.             $max_len = length $test if length $test > $max_len;
  494.         }
  495.  
  496.         for my $test (@$tests) {
  497.             (my $test_name = $test) =~ s/\.t$//;
  498.             my $fill = "." x ($max_len - length $test_name);
  499.             $self->{total_tests_run}++;
  500.  
  501.             my $test_command = "$command $test";
  502.             my $log = '';
  503.             IPC::Run3::run3($test_command, undef, \$log, \$log);
  504.             my $ok = ($log =~ /All tests successful/) ? 1 : 0;
  505.  
  506.             my @core_files_msg = $self->Apache::TestRun::scan_core_incremental(1);
  507.  
  508.             # if the test has caused core file(s) it's not ok
  509.             $ok = 0 if @core_files_msg;
  510.  
  511.             if ($ok) {
  512.                 push @$ra_ok, $test;
  513.                 if ($self->{verbose}) {
  514.                     print STDERR "$test_name${fill}ok\n";
  515.                 }
  516.                 # need to run log_diff to reset the position of the fh
  517.                 my %log_diffs = map { $_ => $self->log_diff($_) } @log_files;
  518.  
  519.             }
  520.             else {
  521.                 push @$ra_nok, $test;
  522.                 $bad = $test;
  523.  
  524.                 if ($self->{verbose}) {
  525.                     print STDERR "$test_name${fill}FAILED\n";
  526.                     error sep("-");
  527.  
  528.                     # give server some time to finish the
  529.                     # logging. it's ok to wait long time since we have
  530.                     # to deal with an error
  531.                     sleep 5;
  532.                     my %log_diffs = map { $_ => $self->log_diff($_) } @log_files;
  533.  
  534.                     # client log
  535.                     error "\t\t*** run log ***";
  536.                     $log =~ s/^/    /mg;
  537.                     print STDERR "$log\n";
  538.  
  539.                     # server logs
  540.                     for my $path (@log_files) {
  541.                         next unless length $log_diffs{$path};
  542.                         error "\t\t*** $path ***";
  543.                         $log_diffs{$path} =~ s/^/    /mg;
  544.                         print STDERR "$log_diffs{$path}\n";
  545.                     }
  546.                 }
  547.                 if (@core_files_msg) {
  548.                     unless ($self->{verbose}) {
  549.                         # currently the output of 'run log' already
  550.                         # includes the information about core files once
  551.                         # Test::Harness::Straps allows us to run callbacks
  552.                         # after each test, and we move back to run all
  553.                         # tests at once, we will log the message here
  554.                         error "$test_name caused core";
  555.                         print STDERR join "\n", @core_files_msg, "\n";
  556.                     }
  557.                 }
  558.  
  559.                 if ($self->{verbose}) {
  560.                     error sep("-");
  561.                 }
  562.  
  563.                 unless ($self->{bug_mode}) {
  564.                     # normal smoke stop the run, but in the bug_mode
  565.                     # we want to complete all the tests
  566.                     last;
  567.                 }
  568.             }
  569.  
  570.  
  571.         }
  572.     }
  573.  
  574.     $self->logs_end();
  575.  
  576.     # stop server
  577.     $self->kill_proc();
  578.  
  579.     if ($self->{bug_mode}) {
  580.         warning sep("-");
  581.         if (@$ra_nok == 0) {
  582.             printf STDERR "All tests successful (%d)\n", scalar @$ra_ok;
  583.         }
  584.         else {
  585.             error sprintf "error running %d tests out of %d\n",
  586.                 scalar(@$ra_nok), scalar @$ra_ok + @$ra_nok;
  587.         }
  588.     }
  589.     else {
  590.         return $bad;
  591.     }
  592.  
  593.  
  594. }
  595.  
  596. sub report_start {
  597.     my($self) = shift;
  598.  
  599.     my $time = scalar localtime;
  600.     $self->{start_time} = $time;
  601.     $time =~ s/\s/_/g;
  602.     $time =~ s/:/-/g; # winFU
  603.     my $file = $self->{opts}->{report} || "../smoke-report-$time.txt";
  604.  
  605.     open my $fh, ">$file" or die "cannot open $file for writing: $!";
  606.     $self->{fh} = $fh;
  607.     my $sep = sep("-");
  608.     my $title = sep('=', "Special Tests Sequence Failure Finder Report");
  609.  
  610.         print $fh <<EOM;
  611. $title
  612. $sep
  613. First iteration used:
  614. $self->{base_command} @{$self->{tests}}
  615. $sep
  616. EOM
  617.  
  618. }
  619.  
  620. sub report_success {
  621.     my($self, $iter, $reduce_iter, $sequence, $tests) = @_;
  622.  
  623.     my @report = ("iteration $iter ($tests tests):\n",
  624.         "\t$sequence\n",
  625.         "(made $reduce_iter successful reductions)\n\n");
  626.  
  627.     print @report;
  628.     if (my $fh = $self->{fh}) {
  629.         print $fh @report;
  630.     }
  631. }
  632.  
  633. sub report_finish {
  634.     my($self) = @_;
  635.  
  636.     my $start_time = $self->{start_time};
  637.     my $end_time   = scalar localtime;
  638.     if (my $fh = delete $self->{fh}) {
  639.         my $failures = scalar keys %{ $self->{results} };
  640.  
  641.         my $sep = sep("-");
  642.         my $cfg_as_string = $self->build_config_as_string;
  643.         my $unique_seqs   = scalar keys %{ $self->{results} };
  644.         my $attempts      = $self->{total_reduction_attempts};
  645.         my $successes     = $self->{total_reduction_successes};
  646.         my $completion    = $self->{smoking_completed} 
  647.             ? "Completed"
  648.             : "Not Completed (aborted by user)";
  649.  
  650.         my $status = "Unknown";
  651.         if ($self->{total_iterations} > 0) {
  652.             if ($failures) {
  653.                 $status = "*** NOT OK ***";
  654.             }
  655.             else {
  656.                 $status = "+++ OK +++";
  657.             }
  658.         }
  659.  
  660.         my $title = sep('=', "Summary");
  661.  
  662.         my $iter_made = sprintf "Iterations (%s) made : %d",
  663.             $self->{order}, $self->{total_iterations};
  664.  
  665.         print $fh <<EOM;
  666.  
  667. $title
  668. Completion               : $completion
  669. Status                   : $status
  670. Tests run                : $self->{total_tests_run}
  671. $iter_made
  672. EOM
  673.  
  674.         if ($attempts > 0 && $failures) {
  675.             my $reduction_stats = sprintf "%d/%d (%d%% success)",
  676.                 $attempts, $successes, $successes / $attempts * 100;
  677.  
  678.             print $fh <<EOM;
  679. Unique sequences found  : $unique_seqs
  680. Reduction tries/success : $reduction_stats
  681. EOM
  682.         }
  683.  
  684.         print $fh <<EOM;
  685. $sep
  686. --- Started at: $start_time ---
  687. --- Ended   at: $end_time ---
  688. $sep
  689. The smoke testing was run on the system with the following
  690. parameters:
  691.  
  692. $cfg_as_string
  693.  
  694. -- this report was generated by $0
  695. EOM
  696.         close $fh;
  697.     }
  698. }
  699.  
  700. sub build_config_as_string {
  701.     Apache::TestConfig::as_string();
  702. }
  703.  
  704. sub kill_proc {
  705.     my($self) = @_;
  706.  
  707.     my $command = $self->{stop_command};
  708.     my $log = '';
  709.     require IPC::Run3;
  710.     IPC::Run3::run3($command, undef, \$log, \$log);
  711.  
  712.     my $stopped_ok = ($log =~ /shutdown/) ? 1 : 0;
  713.     unless ($stopped_ok) {
  714.         error "failed to stop server\n $log";
  715.     }
  716. }
  717.  
  718. sub opt_help {
  719.     my $self = shift;
  720.  
  721.     print <<EOM;
  722. usage: t/SMOKE [options ...] [tests]
  723.     where the options are:
  724. EOM
  725.  
  726.     for (sort keys %usage){
  727.         printf "   -%-16s %s\n", $_, $usage{$_};
  728.     }
  729.     print <<EOM;
  730.  
  731.     if 'tests' argument is not provided all available tests will be run
  732. EOM
  733. }
  734.  
  735. # generate t/SMOKE script (or a different filename) which will drive
  736. # Apache::TestSmoke
  737. sub generate_script {
  738.     my ($class, $file) = @_;
  739.  
  740.     $file ||= catfile 't', 'SMOKE';
  741.  
  742.     my $content = join "\n",
  743.         "BEGIN { eval { require blib; } }",
  744.         Apache::TestConfig->modperl_2_inc_fixup,
  745.         Apache::TestConfig->perlscript_header,
  746.         "use $class;",
  747.         "$class->new(\@ARGV)->run;";
  748.  
  749.     Apache::Test::config()->write_perlscript($file, $content);
  750. }
  751.  
  752. 1;
  753. __END__
  754.  
  755. =head1 NAME
  756.  
  757. Apache::TestSmoke - Special Tests Sequence Failure Finder
  758.  
  759. =head1 SYNOPSIS
  760.  
  761.   # get the usage and the default values
  762.   % t/SMOKE -help
  763.  
  764.   # repeat all tests 5 times and try 20 random iterations
  765.   # and save the report into the file 'myreport'
  766.   % t/SMOKE -times=5 -iterations=20 -report=myreport
  767.  
  768.   # run all tests default number of iterations, and repeat tests
  769.   # default number of times
  770.   % t/SMOKE
  771.  
  772.   # same as above but work only the specified tests
  773.   % t/SMOKE foo/bar foo/tar
  774.  
  775.   # run once a sequence of tests in a non-random mode
  776.   # e.g. when trying to reduce a known long sequence that fails
  777.   % t/SMOKE -order=rotate -times=1 foo/bar foo/tar
  778.  
  779.   # show me each currently running test
  780.   # it's not the same as running the tests in the verbose mode
  781.   % t/SMOKE -verbose
  782.  
  783.   # run t/TEST, but show any problems after *each* tests is run
  784.   # useful for bug reports (it actually runs t/TEST -start, then
  785.   # t/TEST -run for each test separately and finally t/TEST -stop
  786.   % t/SMOKE -bug_mode
  787.  
  788.   # now read the created report file
  789.  
  790. =head1 DESCRIPTION
  791.  
  792. =head2 The Problem
  793.  
  794. When we try to test a stateless machine (i.e. all tests are
  795. independent), running all tests once ensures that all tested things
  796. properly work. However when a state machine is tested (i.e. where a
  797. run of one test may influence another test) it's not enough to run all
  798. the tests once to know that the tested features actually work. It's
  799. quite possible that if the same tests are run in a different order
  800. and/or repeated a few times, some tests may fail.  This usually
  801. happens when some tests don't restore the system under test to its
  802. pristine state at the end of the run, which may influence other tests
  803. which rely on the fact that they start on pristine state, when in fact
  804. it's not true anymore. In fact it's possible that a single test may
  805. fail when run twice or three times in a sequence.
  806.  
  807. =head2 The Solution
  808.  
  809. To reduce the possibility of such dependency errors, it's helpful to
  810. run random testing repeated many times with many different srand
  811. seeds. Of course if no failures get spotted that doesn't mean that
  812. there are no tests inter-dependencies, which may cause a failure in
  813. production. But random testing definitely helps to spot many problems
  814. and can give better test coverage.
  815.  
  816. =head2 Resolving Sequence Problems
  817.  
  818. When this kind of testing is used and a failure is detected there are
  819. two problems:
  820.  
  821. =over
  822.  
  823. =item 1
  824.  
  825. First is to be able to reproduce the problem so if we think we fixed
  826. it, we could verify the fix. This one is easy, just remember the
  827. sequence of tests run till the failed test and rerun the same sequence
  828. once again after the problem has been fixed.
  829.  
  830. =item 2
  831.  
  832. Second is to be able to understand the cause of the problem. If during
  833. the random test the failure has happened after running 400 tests, how
  834. can we possibly know which previously running tests has caused to the
  835. failure of the test 401. Chances are that most of the tests were clean
  836. and don't have inter-dependency problem. Therefore it'd be very
  837. helpful if we could reduce the long sequence to a minimum. Preferably
  838. 1 or 2 tests. That's when we can try to understand the cause of the
  839. detected problem.
  840.  
  841. =back
  842.  
  843. This utility attempts to solve both problems, and at the end of each
  844. iteration print a minimal sequence of tests causing to a failure. This
  845. doesn't always succeed, but works in many cases.
  846.  
  847. This utility:
  848.  
  849. =over
  850.  
  851. =item 1
  852.  
  853. Runs the tests randomly until the first failure is detected. Or
  854. non-randomly if the option I<-order> is set to I<repeat> or I<rotate>.
  855.  
  856. =item 2
  857.  
  858. Then it tries to reduce that sequence of tests to a minimum, and this
  859. sequence still causes to the same failure.
  860.  
  861. =item 3
  862.  
  863. (XXX: todo): then it reruns the minimal sequence in the verbose mode
  864. and saves the output.
  865.  
  866. =item 4
  867.  
  868. It reports all the successful reductions as it goes to STDOUT and
  869. report file of the format: smoke-report-<date>.txt.
  870.  
  871. In addition the systems build parameters are logged into the report
  872. file, so the detected problems could be reproduced.
  873.  
  874. =item 5
  875.  
  876. Goto 1 and run again using a new random seed, which potentially should
  877. detect different failures.
  878.  
  879. =back
  880.  
  881. =head1 Reduction Algorithm
  882.  
  883. Currently for each reduction path, the following reduction algorithms
  884. get applied:
  885.  
  886. =over
  887.  
  888. =item 1
  889.  
  890. Binary search: first try the upper half then the lower.
  891.  
  892. =item 2
  893.  
  894. Random window: randomize the left item, then the right item and return
  895. the items between these two points.
  896.  
  897. =back
  898.  
  899. =head1 t/SMOKE.PL
  900.  
  901. I<t/SMOKE.PL> is driving this module, if you don't have it, create it:
  902.  
  903.   #!perl
  904.   
  905.   use strict;
  906.   use warnings FATAL => 'all';
  907.   
  908.   use FindBin;
  909.   use lib "$FindBin::Bin/../Apache-Test/lib";
  910.   use lib "$FindBin::Bin/../lib";
  911.   
  912.   use Apache::TestSmoke ();
  913.   
  914.   Apache::TestSmoke->new(@ARGV)->run;
  915.  
  916. usually I<Makefile.PL> converts it into I<t/SMOKE> while adjusting the
  917. perl path, but you create I<t/SMOKE> in first place as well.
  918.  
  919. =head1 AUTHOR
  920.  
  921. Stas Bekman
  922.  
  923. =cut
  924.