From 16fc7eb785ed7ef8a534ad22c3c8552cf2c55b01 Mon Sep 17 00:00:00 2001 From: Vaishnavi Prabakaran Date: Wed, 27 Dec 2017 12:46:25 +1100 Subject: [PATCH] Tap test support for backup with tablespace mapping --- src/test/perl/PostgresNode.pm | 73 +++++++++++++++++++++++++++++++++++ src/test/recovery/t/001_stream_rep.pl | 38 +++++++++++++++++- 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/src/test/perl/PostgresNode.pm b/src/test/perl/PostgresNode.pm index 93faadc..90db47a 100644 --- a/src/test/perl/PostgresNode.pm +++ b/src/test/perl/PostgresNode.pm @@ -86,6 +86,7 @@ use Config; use Cwd; use Exporter 'import'; use File::Basename; +use File::Copy; use File::Path qw(rmtree); use File::Spec; use File::Temp (); @@ -510,6 +511,19 @@ sub backup print "# Backup finished\n"; } +sub backup_withtablespace +{ + my ($self, $backup_name, $old_tablespacedir, $new_tablespacedir) = @_; + my $backup_path = $self->backup_dir . '/' . $backup_name; + my $port = $self->port; + my $name = $self->name; + + print "# Taking pg_basebackup $backup_name from node \"$name\"\n"; + TestLib::system_or_bail('pg_basebackup', '-D', $backup_path, '-p', $port, + '--no-sync','-T',"$old_tablespacedir=$new_tablespacedir"); + print "# Backup with tablespace option finished\n"; +} + =item $node->backup_fs_hot(backup_name) Create a backup with a filesystem level copy in subdirectory B of @@ -646,6 +660,65 @@ port = $port =pod +=item $node->convert_backup_to_standby(root_node, backup_name) + +Initialize a node from a backup, which may come from this node or a different +node. root_node must be a PostgresNode reference, backup_name the string name +of a backup previously created on that node with $node->backup. + +Does not start the node after initializing it. + +A recovery.conf is not created. + +Streaming replication can be enabled on this node by passing the keyword +parameter has_streaming => 1. This is disabled by default. + +Restoring WAL segments from archives using restore_command can be enabled +by passing the keyword parameter has_restoring => 1. This is disabled by +default. + +The backup is moved, so backup directory no more exists. pg_hba.conf is +unconditionally set to enable replication connections. + +=cut + +sub convert_backup_to_standby +{ + my ($self, $root_node, $backup_name, %params) = @_; + my $backup_path = $root_node->backup_dir . '/' . $backup_name; + my $port = $self->port; + my $node_name = $self->name; + my $root_name = $root_node->name; + + $params{has_streaming} = 0 unless defined $params{has_streaming}; + $params{has_restoring} = 0 unless defined $params{has_restoring}; + + print +"# Initializing node \"$node_name\" from backup \"$backup_name\" of node \"$root_name\"\n"; + die "Backup \"$backup_name\" does not exist at $backup_path" + unless -d $backup_path; + + mkdir $self->backup_dir; + mkdir $self->archive_dir; + my $data_path = $self->data_dir; + + rmdir($data_path); + move("$backup_path", "$self->{_basedir}/pgdata"); + + chmod(0700, $data_path); + + # Base configuration for this node + $self->append_conf( + 'postgresql.conf', + qq( + port = $port + )); + $self->enable_streaming($root_node) if $params{has_streaming}; + $self->enable_restoring($root_node) if $params{has_restoring}; +} + +=pod + =item $node->start() Wrapper for pg_ctl start diff --git a/src/test/recovery/t/001_stream_rep.pl b/src/test/recovery/t/001_stream_rep.pl index fb27925..5129b66 100644 --- a/src/test/recovery/t/001_stream_rep.pl +++ b/src/test/recovery/t/001_stream_rep.pl @@ -3,7 +3,7 @@ use strict; use warnings; use PostgresNode; use TestLib; -use Test::More tests => 28; +use Test::More tests => 29; # Initialize master node my $node_master = get_new_node('master'); @@ -303,3 +303,39 @@ $node_standby_2->start; get_slot_xmins($node_standby_1, $slotname_2, "xmin IS NULL"); is($xmin, '', 'xmin of cascaded slot reset after startup with hs feedback reset'); + +# Convert backup taken with tablespace mapping to standby +my $master_tablespacedir = "$TestLib::tmp_check/master_securetblspcdir"; +mkdir $master_tablespacedir; +my $standby_tablespacedir = "$TestLib::tmp_check/standby_securetblspcdir"; +mkdir $standby_tablespacedir; +my $backup_with_tablespace='backup_with_tablespace'; + +#Create tablespace with contents + +$node_master->safe_psql('postgres', + "CREATE TABLESPACE testtblspc location '$master_tablespacedir';"); +$node_master->safe_psql('postgres', + "CREATE TABLE test_table(id integer,name varchar(20)) TABLESPACE testtblspc;"); +$node_master->safe_psql('postgres', + "INSERT INTO test_table VALUES(generate_series(1,10),'some_name');"); + +# Backup master with tablespace option +$node_master->backup_withtablespace($backup_with_tablespace,$master_tablespacedir,$standby_tablespacedir); + +# Create streaming standby linking to master +my $node_standby_3 = get_new_node('standby_3'); + +$node_standby_3->convert_backup_to_standby($node_master, $backup_name, + has_streaming => 1); +print "standby 3 setup success\n"; +# Start the standby +$node_standby_3->start; + +print "standby 3 start success\n"; + +# Test inserted values in standby. +$result = + $node_standby_3->safe_psql('postgres', "SELECT count(*) FROM test_table"); +print "standby 3: $result\n"; +is($result, qq(10), 'check streamed content on standby 3'); -- 2.7.4.windows.1