From: Peter Eisentraut Date: Tue, 26 Jul 2016 10:48:43 -0400 Subject: [PATCH 2/5] pg_ctl: Add tests for promote action --- src/bin/pg_ctl/t/003_promote.pl | 41 +++++++++++++++++++++++++++++++++++++++++ src/test/perl/TestLib.pm | 11 +++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/bin/pg_ctl/t/003_promote.pl diff --git a/src/bin/pg_ctl/t/003_promote.pl b/src/bin/pg_ctl/t/003_promote.pl new file mode 100644 index 0000000..d7e7b99 --- /dev/null +++ b/src/bin/pg_ctl/t/003_promote.pl @@ -0,0 +1,41 @@ +use strict; +use warnings; + +use PostgresNode; +use TestLib; +use Test::More tests => 9; + +my $tempdir = TestLib::tempdir; + +command_fails_like([ 'pg_ctl', 'promote', '-D', "$tempdir/nonexistent" ], + qr/directory .* does not exist/, + 'pg_ctl promote with nonexistent directory'); + +my $node_primary = get_new_node('primary'); +$node_primary->init(allows_streaming => 1); + +command_fails_like([ 'pg_ctl', 'promote', '-D', $node_primary->data_dir ], + qr/PID file .* does not exist/, + 'pg_ctl promote of not running instance fails'); + +$node_primary->start; + +command_fails_like([ 'pg_ctl', 'promote', '-D', $node_primary->data_dir ], + qr/not in standby mode/, + 'pg_ctl promote of primary instance fails'); + +my $node_standby = get_new_node('standby'); +$node_primary->backup('my_backup'); +$node_standby->init_from_backup($node_primary, 'my_backup', has_streaming => 1); +$node_standby->start; + +is($node_standby->safe_psql('postgres', 'SELECT pg_is_in_recovery()'), + 't', 'standby is in recovery'); + +command_ok([ 'pg_ctl', 'promote', '-D', $node_standby->data_dir ], + 'pg_ctl promote of standby runs'); + +$node_standby->poll_query_until('postgres', 'SELECT NOT pg_is_in_recovery()'); + +is($node_standby->safe_psql('postgres', 'SELECT pg_is_in_recovery()'), + 'f', 'promoted standby is not in recovery'); diff --git a/src/test/perl/TestLib.pm b/src/test/perl/TestLib.pm index c7b3262..51b533e 100644 --- a/src/test/perl/TestLib.pm +++ b/src/test/perl/TestLib.pm @@ -34,6 +34,7 @@ our @EXPORT = qw( program_version_ok program_options_handling_ok command_like + command_fails_like $windows_os ); @@ -281,4 +282,14 @@ sub command_like like($stdout, $expected_stdout, "$test_name: matches"); } +sub command_fails_like +{ + my ($cmd, $expected_stderr, $test_name) = @_; + my ($stdout, $stderr); + print("# Running: " . join(" ", @{$cmd}) . "\n"); + my $result = IPC::Run::run $cmd, '>', \$stdout, '2>', \$stderr; + ok(!$result, "$test_name: exit code not 0"); + like($stderr, $expected_stderr, "$test_name: matches"); +} + 1; -- 2.9.2 --------------078DFD7E5090B3A6C622472C Content-Type: text/x-patch; name="0003-pg_ctl-Detect-current-standby-state-from-pg_control.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0003-pg_ctl-Detect-current-standby-state-from-pg_control.pat"; filename*1="ch"