Skip to content

Commit ffc9dda

Browse files
committed
Add TAP tests for ZLIB compression for pg_receivewal
There is a non-trivial amount of code that handles ZLIB compression in pg_receivewal, from basics like the format name, the calculation of the start streaming position and of course the compression itself, but there was no automated coverage for it. This commit introduces a set of conditional tests (if the build supports ZLIB) to cover the creation of ZLIB-compressed WAL segments, the handling of the partial, compressed, WAL segments and the compression operation in itself. Note that there is an extra phase checking the validity of the generated files by using directly a gzip command, passed down by the Makefile of pg_receivewal. This part is skipped if the command cannot be found, something likely going to happen on Windows with MSVC except if one sets the variable GZIP_PROGRAM in the environment of the test. This set of tests will become handy for upcoming patches that add more options for the compression methods used by pg_receivewal, like LZ4, to make sure that no existing facilities are broken. Author: Georgios Kokolatos Reviewed-by: Gilles Darold, Michael Paquier Discussion: https://postgr.es/m/07BK3Mk5aEOsTwGaY77qBVyf9GjoEzn8TMgHLyPGfEFPIpTEmoQuP2P4c7teesjSg-LPeUafsp1flnPeQYINMSMB_UpggJDoduB5EDYBqaQ=@protonmail.com
1 parent dc2db1e commit ffc9dda

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

src/bin/pg_basebackup/Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ subdir = src/bin/pg_basebackup
1818
top_builddir = ../../..
1919
include $(top_builddir)/src/Makefile.global
2020

21-
# make this available to TAP test scripts
21+
# make these available to TAP test scripts
2222
export TAR
23+
# Note that GZIP cannot be used directly as this environment variable is
24+
# used by the command "gzip" to pass down options, so stick with a different
25+
# name.
26+
export GZIP_PROGRAM=$(GZIP)
2327

2428
override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS)
2529
LDFLAGS_INTERNAL += -L$(top_builddir)/src/fe_utils -lpgfeutils $(libpq_pgport)

src/bin/pg_basebackup/t/020_pg_receivewal.pl

+77-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use warnings;
66
use TestLib;
77
use PostgresNode;
8-
use Test::More tests => 19;
8+
use Test::More tests => 27;
99

1010
program_help_ok('pg_receivewal');
1111
program_version_ok('pg_receivewal');
@@ -58,14 +58,89 @@
5858
$primary->psql('postgres',
5959
'INSERT INTO test_table VALUES (generate_series(1,100));');
6060

61-
# Stream up to the given position.
61+
# Stream up to the given position. This is necessary to have a fixed
62+
# started point for the next commands done in this test, with or without
63+
# compression involved.
6264
$primary->command_ok(
6365
[
6466
'pg_receivewal', '-D', $stream_dir, '--verbose',
6567
'--endpos', $nextlsn, '--synchronous', '--no-loop'
6668
],
6769
'streaming some WAL with --synchronous');
6870

71+
# Verify that one partial file was generated and keep track of it
72+
my @partial_wals = glob "$stream_dir/*\.partial";
73+
is(scalar(@partial_wals), 1, "one partial WAL segment was created");
74+
75+
# Check ZLIB compression if available.
76+
SKIP:
77+
{
78+
skip "postgres was not built with ZLIB support", 5
79+
if (!check_pg_config("#define HAVE_LIBZ 1"));
80+
81+
# Generate more WAL worth one completed, compressed, segment.
82+
$primary->psql('postgres', 'SELECT pg_switch_wal();');
83+
$nextlsn =
84+
$primary->safe_psql('postgres', 'SELECT pg_current_wal_insert_lsn();');
85+
chomp($nextlsn);
86+
$primary->psql('postgres',
87+
'INSERT INTO test_table VALUES (generate_series(100,200));');
88+
89+
$primary->command_ok(
90+
[
91+
'pg_receivewal', '-D', $stream_dir, '--verbose',
92+
'--endpos', $nextlsn, '--compress', '1'
93+
],
94+
"streaming some WAL using ZLIB compression");
95+
96+
# Verify that the stored files are generated with their expected
97+
# names.
98+
my @zlib_wals = glob "$stream_dir/*.gz";
99+
is(scalar(@zlib_wals), 1,
100+
"one WAL segment compressed with ZLIB was created");
101+
my @zlib_partial_wals = glob "$stream_dir/*.gz.partial";
102+
is(scalar(@zlib_partial_wals),
103+
1, "one partial WAL segment compressed with ZLIB was created");
104+
105+
# Verify that the start streaming position is computed correctly by
106+
# comparing it with the partial file generated previously. The name
107+
# of the previous partial, now-completed WAL segment is updated, keeping
108+
# its base number.
109+
$partial_wals[0] =~ s/\.partial$/.gz/;
110+
is($zlib_wals[0] =~ m/$partial_wals[0]/,
111+
1, "one partial WAL segment is now completed");
112+
# Update the list of partial wals with the current one.
113+
@partial_wals = @zlib_partial_wals;
114+
115+
# There is one complete and one partial file compressed with ZLIB.
116+
# Check the integrity of both, if gzip is a command available.
117+
my $gzip = $ENV{GZIP_PROGRAM};
118+
skip "program gzip is not found in your system", 1
119+
if ( !defined $gzip
120+
|| $gzip eq ''
121+
|| system_log($gzip, '--version') != 0);
122+
123+
push(@zlib_wals, @zlib_partial_wals);
124+
my $gzip_is_valid = system_log($gzip, '--test', @zlib_wals);
125+
is($gzip_is_valid, 0,
126+
"gzip verified the integrity of compressed WAL segments");
127+
}
128+
129+
# Verify that the start streaming position is computed and that the value is
130+
# correct regardless of whether ZLIB is available.
131+
$primary->psql('postgres', 'SELECT pg_switch_wal();');
132+
$nextlsn =
133+
$primary->safe_psql('postgres', 'SELECT pg_current_wal_insert_lsn();');
134+
chomp($nextlsn);
135+
$primary->psql('postgres',
136+
'INSERT INTO test_table VALUES (generate_series(200,300));');
137+
$primary->command_ok(
138+
[ 'pg_receivewal', '-D', $stream_dir, '--verbose', '--endpos', $nextlsn ],
139+
"streaming some WAL");
140+
141+
$partial_wals[0] =~ s/(\.gz)?.partial//;
142+
ok(-e $partial_wals[0], "check that previously partial WAL is now complete");
143+
69144
# Permissions on WAL files should be default
70145
SKIP:
71146
{

0 commit comments

Comments
 (0)