On Thu, Nov 28, 2019 at 8:54 AM Amit Kapila <amit.kapil...@gmail.com> wrote:
>
> On Wed, Nov 27, 2019 at 10:15 AM vignesh C <vignes...@gmail.com> wrote:
> >
> >
> > Attached patch has the fixes for the above comments.
> >
>
> I have pushed the refactoring patch.  In the second patch, I have a
> few more comments.  I am not completely sure if it is a good idea to
> write a new test file 060_dropdb_force.pl when we already have an
> existing file 050_dropdb.pl for dropdb tests, but I think if we want
> to do that, then lets move existing test for dropdb '-f' from
> 050_dropdb.pl to new file and it might be better to name new file as
> 051_dropdb_force.pl.  I see that in some other cases like vacuumdb and
> clusterdb, we have separate test files to cover a different kinds of
> scenarios, so it should be okay to have a new file for dropdb tests.
>

Thanks for pushing the patch. Please find the attached patch having
the fixes for the comments suggested.

Regards,
Vignesh
EnterpriseDB: http://www.enterprisedb.com
From 21423c5e14a4279722d93866857910cbf86b5bff Mon Sep 17 00:00:00 2001
From: Pavel Stehule <pavel.steh...@gmail.com>, Vignesh
Date: Thu, 28 Nov 2019 09:58:11 +0530
Subject: [PATCH] Add tests for the support of '-f' option in dropdb utility.

Add tests for the support of '-f' option in dropdb utility.
---
 src/bin/scripts/t/050_dropdb.pl       |   8 +--
 src/bin/scripts/t/051_dropdb_force.pl | 100 ++++++++++++++++++++++++++++++++++
 2 files changed, 101 insertions(+), 7 deletions(-)
 create mode 100644 src/bin/scripts/t/051_dropdb_force.pl

diff --git a/src/bin/scripts/t/050_dropdb.pl b/src/bin/scripts/t/050_dropdb.pl
index c51babe..25aa54a 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 0000000..0f32dc5
--- /dev/null
+++ b/src/bin/scripts/t/051_dropdb_force.pl
@@ -0,0 +1,100 @@
+#
+# Tests drop database with force option.
+#
+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 database that will be 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');
+
+# Check database foobar does not exist.
+is( $node->safe_psql(
+		'postgres',
+		qq[SELECT EXISTS(SELECT * FROM pg_database WHERE datname='foobar');]
+	),
+	'f',
+	'database foobar was removed');
+
+# Create database that will be 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;
+
+# Check database foobar1 does not exist.
+is( $node->safe_psql(
+		'postgres',
+		qq[SELECT EXISTS(SELECT * FROM pg_database WHERE datname='foobar1');]
+	),
+	'f',
+	'database foobar1 was removed');
+
+$node->stop();
-- 
1.8.3.1

Reply via email to