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