]> Sergey Matveev's repositories - public-inbox.git/blob - t/watch_maildir.t
rename WatchMaildir => Watch
[public-inbox.git] / t / watch_maildir.t
1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use Test::More;
5 use PublicInbox::Eml;
6 use Cwd;
7 use PublicInbox::Config;
8 use PublicInbox::TestCommon;
9 use PublicInbox::Import;
10 my ($tmpdir, $for_destroy) = tmpdir();
11 my $git_dir = "$tmpdir/test.git";
12 my $maildir = "$tmpdir/md";
13 my $spamdir = "$tmpdir/spam";
14 use_ok 'PublicInbox::Watch';
15 use_ok 'PublicInbox::Emergency';
16 my $cfgpfx = "publicinbox.test";
17 my $addr = 'test-public@example.com';
18 PublicInbox::Import::init_bare($git_dir);
19
20 my $msg = <<EOF;
21 From: user\@example.com
22 To: $addr
23 Subject: spam
24 Message-Id: <a\@b.com>
25 Date: Sat, 18 Jun 2016 00:00:00 +0000
26
27 something
28 EOF
29 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
30 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
31         'create FIFO to ensure we do not get stuck on it :P');
32 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
33
34 {
35         my @w;
36         local $SIG{__WARN__} = sub { push @w, @_ };
37         my $config = PublicInbox::Config->new(\<<EOF);
38 $cfgpfx.address=$addr
39 $cfgpfx.inboxdir=$git_dir
40 $cfgpfx.watch=maildir:$spamdir
41 publicinboxlearn.watchspam=maildir:$spamdir
42 EOF
43         my $wm = PublicInbox::Watch->new($config);
44         is(scalar grep(/is a spam folder/, @w), 1, 'got warning about spam');
45         is_deeply($wm->{mdmap}, { "$spamdir/cur" => 'watchspam' },
46                 'only got the spam folder to watch');
47 }
48
49 my $cfg_path = "$tmpdir/config";
50 {
51         open my $fh, '>', $cfg_path or BAIL_OUT $!;
52         print $fh <<EOF or BAIL_OUT $!;
53 [publicinbox "test"]
54         address = $addr
55         inboxdir = $git_dir
56         watch = maildir:$maildir
57         filter = PublicInbox::Filter::Vger
58 [publicinboxlearn]
59         watchspam = maildir:$spamdir
60 EOF
61         close $fh or BAIL_OUT $!;
62 }
63
64 my $config = PublicInbox::Config->new($cfg_path);
65 PublicInbox::Watch->new($config)->scan('full');
66 my $git = PublicInbox::Git->new($git_dir);
67 my @list = $git->qx(qw(rev-list refs/heads/master));
68 is(scalar @list, 1, 'one revision in rev-list');
69
70 my $write_spam = sub {
71         is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
72         $sem->prepare(\$msg);
73         $sem->commit;
74         my @new = glob("$spamdir/new/*");
75         is(scalar @new, 1);
76         my @p = split(m!/+!, $new[0]);
77         ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
78         is(unlink($new[0]), 1);
79 };
80 $write_spam->();
81 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
82 PublicInbox::Watch->new($config)->scan('full');
83 @list = $git->qx(qw(rev-list refs/heads/master));
84 is(scalar @list, 2, 'two revisions in rev-list');
85 @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
86 is(scalar @list, 0, 'tree is empty');
87 is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
88
89 # check with scrubbing
90 {
91         $msg .= qq(--
92 To unsubscribe from this list: send the line "unsubscribe git" in
93 the body of a message to majordomo\@vger.kernel.org
94 More majordomo info at  http://vger.kernel.org/majordomo-info.html\n);
95         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
96         PublicInbox::Watch->new($config)->scan('full');
97         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
98         is(scalar @list, 1, 'tree has one file');
99         my $mref = $git->cat_file('HEAD:'.$list[0]);
100         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
101
102         is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
103         $write_spam->();
104         PublicInbox::Watch->new($config)->scan('full');
105         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
106         is(scalar @list, 0, 'tree is empty');
107         @list = $git->qx(qw(rev-list refs/heads/master));
108         is(scalar @list, 4, 'four revisions in rev-list');
109         is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
110 }
111
112 {
113         my $fail_bin = getcwd()."/t/fail-bin";
114         ok(-x "$fail_bin/spamc", "mock spamc exists");
115         my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
116         local $ENV{PATH} = $fail_path;
117         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
118         $config->{'publicinboxwatch.spamcheck'} = 'spamc';
119         {
120                 local $SIG{__WARN__} = sub {}; # quiet spam check warning
121                 PublicInbox::Watch->new($config)->scan('full');
122         }
123         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
124         is(scalar @list, 0, 'tree has no files spamc checked');
125         is(unlink(glob("$maildir/new/*")), 1);
126 }
127
128 {
129         my $main_bin = getcwd()."/t/main-bin";
130         ok(-x "$main_bin/spamc", "mock spamc exists");
131         my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
132         local $ENV{PATH} = $main_path;
133         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
134         $config->{'publicinboxwatch.spamcheck'} = 'spamc';
135         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
136         PublicInbox::Watch->new($config)->scan('full');
137         @list = $git->qx(qw(ls-tree -r --name-only refs/heads/master));
138         is(scalar @list, 1, 'tree has one file after spamc checked');
139
140         # XXX: workaround some weird caching/memoization in cat-file,
141         # shouldn't be an issue in real-world use, though...
142         $git = PublicInbox::Git->new($git_dir);
143
144         my $mref = $git->cat_file('refs/heads/master:'.$list[0]);
145         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
146 }
147
148 # end-to-end test which actually uses inotify/kevent
149 {
150         my $env = { PI_CONFIG => $cfg_path };
151         $git->cleanup;
152
153         # n.b. --no-scan is only intended for testing atm
154         my $wm = start_script([qw(-watch --no-scan)], $env);
155         my $eml = eml_load('t/data/0001.patch');
156         $eml->header_set('Cc', $addr);
157         my $em = PublicInbox::Emergency->new($maildir);
158         $em->prepare(\($eml->as_string));
159
160         use_ok 'PublicInbox::InboxIdle';
161         use_ok 'PublicInbox::DS';
162         my $delivered = 0;
163         my $cb = sub {
164                 my ($ibx) = @_;
165                 diag "message delivered to `$ibx->{name}'";
166                 $delivered++;
167         };
168         PublicInbox::DS->Reset;
169         my $ii = PublicInbox::InboxIdle->new($config);
170         my $obj = bless \$cb, 'PublicInbox::TestCommon::InboxWakeup';
171         $config->each_inbox(sub { $_[0]->subscribe_unlock('ident', $obj) });
172         PublicInbox::DS->SetPostLoopCallback(sub { $delivered == 0 });
173
174         # wait for -watch to setup inotify watches
175         my $sleep = 1;
176         if (eval { require Linux::Inotify2 } && -d "/proc/$wm->{pid}/fd") {
177                 my $end = time + 2;
178                 my (@ino, @ino_info);
179                 do {
180                         @ino = grep {
181                                 (readlink($_)//'') =~ /\binotify\b/
182                         } glob("/proc/$wm->{pid}/fd/*");
183                 } until (@ino || time > $end || !tick);
184                 if (scalar(@ino) == 1) {
185                         my $ino_fd = (split('/', $ino[0]))[-1];
186                         my $ino_fdinfo = "/proc/$wm->{pid}/fdinfo/$ino_fd";
187                         while (time < $end && open(my $fh, '<', $ino_fdinfo)) {
188                                 @ino_info = grep(/^inotify wd:/, <$fh>);
189                                 last if @ino_info >= 3;
190                                 tick;
191                         }
192                         $sleep = undef if @ino_info >= 3;
193                 }
194         }
195         if ($sleep) {
196                 diag "waiting ${sleep}s for -watch to start up";
197                 sleep $sleep;
198         }
199
200         $em->commit; # wake -watch up
201         diag 'waiting for -watch to import new message';
202         PublicInbox::DS->EventLoop;
203         $wm->kill;
204         $wm->join;
205         $ii->close;
206         PublicInbox::DS->Reset;
207         my $head = $git->qx(qw(cat-file commit HEAD));
208         my $subj = $eml->header('Subject');
209         like($head, qr/^\Q$subj\E/sm, 'new commit made');
210 }
211
212 sub is_maildir {
213         my ($dir) = @_;
214         PublicInbox::Watch::is_maildir($dir);
215 }
216
217 is(is_maildir('maildir:/hello//world'), '/hello/world', 'extra slash gone');
218 is(is_maildir('maildir:/hello/world/'), '/hello/world', 'trailing slash gone');
219 is(is_maildir('faildir:/hello/world/'), undef, 'non-maildir rejected');
220
221 done_testing;