]> Sergey Matveev's repositories - public-inbox.git/blob - xt/imapd-mbsync-oimap.t
0baf5b4cc073d0421c9f030eb5b4be7f245311c4
[public-inbox.git] / xt / imapd-mbsync-oimap.t
1 #!perl -w
2 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # ensure mbsync and offlineimap compatibility
5 use strict;
6 use v5.10.1;
7 use File::Path qw(mkpath);
8 use PublicInbox::TestCommon;
9 use PublicInbox::Spawn qw(which spawn);
10 require_mods(qw(-imapd));
11 my $inboxdir = $ENV{GIANT_INBOX_DIR};
12 (defined($inboxdir) && -d $inboxdir) or
13         plan skip_all => "GIANT_INBOX_DIR not defined for $0";
14 plan skip_all => "bad characters in $inboxdir" if $inboxdir =~ m![^\w\.\-/]!;
15 my ($tmpdir, $for_destroy) = tmpdir();
16 my $cfg = "$tmpdir/cfg";
17 my $newsgroup = 'inbox.test';
18 my $mailbox = "$newsgroup.0";
19 {
20         open my $fh, '>', $cfg or BAIL_OUT "open: $!";
21         print $fh <<EOF or BAIL_OUT "print: $!";
22 [publicinbox "test"]
23         newsgroup = $newsgroup
24         address = oimap\@example.com
25         inboxdir = $inboxdir
26 EOF
27         close $fh or BAIL_OUT "close: $!";
28 }
29 my ($out, $err) = ("$tmpdir/stdout.log", "$tmpdir/stderr.log");
30 my $sock = tcp_server();
31 my $cmd = [ '-imapd', '-W0', "--stdout=$out", "--stderr=$err" ];
32 my $env = { PI_CONFIG => $cfg };
33 my $td = start_script($cmd, $env, { 3 => $sock }) or BAIL_OUT "-imapd: $?";
34 {
35         my $c = tcp_connect($sock);
36         like(readline($c), qr/CAPABILITY /, 'got greeting');
37 }
38
39 my $host_port = tcp_host_port($sock);
40 my ($host, $port) = ($sock->sockhost, $sock->sockport);
41 my %pids;
42
43 SKIP: {
44         mkpath([map { "$tmpdir/oimapdir/$_" } qw(cur new tmp)]);
45         my $oimap = which('offlineimap') or skip 'no offlineimap(1)', 1;
46         open my $fh, '>', "$tmpdir/.offlineimaprc" or BAIL_OUT "open: $!";
47         print $fh <<EOF or BAIL_OUT "print: $!";
48 [general]
49 accounts = test
50 socktimeout = 10
51 fsync = false
52
53 [Account test]
54 localrepository = l.test
55 remoterepository = r.test
56
57 [Repository l.test]
58 type = Maildir
59 localfolders = ~/oimapdir
60
61 [Repository r.test]
62 type = IMAP
63 ssl = no
64 remotehost = $host
65 remoteport = $port
66 remoteuser = anonymous
67 remotepass = Hunter2
68
69 # python-imaplib2 times out on select/poll when compression is enabled
70 # <https://bugs.debian.org/961713>
71 usecompression = no
72 EOF
73         close $fh or BAIL_OUT "close: $!";
74         my $cmd = [ $oimap, qw(-o -q -u quiet) ];
75         my $pid = spawn($cmd, { HOME => $tmpdir }, { 1 => 2 });
76         $pids{$pid} = $cmd;
77 }
78
79 SKIP: {
80         mkpath([map { "$tmpdir/mbsyncdir/test/$_" } qw(cur new tmp)]);
81         my $mbsync = which('mbsync') or skip 'no mbsync(1)', 1;
82         open my $fh, '>', "$tmpdir/.mbsyncrc" or BAIL_OUT "open: $!";
83         print $fh <<EOF or BAIL_OUT "print: $!";
84 Create Slave
85 SyncState *
86 Remove None
87 FSync no
88
89 MaildirStore local
90 Path ~/mbsyncdir/
91 Inbox ~/mbsyncdir/test
92 SubFolders verbatim
93
94 IMAPStore remote
95 Host $host
96 Port $port
97 User anonymous
98 Pass Hunter2
99 SSLType None
100 UseNamespace no
101 # DisableExtension COMPRESS=DEFLATE
102
103 Channel "test"
104 Master ":remote:INBOX"
105 Slave ":local:test"
106 Expunge None
107 Sync PullNew
108 Patterns *
109 EOF
110         close $fh or BAIL_OUT "close: $!";
111         my $cmd = [ $mbsync, qw(-aqq) ];
112         my $pid = spawn($cmd, { HOME => $tmpdir }, { 1 => 2 });
113         $pids{$pid} = $cmd;
114 }
115
116 while (scalar keys %pids) {
117         my $pid = waitpid(-1, 0) or next;
118         my $cmd = delete $pids{$pid} or next;
119         is($?, 0, join(' ', @$cmd, 'done'));
120 }
121
122 my $sec = $ENV{TEST_PERSIST} // 0;
123 diag "TEST_PERSIST=$sec";
124 if ($sec) {
125         diag "sleeping ${sec}s, imap://$host_port/$mailbox available";
126         diag "tmpdir=$tmpdir (Maildirs available)";
127         diag "stdout=$out";
128         diag "stderr=$err";
129         diag "pid=$td->{pid}";
130         sleep $sec;
131 }
132 $td->kill;
133 $td->join;
134 is($?, 0, 'no error on -imapd exit');
135 done_testing;