15 This module contains various testing routines used to test debbugs in
24 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
25 use base qw(Exporter);
28 use File::Temp qw(tempdir);
30 use Debbugs::MIME qw(create_mime_message);
31 use File::Basename qw(dirname basename);
35 use Params::Validate qw(validate_with :types);
39 $DEBUG = 0 unless defined $DEBUG;
42 %EXPORT_TAGS = (configuration => [qw(dirsize create_debbugs_configuration send_message)],
45 Exporter::export_ok_tags(qw(configuration));
46 $EXPORT_TAGS{all} = [@EXPORT_OK];
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.
55 sub create_debbugs_configuration {
56 my %param = validate_with(params => \@_,
57 spec => {debug => {type => BOOLEAN,
60 cleanup => {type => BOOLEAN,
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});
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.);
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';
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,
91 "$config_dir/pseudo-packages.description" => '',
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: $!";
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");
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");
115 return (spool_dir => $spool_dir,
116 sendmail_dir => $sendmail_dir,
117 config_dir => $config_dir,
124 my @content = grep {!/^\.\.?$/} readdir(DIR);
126 return scalar @content;
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
135 my %param = validate_with(params => \@_,
136 spec => {to => {type => SCALAR,
137 default => 'submit@bugs.something',
139 headers => {type => ARRAYREF,
141 body => {type => SCALAR,
143 run_processall =>{type => BOOLEAN,
148 $ENV{LOCAL_PART} = $param{to};
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";
159 my $childpid = waitpid($pid,0);
160 if ($childpid != -1) {
162 print STDERR "receive.in pid: $pid doesn't match childpid: $childpid\n" if $childpid != $pid;
165 my $rfh = IO::Handle->new_from_fd($rfd,'r') or die "Unable to create filehandle: $!";
168 while ($rv = $rfh->sysread($output,1000,length($output))) {}
169 if (not defined $rv) {
170 print STDERR "Reading from STDOUT/STDERR would have blocked.";
172 print STDERR $output,qq(\n);
173 die "receive.in failed with exit status $err";
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";
182 package DebbugsTest::HTTPServer;
183 use base qw(HTTP::Server::Simple::CGI);
185 our $child_pid = undef;
186 our $webserver = undef;
187 our $server_handler = undef;
190 if (defined $child_pid) {
197 sub fork_and_create_webserver {
198 my ($handler,$port) = @_;
200 if (defined $child_pid) {
201 die "We appear to have already forked once";
203 $server_handler = $handler;
205 return 0 if not defined $pid;
208 # Wait here for a second to let the child start up
213 $webserver = DebbugsTest::HTTPServer->new($port);
220 if (defined $server_handler) {
221 $server_handler->(@_);
224 warn "No handler defined\n";
225 print "No handler defined\n";