]> git.donarmstrong.com Git - debbugs.git/blob - t/lib/DebbugsTest.pm
Create an empty pseudo-packages.description, and sleep after creating the webserver
[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                             "$config_dir/pseudo-packages.description" => '',
89                            );
90      while (my ($file,$contents) = each %files_to_create) {
91           system('mkdir','-p',dirname($file));
92           my $fh = IO::File->new($file,'w') or
93                die "Unable to create $file: $!";
94           print {$fh} $contents or die "Unable to write $contents to $file: $!";
95           close $fh or die "Unable to close $file: $!";
96      }
97
98      system('touch',"$spool_dir/index.db.realtime");
99      system('ln','-s','index.db.realtime',
100             "$spool_dir/index.db");
101      system('touch',"$spool_dir/index.archive.realtime");
102      system('ln','-s','index.archive.realtime',
103             "$spool_dir/index.archive");
104
105      # create the spool files and sub directories
106      map {system('mkdir','-p',"$spool_dir/$_"); }
107           map {('db-h/'.$_,'archive/'.$_)}
108                map { sprintf "%02d",$_ % 100} 0..99;
109      system('mkdir','-p',"$spool_dir/incoming");
110      system('mkdir','-p',"$spool_dir/lock");
111
112      return (spool_dir => $spool_dir,
113              sendmail_dir => $sendmail_dir,
114              config_dir => $config_dir,
115             );
116 }
117
118 sub dirsize{
119      my ($dir) = @_;
120      opendir(DIR,$dir);
121      my @content = grep {!/^\.\.?$/} readdir(DIR);
122      closedir(DIR);
123      return scalar @content;
124 }
125
126
127 # We're going to use create mime message to create these messages, and
128 # then just send them to receive.
129 # First, check that submit@ works
130
131 sub send_message{
132      my %param = validate_with(params => \@_,
133                                spec   => {to => {type => SCALAR,
134                                                  default => 'submit@bugs.something',
135                                                 },
136                                           headers => {type => ARRAYREF,
137                                                      },
138                                           body    => {type => SCALAR,
139                                                      },
140                                           run_processall =>{type => BOOLEAN,
141                                                             default => 1,
142                                                            },
143                                          }
144                               );
145      $ENV{LOCAL_PART} = $param{to};
146      my $receive = new IO::File ('|scripts/receive.in') or die "Unable to start receive.in: $!";
147
148      print {$receive} create_mime_message($param{headers},
149                                           $param{body}) or die "Unable to to print to receive.in";
150      close($receive) or die "Unable to close receive.in";
151      $? == 0 or die "receive.in failed";
152      # now we should run processall to see if the message gets processed
153      if ($param{run_processall}) {
154           system('scripts/processall.in') == 0 or die "processall.in failed";
155      }
156 }
157
158 {
159      package DebbugsTest::HTTPServer;
160      use base qw(HTTP::Server::Simple::CGI);
161
162      our $child_pid = undef;
163      our $webserver = undef;
164      our $server_handler = undef;
165
166      END {
167           if (defined $child_pid) {
168                # stop the child
169                kill(15,$child_pid);
170                waitpid(-1,0);
171           }
172      }
173
174      sub fork_and_create_webserver {
175           my ($handler,$port) = @_;
176           $port ||= 8080;
177           if (defined $child_pid) {
178                die "We appear to have already forked once";
179           }
180           $server_handler = $handler;
181           my $pid = fork;
182           return 0 if not defined $pid;
183           if ($pid) {
184                $child_pid = $pid;
185                # Wait here for a second to let the child start up
186                sleep 1;
187                return $pid;
188           }
189           else {
190                $webserver = DebbugsTest::HTTPServer->new($port);
191                $webserver->run;
192           }
193
194      }
195
196      sub handle_request {
197           if (defined $server_handler) {
198                $server_handler->(@_);
199           }
200           else {
201                warn "No handler defined\n";
202                print "No handler defined\n";
203           }
204      }
205 }
206
207
208 1;
209
210 __END__
211
212
213