From 1273e4d1ee20bde577f1caf42f5b38d9f5808430 Mon Sep 17 00:00:00 2001
From: Mark Dilger <mark.dilger@enterprisedb.com>
Date: Tue, 6 Apr 2021 21:46:25 -0700
Subject: [PATCH v3 2/2] Adding modules/test_cross_version

This creates a framework for checking interactions between various
installed versions of PostgreSQL.  For now, the only test is that
older clients can connect to a new server, and that a new client can
connect to older servers.  But the framework can easily be extended
with more tests later.
---
 src/test/modules/Makefile                     |   1 +
 src/test/modules/test_cross_version/Makefile  |  14 +++
 src/test/modules/test_cross_version/README    |  15 +++
 .../test_cross_version/t/001_cross_connect.pl |  66 +++++++++++
 .../modules/test_cross_version/versions.dat   |  17 +++
 src/test/perl/CrossVersion.pm                 | 108 ++++++++++++++++++
 6 files changed, 221 insertions(+)
 create mode 100644 src/test/modules/test_cross_version/Makefile
 create mode 100644 src/test/modules/test_cross_version/README
 create mode 100644 src/test/modules/test_cross_version/t/001_cross_connect.pl
 create mode 100644 src/test/modules/test_cross_version/versions.dat
 create mode 100644 src/test/perl/CrossVersion.pm

diff --git a/src/test/modules/Makefile b/src/test/modules/Makefile
index dffc79b2d9..f1a27bc9dd 100644
--- a/src/test/modules/Makefile
+++ b/src/test/modules/Makefile
@@ -15,6 +15,7 @@ SUBDIRS = \
 		  snapshot_too_old \
 		  spgist_name_ops \
 		  test_bloomfilter \
+		  test_cross_version \
 		  test_ddl_deparse \
 		  test_extensions \
 		  test_ginpostinglist \
