home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / TestSmoke.pm < prev    next >
Encoding:
Perl POD Document  |  2004-08-06  |  25.8 KB  |  937 lines

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