From 75619245b02c7cd659d826d6ea8b3964445155a5 Mon Sep 17 00:00:00 2001
From: Georgios Kokolatos <gkokolatos@pm.me>
Date: Mon, 5 Dec 2022 12:14:22 +0000
Subject: [PATCH v16 1/4] Provide coverage for pg_dump default compression for
 dir and custom format

The restore program will succeed regardless of whether the dumped output was
compressed or not. This commit implements a portable way to check the contents
of the directory via perl's build in filename expansion. It also implements a
way to check the custom's format data segmenets commpression by examining the
header information during the TOC summary output.
---
 src/bin/pg_dump/t/002_pg_dump.pl | 65 +++++++++++++++++++++++++++++---
 1 file changed, 59 insertions(+), 6 deletions(-)

diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl
index c2da1df39d..248540db8c 100644
--- a/src/bin/pg_dump/t/002_pg_dump.pl
+++ b/src/bin/pg_dump/t/002_pg_dump.pl
@@ -36,6 +36,9 @@ my $tempdir = PostgreSQL::Test::Utils::tempdir;
 # to test pg_restore's ability to parse manually compressed files
 # that otherwise pg_dump does not compress on its own (e.g. *.toc).
 #
+# glob_patterns is an optional array consisting of strings compilable
+# with glob() to check the files generated after a dump.
+#
 # restore_cmd is the pg_restore command to run, if any.  Note
 # that this should generally be used when the pg_dump goes to
 # a non-text file and that the restore can then be used to
@@ -46,6 +49,10 @@ my $tempdir = PostgreSQL::Test::Utils::tempdir;
 # database and then pg_dump *that* database (or something along
 # those lines) to validate that part of the process.
 
+my $supports_icu  = ($ENV{with_icu} eq 'yes');
+my $supports_lz4  = check_pg_config("#define USE_LZ4 1");
+my $supports_gzip = check_pg_config("#define HAVE_LIBZ 1");
+
 my %pgdump_runs = (
 	binary_upgrade => {
 		dump_cmd => [
@@ -79,6 +86,14 @@ my %pgdump_runs = (
 			"--file=$tempdir/compression_gzip_custom.sql",
 			"$tempdir/compression_gzip_custom.dump",
 		],
+		command_like => {
+			command => [
+				'pg_restore',
+				'-l', "$tempdir/compression_gzip_custom.dump",
+			],
+			expected => qr/Compression: 1/,
+			name => 'data content is gzip compressed'
+		},
 	},
 
 	# Do not use --no-sync to give test coverage for data sync.
@@ -96,6 +111,11 @@ my %pgdump_runs = (
 			program => $ENV{'GZIP_PROGRAM'},
 			args    => [ '-f', "$tempdir/compression_gzip_dir/blobs.toc", ],
 		},
+		# Verify that only data files where compressed
+		glob_patterns => [
+			"$tempdir/compression_gzip_dir/toc.dat",
+		    "$tempdir/compression_gzip_dir/*.dat.gz",
+		],
 		restore_cmd => [
 			'pg_restore', '--jobs=2',
 			"--file=$tempdir/compression_gzip_dir.sql",
@@ -200,9 +220,8 @@ my %pgdump_runs = (
 	# Do not use --no-sync to give test coverage for data sync.
 	defaults_custom_format => {
 		test_key => 'defaults',
-		compile_option => 'gzip',
 		dump_cmd => [
-			'pg_dump', '-Fc', '-Z6',
+			'pg_dump', '-Fc',
 			"--file=$tempdir/defaults_custom_format.dump", 'postgres',
 		],
 		restore_cmd => [
@@ -210,9 +229,22 @@ my %pgdump_runs = (
 			"--file=$tempdir/defaults_custom_format.sql",
 			"$tempdir/defaults_custom_format.dump",
 		],
+		command_like => {
+			command => [
+				'pg_restore',
+				'-l', "$tempdir/defaults_custom_format.dump",
+			],
+			expected => $supports_gzip ?
+							qr/Compression: -1/ :
+							qr/Compression: 0/,
+			name => 'data content is gzip compressed by default if available'
+		},
 	},
 
 	# Do not use --no-sync to give test coverage for data sync.
+	# By default, the directory format compresses its data files
+	# when the code is compiled with gzip support, and lets them
+	# uncompressed when not compiled with it.
 	defaults_dir_format => {
 		test_key => 'defaults',
 		dump_cmd => [
@@ -224,6 +256,13 @@ my %pgdump_runs = (
 			"--file=$tempdir/defaults_dir_format.sql",
 			"$tempdir/defaults_dir_format",
 		],
+		glob_patterns => [
+			"$tempdir/defaults_dir_format/toc.dat",
+			"$tempdir/defaults_dir_format/blobs.toc",
+		     $supports_gzip ?
+				"$tempdir/defaults_dir_format/*.dat.gz" :
+				"$tempdir/defaults_dir_format/*.dat",
+		],
 	},
 
 	# Do not use --no-sync to give test coverage for data sync.
@@ -3920,10 +3959,6 @@ if ($collation_check_stderr !~ /ERROR: /)
 	$collation_support = 1;
 }
 
-my $supports_icu  = ($ENV{with_icu} eq 'yes');
-my $supports_lz4  = check_pg_config("#define USE_LZ4 1");
-my $supports_gzip = check_pg_config("#define HAVE_LIBZ 1");
-
 # ICU doesn't work with some encodings
 my $encoding = $node->safe_psql('postgres', 'show server_encoding');
 $supports_icu = 0 if $encoding eq 'SQL_ASCII';
@@ -4153,6 +4188,24 @@ foreach my $run (sort keys %pgdump_runs)
 		command_ok(\@full_compress_cmd, "$run: compression commands");
 	}
 
+	if ($pgdump_runs{$run}->{glob_patterns})
+	{
+		my $glob_patterns = $pgdump_runs{$run}->{glob_patterns};
+		foreach my $glob_pattern (@{$glob_patterns})
+		{
+			my @glob_output = glob($glob_pattern);
+			is(scalar(@glob_output) > 0, 1, "$run: glob check for $glob_pattern");
+		}
+	}
+
+	if ($pgdump_runs{$run}->{command_like})
+	{
+		my $cmd_like = $pgdump_runs{$run}->{command_like};
+		$node->command_like(\@{ $cmd_like->{command} },
+				$cmd_like->{expected},
+				$cmd_like->{name})
+	}
+
 	if ($pgdump_runs{$run}->{restore_cmd})
 	{
 		$node->command_ok(\@{ $pgdump_runs{$run}->{restore_cmd} },
-- 
2.34.1

