From 7c815b425612dd3682ddd02353e3862815798119 Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Date: Wed, 13 Nov 2024 11:41:45 +0530
Subject: [PATCH 3/3] Add PostreSQL::Test::Utils::compare_dumps()

Multiple tests compare pg_dump outputs taken from two clusters in plain format
as a way to compare the contents of those clusters. Standardize and modularize
that process into a routine.

Author: Ashutosh Bapat (ashutosh.bapat.oss@gmail.com) per suggestion by Michael Pacquier
Discussion: https://www.postgresql.org/message-id/CAExHW5uF5V=Cjecx3_Z=7xfh4rg2Wf61PT+hfquzjBqouRzQJQ@mail.gmail.com
---
 .../pg_combinebackup/t/002_compare_backups.pl | 18 +------
 src/bin/pg_upgrade/t/002_pg_upgrade.pl        |  5 +-
 src/test/perl/PostgreSQL/Test/Utils.pm        | 48 +++++++++++++++++++
 src/test/recovery/t/027_stream_regress.pl     | 14 +++---
 4 files changed, 57 insertions(+), 28 deletions(-)

diff --git a/src/bin/pg_combinebackup/t/002_compare_backups.pl b/src/bin/pg_combinebackup/t/002_compare_backups.pl
index 63a0255de15..23bc8504eb3 100644
--- a/src/bin/pg_combinebackup/t/002_compare_backups.pl
+++ b/src/bin/pg_combinebackup/t/002_compare_backups.pl
@@ -185,22 +185,6 @@ $pitr1->command_ok(
 	'dump from PITR 2');
 
 # Compare the two dumps, there should be no differences.
-my $compare_res = compare($dump1, $dump2);
-note($dump1);
-note($dump2);
-is($compare_res, 0, "dumps are identical");
-
-# Provide more context if the dumps do not match.
-if ($compare_res != 0)
-{
-	my ($stdout, $stderr) =
-	  run_command([ 'diff', '-u', $dump1, $dump2 ]);
-	print "=== diff of $dump1 and $dump2\n";
-	print "=== stdout ===\n";
-	print $stdout;
-	print "=== stderr ===\n";
-	print $stderr;
-	print "=== EOF ===\n";
-}
+compare_dumps($dump1, $dump2, "dumps are identical");
 
 done_testing();
diff --git a/src/bin/pg_upgrade/t/002_pg_upgrade.pl b/src/bin/pg_upgrade/t/002_pg_upgrade.pl
index 35d3d3970d8..216574dc6d6 100644
--- a/src/bin/pg_upgrade/t/002_pg_upgrade.pl
+++ b/src/bin/pg_upgrade/t/002_pg_upgrade.pl
@@ -6,9 +6,8 @@ use warnings FATAL => 'all';
 
 use Cwd            qw(abs_path);
 use File::Basename qw(dirname);
-use File::Compare;
-use File::Find qw(find);
-use File::Path qw(rmtree);
+use File::Find     qw(find);
+use File::Path     qw(rmtree);
 
 use PostgreSQL::Test::Cluster;
 use PostgreSQL::Test::Utils;
diff --git a/src/test/perl/PostgreSQL/Test/Utils.pm b/src/test/perl/PostgreSQL/Test/Utils.pm
index 022b44ba22b..6efe5faf77d 100644
--- a/src/test/perl/PostgreSQL/Test/Utils.pm
+++ b/src/test/perl/PostgreSQL/Test/Utils.pm
@@ -50,6 +50,7 @@ use Cwd;
 use Exporter 'import';
 use Fcntl qw(:mode :seek);
 use File::Basename;
+use File::Compare;
 use File::Find;
 use File::Spec;
 use File::stat qw(stat);
@@ -89,6 +90,8 @@ our @EXPORT = qw(
   command_fails_like
   command_checks_all
 
+  compare_dumps
+
   $windows_os
   $is_msys2
   $use_unix_sockets
@@ -1081,6 +1084,51 @@ sub command_checks_all
 
 =pod
 
+=item compare_dumps(dump1, dump2, testname)
+
+Test that the given two files match. The files usually contain pg_dump output in
+"plain" format. Output the difference if any.
+
+=over
+
+=item C<dump1> and C<dump2>: Dump files to compare
+
+=item C<testname>: test name
+
+=back
+
+=cut
+
+sub compare_dumps
+{
+	my ($dump1, $dump2, $testname) = @_;
+
+	my $compare_res = compare($dump1, $dump2);
+	is($compare_res, 0, $testname);
+
+	# Provide more context
+	if ($compare_res != 0)
+	{
+		my ($stdout, $stderr) =
+		  run_command([ 'diff', '-u', $dump1, $dump2 ]);
+		print "=== diff of $dump1 and $dump2\n";
+		print "=== stdout ===\n";
+		print $stdout;
+		print "=== stderr ===\n";
+		print $stderr;
+		print "=== EOF ===\n";
+	}
+	else
+	{
+		note('first dump file: ' . $dump1);
+		note('second dump file: ' . $dump2);
+	}
+
+	return;
+}
+
+=pod
+
 =back
 
 =cut
diff --git a/src/test/recovery/t/027_stream_regress.pl b/src/test/recovery/t/027_stream_regress.pl
index d1ae32d97d6..b5ea1356751 100644
--- a/src/test/recovery/t/027_stream_regress.pl
+++ b/src/test/recovery/t/027_stream_regress.pl
@@ -116,8 +116,9 @@ command_ok(
 		'--no-sync', '-p', $node_standby_1->port
 	],
 	'dump standby server');
-command_ok(
-	[ 'diff', $outputdir . '/primary.dump', $outputdir . '/standby.dump' ],
+compare_dumps(
+	$outputdir . '/primary.dump',
+	$outputdir . '/standby.dump',
 	'compare primary and standby dumps');
 
 # Likewise for the catalogs of the regression database, after disabling
@@ -146,12 +147,9 @@ command_ok(
 		'regression'
 	],
 	'dump catalogs of standby server');
-command_ok(
-	[
-		'diff',
-		$outputdir . '/catalogs_primary.dump',
-		$outputdir . '/catalogs_standby.dump'
-	],
+compare_dumps(
+	$outputdir . '/catalogs_primary.dump',
+	$outputdir . '/catalogs_standby.dump',
 	'compare primary and standby catalog dumps');
 
 # Check some data from pg_stat_statements.
-- 
2.34.1

