From 9af968e92d8989e0a1aee3072cf316c559755c27 Mon Sep 17 00:00:00 2001 From: Hayato Kuroda Date: Fri, 16 Feb 2024 09:04:47 +0000 Subject: [PATCH v23 11/13] Update test codes --- .../t/040_pg_createsubscriber.pl | 2 +- .../t/041_pg_createsubscriber_standby.pl | 197 +++++++++--------- 2 files changed, 105 insertions(+), 94 deletions(-) diff --git a/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl b/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl index 95eb4e70ac..65eba6f623 100644 --- a/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl +++ b/src/bin/pg_basebackup/t/040_pg_createsubscriber.pl @@ -5,7 +5,7 @@ # use strict; -use warnings; +use warnings FATAL => 'all'; use PostgreSQL::Test::Utils; use Test::More; diff --git a/src/bin/pg_basebackup/t/041_pg_createsubscriber_standby.pl b/src/bin/pg_basebackup/t/041_pg_createsubscriber_standby.pl index 93148417db..06ef05d5e8 100644 --- a/src/bin/pg_basebackup/t/041_pg_createsubscriber_standby.pl +++ b/src/bin/pg_basebackup/t/041_pg_createsubscriber_standby.pl @@ -4,26 +4,23 @@ # Test using a standby server as the subscriber. use strict; -use warnings; +use warnings FATAL => 'all'; use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; -my $node_p; -my $node_f; -my $node_s; -my $node_c; -my $result; -my $slotname; - # Set up node P as primary -$node_p = PostgreSQL::Test::Cluster->new('node_p'); +my $node_p = PostgreSQL::Test::Cluster->new('node_p'); $node_p->init(allows_streaming => 'logical'); $node_p->start; -# Set up node F as about-to-fail node -# Force it to initialize a new cluster instead of copying a -# previously initdb'd cluster. +# ------------------------------ +# Check pg_createsubscriber fails when the target server is not a +# standby of the source. +# +# Set up node F as about-to-fail node. Force it to initialize a new cluster +# instead of copying a previously initdb'd cluster. +my $node_f; { local $ENV{'INITDB_TEMPLATE'} = undef; @@ -32,112 +29,91 @@ $node_p->start; $node_f->start; } -# On node P -# - create databases -# - create test tables -# - insert a row -# - create a physical replication slot -$node_p->safe_psql( - 'postgres', q( - CREATE DATABASE pg1; - CREATE DATABASE pg2; -)); -$node_p->safe_psql('pg1', 'CREATE TABLE tbl1 (a text)'); -$node_p->safe_psql('pg1', "INSERT INTO tbl1 VALUES('first row')"); -$node_p->safe_psql('pg2', 'CREATE TABLE tbl2 (a text)'); -$slotname = 'physical_slot'; -$node_p->safe_psql('pg2', - "SELECT pg_create_physical_replication_slot('$slotname')"); +# Run pg_createsubscriber on about-to-fail node F +command_checks_all( + [ + 'pg_createsubscriber', '--verbose', '--pgdata', $node_f->data_dir, + '--publisher-server', $node_p->connstr('postgres'), + '--port', $node_f->port, '--socketdir', $node_f->host, + '--database', 'postgres' + ], + 1, + [qr//], + [ + qr/subscriber data directory is not a copy of the source database cluster/ + ], + 'subscriber data directory is not a copy of the source database cluster'); +# ------------------------------ +# Check pg_createsubscriber fails when the target server is not running +# # Set up node S as standby linking to node P $node_p->backup('backup_1'); -$node_s = PostgreSQL::Test::Cluster->new('node_s'); +my $node_s = PostgreSQL::Test::Cluster->new('node_s'); $node_s->init_from_backup($node_p, 'backup_1', has_streaming => 1); -$node_s->append_conf( - 'postgresql.conf', qq[ -log_min_messages = debug2 -primary_slot_name = '$slotname' -]); $node_s->set_standby_mode(); -# Run pg_createsubscriber on about-to-fail node F -command_fails( - [ - 'pg_createsubscriber', '--verbose', - '--pgdata', $node_f->data_dir, - '--publisher-server', $node_p->connstr('pg1'), - '--port', $node_f->port, - '--host', $node_f->host, - '--database', 'pg1', - '--database', 'pg2' - ], - 'subscriber data directory is not a copy of the source database cluster'); - # Run pg_createsubscriber on the stopped node -command_fails( +command_checks_all( [ - 'pg_createsubscriber', '--verbose', - '--dry-run', '--pgdata', - $node_s->data_dir, '--publisher-server', - $node_p->connstr('pg1'), '--port', - $node_s->port, '--host', - $node_s->host, '--database', - 'pg1', '--database', - 'pg2' + 'pg_createsubscriber', '--verbose', '--pgdata', $node_s->data_dir, + '--publisher-server', $node_p->connstr('postgres'), + '--port', $node_s->port, '--socketdir', $node_s->host, + '--database', 'postgres' ], + 1, + [qr//], + [qr/standby is not running/], 'target server must be running'); $node_s->start; +# ------------------------------ +# Check pg_createsubscriber fails when the target server is a member of +# the cascading standby. +# # Set up node C as standby linking to node S $node_s->backup('backup_2'); -$node_c = PostgreSQL::Test::Cluster->new('node_c'); +my $node_c = PostgreSQL::Test::Cluster->new('node_c'); $node_c->init_from_backup($node_s, 'backup_2', has_streaming => 1); -$node_c->append_conf( - 'postgresql.conf', qq[ -log_min_messages = debug2 -]); $node_c->set_standby_mode(); $node_c->start; # Run pg_createsubscriber on node C (P -> S -> C) -command_fails( +command_checks_all( [ - 'pg_createsubscriber', '--verbose', - '--dry-run', '--pgdata', - $node_c->data_dir, '--publisher-server', - $node_s->connstr('pg1'), - '--port', $node_c->port, - '--socketdir', $node_c->host, - '--database', 'pg1', - '--database', 'pg2' + 'pg_createsubscriber', '--verbose', '--pgdata', $node_c->data_dir, + '--publisher-server', $node_s->connstr('postgres'), + '--port', $node_c->port, '--socketdir', $node_c->host, + '--database', 'postgres' ], - 'primary server is in recovery'); + 1, + [qr//], + [qr/primary server cannot be in recovery/], + 'target server must be running'); # Stop node C -$node_c->teardown_node; - -# Insert another row on node P and wait node S to catch up -$node_p->safe_psql('pg1', "INSERT INTO tbl1 VALUES('second row')"); -$node_p->wait_for_replay_catchup($node_s); +$node_c->stop; -# dry run mode on node S +# ------------------------------ +# Check successful dry-run +# +# Dry run mode on node S command_ok( [ 'pg_createsubscriber', '--verbose', '--dry-run', '--pgdata', $node_s->data_dir, '--publisher-server', - $node_p->connstr('pg1'), '--port', - $node_s->port, '--socketdir', - $node_s->host, '--database', - 'pg1', '--database', - 'pg2' + $node_p->connstr('postgres'), + '--port', $node_s->port, + '--socketdir', $node_s->host, + '--database', 'postgres' ], 'run pg_createsubscriber --dry-run on node S'); # Check if node S is still a standby -is($node_s->safe_psql('postgres', 'SELECT pg_catalog.pg_is_in_recovery()'), - 't', 'standby is in recovery'); +my $result = $node_s->safe_psql('postgres', 'SELECT pg_catalog.pg_is_in_recovery()'); +is($result, 't', 'standby is in recovery'); # pg_createsubscriber can run without --databases option command_ok( @@ -145,12 +121,39 @@ command_ok( 'pg_createsubscriber', '--verbose', '--dry-run', '--pgdata', $node_s->data_dir, '--publisher-server', - $node_p->connstr('pg1'), '--port', + $node_p->connstr('postgres'), '--port', $node_s->port, '--socketdir', $node_s->host, ], 'run pg_createsubscriber without --databases'); +# ------------------------------ +# Check successful conversion +# +# Prepare databases and a physical replication slot +my $slotname = 'physical_slot'; +$node_p->safe_psql( + 'postgres', qq[ + CREATE DATABASE pg1; + CREATE DATABASE pg2; + SELECT pg_create_physical_replication_slot('$slotname'); +]); + +# Use the created slot for physical replication +$node_s->append_conf('postgresql.conf', "primary_slot_name = $slotname"); +$node_s->reload; + +# Prepare tables and initial data on pg1 and pg2 +$node_p->safe_psql( + 'pg1', qq[ + CREATE TABLE tbl1 (a text); + INSERT INTO tbl1 VALUES('first row'); + INSERT INTO tbl1 VALUES('second row') +]); +$node_p->safe_psql('pg2', "CREATE TABLE tbl2 (a text);"); + +$node_p->wait_for_replay_catchup($node_s); + # Run pg_createsubscriber on node S command_ok( [ @@ -176,15 +179,23 @@ is($result, qq(0), 'the physical replication slot used as primary_slot_name has been removed' ); -# Insert rows on P -$node_p->safe_psql('pg1', "INSERT INTO tbl1 VALUES('third row')"); -$node_p->safe_psql('pg2', "INSERT INTO tbl2 VALUES('row 1')"); - # PID sets to undefined because subscriber was stopped behind the scenes. # Start subscriber $node_s->{_pid} = undef; $node_s->start; +# Confirm two subscriptions has been created +$result = $node_s->safe_psql('postgres', + "SELECT count(distinct subdbid) FROM pg_subscription WHERE subname ~ '^pg_createsubscriber_';" +); +is($result, qq(2), + 'Subscriptions has been created to all the specified databases' +); + +# Insert rows on P +$node_p->safe_psql('pg1', "INSERT INTO tbl1 VALUES('third row')"); +$node_p->safe_psql('pg2', "INSERT INTO tbl2 VALUES('row 1')"); + # Get subscription names $result = $node_s->safe_psql( 'postgres', qq( @@ -214,9 +225,9 @@ my $sysid_s = $node_s->safe_psql('postgres', 'SELECT system_identifier FROM pg_control_system()'); ok($sysid_p != $sysid_s, 'system identifier was changed'); -# clean up -$node_p->teardown_node; -$node_s->teardown_node; -$node_f->teardown_node; +# Clean up +$node_p->stop; +$node_s->stop; +$node_f->stop; done_testing(); -- 2.41.0.windows.3