1use strict;
2use warnings;
3
4use PostgresNode;
5use TestLib;
6use Test::More tests => 12;
7
8my $tempdir = TestLib::tempdir;
9
10command_fails_like(
11	[ 'pg_ctl', '-D', "$tempdir/nonexistent", 'promote' ],
12	qr/directory .* does not exist/,
13	'pg_ctl promote with nonexistent directory');
14
15my $node_primary = get_new_node('primary');
16$node_primary->init(allows_streaming => 1);
17
18command_fails_like(
19	[ 'pg_ctl', '-D', $node_primary->data_dir, 'promote' ],
20	qr/PID file .* does not exist/,
21	'pg_ctl promote of not running instance fails');
22
23$node_primary->start;
24
25command_fails_like(
26	[ 'pg_ctl', '-D', $node_primary->data_dir, 'promote' ],
27	qr/not in standby mode/,
28	'pg_ctl promote of primary instance fails');
29
30my $node_standby = get_new_node('standby');
31$node_primary->backup('my_backup');
32$node_standby->init_from_backup($node_primary, 'my_backup',
33	has_streaming => 1);
34$node_standby->start;
35
36is($node_standby->safe_psql('postgres', 'SELECT pg_is_in_recovery()'),
37	't', 'standby is in recovery');
38
39command_ok([ 'pg_ctl', '-D', $node_standby->data_dir, '-W', 'promote' ],
40	'pg_ctl -W promote of standby runs');
41
42ok( $node_standby->poll_query_until(
43		'postgres', 'SELECT NOT pg_is_in_recovery()'),
44	'promoted standby is not in recovery');
45
46# same again with default wait option
47$node_standby = get_new_node('standby2');
48$node_standby->init_from_backup($node_primary, 'my_backup',
49	has_streaming => 1);
50$node_standby->start;
51
52is($node_standby->safe_psql('postgres', 'SELECT pg_is_in_recovery()'),
53	't', 'standby is in recovery');
54
55command_ok([ 'pg_ctl', '-D', $node_standby->data_dir, 'promote' ],
56	'pg_ctl promote of standby runs');
57
58# no wait here
59
60is($node_standby->safe_psql('postgres', 'SELECT pg_is_in_recovery()'),
61	'f', 'promoted standby is not in recovery');
62