diff --git a/src/test/modules/test_cross_version/Makefile b/src/test/modules/test_cross_version/Makefile
new file mode 100644
index 0000000000..6a79a283a5
--- /dev/null
+++ b/src/test/modules/test_cross_version/Makefile
@@ -0,0 +1,14 @@
+# src/test/modules/test_cross_version/Makefile
+
+TAP_TESTS = 1
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = src/test/modules/test_cross_version
+top_builddir = ../../../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/src/test/modules/test_cross_version/README b/src/test/modules/test_cross_version/README
new file mode 100644
index 0000000000..5cfaf115da
--- /dev/null
+++ b/src/test/modules/test_cross_version/README
@@ -0,0 +1,15 @@
+The test_cross_version module exists to perform any tests requiring a database
+cluster that has been upgraded from one or more prior versions, or multiple
+database clusters running differing versions of PostgreSQL.  It is not intended
+to be used in production.
+
+Rationale
+=========
+
+Usage
+=====
+
+Author
+======
+
+Mark Dilger <mark.dilger@enterprisedb.com>
diff --git a/src/test/modules/test_cross_version/t/001_cross_connect.pl b/src/test/modules/test_cross_version/t/001_cross_connect.pl
new file mode 100644
index 0000000000..09c805bc89
--- /dev/null
+++ b/src/test/modules/test_cross_version/t/001_cross_connect.pl
@@ -0,0 +1,66 @@
+use strict;
+use warnings;
+
+use Config;
+use PostgresNode;
+use TestLib;
+use Test::More;
+use CrossVersion;
+
+# Read the { node_name => install_path } mapping from the default location, or
+# an alternate location if passed to us via the environment variable.
+my $pathmap = CrossVersion::nodes($ENV{PG_TEST_VERSIONS_FILE});
+
+# A fresh clone of the postgres git repository contains an empty versions.dat
+# file.  Unless that has been edited, or PG_TEST_VERSIONS_FILE defined to point
+# to a non-trivial versions file, there will be no testing for us to do
+plan skip_all => "No older PostgreSQL installation directories supplied"
+	unless keys %$pathmap;
+
+# BAIL_OUT("received mapping: " . join(", ", map { join(" => ", $_, $pathmap->{$_}) } sort keys %$pathmap));
+
+# Ok, we have at least one older installation directory.  Create one node per
+# entry in the mapping.  Note that the more than one node name may map to the
+# same installation directory, but the node names themselves are unique.
+my @nodes = map {
+	PostgresNode->get_new_node($_, install_path => $pathmap->{$_})
+} keys %$pathmap;
+
+# Calculate how many tests we will perform based on the number of nodes.
+plan tests => 2 + 4 * scalar(@nodes);
+
+# Get one node based on the version of postgres under development.  The point
+# of this entire test file is to verify cross-version compatibility between all
+# the other nodes in @nodes against this one.
+my $new = get_new_node('new');
+spin_up($new);
+my $newconnstr = $new->connstr('template1');
+
+for my $old (@nodes)
+{
+	my $oldname = $old->name();
+
+	spin_up($old);
+	my $oldconnstr = $old->connstr('template1');
+
+	# Test the older psql binary can connect to the new server
+	$old->connect_ok('template1', "$oldname psql connects to new server",
+					 host => $new->host, port => $new->port);
+
+	# Test the new psql binary can connect to the older server
+	$new->connect_ok('template1', "new psql connects to $oldname server",
+					 host => $old->host, port => $old->port);
+}
+
+sub spin_up
+{
+	my ($node) = @_;
+	my $name = $node->name;
+
+	$node->init();
+	ok(1, "$name initializes without error");
+	$node->start();
+	my $host = $node->host;
+	my $port = $node->port;
+	ok(1, "$name starts on host $host, port $port without error");
+}
diff --git a/src/test/modules/test_cross_version/versions.dat b/src/test/modules/test_cross_version/versions.dat
new file mode 100644
index 0000000000..5f51e74d7a
--- /dev/null
+++ b/src/test/modules/test_cross_version/versions.dat
@@ -0,0 +1,17 @@
+# When testing one or more older versions of PostgreSQL against the version
+# being developed, append this file with the paths to all installed older
+# PostgreSQL versions you wish to test, one name and path per line.  Lines
+# starting with a '#' are ignored.  Blank lines are ignored.  Leading and
+# trailing whitespace are ignored.  The first non-whitespace token is treated
+# as the node name, and everything else (including embedded whitespace) will be
+# treated as part of the installation path.  Names should be unique.
+#
+# For each PATH, $PATH/bin and $PATH/lib should exist and contain the postgres
+# binaries and library files.  No support exists for installations configured
+# with alternate --bindir or --libdir options.
+#
+# Examples:
+#
+#   8.1    /usr/local/postgres/8.1
+#   9.1    /usr/local/postgres/9.1.24
+#
diff --git a/src/test/perl/CrossVersion.pm b/src/test/perl/CrossVersion.pm
new file mode 100644
index 0000000000..b547ff4d2d
--- /dev/null
+++ b/src/test/perl/CrossVersion.pm
@@ -0,0 +1,108 @@
+
+=pod
+
+=head1 NAME
+
+CrossVersion - class representing multiple PostgreSQL server version information
+
+=head1 SYNOPSIS
+
+  use CrossVersion;
+
+=head1 DESCRIPTION
+
+CrossVersion
+
+=cut
+
+package CrossVersion;
+
+use strict;
+use warnings;
+use Test::More;
+
+sub config_info
+{
+	my ($node) = @_;
+
+	my %config;
+	my $node_name = $node->name();
+
+	local %ENV = $node->_get_env();
+	my $fh = IO::File->new("pg_config |")
+		or BAIL_OUT("Cannot run pg_config for node $node_name: $!");
+	while (my $line = <$fh>)
+	{
+		if ($line =~ m/^(.+?) = (.*)$/)
+		{
+			$config{$1} = $2;
+		}
+		else
+		{
+			die "Cannot parse config output for node $node_name: $line";
+		}
+	}
+
+	%config;
+}
+
+sub nodes
+{
+	my ($versions, %params) = @_;
+	my %result;
+	my %seen;
+
+	$versions = "versions.dat" unless (defined $versions);
+
+	# Parse all name/path pairs
+	my $fh = IO::File->new($versions)
+		or BAIL_OUT("Cannot open \"$versions\" for reading: $!");
+	while (my $line = <$fh>)
+	{
+		# Strip comments
+		$line =~ s/\s*#.*$//;
+
+		# Skip blank lines
+		next unless $line =~ m/\S/;
+
+		# Node names cannot contain whitespace characters
+		my $namere = qr/\S+/;
+
+		# Paths must begin and end with non-whitespace characters
+		my $pathre = qr/\S+(?:.*\S)?/;
+
+		# Data lines should be a name and path, whitespace separated
+		if ($line =~ m/^\s*($namere)\s+($pathre)\s*$/)
+		{
+			my ($node_name, $path) = ($1, $2);
+
+			# PostgresNode will fail with a filesystem error if given the
+			# same name twice.  Complaining here makes it easier for the
+			# user to debug and fix the problem
+			BAIL_OUT(sprintf("%s line %d: duplicate node name: %s: previously appeared on line %d",
+							 $versions, $fh->input_line_number, $node_name, $seen{$node_name}))
+				if ($seen{$node_name});
+
+			# Sanity-check this next directory parsed from the versions file
+			BAIL_OUT(sprintf("%s line %d: directory not found: %s",
+							 $versions, $fh->input_line_number, $path))
+				unless (-d $path);
+			BAIL_OUT(sprintf("%s line %d: directory does not appear to be a postgresql installation: %s",
+							 $versions, $fh->input_line_number, $path))
+				unless (-d "$path/bin" and -x "$path/bin/pg_config");
+
+			$result{$node_name} = $path;
+			$seen{$node_name} = $fh->input_line_number;
+		}
+		else
+		{
+			BAIL_OUT(sprintf("syntax error: %s line %d: $line",
+							 $versions, $fh->input_line_number));
+		}
+	}
+	$fh->close;
+
+	return \%result;
+}
+
+1;
-- 
2.21.1 (Apple Git-122.3)

