From 4408c259c6ac09b32c9597386bc6c329165a6ca7 Mon Sep 17 00:00:00 2001
From: Shaoqi Bai <sbai@pivotal.io>
Date: Thu, 21 Mar 2019 23:20:57 +0800
Subject: [PATCH 2/2] Add new test with integration in RewindTest.pm

---
 src/bin/pg_rewind/t/006_tablespace.pl | 83 +++++++++++++++++++++++++++
 src/bin/pg_rewind/t/RewindTest.pm     |  4 +-
 2 files changed, 85 insertions(+), 2 deletions(-)
 create mode 100644 src/bin/pg_rewind/t/006_tablespace.pl

diff --git a/src/bin/pg_rewind/t/006_tablespace.pl b/src/bin/pg_rewind/t/006_tablespace.pl
new file mode 100644
index 0000000000..e4afd3be1d
--- /dev/null
+++ b/src/bin/pg_rewind/t/006_tablespace.pl
@@ -0,0 +1,83 @@
+use strict;
+use warnings;
+use File::Path qw(rmtree);
+use TestLib;
+use Test::More tests => 6;
+
+use FindBin;
+use lib $FindBin::RealBin;
+
+use RewindTest;
+
+my $tempdir = TestLib::tempdir;
+
+sub run_test
+{
+	my $test_mode = shift;
+
+	RewindTest::setup_cluster($test_mode, ['-g']);
+	RewindTest::start_master();
+
+	rmtree("$tempdir/inmaster");
+	rmtree("$tempdir/instandby");
+	rmtree("$tempdir/master_beforepromotion");
+	rmtree("$tempdir/master_afterpromotion");
+	rmtree("$tempdir/standby_afterpromotion");
+
+	mkdir "$tempdir/inmaster";
+	mkdir "$tempdir/instandby";
+
+	# Create a tablespace in master.
+	master_psql("CREATE TABLESPACE inmaster LOCATION '$tempdir/inmaster'");
+
+	RewindTest::create_standby($test_mode, has_tablespace_mapping =>"$tempdir/inmaster=$tempdir/instandby");
+
+	mkdir "$tempdir/master_beforepromotion";
+
+	# Create a tablespace, it has to be droped before doing pg_rewind, or else pg_rewind will fail
+	master_psql("CREATE TABLESPACE master_beforepromotion LOCATION '$tempdir/master_beforepromotion'");
+
+	RewindTest::promote_standby();
+
+	mkdir "$tempdir/master_afterpromotion";
+	mkdir "$tempdir/standby_afterpromotion";
+
+	# Create tablespaces in the old master and the new promoted standby.
+	master_psql("CREATE TABLESPACE master_afterpromotion LOCATION '$tempdir/master_afterpromotion'");
+	standby_psql("CREATE TABLESPACE standby_afterpromotion LOCATION '$tempdir/standby_afterpromotion'");
+	# Drop tablespace in the new promoted standby, because pg_rewind can not handle this case.
+	standby_psql("DROP TABLESPACE standby_afterpromotion");
+
+	# The clusters are now diverged.
+
+	RewindTest::run_pg_rewind($test_mode);
+
+	# Check that the correct databases are present after pg_rewind.
+	check_query(
+		'SELECT spcname FROM pg_tablespace ORDER BY spcname',
+		qq(inmaster
+master_beforepromotion
+pg_default
+pg_global
+),
+		'tablespace names');
+
+	# Permissions on PGDATA should have group permissions
+  SKIP:
+	{
+		skip "unix-style permissions not supported on Windows", 1
+		  if ($windows_os);
+
+		ok(check_mode_recursive($node_master->data_dir(), 0750, 0640),
+			'check PGDATA permissions');
+	}
+
+	RewindTest::clean_rewind_test();
+	return;
+}
+
+# Run the test in both modes.
+run_test('local');
+run_test('remote');
+
+exit(0);
diff --git a/src/bin/pg_rewind/t/RewindTest.pm b/src/bin/pg_rewind/t/RewindTest.pm
index 85cae7e47b..cd106a55dd 100644
--- a/src/bin/pg_rewind/t/RewindTest.pm
+++ b/src/bin/pg_rewind/t/RewindTest.pm
@@ -150,11 +150,11 @@ sub start_master
 
 sub create_standby
 {
-	my $extra_name = shift;
+	my ($extra_name, %params) = @_;
 
 	$node_standby =
 	  get_new_node('standby' . ($extra_name ? "_${extra_name}" : ''));
-	$node_master->backup('my_backup');
+	$node_master->backup('my_backup', %params);
 	$node_standby->init_from_backup($node_master, 'my_backup');
 	my $connstr_master = $node_master->connstr();
 
-- 
2.19.1

