From f1ebb48ad964d2883f92c9ec50ac7e81f3ba8809 Mon Sep 17 00:00:00 2001
From: Georgios Kokolatos <gkokolatos@pm.me>
Date: Fri, 9 Jul 2021 11:10:12 +0000
Subject: Introduce pg_receivewal gzip compression tests

There exists a non trivial amount of code that handles gzip compression. The
current patch introduces tests that cover creation of gzip compressed WAL files
and the handling of the partial segments. Also the integrity of the compressed
files is verified.
---
 src/bin/pg_basebackup/t/020_pg_receivewal.pl | 58 +++++++++++++++++++-
 1 file changed, 57 insertions(+), 1 deletion(-)

diff --git a/src/bin/pg_basebackup/t/020_pg_receivewal.pl b/src/bin/pg_basebackup/t/020_pg_receivewal.pl
index a547c97ef1..90c97b104d 100644
--- a/src/bin/pg_basebackup/t/020_pg_receivewal.pl
+++ b/src/bin/pg_basebackup/t/020_pg_receivewal.pl
@@ -5,7 +5,7 @@ use strict;
 use warnings;
 use TestLib;
 use PostgresNode;
-use Test::More tests => 19;
+use Test::More tests => 25;
 
 program_help_ok('pg_receivewal');
 program_version_ok('pg_receivewal');
@@ -66,6 +66,62 @@ $primary->command_ok(
 	],
 	'streaming some WAL with --synchronous');
 
+# Check gzip compression if available
+SKIP:
+{
+	skip "postgres was not build with gzip support", 6
+		if (!check_pg_config("#define HAVE_LIBZ 1"));
+
+	# Generate some WAL
+	$primary->psql('postgres', 'SELECT pg_switch_wal();');
+	$nextlsn = $primary->safe_psql('postgres',
+		'SELECT pg_current_wal_insert_lsn();');
+	chomp($nextlsn);
+	$primary->psql('postgres',
+		'INSERT INTO test_table VALUES (generate_series(100,200));');
+	$primary->psql('postgres', 'SELECT pg_switch_wal();');
+
+	$primary->command_ok(
+		[
+			'pg_receivewal', '-D',     $stream_dir,     '--verbose',
+			'--endpos',      $nextlsn, '-Z', '5'
+		],
+		"streaming some WAL using level 5 gzip compression");
+
+	# Verify that the stored file is compressed
+	my @gzip_wals = glob "$stream_dir/*.gz";
+	is (scalar(@gzip_wals), 1, "one gzip compressed WAL was created");
+
+	# Verify compressed file's integrity
+	my $gzip_is_valid = system_log('gzip', '--test', $gzip_wals[0]);
+	is($gzip_is_valid, 0, "program gzip verified file's integrity");
+
+	# There should be one .gz partial file
+	my @gzip_partial_wals = glob "$stream_dir/*.gz.partial";
+	is (scalar(@gzip_partial_wals), 1,
+		"one partial gzip compressed WAL was created");
+
+	# Generate some more WAL
+	$nextlsn = $primary->safe_psql('postgres',
+		'SELECT pg_current_wal_insert_lsn();');
+	$primary->psql('postgres',
+		'INSERT INTO test_table VALUES (generate_series(200,300));');
+	chomp($nextlsn);
+	$primary->psql('postgres', 'SELECT pg_switch_wal();');
+
+	$primary->command_ok(
+		[
+			'pg_receivewal', '-D',     $stream_dir,     '--verbose',
+			'--endpos',      $nextlsn, '--compress', '1'
+		],
+		"streaming some WAL using level 1 gzip compression");
+
+	# The .gz.partial file should now be complete
+	$gzip_partial_wals[0] =~ s/\.partial$//;
+	ok(-e $gzip_partial_wals[0],
+		"check that previously partial gzip compressed WAL is now complete");
+}
+
 # Permissions on WAL files should be default
 SKIP:
 {
-- 
2.25.1

