Skip to content

Commit 8a7e9e9

Browse files
author
Amit Kapila
committed
Add tests for '-f' option in dropdb utility.
This will test that the force option works when there is an active backend connected to the database being dropped. Author: Pavel Stehule and Vignesh C Reviewed-by: Amit Kapila and Vignesh C Discussion: https://postgr.es/m/CAP_rwwmLJJbn70vLOZFpxGw3XD7nLB_7+NKz46H5EOO2k5H7OQ@mail.gmail.com
1 parent 290acac commit 8a7e9e9

File tree

2 files changed

+105
-7
lines changed

2 files changed

+105
-7
lines changed

src/bin/scripts/t/050_dropdb.pl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use PostgresNode;
55
use TestLib;
6-
use Test::More tests => 13;
6+
use Test::More tests => 11;
77

88
program_help_ok('dropdb');
99
program_version_ok('dropdb');
@@ -19,11 +19,5 @@
1919
qr/statement: DROP DATABASE foobar1/,
2020
'SQL DROP DATABASE run');
2121

22-
$node->safe_psql('postgres', 'CREATE DATABASE foobar2');
23-
$node->issues_sql_like(
24-
[ 'dropdb', '--force', 'foobar2' ],
25-
qr/statement: DROP DATABASE foobar2 WITH \(FORCE\);/,
26-
'SQL DROP DATABASE (FORCE) run');
27-
2822
$node->command_fails([ 'dropdb', 'nonexistent' ],
2923
'fails with nonexistent database');

src/bin/scripts/t/051_dropdb_force.pl

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#
2+
# Tests the force option of drop database command.
3+
#
4+
use strict;
5+
use warnings;
6+
7+
use PostgresNode;
8+
use TestLib;
9+
use Test::More tests => 9;
10+
11+
# To avoid hanging while expecting some specific input from a psql
12+
# instance being driven by us, add a timeout high enough that it
13+
# should never trigger even on very slow machines, unless something
14+
# is really wrong.
15+
my $psql_timeout = IPC::Run::timer(60);
16+
17+
my $node = get_new_node('master');
18+
$node->init;
19+
$node->start;
20+
21+
# Create a database that will be dropped. This will test that the force
22+
# option works when no other backend is connected to the database being
23+
# dropped.
24+
$node->safe_psql('postgres', 'CREATE DATABASE foobar');
25+
$node->issues_sql_like(
26+
[ 'dropdb', '--force', 'foobar' ],
27+
qr/statement: DROP DATABASE foobar WITH \(FORCE\);/,
28+
'SQL DROP DATABASE (FORCE) run');
29+
30+
# database foobar must not exist.
31+
is( $node->safe_psql(
32+
'postgres',
33+
qq[SELECT EXISTS(SELECT * FROM pg_database WHERE datname='foobar');]
34+
),
35+
'f',
36+
'database foobar was removed');
37+
38+
# Create a database that will be dropped. This will test that the force
39+
# option works when one other backend is connected to the database being
40+
# dropped.
41+
$node->safe_psql('postgres', 'CREATE DATABASE foobar1');
42+
43+
# Run psql, keeping session alive, so we have an alive backend to kill.
44+
my ($killme_stdin, $killme_stdout, $killme_stderr) = ('', '', '');
45+
my $killme = IPC::Run::start(
46+
[
47+
'psql', '-X', '-qAt', '-v', 'ON_ERROR_STOP=1', '-f', '-', '-d',
48+
$node->connstr('foobar1')
49+
],
50+
'<',
51+
\$killme_stdin,
52+
'>',
53+
\$killme_stdout,
54+
'2>',
55+
\$killme_stderr,
56+
$psql_timeout);
57+
58+
# Ensure killme process is active.
59+
$killme_stdin .= q[
60+
SELECT pg_backend_pid();
61+
];
62+
ok( TestLib::pump_until(
63+
$killme, $psql_timeout, \$killme_stdout, qr/[[:digit:]]+[\r\n]$/m),
64+
'acquired pid for SIGTERM');
65+
my $pid = $killme_stdout;
66+
chomp($pid);
67+
$killme_stdout = '';
68+
$killme_stderr = '';
69+
70+
# Check the connections on foobar1 database.
71+
is( $node->safe_psql(
72+
'postgres',
73+
qq[SELECT pid FROM pg_stat_activity WHERE datname='foobar1' AND pid = $pid;]
74+
),
75+
$pid,
76+
'database foobar1 is used');
77+
78+
# Now drop database with dropdb --force command.
79+
$node->issues_sql_like(
80+
[ 'dropdb', '--force', 'foobar1' ],
81+
qr/statement: DROP DATABASE foobar1 WITH \(FORCE\);/,
82+
'SQL DROP DATABASE (FORCE) run');
83+
84+
# Check that psql sees the killed backend as having been terminated.
85+
$killme_stdin .= q[
86+
SELECT 1;
87+
];
88+
ok( TestLib::pump_until(
89+
$killme, $psql_timeout, \$killme_stderr,
90+
qr/FATAL: terminating connection due to administrator command/m),
91+
"psql query died successfully after SIGTERM");
92+
$killme_stderr = '';
93+
$killme_stdout = '';
94+
$killme->finish;
95+
96+
# database foobar1 must not exist.
97+
is( $node->safe_psql(
98+
'postgres',
99+
qq[SELECT EXISTS(SELECT * FROM pg_database WHERE datname='foobar1');]
100+
),
101+
'f',
102+
'database foobar1 was removed');
103+
104+
$node->stop();

0 commit comments

Comments
 (0)