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