From b54f40721fedb566cd212061fd2a10fe50c31a5a Mon Sep 17 00:00:00 2001
From: Dipesh Pandit <dipesh.pandit@enterprisedb.com>
Date: Thu, 20 Jan 2022 17:44:52 +0530
Subject: [PATCH 2/2] Test plain format server compressed gzip backup

---
 src/bin/pg_verifybackup/t/008_untar.pl | 111 ++++++++++++++++++++++-----------
 1 file changed, 74 insertions(+), 37 deletions(-)
 mode change 100644 => 100755 src/bin/pg_verifybackup/t/008_untar.pl

diff --git a/src/bin/pg_verifybackup/t/008_untar.pl b/src/bin/pg_verifybackup/t/008_untar.pl
old mode 100644
new mode 100755
index 85946cf..0885c5c
--- a/src/bin/pg_verifybackup/t/008_untar.pl
+++ b/src/bin/pg_verifybackup/t/008_untar.pl
@@ -11,7 +11,7 @@ use Config;
 use File::Path qw(rmtree);
 use PostgreSQL::Test::Cluster;
 use PostgreSQL::Test::Utils;
-use Test::More tests => 6;
+use Test::More tests => 10;
 
 my $primary = PostgreSQL::Test::Cluster->new('primary');
 $primary->init(allows_streaming => 1);
@@ -35,6 +35,12 @@ my @test_configuration = (
 		'decompress_program' => $ENV{'GZIP_PROGRAM'},
 		'decompress_flags' => [ '-d' ],
 		'enabled' => check_pg_config("#define HAVE_LIBZ 1")
+	},
+	{
+		'compression_method' => 'gzip',
+		'backup_flags' => ['--server-compress', 'gzip', '-Fp'],
+		'plain_format' => 1,
+		'enabled' => check_pg_config("#define HAVE_LIBZ 1"),
 	}
 );
 
@@ -51,54 +57,85 @@ for my $tc (@test_configuration)
 
 		# Take a server-side backup.
 		my @backup = (
-			'pg_basebackup', '--no-sync', '-cfast', '--target',
-			"server:$backup_path", '-Xfetch'
+			'pg_basebackup', '--no-sync', '-cfast', '-Xfetch'
 		);
+
+		if (! $tc->{'plain_format'})
+		{
+			push @backup, '--target', "server:$backup_path";
+		}
+		else
+		{
+			# Target cannot be used with plain format backup.
+			push @backup, '-D', "$backup_path";
+
+			# Make sure that backup directory is empty.
+			rmtree($backup_path);
+		}
+
 		push @backup, @{$tc->{'backup_flags'}};
 		$primary->command_ok(\@backup,
 							 "server side backup, compression $method");
 
 
-		# Verify that the we got the files we expected.
-		my $backup_files = join(',',
-			sort grep { $_ ne '.' && $_ ne '..' } slurp_dir($backup_path));
-		my $expected_backup_files = join(',',
-			sort ('backup_manifest', $tc->{'backup_archive'}));
-		is($backup_files,$expected_backup_files,
-			"found expected backup files, compression $method");
-
-		# Decompress.
-		if (exists $tc->{'decompress_program'})
+		if (! $tc->{'plain_format'})
 		{
-			my @decompress = ($tc->{'decompress_program'});
-			push @decompress, @{$tc->{'decompress_flags'}}
-				if $tc->{'decompress_flags'};
-			push @decompress, $backup_path . '/' . $tc->{'backup_archive'};
-			system_or_bail(@decompress);
-		}
+			# Verify that the we got the files we expected.
+			my $backup_files = join(',',
+				sort grep { $_ ne '.' && $_ ne '..' } slurp_dir($backup_path));
+			my $expected_backup_files = join(',',
+				sort ('backup_manifest', $tc->{'backup_archive'}));
+			is($backup_files,$expected_backup_files,
+				"found expected backup files, compression $method");
+
+			# Decompress.
+			if (exists $tc->{'decompress_program'})
+			{
+				my @decompress = ($tc->{'decompress_program'});
+				push @decompress, @{$tc->{'decompress_flags'}}
+					if $tc->{'decompress_flags'};
+				push @decompress, $backup_path . '/' . $tc->{'backup_archive'};
+				system_or_bail(@decompress);
+			}
+
+			SKIP: {
+				my $tar = $ENV{TAR};
+				# don't check for a working tar here, to accommodate various odd
+				# cases such as AIX. If tar doesn't work the init_from_backup below
+				# will fail.
+				skip "no tar program available", 1
+					if (!defined $tar || $tar eq '');
 
-		SKIP: {
-			my $tar = $ENV{TAR};
-			# don't check for a working tar here, to accomodate various odd
-			# cases such as AIX. If tar doesn't work the init_from_backup below
-			# will fail.
-			skip "no tar program available", 1
-				if (!defined $tar || $tar eq '');
+				# Untar.
+				mkdir($extract_path);
+				system_or_bail($tar, 'xf', $backup_path . '/base.tar',
+					'-C', $extract_path);
 
-			# Untar.
-			mkdir($extract_path);
-			system_or_bail($tar, 'xf', $backup_path . '/base.tar',
-				'-C', $extract_path);
+				# Verify.
+				$primary->command_ok([ 'pg_verifybackup', '-n',
+						'-m', "$backup_path/backup_manifest", '-e', $extract_path ],
+					    "verify backup, compression $method");
+			}
 
-			# Verify.
+			# Cleanup.
+			unlink($backup_path . '/backup_manifest');
+			unlink($backup_path . '/base.tar');
+			rmtree($extract_path);
+		}
+		else
+		{
+			# Verify that the we got the files we expected.
+			ok (-f "$backup_path/PG_VERSION", "backup with plain format created");
+			ok (-f "$backup_path/backup_manifest", "backup manifest included");
+
+			# Verify plain format backup with server compression
 			$primary->command_ok([ 'pg_verifybackup', '-n',
-				'-m', "$backup_path/backup_manifest", '-e', $extract_path ],
-				"verify backup, compression $method");
+					'-m', "$backup_path/backup_manifest", '-e', $backup_path ],
+					"verify plain format backup, compression $method");
+
+			# Cleanup.
+			rmtree($backup_path);
 		}
 
-		# Cleanup.
-		unlink($backup_path . '/backup_manifest');
-		unlink($backup_path . '/base.tar');
-		rmtree($extract_path);
 	}
 }
-- 
1.8.3.1

