lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / lei_to_mail.t
blobdbd33909bd333694b8c57645ecad68909678dc07
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 # tests PublicInbox::LeiToMail internals (unstable API)
5 # Not as needed now that lei functionality has been ironed out
6 use v5.12;
7 use autodie qw(open sysopen unlink);
8 use PublicInbox::TestCommon;
9 use PublicInbox::Eml;
10 use Fcntl qw(SEEK_SET O_RDONLY O_NONBLOCK);
11 use PublicInbox::Spawn qw(popen_rd);
12 use List::Util qw(shuffle);
13 require_mods(qw(DBD::SQLite));
14 require PublicInbox::MdirReader;
15 require PublicInbox::MboxReader;
16 require PublicInbox::LeiOverview;
17 require PublicInbox::LEI;
18 use_ok 'PublicInbox::LeiToMail';
19 my $from = "Content-Length: 10\nSubject: x\n\nFrom hell\n";
20 my $noeol = "Subject: x\n\nFrom hell";
21 my $crlf = $noeol;
22 $crlf =~ s/\n/\r\n/g;
23 my $kw = [qw(seen answered flagged)];
24 my $smsg = { kw => $kw, blob => '0'x40 };
25 my @MBOX = qw(mboxcl2 mboxrd mboxcl mboxo);
26 for my $mbox (@MBOX) {
27         my $m = "eml2$mbox";
28         my $cb = PublicInbox::LeiToMail->can($m);
29         my $s = $cb->(PublicInbox::Eml->new($from), $smsg);
30         is(substr($$s, -1, 1), "\n", "trailing LF in normal $mbox");
31         my $eml = PublicInbox::Eml->new($s);
32         is($eml->header('Status'), 'RO', "Status: set by $m");
33         is($eml->header('X-Status'), 'AF', "X-Status: set by $m");
34         if ($mbox eq 'mboxcl2') {
35                 like($eml->body_raw, qr/^From /, "From not escaped $m");
36         } else {
37                 like($eml->body_raw, qr/^>From /, "From escaped once by $m");
38         }
39         my @cl = $eml->header('Content-Length');
40         if ($mbox =~ /mboxcl/) {
41                 is(scalar(@cl), 1, "$m only has one Content-Length header");
42                 is($cl[0] + length("\n"),
43                         length($eml->body_raw), "$m Content-Length matches");
44         } else {
45                 is(scalar(@cl), 0, "$m clobbered Content-Length");
46         }
47         $s = $cb->(PublicInbox::Eml->new($noeol), $smsg);
48         is(substr($$s, -1, 1), "\n",
49                 "trailing LF added by $m when original lacks EOL");
50         $eml = PublicInbox::Eml->new($s);
51         if ($mbox eq 'mboxcl2') {
52                 is($eml->body_raw, "From hell\n", "From not escaped by $m");
53         } else {
54                 is($eml->body_raw, ">From hell\n", "From escaped once by $m");
55         }
56         $s = $cb->(PublicInbox::Eml->new($crlf), $smsg);
57         is(substr($$s, -2, 2), "\r\n",
58                 "trailing CRLF added $m by original lacks EOL");
59         $eml = PublicInbox::Eml->new($s);
60         if ($mbox eq 'mboxcl2') {
61                 is($eml->body_raw, "From hell\r\n", "From not escaped by $m");
62         } else {
63                 is($eml->body_raw, ">From hell\r\n", "From escaped once by $m");
64         }
65         if ($mbox =~ /mboxcl/) {
66                 is($eml->header('Content-Length') + length("\r\n"),
67                         length($eml->body_raw), "$m Content-Length matches");
68         } elsif ($mbox eq 'mboxrd') {
69                 $s = $cb->($eml, $smsg);
70                 $eml = PublicInbox::Eml->new($s);
71                 is($eml->body_raw,
72                         ">>From hell\r\n\r\n", "From escaped again by $m");
73         }
76 my ($tmpdir, $for_destroy) = tmpdir();
77 local $ENV{TMPDIR} = $tmpdir;
78 open my $err, '>>', "$tmpdir/lei.err";
79 my $lei = bless { 2 => $err, cmd => 'test' }, 'PublicInbox::LEI';
80 my $commit = sub {
81         $_[0] = undef; # wcb
82         delete $lei->{1};
84 my $buf = <<'EOM';
85 From: x@example.com
86 Subject: x
88 blah
89 EOM
90 my $fn = "$tmpdir/x.mbox";
91 my ($mbox) = shuffle(@MBOX); # pick one, shouldn't matter
92 my $wcb_get = sub {
93         my ($fmt, $dst) = @_;
94         delete $lei->{dedupe}; # to be recreated
95         $lei->{ovv} = bless {
96                 fmt => $fmt,
97                 dst => $dst
98         }, 'PublicInbox::LeiOverview';
99         my $l2m = PublicInbox::LeiToMail->new($lei);
100         SKIP: {
101                 require_mods('Storable', 1);
102                 my $dup = Storable::thaw(Storable::freeze($l2m));
103                 is_deeply($dup, $l2m, "$fmt round-trips through storable");
104         }
105         $l2m->pre_augment($lei);
106         $l2m->do_augment($lei);
107         $l2m->post_augment($lei);
108         $l2m->write_cb($lei);
111 my $deadbeef = { blob => 'deadbeef', kw => [ qw(seen) ] };
112 my $orig = do {
113         my $wcb = $wcb_get->($mbox, $fn);
114         is(ref $wcb, 'CODE', 'write_cb returned callback');
115         ok(-f $fn && !-s _, 'empty file created');
116         $wcb->(\(my $dup = $buf), $deadbeef);
117         $commit->($wcb);
118         open my $fh, '<', $fn;
119         my $raw = do { local $/; <$fh> };
120         like($raw, qr/^blah\n/sm, 'wrote content');
121         unlink $fn;
123         $wcb = $wcb_get->($mbox, $fn);
124         ok(-f $fn && !-s _, 'truncated mbox destination');
125         $wcb->(\($dup = $buf), $deadbeef);
126         $commit->($wcb);
127         open $fh, '<', $fn;
128         is(do { local $/; <$fh> }, $raw, 'wrote identical content');
129         $raw;
132 test_lei({tmpdir => "$tmpdir/using -F"}, sub {
133         lei_ok(qw(import -F), $mbox, $fn, \'imported mbox');
134         lei_ok(qw(q s:x), \'lei q works') or diag $lei_err;
135         my $res = json_utf8->decode($lei_out);
136         my $x = $res->[0];
137         is($x->{'s'}, 'x', 'subject imported') or diag $lei_out;
138         is_deeply($x->{'kw'}, ['seen'], 'kw imported') or diag $lei_out;
139         is($res->[1], undef, 'only one result');
142 test_lei({tmpdir => "$tmpdir/using TYPE: prefix"}, sub {
143         lei_ok('import', "$mbox:$fn", \'imported mbox:/path') or diag $lei_err;
144         lei_ok(qw(q s:x), \'lei q works') or diag $lei_err;
145         my $res = json_utf8->decode($lei_out);
146         my $x = $res->[0];
147         is($x->{'s'}, 'x', 'subject imported') or diag $lei_out;
148         is_deeply($x->{'kw'}, ['seen'], 'kw imported') or diag $lei_out;
149         is($res->[1], undef, 'only one result');
152 my $zsfx2cmd = PublicInbox::MboxReader->can('zsfx2cmd');
153 for my $zsfx (qw(gz bz2 xz)) {
154         SKIP: {
155                 my $cmd = eval { $zsfx2cmd->($zsfx, 0, $lei) };
156                 skip $@, 3 if $@;
157                 my $dc_cmd = eval { $zsfx2cmd->($zsfx, 1, $lei) };
158                 ok($dc_cmd, "decompressor for .$zsfx");
159                 my $f = "$fn.$zsfx";
160                 my $wcb = $wcb_get->($mbox, $f);
161                 $wcb->(\(my $dup = $buf), { %$deadbeef });
162                 $commit->($wcb);
163                 my $uncompressed = xqx([@$dc_cmd, $f]);
164                 is($uncompressed, $orig, "$zsfx works unlocked");
166                 unlink $f;
167                 $wcb = $wcb_get->($mbox, $f);
168                 $wcb->(\($dup = $buf), { %$deadbeef });
169                 $commit->($wcb);
170                 is(xqx([@$dc_cmd, $f]), $orig, "$zsfx matches with lock");
172                 local $lei->{opt} = { augment => 1 };
173                 $wcb = $wcb_get->($mbox, $f);
174                 $wcb->(\($dup = $buf . "\nx\n"), { %$deadbeef });
175                 $commit->($wcb);
177                 my $cat = popen_rd([@$dc_cmd, $f]);
178                 my @raw;
179                 PublicInbox::MboxReader->$mbox($cat,
180                         sub { push @raw, shift->as_string });
181                 like($raw[1], qr/\nblah\n\nx\n\z/s, "augmented $zsfx");
182                 like($raw[0], qr/\nblah\n\z/s, "original preserved $zsfx");
184                 local $lei->{opt} = { augment => 1 };
185                 $wcb = $wcb_get->($mbox, $f);
186                 $wcb->(\($dup = $buf . "\ny\n"), { %$deadbeef });
187                 $commit->($wcb);
189                 my @raw3;
190                 $cat = popen_rd([@$dc_cmd, $f]);
191                 PublicInbox::MboxReader->$mbox($cat,
192                         sub { push @raw3, shift->as_string });
193                 my $y = pop @raw3;
194                 is_deeply(\@raw3, \@raw, 'previous messages preserved');
195                 like($y, qr/\nblah\n\ny\n\z/s, "augmented $zsfx (atomic)");
196         }
199 my $as_orig = sub {
200         my ($eml) = @_;
201         $eml->header_set('Status');
202         $eml->as_string;
205 unlink $fn;
206 if ('default deduplication uses content_hash') {
207         my $wcb = $wcb_get->('mboxo', $fn);
208         $deadbeef->{kw} = [];
209         $wcb->(\(my $x = $buf), $deadbeef) for (1..2);
210         $commit->($wcb);
211         my $cmp = '';
212         open my $fh, '<', $fn;
213         PublicInbox::MboxReader->mboxo($fh, sub { $cmp .= $as_orig->(@_) });
214         is($cmp, $buf, 'only one message written');
216         local $lei->{opt} = { augment => 1 };
217         $wcb = $wcb_get->('mboxo', $fn);
218         $wcb->(\($x = $buf . "\nx\n"), $deadbeef) for (1..2);
219         $commit->($wcb);
220         open $fh, '<', $fn;
221         my @x;
222         PublicInbox::MboxReader->mboxo($fh, sub { push @x, $as_orig->(@_) });
223         is(scalar(@x), 2, 'augmented mboxo');
224         is($x[0], $cmp, 'original message preserved');
225         is($x[1], $buf . "\nx\n", 'new message appended');
228 { # stdout support
229         open my $tmp, '+>', undef;
230         local $lei->{1} = $tmp;
231         my $wcb = $wcb_get->('mboxrd', '/dev/stdout');
232         $wcb->(\(my $x = $buf), $deadbeef);
233         $commit->($wcb);
234         seek($tmp, 0, SEEK_SET);
235         my $cmp = '';
236         PublicInbox::MboxReader->mboxrd($tmp, sub { $cmp .= $as_orig->(@_) });
237         is($cmp, $buf, 'message written to stdout');
240 SKIP: { # FIFO support
241         use POSIX qw(mkfifo);
242         my $fn = "$tmpdir/fifo";
243         mkfifo($fn, 0600) or skip("mkfifo not supported: $!", 1);
244         sysopen(my $cat, $fn, O_RDONLY|O_NONBLOCK);
245         my $wcb = $wcb_get->('mboxo', $fn);
246         $wcb->(\(my $x = $buf), $deadbeef);
247         $commit->($wcb);
248         my $cmp = '';
249         $cat->blocking(1);
250         PublicInbox::MboxReader->mboxo($cat, sub { $cmp .= $as_orig->(@_) });
251         is($cmp, $buf, 'message written to FIFO');
254 { # Maildir support
255         my $mdr = PublicInbox::MdirReader->new;
256         my $md = "$tmpdir/maildir/";
257         my $wcb = $wcb_get->('maildir', $md);
258         is(ref($wcb), 'CODE', 'got Maildir callback');
259         my $b4dc0ffee = { blob => 'badc0ffee', kw => [] };
260         $wcb->(\(my $x = $buf), $b4dc0ffee);
262         my @f;
263         $mdr->maildir_each_file($md, sub { push @f, shift });
264         open my $fh, '<', $f[0];
265         is(do { local $/; <$fh> }, $buf, 'wrote to Maildir');
267         $wcb = $wcb_get->('maildir', $md);
268         my $deadcafe = { blob => 'deadcafe', kw => [] };
269         $wcb->(\($x = $buf."\nx\n"), $deadcafe);
271         my @x = ();
272         $mdr->maildir_each_file($md, sub { push @x, shift });
273         is(scalar(@x), 1, 'wrote one new file');
274         ok(!-f $f[0], 'old file clobbered');
275         open $fh, '<', $x[0];
276         is(do { local $/; <$fh> }, $buf."\nx\n", 'wrote new file to Maildir');
278         local $lei->{opt}->{augment} = 1;
279         $wcb = $wcb_get->('maildir', $md);
280         $wcb->(\($x = $buf."\ny\n"), $deadcafe);
281         $wcb->(\($x = $buf."\ny\n"), $b4dc0ffee); # skipped by dedupe
282         @f = ();
283         $mdr->maildir_each_file($md, sub { push @f, shift });
284         is(scalar grep(/\A\Q$x[0]\E\z/, @f), 1, 'old file still there');
285         my @new = grep(!/\A\Q$x[0]\E\z/, @f);
286         is(scalar @new, 1, '1 new file written (b4dc0ffee skipped)');
287         open $fh, '<', $x[0];
288         is(do { local $/; <$fh> }, $buf."\nx\n", 'old file untouched');
289         open $fh, '<', $new[0];
290         is(do { local $/; <$fh> }, $buf."\ny\n", 'new file written');
293 done_testing;