]> git.donarmstrong.com Git - debbugs.git/blob - t/06_mail_handling.t
* More changes to sendmail tester and the mail handling test script
[debbugs.git] / t / 06_mail_handling.t
1 # -*- mode: cperl;-*-
2 # $Id: 05_mail.t,v 1.1 2005/08/17 21:46:17 don Exp $
3
4 use Test::More tests => 20;
5
6 use warnings;
7 use strict;
8
9 # Here, we're going to shoot messages through a set of things that can
10 # happen.
11
12 # First, we're going to send mesages to receive.
13 # To do so, we'll first send a message to submit,
14 # then send messages to the newly created bugnumber.
15
16 use IO::File;
17 use File::Temp qw(tempdir);
18 use Cwd qw(getcwd);
19 use Debbugs::MIME qw(create_mime_message);
20 use File::Basename qw(dirname basename);
21
22
23 my $sendmail_dir = tempdir(CLEANUP => $ENV{DEBUG}?0:1);
24 my $spool_dir = tempdir(CLEANUP => $ENV{DEBUG}?0:1);
25 my $config_dir = tempdir(CLEANUP => $ENV{DEBUG}?0:1);
26
27 END{
28      if ($ENV{DEBUG}) {
29           print STDERR "\nspool_dir:   $spool_dir\n";
30           print STDERR "config_dir:   $config_dir\n";
31           print STDERR "sendmail_dir: $sendmail_dir\n";
32      }
33 }
34
35 $ENV{DEBBUGS_CONFIG_FILE}  ="$config_dir/debbugs_config";
36 $ENV{PERL5LIB} = getcwd();
37 $ENV{SENDMAIL_TESTDIR} = $sendmail_dir;
38 my $sendmail_tester = getcwd().'/t/sendmail_tester';
39
40
41 unless (-x $sendmail_tester) {
42      BAIL_OUT(q(t/sendmail_tester doesn't exist or isn't executable. You may be in the wrong directory.));
43 }
44
45 my %files_to_create = ("$config_dir/debbugs_config" => <<END,
46 \$gSendmail='$sendmail_tester';
47 \$gSpoolDir='$spool_dir';
48 \$gLibPath='@{[getcwd()]}/scripts';
49 1;
50 END
51                        "$spool_dir/nextnumber" => qq(1\n),
52                        "$config_dir/Maintainers" => qq(foo Blah Bleargh <bar\@baz.com>\n),
53                        "$config_dir/Maintainers.override" => qq(),
54                        "$config_dir/indices/sources" => <<END,
55 foo main foo
56 END
57                        );
58 while (my ($file,$contents) = each %files_to_create) {
59      system('mkdir','-p',dirname($file));
60      my $fh = IO::File->new($file,'w') or
61           BAIL_OUT("Unable to create $file: $!");
62      print {$fh} $contents;
63      close $fh;
64 }
65
66 system('touch',"$spool_dir/index.db.realtime");
67 system('ln','-s','index.db.realtime',
68        "$spool_dir/index.db");
69 system('touch',"$spool_dir/index.archive.realtime");
70 system('ln','-s','index.archive.realtime',
71        "$spool_dir/index.archive");
72
73
74
75 # create the spool files and sub directories
76 map {system('mkdir','-p',"$spool_dir/$_"); }
77      map {('db-h/'.$_,'archive/'.$_)}
78      map { sprintf "%02d",$_ % 100} 0..99;
79 system('mkdir','-p',"$spool_dir/incoming");
80 system('mkdir','-p',"$spool_dir/lock");
81
82
83 # We're going to use create mime message to create these messages, and
84 # then just send them to receive.
85
86 # First, check that submit@ works
87
88 $ENV{LOCAL_PART} = 'submit@bugs.something';
89 my $receive = new IO::File ('|scripts/receive.in') or BAIL_OUT("Unable to start receive.in: $!");
90
91 print {$receive} create_mime_message([To   => 'submit@bugs.something',
92                                       From => 'foo@bugs.something',
93                                       Subject => 'Submiting a bug',
94                                      ],
95                                      <<EOF);
96 Package: foo
97 Severity: normal
98
99 This is a silly bug
100 EOF
101
102 close($receive);
103 ok($?==0,'receive took the mail');
104 # now we should run processall to see if the message gets processed
105 ok(system('scripts/processall.in') == 0,'processall ran');
106
107 # now we check to see that we have a bug, and nextnumber has been incremented
108 ok(-e "$spool_dir/db-h/01/1.log",'log file created');
109 ok(-e "$spool_dir/db-h/01/1.summary",'sumary file created');
110 ok(-e "$spool_dir/db-h/01/1.status",'status file created');
111 ok(-e "$spool_dir/db-h/01/1.report",'report file created');
112
113 # next, we check to see that (at least) the proper messages have been
114 # sent out. 1) ack to submitter 2) mail to maintainer
115
116 sub dirsize{
117      my ($dir) = @_;
118      opendir(DIR,$dir);
119      my @content = grep {!/^\.\.?$/} readdir(DIR);
120      closedir(DIR);
121      return scalar @content;
122 }
123
124 # This keeps track of the previous size of the sendmail directory
125 my $SD_SIZE_PREV = 0;
126 my $SD_SIZE_NOW = dirsize($sendmail_dir);
127 ok($SD_SIZE_NOW-$SD_SIZE_PREV >= 2,'submit messages appear to have been sent out properly');
128 $SD_SIZE_PREV=$SD_SIZE_NOW;
129
130 # now send a message to the bug
131
132 $ENV{LOCAL_PART} = '1@bugs.something';
133 $receive = new IO::File ('|scripts/receive.in') or
134      BAIL_OUT("Unable to start receive.in: $!");
135
136 print {$receive} create_mime_message([To   => '1@bugs.something',
137                                       From => 'foo@bugs.something',
138                                       Subject => 'Sending a message to a bug',
139                                      ],
140                                      <<EOF);
141 Package: foo
142 Severity: normal
143
144 This is a silly bug
145 EOF
146
147 close($receive);
148 ok($?==0,'receive took the mail');
149 # now we should run processall to see if the message gets processed
150 ok(system('scripts/processall.in') == 0,'processall ran');
151 $SD_SIZE_NOW = dirsize($sendmail_dir);
152 ok($SD_SIZE_NOW-$SD_SIZE_PREV >= 2,'1@bugs.something messages appear to have been sent out properly');
153 $SD_SIZE_PREV=$SD_SIZE_NOW;
154
155 # just check to see that control doesn't explode
156 $ENV{LOCAL_PART} = 'control@bugs.something';
157 $receive = new IO::File ('|scripts/receive.in') or
158      BAIL_OUT("Unable to start receive.in: $!");
159
160 print {$receive} create_mime_message([To   => 'control@bugs.something',
161                                       From => 'foo@bugs.something',
162                                       Subject => 'Munging a bug',
163                                      ],
164                                      <<EOF);
165 severity 1 wishlist
166 retitle 1 new title
167 thanks
168 EOF
169
170 close($receive);
171 ok($?==0,'receive took the mail');
172 # now we should run processall to see if the message gets processed
173 ok(system('scripts/processall.in') == 0,'processall ran');
174 $SD_SIZE_NOW = dirsize($sendmail_dir);
175 ok($SD_SIZE_NOW-$SD_SIZE_PREV >= 1,'control@bugs.something messages appear to have been sent out properly');
176 $SD_SIZE_PREV=$SD_SIZE_NOW;
177 # now we need to check to make sure the control message was processed without errors
178 ok(system("sh -c 'find ".$sendmail_dir.q( -type f | xargs grep -q "Subject: Processed: Munging a bug"')) == 0,
179    'control@bugs.something message was parsed without errors');
180 # now we need to check to make sure that the control message actually did anything
181 # This is an eval because $ENV{DEBBUGS_CONFIG_FILE} isn't set at BEGIN{} time
182 eval "use Debbugs::Status qw(read_bug);";
183 my $status = read_bug(bug=>1);
184 ok($status->{subject} eq 'new title','bug 1 retitled');
185 ok($status->{severity} eq 'wishlist','bug 1 wishlisted');
186
187
188