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);
34 use Params::Validate qw(validate_with :types);
38 $DEBUG = 0 unless defined $DEBUG;
41 %EXPORT_TAGS = (configuration => [qw(dirsize create_debbugs_configuration send_message)],
44 Exporter::export_ok_tags(qw(configuration));
45 $EXPORT_TAGS{all} = [@EXPORT_OK];
48 # First, we're going to send mesages to receive.
49 # To do so, we'll first send a message to submit,
50 # then send messages to the newly created bugnumber.
54 sub create_debbugs_configuration {
55 my %param = validate_with(params => \@_,
56 spec => {debug => {type => BOOLEAN,
59 cleanup => {type => BOOLEAN,
64 $param{cleanup} = $param{debug}?0:1 if not exists $param{cleanup};
65 my $sendmail_dir = tempdir(CLEANUP => $param{cleanup});
66 my $spool_dir = tempdir(CLEANUP => $param{cleanup});
67 my $config_dir = tempdir(CLEANUP => $param{cleanup});
70 $ENV{DEBBUGS_CONFIG_FILE} ="$config_dir/debbugs_config";
71 $ENV{PERL5LIB} = getcwd();
72 $ENV{SENDMAIL_TESTDIR} = $sendmail_dir;
73 my $sendmail_tester = getcwd().'/t/sendmail_tester';
74 unless (-x $sendmail_tester) {
75 die q(t/sendmail_tester doesn't exist or isn't executable. You may be in the wrong directory.);
77 my %files_to_create = ("$config_dir/debbugs_config" => <<END,
78 \$gSendmail='$sendmail_tester';
79 \$gSpoolDir='$spool_dir';
80 \$gLibPath='@{[getcwd()]}/scripts';
83 "$spool_dir/nextnumber" => qq(1\n),
84 "$config_dir/Maintainers" => qq(foo Blah Bleargh <bar\@baz.com>\n),
85 "$config_dir/Maintainers.override" => qq(),
86 "$config_dir/indices/sources" => <<END,
89 "$config_dir/pseudo-packages.description" => '',
91 while (my ($file,$contents) = each %files_to_create) {
92 system('mkdir','-p',dirname($file));
93 my $fh = IO::File->new($file,'w') or
94 die "Unable to create $file: $!";
95 print {$fh} $contents or die "Unable to write $contents to $file: $!";
96 close $fh or die "Unable to close $file: $!";
99 system('touch',"$spool_dir/index.db.realtime");
100 system('ln','-s','index.db.realtime',
101 "$spool_dir/index.db");
102 system('touch',"$spool_dir/index.archive.realtime");
103 system('ln','-s','index.archive.realtime',
104 "$spool_dir/index.archive");
106 # create the spool files and sub directories
107 map {system('mkdir','-p',"$spool_dir/$_"); }
108 map {('db-h/'.$_,'archive/'.$_)}
109 map { sprintf "%02d",$_ % 100} 0..99;
110 system('mkdir','-p',"$spool_dir/incoming");
111 system('mkdir','-p',"$spool_dir/lock");
113 return (spool_dir => $spool_dir,
114 sendmail_dir => $sendmail_dir,
115 config_dir => $config_dir,
122 my @content = grep {!/^\.\.?$/} readdir(DIR);
124 return scalar @content;
128 # We're going to use create mime message to create these messages, and
129 # then just send them to receive.
130 # First, check that submit@ works
133 my %param = validate_with(params => \@_,
134 spec => {to => {type => SCALAR,
135 default => 'submit@bugs.something',
137 headers => {type => ARRAYREF,
139 body => {type => SCALAR,
141 run_processall =>{type => BOOLEAN,
146 $ENV{LOCAL_PART} = $param{to};
149 my $pid = open3($wfd,$rfd,$rfd,'scripts/receive.in')
150 or die "Unable to start receive.in: $!";
151 print {$wfd} create_mime_message($param{headers},
152 $param{body}) or die "Unable to to print to receive.in";
153 close($wfd) or die "Unable to close receive.in";
157 my $rfh = IO::Handle->new_from_fd($rfd,'r') or die "Unable to create filehandle: $!";
160 while ($rv = $rfh->sysread($output,1000,length($output))) {}
161 if (not defined $rv) {
162 print STDERR "Reading from STDOUT/STDERR would have blocked.";
164 print STDERR $output;
165 die "receive.in failed with exit status $err";
167 # now we should run processall to see if the message gets processed
168 if ($param{run_processall}) {
169 system('scripts/processall.in') == 0 or die "processall.in failed";
174 package DebbugsTest::HTTPServer;
175 use base qw(HTTP::Server::Simple::CGI);
177 our $child_pid = undef;
178 our $webserver = undef;
179 our $server_handler = undef;
182 if (defined $child_pid) {
189 sub fork_and_create_webserver {
190 my ($handler,$port) = @_;
192 if (defined $child_pid) {
193 die "We appear to have already forked once";
195 $server_handler = $handler;
197 return 0 if not defined $pid;
200 # Wait here for a second to let the child start up
205 $webserver = DebbugsTest::HTTPServer->new($port);
212 if (defined $server_handler) {
213 $server_handler->(@_);
216 warn "No handler defined\n";
217 print "No handler defined\n";