From 37ca54e7534eaf7a3c8b5ed811e6efae901f97b4 Mon Sep 17 00:00:00 2001
From: Georgios Kokolatos <gkokolatos@pm.me>
Date: Mon, 12 Jul 2021 14:55:11 +0000
Subject: [PATCH v3] 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/Makefile               |  3 +-
 src/bin/pg_basebackup/t/020_pg_receivewal.pl | 64 +++++++++++++++++++-
 2 files changed, 65 insertions(+), 2 deletions(-)

diff --git a/src/bin/pg_basebackup/Makefile b/src/bin/pg_basebackup/Makefile
index 66e0070f1a..56fc12efa3 100644
--- a/src/bin/pg_basebackup/Makefile
+++ b/src/bin/pg_basebackup/Makefile
@@ -18,8 +18,9 @@ subdir = src/bin/pg_basebackup
 top_builddir = ../../..
 include $(top_builddir)/src/Makefile.global
 
-# make this available to TAP test scripts
+# make these available to TAP test scripts
 export TAR
+export GZIP_PROGRAM=$(GZIP)
 
 override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS)
 LDFLAGS_INTERNAL += -L$(top_builddir)/src/fe_utils -lpgfeutils $(libpq_pgport)
diff --git a/src/bin/pg_basebackup/t/020_pg_receivewal.pl b/src/bin/pg_basebackup/t/020_pg_receivewal.pl
index a547c97ef1..0f7acbd7f4 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,68 @@ $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");
+
+	# 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");
+
+	# Finally verify compressed files's integrity
+	my $gzip_program = $ENV{GZIP_PROGRAM};
+	skip "program gzip is not found in your system", 1
+		if (!defined $gzip_program || $gzip_program eq '');
+
+	# There are two gzip compressed files, test both
+	push(@gzip_wals, @gzip_partial_wals);
+	my $gzip_is_valid = system_log($gzip_program, '--test', @gzip_wals);
+	is($gzip_is_valid, 0, "program gzip verified file's integrity");
+}
+
 # Permissions on WAL files should be default
 SKIP:
 {
-- 
2.25.1

