Skip to content

Commit a7885c9

Browse files
committed
Provide test coverage in pg_dump for default behaviors with compression
By default, the contents generated by the custom and directory dump formats are compressed. However, with the existing test facility, the restore program will succeed regardless of whether the dumped output was compressed or not without checking if anything has been compressed. This commit implements a portable way to check the contents of the custom and directory dump formats: - glob_patterns, that can be defined for each test as an array of glob()-compilable strings, tracking the contents that should or should not be compressed. While this is useful to make sure that the table data is compressed, this also checks that blobs.toc and toc.dat are never compressed. - command_like, to execute a command on a dump and check its generated output. This is used here in correlation with pg_restore -l to check if the dumps have been compressed or not, depending on if the build supports gzip, or not. This hole in the tests has come up when working on 5e73a60, where compression has to be applied by default, if available, for both dump formats. The idea of glob_patterns comes from me, and Georgios has come up with the design for command_like. Author: Georgios Kokolatos, Michael Paquier Discussion: https://postgr.es/m/DQn4czCWR1rcbGPLL7p3LfEr5-kGmlySm-H05VgroINdikvhtS5r9EdI6b8D8sjnbKdJ09k-cxs2AqijBeHAWk9Q8gvEAxPRHuLRhwONcGc=@pm.me
1 parent 1bd47d0 commit a7885c9

File tree

1 file changed

+71
-6
lines changed

1 file changed

+71
-6
lines changed

src/bin/pg_dump/t/002_pg_dump.pl

+71-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
# to test pg_restore's ability to parse manually compressed files
3737
# that otherwise pg_dump does not compress on its own (e.g. *.toc).
3838
#
39+
# glob_patterns is an optional array consisting of strings compilable
40+
# with glob() to check the files generated after a dump.
41+
#
42+
# command_like is an optional utility that can be used to compare
43+
# the output generated by a command on the contents of an existing
44+
# dump, like the TOC description of a pg_restore command.
45+
#
3946
# restore_cmd is the pg_restore command to run, if any. Note
4047
# that this should generally be used when the pg_dump goes to
4148
# a non-text file and that the restore can then be used to
@@ -46,6 +53,10 @@
4653
# database and then pg_dump *that* database (or something along
4754
# those lines) to validate that part of the process.
4855

56+
my $supports_icu = ($ENV{with_icu} eq 'yes');
57+
my $supports_lz4 = check_pg_config("#define USE_LZ4 1");
58+
my $supports_gzip = check_pg_config("#define HAVE_LIBZ 1");
59+
4960
my %pgdump_runs = (
5061
binary_upgrade => {
5162
dump_cmd => [
@@ -79,6 +90,13 @@
7990
"--file=$tempdir/compression_gzip_custom.sql",
8091
"$tempdir/compression_gzip_custom.dump",
8192
],
93+
command_like => {
94+
command => [
95+
'pg_restore', '-l', "$tempdir/compression_gzip_custom.dump",
96+
],
97+
expected => qr/Compression: 1/,
98+
name => 'data content is gzip-compressed'
99+
},
82100
},
83101

84102
# Do not use --no-sync to give test coverage for data sync.
@@ -96,6 +114,11 @@
96114
program => $ENV{'GZIP_PROGRAM'},
97115
args => [ '-f', "$tempdir/compression_gzip_dir/blobs.toc", ],
98116
},
117+
# Verify that only data files where compressed
118+
glob_patterns => [
119+
"$tempdir/compression_gzip_dir/toc.dat",
120+
"$tempdir/compression_gzip_dir/*.dat.gz",
121+
],
99122
restore_cmd => [
100123
'pg_restore', '--jobs=2',
101124
"--file=$tempdir/compression_gzip_dir.sql",
@@ -198,21 +221,34 @@
198221
},
199222

