]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-mirror.t
80bc6ed5ab135f266addc9e63c9bbd2fc7e276de
[public-inbox.git] / t / lei-mirror.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 use strict; use v5.10.1; use PublicInbox::TestCommon;
5 require_mods(qw(-httpd lei));
6 my $sock = tcp_server();
7 my ($tmpdir, $for_destroy) = tmpdir();
8 my $http = 'http://'.tcp_host_port($sock);
9 my ($ro_home, $cfg_path) = setup_public_inboxes;
10 my $cmd = [ qw(-httpd -W0), "--stdout=$tmpdir/out", "--stderr=$tmpdir/err" ];
11 my $td = start_script($cmd, { PI_CONFIG => $cfg_path }, { 3 => $sock });
12 test_lei({ tmpdir => $tmpdir }, sub {
13         my $home = $ENV{HOME};
14         my $t1 = "$home/t1-mirror";
15         lei_ok('add-external', $t1, '--mirror', "$http/t1/", \'--mirror v1');
16         ok(-f "$t1/public-inbox/msgmap.sqlite3", 't1-mirror indexed');
17
18         lei_ok('ls-external');
19         like($lei_out, qr!\Q$t1\E!, 't1 added to ls-externals');
20
21         my $t2 = "$home/t2-mirror";
22         lei_ok('add-external', $t2, '--mirror', "$http/t2/", \'--mirror v2');
23         ok(-f "$t2/msgmap.sqlite3", 't2-mirror indexed');
24
25         lei_ok('ls-external');
26         like($lei_out, qr!\Q$t2\E!, 't2 added to ls-externals');
27
28         ok(!lei('add-external', $t2, '--mirror', "$http/t2/"),
29                 '--mirror fails if reused') or diag "$lei_err.$lei_out = $?";
30         like($lei_err, qr/\Q$t2\E' already exists/, 'destination in error');
31
32         ok(!lei('add-external', "$home/t2\nnewline", '--mirror', "$http/t2/"),
33                 '--mirror fails on newline');
34         like($lei_err, qr/`\\n' not allowed/, 'newline noted in error');
35
36         lei_ok('ls-external');
37         like($lei_out, qr!\Q$t2\E!, 'still in ls-externals');
38         unlike($lei_out, qr!\Qnewline\E!, 'newline entry not added');
39
40         ok(!lei('add-external', "$t2-fail", '-Lmedium'), '--mirror v2');
41         like($lei_err, qr/not a directory/, 'non-directory noted');
42         ok(!-d "$t2-fail", 'destination not created on failure');
43         lei_ok('ls-external');
44         unlike($lei_out, qr!\Q$t2-fail\E!, 'not added to ls-external');
45
46         my %phail = (
47                 HTTPS => 'https://public-inbox.org/' . 'phail',
48                 ONION =>
49 'http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/' .
50 'phail,'
51         );
52         for my $t (qw(HTTPS ONION)) {
53         SKIP: {
54                 my $k = "TEST_LEI_EXTERNAL_$t";
55                 $ENV{$k} or skip "$k unset", 1;
56                 my $url = $phail{$t};
57                 my $dir = "phail-$t";
58                 ok(!lei(qw(add-external -Lmedium --mirror),
59                         $url, $dir), '--mirror non-existent v2');
60                 is($? >> 8, 22, 'curl 404');
61                 ok(!-d $dir, 'directory not created');
62                 unlike($lei_err, qr/# mirrored/, 'no success message');
63                 like($lei_err, qr/curl.*404/, "curl 404 shown for $k");
64         } # SKIP
65         } # for
66 });
67
68 ok($td->kill, 'killed -httpd');
69 $td->join;
70
71 done_testing;