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;
6 use File::Path qw(remove_tree);
8 my $stop_daemon = sub { # needed since we don't have inotify
10 chomp(my $pid = $lei_out);
11 $pid > 0 or xbail "bad pid: $pid";
12 kill('TERM', $pid) or xbail "kill: $!";
15 kill(0, $pid) or last;
17 kill(0, $pid) and xbail "daemon still running (PID:$pid)";
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!);
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');
38 $dst =~ s/:2,.*\z// and $dst =~ s!/cur/!/new/! and
39 rename($o[0], $dst) or xbail "rename($o[0] => $dst): $!";
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');
52 rename($dst, "$d/unwatched") or xbail "rename $dst out-of-the-way $!";
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";
60 rename("$d/unwatched", $dst) or xbail "rename $dst back";
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');
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');
77 require_mods(qw(-imapd -nntpd Mail::IMAPClient Net::NNTP), 1);
78 require File::Copy; # stdlib
79 my $home = $ENV{HOME};
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)) {
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");
90 addr => (my $scalar = tcp_host_port($s)),
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');
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';
115 # simulate server downtime
116 $url = "imap://$srv->{imapd}->{addr}/t.v2.0";
117 lei_ok 'import', $url, '+L:v2';
119 lei_ok 'inspect', "blob:$oid";
121 delete $srv->{imapd}->{td}; # kill + join daemon
123 ok(!(lei 'refresh-mail-sync', $url), 'URL fails on dead -imapd');
124 ok(!(lei 'refresh-mail-sync', '--all'), '--all fails on dead -imapd');
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";
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