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