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>
6 use PublicInbox::TestCommon;
7 use PublicInbox::Config;
8 use PublicInbox::Spawn qw(which spawn);
9 which('sqlite3') or plan skip_all => 'sqlite3 binary missing';
10 require_mods(qw(DBD::SQLite HTTP::Request::Common Plack::Test URI::Escape
11 Plack::Builder IO::Uncompress::Gunzip));
12 use_ok($_) for qw(Plack::Test HTTP::Request::Common);
13 require_ok 'PublicInbox::Msgmap';
14 require_ok 'PublicInbox::AltId';
15 require_ok 'PublicInbox::WWW';
16 my ($tmpdir, $for_destroy) = tmpdir();
19 my $ibx = create_inbox 'test', indexlevel => 'basic', sub {
21 $im->add(PublicInbox::Eml->new(<<'EOF')) or BAIL_OUT;
23 Message-Id: <a@example.com>
27 my $spec = "serial:$aid:file=blah.sqlite3";
28 my $altid = PublicInbox::AltId->new($ibx, $spec, 1);
29 $altid->mm_alt->mid_set(1, 'a@example.com');
30 $cfgpath = "$ibx->{inboxdir}/cfg";
31 open my $fh, '>', $cfgpath or BAIL_OUT "open $cfgpath: $!";
32 print $fh <<EOF or BAIL_OUT $!;
34 inboxdir = $ibx->{inboxdir}
35 address = $ibx->{-primary_address}
37 url = http://example.com/test
39 close $fh or BAIL_OUT $!;
41 $cfgpath //= "$ibx->{inboxdir}/cfg";
42 my $cfg = PublicInbox::Config->new($cfgpath);
43 my $www = PublicInbox::WWW->new($cfg);
44 my $cmpfile = "$tmpdir/cmp.sqlite3";
47 my $res = $cb->(POST("/test/$aid.sql.gz"));
48 is($res->code, 200, 'retrieved gzipped dump');
49 IO::Uncompress::Gunzip::gunzip(\($res->content) => \(my $buf));
50 pipe(my ($r, $w)) or die;
51 my $cmd = ['sqlite3', $cmpfile];
52 my $pid = spawn($cmd, undef, { 0 => $r });
55 is(waitpid($pid, 0), $pid, 'sqlite3 exited');
56 is($?, 0, 'sqlite3 loaded dump');
57 my $mm_cmp = PublicInbox::Msgmap->new_file($cmpfile);
58 is($mm_cmp->mid_for(1), 'a@example.com', 'sqlite3 dump valid');
60 unlink $cmpfile or die;
62 test_psgi(sub { $www->call(@_) }, $client);
64 require_mods(qw(Plack::Test::ExternalServer), 4);
65 my $env = { PI_CONFIG => $cfgpath };
66 my $sock = tcp_server() or die;
67 my ($out, $err) = map { "$tmpdir/std$_.log" } qw(out err);
68 my $cmd = [ qw(-httpd -W0), "--stdout=$out", "--stderr=$err" ];
69 my $td = start_script($cmd, $env, { 3 => $sock });
70 my ($h, $p) = tcp_host_port($sock);
71 local $ENV{PLACK_TEST_EXTERNALSERVER_URI} = "http://$h:$p";
72 Plack::Test::ExternalServer::test_psgi(client => $client);