From 7311b96b852b6db2c61bd49bbc70e66ca053a168 Mon Sep 17 00:00:00 2001
From: Lee Dong Wook <sh95119@gmail.com>
Date: Fri, 29 Jul 2022 11:46:07 +0900
Subject: [PATCH] pg_basebackup: add test for zstd coverage

---
 src/bin/pg_basebackup/Makefile               |  1 +
 src/bin/pg_basebackup/t/010_pg_basebackup.pl | 38 ++++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/src/bin/pg_basebackup/Makefile b/src/bin/pg_basebackup/Makefile
index 0035ebcef540..9cf092f57498 100644
--- a/src/bin/pg_basebackup/Makefile
+++ b/src/bin/pg_basebackup/Makefile
@@ -21,6 +21,7 @@ include $(top_builddir)/src/Makefile.global
 # make these available to TAP test scripts
 export LZ4
 export TAR
+export ZSTD
 # Note that GZIP cannot be used directly as this environment variable is
 # used by the command "gzip" to pass down options, so stick with a different
 # name.
diff --git a/src/bin/pg_basebackup/t/010_pg_basebackup.pl b/src/bin/pg_basebackup/t/010_pg_basebackup.pl
index 3d1a4ddd5c0e..264cae392fd1 100644
--- a/src/bin/pg_basebackup/t/010_pg_basebackup.pl
+++ b/src/bin/pg_basebackup/t/010_pg_basebackup.pl
@@ -865,6 +865,44 @@
 	rmtree("$tempdir/backup_gzip3");
 }
 
+# Check ZSTD compression if available.
+SKIP:
+{
+	skip "postgres was not built with ZSTD support", 3
+	  if (!check_pg_config("#define USE_ZSTD 1"));
+
+	$node->command_ok(
+		[
+			@pg_basebackup_defs,    '-D',
+			"$tempdir/backup_zstd", '--compress',
+			'server-zstd',          '--compress',
+			'client-zstd',          '--wal-method',
+            'none',                 '--format',
+            't'
+		],
+		'pg_basebackup with --compress=ZSTD');
+
+	# Verify that the stored files are generated with their expected
+	# names.
+	my @zstd_files = glob "$tempdir/backup_zstd/*.tar.zst";
+	is(scalar(@zstd_files), 1,
+		"file created with --compress=zstd (base.tar.zstd)"
+	);
+
+	# Check the integrity of the files generated.
+	my $zstd = $ENV{ZSTD};
+
+	skip "program zstd is not found in your system", 1
+	  if (!defined $zstd
+		|| $zstd eq '');
+
+	my $zstd_is_valid =
+	  system_log($zstd, '--test', @zstd_files);
+
+	is($zstd_is_valid, 0, "zstd verified the integrity of compressed data");
+	rmtree("$tempdir/backup_zstd");
+}
+
 # Test background stream process terminating before the basebackup has
 # finished, the main process should exit gracefully with an error message on
 # stderr. To reduce the risk of timing related issues we invoke the base
