summaryrefslogtreecommitdiffstats
path: root/src/bin/pg_ctl/t/003_promote.pl
blob: 84d28f4c77571586978e7e6a66c41e8bb84ec28a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Copyright (c) 2021-2022, PostgreSQL Global Development Group

use strict;
use warnings;

use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;

my $tempdir = PostgreSQL::Test::Utils::tempdir;

command_fails_like(
	[ 'pg_ctl', '-D', "$tempdir/nonexistent", 'promote' ],
	qr/directory .* does not exist/,
	'pg_ctl promote with nonexistent directory');

my $node_primary = PostgreSQL::Test::Cluster->new('primary');
$node_primary->init(allows_streaming => 1);

command_fails_like(
	[ 'pg_ctl', '-D', $node_primary->data_dir, 'promote' ],
	qr/PID file .* does not exist/,
	'pg_ctl promote of not running instance fails');

$node_primary->start;

command_fails_like(
	[ 'pg_ctl', '-D', $node_primary->data_dir, 'promote' ],
	qr/not in standby mode/,
	'pg_ctl promote of primary instance fails');

my $node_standby = PostgreSQL::Test::Cluster->new('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', '-D', $node_standby->data_dir, '-W', 'promote' ],
	'pg_ctl -W promote of standby runs');

ok( $node_standby->poll_query_until(
		'postgres', 'SELECT NOT pg_is_in_recovery()'),
	'promoted standby is not in recovery');

# same again with default wait option
$node_standby = PostgreSQL::Test::Cluster->new('standby2');
$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', '-D', $node_standby->data_dir, 'promote' ],
	'pg_ctl promote of standby runs');

# no wait here

is($node_standby->safe_psql('postgres', 'SELECT pg_is_in_recovery()'),
	'f', 'promoted standby is not in recovery');

done_testing();