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