1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 use PublicInbox::TestCommon;
6 use PublicInbox::Inbox;
7 use PublicInbox::InboxWritable;
8 use PublicInbox::Config;
9 use PublicInbox::Spawn qw(which spawn);
10 which('sqlite3') or plan skip_all => 'sqlite3 binary missing';
11 require_mods(qw(DBD::SQLite HTTP::Request::Common Plack::Test URI::Escape
12 Plack::Builder IO::Uncompress::Gunzip));
13 use_ok($_) for qw(Plack::Test HTTP::Request::Common);
14 require_ok 'PublicInbox::Msgmap';
15 require_ok 'PublicInbox::AltId';
16 require_ok 'PublicInbox::WWW';
17 my ($inboxdir, $for_destroy) = tmpdir();
19 my $spec = "serial:$aid:file=blah.sqlite3";
22 inboxdir => $inboxdir,
24 -primary_address => 'test@example.com',
26 my $ibx = PublicInbox::Inbox->new($opts);
27 $ibx = PublicInbox::InboxWritable->new($ibx, 1);
28 my $im = $ibx->importer(0);
29 my $mime = PublicInbox::Eml->new(<<'EOF');
31 Message-Id: <a@example.com>
36 mkdir "$inboxdir/public-inbox" or die;
37 my $altid = PublicInbox::AltId->new($ibx, $spec, 1);
38 $altid->mm_alt->mid_set(1, 'a@example.com');
41 my $cfgpath = "$inboxdir/cfg";
42 open my $fh, '>', $cfgpath or die;
43 print $fh <<EOF or die;
46 address = test\@example.com
48 url = http://example.com/test
51 my $cfg = PublicInbox::Config->new($cfgpath);
52 my $www = PublicInbox::WWW->new($cfg);
53 my $cmpfile = "$inboxdir/cmp.sqlite3";
56 my $res = $cb->(POST("/test/$aid.sql.gz"));
57 is($res->code, 200, 'retrieved gzipped dump');
58 IO::Uncompress::Gunzip::gunzip(\($res->content) => \(my $buf));
59 pipe(my ($r, $w)) or die;
60 my $cmd = ['sqlite3', $cmpfile];
61 my $pid = spawn($cmd, undef, { 0 => $r });
64 is(waitpid($pid, 0), $pid, 'sqlite3 exited');
65 is($?, 0, 'sqlite3 loaded dump');
66 my $mm_cmp = PublicInbox::Msgmap->new_file($cmpfile);
67 is($mm_cmp->mid_for(1), 'a@example.com', 'sqlite3 dump valid');
69 unlink $cmpfile or die;
71 test_psgi(sub { $www->call(@_) }, $client);
73 require_mods(qw(Plack::Test::ExternalServer), 4);
74 my $env = { PI_CONFIG => $cfgpath };
75 my $sock = tcp_server() or die;
76 my ($out, $err) = map { "$inboxdir/std$_.log" } qw(out err);
77 my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
78 my $td = start_script($cmd, $env, { 3 => $sock });
79 my ($h, $p) = ($sock->sockhost, $sock->sockport);
80 local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = "http://$h:$p";
81 Plack::Test::ExternalServer::test_psgi(client => $client);