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