lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / watch_maildir.t
bloba12ceefdf553afeb4b4cf80ea19b8e8afc494317
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use v5.12;
5 use PublicInbox::Eml;
6 use Cwd;
7 use PublicInbox::TestCommon;
8 use PublicInbox::Import;
9 use PublicInbox::IO qw(write_file);
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 $addr = 'test-public@example.com';
17 my $default_branch = PublicInbox::Import::default_branch;
18 PublicInbox::Import::init_bare($git_dir);
19 my $msg = <<EOF;
20 From: user\@example.com
21 To: $addr
22 Subject: spam
23 Message-Id: <a\@b.com>
24 Date: Sat, 18 Jun 2016 00:00:00 +0000
26 something
27 EOF
29 my $ibx_ro = create_inbox 'ro', sub { $_[0]->add(PublicInbox::Eml->new($msg)) };
31 PublicInbox::Emergency->new($maildir)->prepare(\$msg);
32 ok(POSIX::mkfifo("$maildir/cur/fifo", 0777),
33         'create FIFO to ensure we do not get stuck on it :P');
34 my $sem = PublicInbox::Emergency->new($spamdir); # create dirs
37         my @w;
38         local $SIG{__WARN__} = sub { push @w, @_ };
39         my $cfg = cfg_new $tmpdir, <<EOF;
40 [publicinbox "test"]
41         address = $addr
42         inboxdir = $git_dir
43         watch = maildir:$spamdir
44 [publicinboxlearn]
45         watchspam = maildir:$spamdir
46 EOF
47         my $wm = PublicInbox::Watch->new($cfg);
48         is(scalar grep(/is a spam folder/, @w), 1, 'got warning about spam');
49         is_deeply($wm->{d_map}, { "$spamdir/cur" => 'watchspam' },
50                 'only got the spam folder to watch');
53 my $cfg = cfg_new $tmpdir, <<EOF;
54 [publicinbox "test"]
55         address = $addr
56         inboxdir = $git_dir
57         watch = maildir:$maildir
58         filter = PublicInbox::Filter::Vger
59 [publicinboxlearn]
60         watchspam = maildir:$spamdir
61 [publicinbox "test-ro"]
62         watch = false
63         inboxdir = $ibx_ro->{inboxdir}
64         address = ro-test\@example.com
65 EOF
66 my $cfg_path = $cfg->{-f};
67 PublicInbox::Watch->new($cfg)->scan('full');
68 my $git = PublicInbox::Git->new($git_dir);
69 my @list = $git->qx('rev-list', $default_branch);
70 is(scalar @list, 1, 'one revision in rev-list');
72 my $write_spam = sub {
73         is(scalar glob("$spamdir/new/*"), undef, 'no spam existing');
74         $sem->prepare(\$msg);
75         $sem->commit;
76         my @new = glob("$spamdir/new/*");
77         is(scalar @new, 1);
78         my @p = split(m!/+!, $new[0]);
79         ok(link($new[0], "$spamdir/cur/".$p[-1].":2,S"));
80         is(unlink($new[0]), 1);
82 $write_spam->();
83 is(unlink(glob("$maildir/new/*")), 1, 'unlinked old spam');
84 PublicInbox::Watch->new($cfg)->scan('full');
85 @list = $git->qx('rev-list', $default_branch);
86 is(scalar @list, 2, 'two revisions in rev-list');
87 @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
88 is(scalar @list, 0, 'tree is empty');
89 is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
91 @list = $ibx_ro->git->qx(qw(ls-tree -r --name-only), $default_branch);
92 undef $ibx_ro;
93 is scalar(@list), 1, 'read-only inbox is unchanged';
95 # check with scrubbing
97         $msg .= qq(--
98 To unsubscribe from this list: send the line "unsubscribe git" in
99 the body of a message to majordomo\@vger.kernel.org
100 More majordomo info at  http://vger.kernel.org/majordomo-info.html\n);
101         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
102         PublicInbox::Watch->new($cfg)->scan('full');
103         @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
104         is(scalar @list, 1, 'tree has one file');
105         chomp(@list);
106         my $mref = $git->cat_file('HEAD:'.$list[0]);
107         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
109         is(unlink(glob("$maildir/new/*")), 1, 'unlinked spam');
110         $write_spam->();
111         PublicInbox::Watch->new($cfg)->scan('full');
112         @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
113         is(scalar @list, 0, 'tree is empty');
114         @list = $git->qx('rev-list', $default_branch);
115         is(scalar @list, 4, 'four revisions in rev-list');
116         is(unlink(glob("$spamdir/cur/*")), 1, 'unlinked trained spam');
120         my $fail_bin = getcwd()."/t/fail-bin";
121         ok(-x "$fail_bin/spamc", "mock spamc exists");
122         my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc ham mock
123         local $ENV{PATH} = $fail_path;
124         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
125         $cfg->{'publicinboxwatch.spamcheck'} = 'spamc';
126         {
127                 local $SIG{__WARN__} = sub {}; # quiet spam check warning
128                 PublicInbox::Watch->new($cfg)->scan('full');
129         }
130         @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
131         is(scalar @list, 0, 'tree has no files spamc checked');
132         is(unlink(glob("$maildir/new/*")), 1);
136         my $main_bin = getcwd()."/t/main-bin";
137         ok(-x "$main_bin/spamc", "mock spamc exists");
138         my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
139         local $ENV{PATH} = $main_path;
140         PublicInbox::Emergency->new($maildir)->prepare(\$msg);
141         $cfg->{'publicinboxwatch.spamcheck'} = 'spamc';
142         @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
143         PublicInbox::Watch->new($cfg)->scan('full');
144         @list = $git->qx('ls-tree', '-r', '--name-only', $default_branch);
145         is(scalar @list, 1, 'tree has one file after spamc checked');
146         chomp(@list);
148         my $mref = $git->cat_file($default_branch.':'.$list[0]);
149         like($$mref, qr/something\n\z/s, 'message scrubbed on import');
152 # end-to-end test which actually uses inotify/kevent
154         my $env = { PI_CONFIG => $cfg_path };
155         $git->cleanup;
157         write_file '>>', $cfg_path, <<EOM;
158 [publicinboxImport]
159         dropUniqueUnsubscribe
161         # n.b. --no-scan is only intended for testing atm
162         my $wm = start_script([qw(-watch --no-scan)], $env);
163         no_pollerfd($wm->{pid});
164         my $eml = eml_load('t/data/0001.patch');
165         $eml->header_set('Cc', $addr);
166         my $em = PublicInbox::Emergency->new($maildir);
167         $em->prepare(\($eml->as_string));
169         use_ok 'PublicInbox::InboxIdle';
170         use_ok 'PublicInbox::DS';
171         my $delivered = 0;
172         my $cb = sub {
173                 my ($ibx) = @_;
174                 diag "message delivered to `$ibx->{name}'";
175                 $delivered++;
176         };
177         PublicInbox::DS->Reset;
178         my $ii = PublicInbox::InboxIdle->new($cfg);
179         my $obj = bless \$cb, 'PublicInbox::TestCommon::InboxWakeup';
180         $cfg->each_inbox(sub { $_[0]->subscribe_unlock('ident', $obj) });
181         local @PublicInbox::DS::post_loop_do = (sub { $delivered == 0 });
183         # wait for -watch to setup inotify watches
184         my $sleep = 1;
185         if (eval { require PublicInbox::Inotify } && -d "/proc/$wm->{pid}/fd") {
186                 my $end = time + 2;
187                 my (@ino, @ino_info);
188                 do {
189                         @ino = grep {
190                                 (readlink($_)//'') =~ /\binotify\b/
191                         } glob("/proc/$wm->{pid}/fd/*");
192                 } until (@ino || time > $end || !tick);
193                 if (scalar(@ino) == 1) {
194                         my $ino_fd = (split('/', $ino[0]))[-1];
195                         my $ino_fdinfo = "/proc/$wm->{pid}/fdinfo/$ino_fd";
196                         while (time < $end && open(my $fh, '<', $ino_fdinfo)) {
197                                 @ino_info = grep(/^inotify wd:/, <$fh>);
198                                 last if @ino_info >= 3;
199                                 tick;
200                         }
201                         $sleep = undef if @ino_info >= 3;
202                 }
203         }
204         if ($sleep) {
205                 diag "waiting ${sleep}s for -watch to start up";
206                 sleep $sleep;
207         }
209         $em->commit; # wake -watch up
210         diag 'waiting for -watch to import new message';
211         PublicInbox::DS::event_loop();
213         my $head = $git->qx(qw(cat-file commit HEAD));
214         my $subj = $eml->header('Subject');
215         like($head, qr/^\Q$subj\E/sm, 'new commit made');
217         # try dropUniqueUnsubscribe
218         $delivered = 0;
219         $eml->header_set('Message-ID', '<unsubscribe@example>');
220         $eml->header_set('List-Unsubscribe',
221                         '<https://example.com/some-UUID-here/test');
222         $eml->header_set('List-Unsubscribe-Post', 'List-Unsubscribe=One-Click');
223         $em = PublicInbox::Emergency->new($maildir);
224         $em->prepare(\($eml->as_string));
225         $em->commit; # wake -watch up
226         diag 'waiting for -watch to import dropUniqueUnsubscribe message';
227         PublicInbox::DS::event_loop();
228         my $cur = $git->qx(qw(diff HEAD~1..HEAD));
229         like $cur, qr/Message-ID: <unsubscribe\@example>/,
230                 'unsubscribe@example imported';
231         unlike $cur, qr/List-Unsubscribe\b/,
232                 'List-Unsubscribe-* headers gone w/ dropUniqueUnsubscribe';
234         $wm->kill;
235         $wm->join;
236         $ii->close;
237         PublicInbox::DS->Reset;
240 sub is_maildir {
241         my ($dir) = @_;
242         PublicInbox::Watch::is_maildir($dir);
245 is(is_maildir('maildir:/hello//world'), '/hello/world', 'extra slash gone');
246 is(is_maildir('maildir:/hello/world/'), '/hello/world', 'trailing slash gone');
247 is(is_maildir('faildir:/hello/world/'), undef, 'non-maildir rejected');
249 done_testing;