200223
# Do not use --no-sync to give test coverage for data sync.
224+
# By default, the custom format compresses its data file
225+
# when the code is compiled with gzip support, and lets them
226+
# uncompressed when not compiled with it.
201227
defaults_custom_format => {
202228
test_key => 'defaults',
203-
compile_option => 'gzip',
204229
dump_cmd => [
205-
'pg_dump', '-Fc', '-Z6',
230+
'pg_dump', '-Fc',
206231
"--file=$tempdir/defaults_custom_format.dump", 'postgres',
207232
],
208233
restore_cmd => [
209234
'pg_restore', '-Fc',
210235
"--file=$tempdir/defaults_custom_format.sql",
211236
"$tempdir/defaults_custom_format.dump",
212237
],
238+
command_like => {
239+
command =>
240+
[ 'pg_restore', '-l', "$tempdir/defaults_custom_format.dump", ],
241+
expected => $supports_gzip ?
242+
qr/Compression: -1/ :
243+
qr/Compression: 0/,
244+
name => 'data content is gzip-compressed by default if available',
245+
},
213246
},
214247

215248
# Do not use --no-sync to give test coverage for data sync.
249+
# By default, the directory format compresses its data files
250+
# when the code is compiled with gzip support, and lets them
251+
# uncompressed when not compiled with it.
216252
defaults_dir_format => {
217253
test_key => 'defaults',
218254
dump_cmd => [
@@ -224,6 +260,21 @@
224260
"--file=$tempdir/defaults_dir_format.sql",
225261
"$tempdir/defaults_dir_format",
226262
],
263+
command_like => {
264+
command =>
265+
[ 'pg_restore', '-l', "$tempdir/defaults_dir_format", ],
266+
expected => $supports_gzip ?
267+
qr/Compression: -1/ :
268+
qr/Compression: 0/,
269+
name => 'data content is gzip-compressed by default',
270+
},
271+
glob_patterns => [
272+
"$tempdir/defaults_dir_format/toc.dat",
273+
"$tempdir/defaults_dir_format/blobs.toc",
274+
$supports_gzip ?
275+
"$tempdir/defaults_dir_format/*.dat.gz" :
276+
"$tempdir/defaults_dir_format/*.dat",
277+
],
227278
},
228279

229280
# Do not use --no-sync to give test coverage for data sync.
@@ -3920,10 +3971,6 @@
39203971
$collation_support = 1;
39213972
}
39223973
3923-
my $supports_icu = ($ENV{with_icu} eq 'yes');
3924-
my $supports_lz4 = check_pg_config("#define USE_LZ4 1");
3925-
my $supports_gzip = check_pg_config("#define HAVE_LIBZ 1");
3926-
39273974
# ICU doesn't work with some encodings
39283975
my $encoding = $node->safe_psql('postgres', 'show server_encoding');
39293976
$supports_icu = 0 if $encoding eq 'SQL_ASCII';
@@ -4153,6 +4200,24 @@
41534200
command_ok(\@full_compress_cmd, "$run: compression commands");
41544201
}
41554202
4203+
if ($pgdump_runs{$run}->{glob_patterns})
4204+
{
4205+
my $glob_patterns = $pgdump_runs{$run}->{glob_patterns};
4206+
foreach my $glob_pattern (@{$glob_patterns})
4207+
{
4208+
my @glob_output = glob($glob_pattern);
4209+
is(scalar(@glob_output) > 0, 1, "$run: glob check for $glob_pattern");
4210+
}
4211+
}
4212+
4213+
if ($pgdump_runs{$run}->{command_like})
4214+
{
4215+
my $cmd_like = $pgdump_runs{$run}->{command_like};
4216+
$node->command_like(\@{ $cmd_like->{command} },
4217+
$cmd_like->{expected},
4218+
"$run: " . $cmd_like->{name})
4219+
}
4220+
41564221
if ($pgdump_runs{$run}->{restore_cmd})
41574222
{
41584223
$node->command_ok(\@{ $pgdump_runs{$run}->{restore_cmd} },

0 commit comments

Comments
 (0)