]> git.donarmstrong.com Git - debbugs.git/blob - t/lib/DebbugsTest.pm
merge changes from archimedes
[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 \$gTemplateDir='@{[getcwd()]}/templates';
83 \$gWebHost='localhost';
84 1;
85 END
86                             "$spool_dir/nextnumber" => qq(1\n),
87                             "$config_dir/Maintainers" => qq(foo Blah Bleargh <bar\@baz.com>\n),
88                             "$config_dir/Maintainers.override" => qq(),
89                             "$config_dir/indices/sources" => <<END,
90 foo main foo
91 END
92                             "$config_dir/pseudo-packages.description" => '',
93                            );
94      while (my ($file,$contents) = each %files_to_create) {
95           system('mkdir','-p',dirname($file));
96           my $fh = IO::File->new($file,'w') or
97                die "Unable to create $file: $!";
98           print {$fh} $contents or die "Unable to write $contents to $file: $!";
99           close $fh or die "Unable to close $file: $!";
100      }
101
102      system('touch',"$spool_dir/index.db.realtime");
103      system('ln','-s','index.db.realtime',
104             "$spool_dir/index.db");
105      system('touch',"$spool_dir/index.archive.realtime");
106      system('ln','-s','index.archive.realtime',
107             "$spool_dir/index.archive");
108
109      # create the spool files and sub directories
110      map {system('mkdir','-p',"$spool_dir/$_"); }
111           map {('db-h/'.$_,'archive/'.$_)}
112                map { sprintf "%02d",$_ % 100} 0..99;
113      system('mkdir','-p',"$spool_dir/incoming");
114      system('mkdir','-p',"$spool_dir/lock");
115
116      return (spool_dir => $spool_dir,
117              sendmail_dir => $sendmail_dir,
118              config_dir => $config_dir,
119             );
120 }
121
122 sub dirsize{
123      my ($dir) = @_;
124      opendir(DIR,$dir);
125      my @content = grep {!/^\.\.?$/} readdir(DIR);
126      closedir(DIR);
127      return scalar @content;
128 }
129
130
131 # We're going to use create mime message to create these messages, and
132 # then just send them to receive.
133 # First, check that submit@ works
134
135 sub send_message{
136      my %param = validate_with(params => \@_,
137                                spec   => {to => {type => SCALAR,
138                                                  default => 'submit@bugs.something',
139                                                 },
140                                           headers => {type => ARRAYREF,
141                                                      },
142                                           body    => {type => SCALAR,
143                                                      },
144                                           run_processall =>{type => BOOLEAN,
145                                                             default => 1,
146                                                            },
147                                          }
148                               );
149      $ENV{LOCAL_PART} = $param{to};
150      my ($rfd,$wfd);
151      my $output='';
152      local $SIG{PIPE} = 'IGNORE';
153      local $SIG{CHLD} = sub {};
154      my $pid = open3($wfd,$rfd,$rfd,'scripts/receive')
155           or die "Unable to start receive: $!";
156      print {$wfd} create_mime_message($param{headers},
157                                          $param{body}) or die "Unable to to print to receive";
158      close($wfd) or die "Unable to close receive";
159      my $err = $? >> 8;
160      my $childpid = waitpid($pid,0);
161      if ($childpid != -1) {
162           $err = $? >> 8;
163           print STDERR "receive pid: $pid doesn't match childpid: $childpid\n" if $childpid != $pid;
164      }
165      if ($err != 0 ) {
166           my $rfh =  IO::Handle->new_from_fd($rfd,'r') or die "Unable to create filehandle: $!";
167           $rfh->blocking(0);
168           my $rv;
169           while ($rv = $rfh->sysread($output,1000,length($output))) {}
170           if (not defined $rv) {
171                print STDERR "Reading from STDOUT/STDERR would have blocked.";
172           }
173           print STDERR $output,qq(\n);
174           die "receive failed with exit status $err";
175      }
176      # now we should run processall to see if the message gets processed
177      if ($param{run_processall}) {
178           system('scripts/processall') == 0 or die "processall failed";
179      }
180 }
181
182 {
183      package DebbugsTest::HTTPServer;
184      use base qw(HTTP::Server::Simple::CGI);
185
186      our $child_pid = undef;
187      our $webserver = undef;
188      our $server_handler = undef;
189
190      END {
191           if (defined $child_pid) {
192                # stop the child
193                my $temp_exit = $?;
194                kill(15,$child_pid);
195                waitpid(-1,0);
196                $? = $temp_exit;
197           }
198      }
199
200      sub fork_and_create_webserver {
201           my ($handler,$port) = @_;
202           $port ||= 8080;
203           if (defined $child_pid) {
204                die "We appear to have already forked once";
205           }
206           $server_handler = $handler;
207           my $pid = fork;
208           return 0 if not defined $pid;
209           if ($pid) {
210                $child_pid = $pid;
211                # Wait here for a second to let the child start up
212                sleep 1;
213                return $pid;
214           }
215           else {
216                $webserver = DebbugsTest::HTTPServer->new($port);
217                $webserver->run;
218           }
219
220      }
221
222      sub handle_request {
223           if (defined $server_handler) {
224                $server_handler->(@_);
225           }
226           else {
227                warn "No handler defined\n";
228                print "No handler defined\n";
229           }
230      }
231 }
232
233
234 1;
235
236 __END__
237
238
239