]> git.donarmstrong.com Git - debbugs.git/blob - t/lib/DebbugsTest.pm
Output more information when receive.in fails
[debbugs.git] / t / lib / DebbugsTest.pm
1
2 package DebbugsTest;
3
4 =head1 NAME
5
6 DebbugsTest
7
8 =head1 SYNOPSIS
9
10 use DebbugsTest
11
12
13 =head1 DESCRIPTION
14
15 This module contains various testing routines used to test debbugs in
16 a "pseudo install"
17
18 =head1 FUNCTIONS
19
20 =cut
21
22 use warnings;
23 use strict;
24 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
25 use base qw(Exporter);
26
27 use IO::File;
28 use File::Temp qw(tempdir);
29 use Cwd qw(getcwd);
30 use Debbugs::MIME qw(create_mime_message);
31 use File::Basename qw(dirname basename);
32 use IPC::Open3;
33
34 use Params::Validate qw(validate_with :types);
35
36 BEGIN{
37      $VERSION = 1.00;
38      $DEBUG = 0 unless defined $DEBUG;
39
40      @EXPORT = ();
41      %EXPORT_TAGS = (configuration => [qw(dirsize create_debbugs_configuration send_message)],
42                     );
43      @EXPORT_OK = ();
44      Exporter::export_ok_tags(qw(configuration));
45      $EXPORT_TAGS{all} = [@EXPORT_OK];
46 }
47
48 # First, we're going to send mesages to receive.
49 # To do so, we'll first send a message to submit,
50 # then send messages to the newly created bugnumber.
51
52
53
54 sub create_debbugs_configuration {
55      my %param = validate_with(params => \@_,
56                                spec   => {debug => {type => BOOLEAN,
57                                                     default => 0,
58                                                    },
59                                           cleanup => {type => BOOLEAN,
60                                                       optional => 1,
61                                                      },
62                                          },
63                               );
64      $param{cleanup} = $param{debug}?0:1 if not exists $param{cleanup};
65      my $sendmail_dir = tempdir(CLEANUP => $param{cleanup});
66      my $spool_dir = tempdir(CLEANUP => $param{cleanup});
67      my $config_dir = tempdir(CLEANUP => $param{cleanup});
68
69
70      $ENV{DEBBUGS_CONFIG_FILE}  ="$config_dir/debbugs_config";
71      $ENV{PERL5LIB} = getcwd();
72      $ENV{SENDMAIL_TESTDIR} = $sendmail_dir;
73      my $sendmail_tester = getcwd().'/t/sendmail_tester';
74      unless (-x $sendmail_tester) {
75           die q(t/sendmail_tester doesn't exist or isn't executable. You may be in the wrong directory.);
76      }
77      my %files_to_create = ("$config_dir/debbugs_config" => <<END,
78 \$gSendmail='$sendmail_tester';
79 \$gSpoolDir='$spool_dir';
80 \$gLibPath='@{[getcwd()]}/scripts';
81 1;
82 END
83                             "$spool_dir/nextnumber" => qq(1\n),
84                             "$config_dir/Maintainers" => qq(foo Blah Bleargh <bar\@baz.com>\n),
85                             "$config_dir/Maintainers.override" => qq(),
86                             "$config_dir/indices/sources" => <<END,
87 foo main foo
88 END
89                             "$config_dir/pseudo-packages.description" => '',
90                            );
91      while (my ($file,$contents) = each %files_to_create) {
92           system('mkdir','-p',dirname($file));
93           my $fh = IO::File->new($file,'w') or
94                die "Unable to create $file: $!";
95           print {$fh} $contents or die "Unable to write $contents to $file: $!";
96           close $fh or die "Unable to close $file: $!";
97      }
98
99      system('touch',"$spool_dir/index.db.realtime");
100      system('ln','-s','index.db.realtime',
101             "$spool_dir/index.db");
102      system('touch',"$spool_dir/index.archive.realtime");
103      system('ln','-s','index.archive.realtime',
104             "$spool_dir/index.archive");
105
106      # create the spool files and sub directories
107      map {system('mkdir','-p',"$spool_dir/$_"); }
108           map {('db-h/'.$_,'archive/'.$_)}
109                map { sprintf "%02d",$_ % 100} 0..99;
110      system('mkdir','-p',"$spool_dir/incoming");
111      system('mkdir','-p',"$spool_dir/lock");
112
113      return (spool_dir => $spool_dir,
114              sendmail_dir => $sendmail_dir,
115              config_dir => $config_dir,
116             );
117 }
118
119 sub dirsize{
120      my ($dir) = @_;
121      opendir(DIR,$dir);
122      my @content = grep {!/^\.\.?$/} readdir(DIR);
123      closedir(DIR);
124      return scalar @content;
125 }
126
127
128 # We're going to use create mime message to create these messages, and
129 # then just send them to receive.
130 # First, check that submit@ works
131
132 sub send_message{
133      my %param = validate_with(params => \@_,
134                                spec   => {to => {type => SCALAR,
135                                                  default => 'submit@bugs.something',
136                                                 },
137                                           headers => {type => ARRAYREF,
138                                                      },
139                                           body    => {type => SCALAR,
140                                                      },
141                                           run_processall =>{type => BOOLEAN,
142                                                             default => 1,
143                                                            },
144                                          }
145                               );
146      $ENV{LOCAL_PART} = $param{to};
147      my ($rfd,$wfd);
148      my $output='';
149      my $pid = open3($wfd,$rfd,$rfd,'scripts/receive.in')
150           or die "Unable to start receive.in: $!";
151      print {$wfd} create_mime_message($param{headers},
152                                          $param{body}) or die "Unable to to print to receive.in";
153      close($wfd) or die "Unable to close receive.in";
154      waitpid($pid,0);
155      my $err = $?>>8;
156      if ($err != 0 ) {
157           my $rfh =  IO::Handle->new_from_fd($rfd,'r') or die "Unable to create filehandle: $!";
158           $rfh->blocking(0);
159           my $rv;
160           while ($rv = $rfh->sysread($output,1000,length($output))) {}
161           if (not defined $rv) {
162                print STDERR "Reading from STDOUT/STDERR would have blocked.";
163           }
164           print STDERR $output;
165           die "receive.in failed with exit status $err";
166      }
167      # now we should run processall to see if the message gets processed
168      if ($param{run_processall}) {
169           system('scripts/processall.in') == 0 or die "processall.in failed";
170      }
171 }
172
173 {
174      package DebbugsTest::HTTPServer;
175      use base qw(HTTP::Server::Simple::CGI);
176
177      our $child_pid = undef;
178      our $webserver = undef;
179      our $server_handler = undef;
180
181      END {
182           if (defined $child_pid) {
183                # stop the child
184                kill(15,$child_pid);
185                waitpid(-1,0);
186           }
187      }
188
189      sub fork_and_create_webserver {
190           my ($handler,$port) = @_;
191           $port ||= 8080;
192           if (defined $child_pid) {
193                die "We appear to have already forked once";
194           }
195           $server_handler = $handler;
196           my $pid = fork;
197           return 0 if not defined $pid;
198           if ($pid) {
199                $child_pid = $pid;
200                # Wait here for a second to let the child start up
201                sleep 1;
202                return $pid;
203           }
204           else {
205                $webserver = DebbugsTest::HTTPServer->new($port);
206                $webserver->run;
207           }
208
209      }
210
211      sub handle_request {
212           if (defined $server_handler) {
213                $server_handler->(@_);
214           }
215           else {
216                warn "No handler defined\n";
217                print "No handler defined\n";
218           }
219      }
220 }
221
222
223 1;
224
225 __END__
226
227
228