]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-refresh-mail-sync.t
90356b57b3e7ae5ed66435b4c3cfe8ff41bef89b
[public-inbox.git] / t / lei-refresh-mail-sync.t
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 strict; use v5.10.1; use PublicInbox::TestCommon;
5 require_mods(qw(lei));
6 use File::Path qw(remove_tree);
7
8 my $stop_daemon = sub { # needed since we don't have inotify
9         lei_ok qw(daemon-pid);
10         chomp(my $pid = $lei_out);
11         $pid > 0 or xbail "bad pid: $pid";
12         kill('TERM', $pid) or xbail "kill: $!";
13         for (0..10) {
14                 tick;
15                 kill(0, $pid) or last;
16         }
17         kill(0, $pid) and xbail "daemon still running (PID:$pid)";
18 };
19
20 test_lei({ daemon_only => 1 }, sub {
21         my $d = "$ENV{HOME}/d";
22         my ($ro_home, $cfg_path) = setup_public_inboxes;
23         lei_ok qw(daemon-pid);
24         lei_ok qw(add-external), "$ro_home/t2";
25         lei_ok qw(q mid:testmessage@example.com -o), "Maildir:$d";
26         my (@o) = glob("$d/*/*");
27         scalar(@o) == 1 or xbail('multiple results', \@o);
28         my ($bn0) = ($o[0] =~ m!/([^/]+)\z!);
29
30         my $oid = '9bf1002c49eb075df47247b74d69bcd555e23422';
31         lei_ok 'inspect', "blob:$oid";
32         my $before = json_utf8->decode($lei_out);
33         my $exp0 = { 'mail-sync' => { "maildir:$d" => [ $bn0 ] } };
34         is_deeply($before, $exp0, 'inspect shows expected');
35
36         $stop_daemon->();
37         my $dst = $o[0];
38         $dst =~ s/:2,.*\z// and $dst =~ s!/cur/!/new/! and
39                 rename($o[0], $dst) or xbail "rename($o[0] => $dst): $!";
40
41         lei_ok 'inspect', "blob:$oid";
42         is_deeply(json_utf8->decode($lei_out),
43                 $before, 'inspect unchanged immediately after restart');
44         lei_ok 'refresh-mail-sync', '--all';
45         lei_ok 'inspect', "blob:$oid";
46         my ($bn1) = ($dst =~ m!/([^/]+)\z!);
47         my $exp1 = { 'mail-sync' => { "maildir:$d" => [ $bn1 ] } };
48         is_deeply(json_utf8->decode($lei_out), $exp1,
49                 'refresh-mail-sync updated location');
50
51         $stop_daemon->();
52         rename($dst, "$d/unwatched") or xbail "rename $dst out-of-the-way $!";
53
54         lei_ok 'refresh-mail-sync', $d;
55         lei_ok 'inspect', "blob:$oid";
56         is($lei_out, '{}', 'no known locations after "removal"');
57         lei_ok 'refresh-mail-sync', "Maildir:$d";
58
59         $stop_daemon->();
60         rename("$d/unwatched", $dst) or xbail "rename $dst back";
61
62         lei_ok 'refresh-mail-sync', "Maildir:$d";
63         lei_ok 'inspect', "blob:$oid";
64         is_deeply(json_utf8->decode($lei_out), $exp1,
65                 'replaced file noted again');
66
67         $stop_daemon->();
68
69         remove_tree($d);
70         lei_ok 'refresh-mail-sync', '--all';
71         lei_ok 'inspect', "blob:$oid";
72         is($lei_out, '{}', 'no known locations after "removal"');
73         lei_ok 'ls-mail-sync';
74         is($lei_out, '', 'no sync left when folder is gone');
75
76 SKIP: {
77         require_mods(qw(-imapd -nntpd Mail::IMAPClient Net::NNTP), 1);
78         require File::Copy; # stdlib
79         my $home = $ENV{HOME};
80         my $srv;
81         my $cfg_path2 = "$home/cfg2";
82         File::Copy::cp($cfg_path, $cfg_path2);
83         my $env = { PI_CONFIG => $cfg_path2 };
84         for my $x (qw(imapd)) {
85                 my $s = tcp_server;
86                 my $cmd = [ "-$x", '-W0', "--stdout=$home/$x.out",
87                         "--stderr=$home/$x.err" ];
88                 my $td = start_script($cmd, $env, { 3 => $s}) or xbail("-$x");
89                 $srv->{$x} = {
90                         addr => (my $scalar = tcp_host_port($s)),
91                         td => $td,
92                         cmd => $cmd,
93                 };
94         }
95         my $url = "imap://$srv->{imapd}->{addr}/t.v1.0";
96         lei_ok 'import', $url, '+L:v1';
97         lei_ok 'inspect', "blob:$oid";
98         $before = json_utf8->decode($lei_out);
99         my @f = grep(m!\Aimap://;AUTH=ANONYMOUS\@\Q$srv->{imapd}->{addr}\E!,
100                 keys %{$before->{'mail-sync'}});
101         is(scalar(@f), 1, 'got IMAP folder') or xbail(\@f);
102         xsys([qw(git config), '-f', $cfg_path2,
103                 qw(--unset publicinbox.t1.newsgroup)]) and
104                 xbail "git config $?";
105         $stop_daemon->(); # drop IMAP IDLE
106         $srv->{imapd}->{td}->kill('HUP');
107         tick; # wait for HUP
108         lei_ok 'refresh-mail-sync', $url;
109         lei_ok 'inspect', "blob:$oid";
110         my $after = json_utf8->decode($lei_out);
111         ok(!$after->{'mail-sync'}, 'no sync info for non-existent mailbox');
112         lei_ok 'ls-mail-sync';
113         unlike $lei_out, qr!^\Q$f[0]\E!, 'IMAP folder gone from mail_sync';
114
115         # simulate server downtime
116         $url = "imap://$srv->{imapd}->{addr}/t.v2.0";
117         lei_ok 'import', $url, '+L:v2';
118
119         lei_ok 'inspect', "blob:$oid";
120         $before = $lei_out;
121         delete $srv->{imapd}->{td}; # kill + join daemon
122
123         ok(!(lei 'refresh-mail-sync', $url), 'URL fails on dead -imapd');
124         ok(!(lei 'refresh-mail-sync', '--all'), '--all fails on dead -imapd');
125
126         # restart server (somewhat dangerous since we released the socket)
127         my $cmd = $srv->{imapd}->{cmd};
128         push @$cmd, '-l', $srv->{imapd}->{addr};
129         $srv->{imapd}->{td} = start_script($cmd, $env) or xbail "@$cmd";
130
131         lei_ok 'refresh-mail-sync', '--all';
132         lei_ok 'inspect', "blob:$oid";
133         is($lei_out, $before, 'no changes when server was down');
134 }; # imapd+nntpd stuff
135 });
136
137 done_testing;