aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAmit Kapila <akapila@postgresql.org>2019-11-28 11:46:57 +0530
committerAmit Kapila <akapila@postgresql.org>2019-11-28 11:46:57 +0530
commit8a7e9e9dad56419ff987e5f6baaf411a03c1951a (patch)
treed919c41a31d503931acc148f96f98da8df0fec59 /src
parent290acac92b1d7bebb4ec68fe8b7a5cb442333eda (diff)
downloadpostgresql-8a7e9e9dad56419ff987e5f6baaf411a03c1951a.tar.gz
postgresql-8a7e9e9dad56419ff987e5f6baaf411a03c1951a.zip
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
Diffstat (limited to 'src')
-rw-r--r--src/bin/scripts/t/050_dropdb.pl8
-rw-r--r--src/bin/scripts/t/051_dropdb_force.pl104
2 files changed, 105 insertions, 7 deletions
diff --git a/src/bin/scripts/t/050_dropdb.pl b/src/bin/scripts/t/050_dropdb.pl
index c51babe093f..25aa54a4ae4 100644
--- a/src/bin/scripts/t/050_dropdb.pl
+++ b/src/bin/scripts/t/050_dropdb.pl
@@ -3,7 +3,7 @@ use warnings;
use PostgresNode;
use TestLib;
-use Test::More tests => 13;
+use Test::More tests => 11;
program_help_ok('dropdb');
program_version_ok('dropdb');
@@ -19,11 +19,5 @@ $node->issues_sql_like(
qr/statement: DROP DATABASE foobar1/,
'SQL DROP DATABASE run');
-$node->safe_psql('postgres', 'CREATE DATABASE foobar2');
-$node->issues_sql_like(
- [ 'dropdb', '--force', 'foobar2' ],
- qr/statement: DROP DATABASE foobar2 WITH \(FORCE\);/,
- 'SQL DROP DATABASE (FORCE) run');
-
$node->command_fails([ 'dropdb', 'nonexistent' ],
'fails with nonexistent database');
diff --git a/src/bin/scripts/t/051_dropdb_force.pl b/src/bin/scripts/t/051_dropdb_force.pl
new file mode 100644
index 00000000000..a252b431d6e
--- /dev/null
+++ b/src/bin/scripts/t/051_dropdb_force.pl
@@ -0,0 +1,104 @@
+#
+# Tests the force option of drop database command.
+#
+use strict;
+use warnings;
+
+use PostgresNode;
+use TestLib;
+use Test::More tests => 9;
+
+# To avoid hanging while expecting some specific input from a psql
+# instance being driven by us, add a timeout high enough that it
+# should never trigger even on very slow machines, unless something
+# is really wrong.
+my $psql_timeout = IPC::Run::timer(60);
+
+my $node = get_new_node('master');
+$node->init;
+$node->start;
+
+# Create a database that will be dropped. This will test that the force
+# option works when no other backend is connected to the database being
+# dropped.
+$node->safe_psql('postgres', 'CREATE DATABASE foobar');
+$node->issues_sql_like(
+ [ 'dropdb', '--force', 'foobar' ],
+ qr/statement: DROP DATABASE foobar WITH \(FORCE\);/,
+ 'SQL DROP DATABASE (FORCE) run');
+
+# database foobar must not exist.
+is( $node->safe_psql(
+ 'postgres',
+ qq[SELECT EXISTS(SELECT * FROM pg_database WHERE datname='foobar');]
+ ),
+ 'f',
+ 'database foobar was removed');
+
+# Create a database that will be dropped. This will test that the force
+# option works when one other backend is connected to the database being
+# dropped.
+$node->safe_psql('postgres', 'CREATE DATABASE foobar1');
+
+# Run psql, keeping session alive, so we have an alive backend to kill.
+my ($killme_stdin, $killme_stdout, $killme_stderr) = ('', '', '');
+my $killme = IPC::Run::start(
+ [
+ 'psql', '-X', '-qAt', '-v', 'ON_ERROR_STOP=1', '-f', '-', '-d',
+ $node->connstr('foobar1')
+ ],
+ '<',
+ \$killme_stdin,
+ '>',
+ \$killme_stdout,
+ '2>',
+ \$killme_stderr,
+ $psql_timeout);
+
+# Ensure killme process is active.
+$killme_stdin .= q[
+SELECT pg_backend_pid();
+];
+ok( TestLib::pump_until(
+ $killme, $psql_timeout, \$killme_stdout, qr/[[:digit:]]+[\r\n]$/m),
+ 'acquired pid for SIGTERM');
+my $pid = $killme_stdout;
+chomp($pid);
+$killme_stdout = '';
+$killme_stderr = '';
+
+# Check the connections on foobar1 database.
+is( $node->safe_psql(
+ 'postgres',
+ qq[SELECT pid FROM pg_stat_activity WHERE datname='foobar1' AND pid = $pid;]
+ ),
+ $pid,
+ 'database foobar1 is used');
+
+# Now drop database with dropdb --force command.
+$node->issues_sql_like(
+ [ 'dropdb', '--force', 'foobar1' ],
+ qr/statement: DROP DATABASE foobar1 WITH \(FORCE\);/,
+ 'SQL DROP DATABASE (FORCE) run');
+
+# Check that psql sees the killed backend as having been terminated.
+$killme_stdin .= q[
+SELECT 1;
+];
+ok( TestLib::pump_until(
+ $killme, $psql_timeout, \$killme_stderr,
+ qr/FATAL: terminating connection due to administrator command/m),
+ "psql query died successfully after SIGTERM");
+$killme_stderr = '';
+$killme_stdout = '';
+$killme->finish;
+
+# database foobar1 must not exist.
+is( $node->safe_psql(
+ 'postgres',
+ qq[SELECT EXISTS(SELECT * FROM pg_database WHERE datname='foobar1');]
+ ),
+ 'f',
+ 'database foobar1 was removed');
+
+$node->stop();