summaryrefslogtreecommitdiff
path: root/src/bin/scripts
diff options
context:
space:
mode:
authorAlvaro Herrera2015-12-02 21:46:16 +0000
committerAlvaro Herrera2015-12-02 21:46:16 +0000
commit1caef31d9e550408d0cbc5788a422dcb69736df5 (patch)
tree451c4745e315c8dba59415a83eb2c4963fded212 /src/bin/scripts
parentc7485a82c3e29103757db75bb9ff8dac597387dc (diff)
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further reuse by creating a new Perl package PostgresNode, which is an object-oriented representation of a single server, with some support routines such as init, start, stop, psql. This serves as a better basis on which to build further test code, and enables writing tests that use more than one server without too much complication. This commit modifies a lot of the existing test files, mostly to remove explicit calls to system commands (pg_ctl) replacing them with method calls of a PostgresNode object. The result is quite a bit more straightforward. Also move some initialization code to BEGIN and INIT blocks instead of having it straight in as top-level code. This commit also introduces package RecursiveCopy so that we can copy whole directories without having to depend on packages that may not be present on vanilla Perl 5.8 installations. I also ran perltidy on the modified files, which changes some code sites that are not otherwise touched by this patch. I tried to avoid this, but it ended up being more trouble than it's worth. Authors: Michael Paquier, Álvaro Herrera Review: Noah Misch
Diffstat (limited to 'src/bin/scripts')
-rw-r--r--src/bin/scripts/t/010_clusterdb.pl22
-rw-r--r--src/bin/scripts/t/011_clusterdb_all.pl13
-rw-r--r--src/bin/scripts/t/020_createdb.pl14
-rw-r--r--src/bin/scripts/t/030_createlang.pl19
-rw-r--r--src/bin/scripts/t/040_createuser.pl18
-rw-r--r--src/bin/scripts/t/050_dropdb.pl14
-rw-r--r--src/bin/scripts/t/060_droplang.pl11
-rw-r--r--src/bin/scripts/t/070_dropuser.pl14
-rw-r--r--src/bin/scripts/t/080_pg_isready.pl9
-rw-r--r--src/bin/scripts/t/090_reindexdb.pl23
-rw-r--r--src/bin/scripts/t/091_reindexdb_all.pl10
-rw-r--r--src/bin/scripts/t/100_vacuumdb.pl17
-rw-r--r--src/bin/scripts/t/101_vacuumdb_all.pl10
-rw-r--r--src/bin/scripts/t/102_vacuumdb_stages.pl13
14 files changed, 126 insertions, 81 deletions
diff --git a/src/bin/scripts/t/010_clusterdb.pl b/src/bin/scripts/t/010_clusterdb.pl
index dc0d78a27d3..5131b35d82b 100644
--- a/src/bin/scripts/t/010_clusterdb.pl
+++ b/src/bin/scripts/t/010_clusterdb.pl
@@ -1,5 +1,7 @@
use strict;
use warnings;
+
+use PostgresNode;
use TestLib;
use Test::More tests => 13;
@@ -7,20 +9,22 @@ program_help_ok('clusterdb');
program_version_ok('clusterdb');
program_options_handling_ok('clusterdb');
-my $tempdir = tempdir;
-start_test_server $tempdir;
+my $node = get_new_node();
+$node->init;
+$node->start;
-issues_sql_like(
- [ 'clusterdb', 'postgres' ],
+$node->issues_sql_like(
+ ['clusterdb'],
qr/statement: CLUSTER;/,
'SQL CLUSTER run');
-command_fails([ 'clusterdb', '-t', 'nonexistent', 'postgres' ],
+$node->command_fails([ 'clusterdb', '-t', 'nonexistent' ],
'fails with nonexistent table');
-psql 'postgres',
-'CREATE TABLE test1 (a int); CREATE INDEX test1x ON test1 (a); CLUSTER test1 USING test1x';
-issues_sql_like(
- [ 'clusterdb', '-t', 'test1', 'postgres' ],
+$node->psql('postgres',
+'CREATE TABLE test1 (a int); CREATE INDEX test1x ON test1 (a); CLUSTER test1 USING test1x'
+);
+$node->issues_sql_like(
+ [ 'clusterdb', '-t', 'test1' ],
qr/statement: CLUSTER test1;/,
'cluster specific table');
diff --git a/src/bin/scripts/t/011_clusterdb_all.pl b/src/bin/scripts/t/011_clusterdb_all.pl
index 7769f70bb15..15cd30cf4a1 100644
--- a/src/bin/scripts/t/011_clusterdb_all.pl
+++ b/src/bin/scripts/t/011_clusterdb_all.pl
@@ -1,12 +1,19 @@
use strict;
use warnings;
+
+use PostgresNode;
use TestLib;
use Test::More tests => 2;
-my $tempdir = tempdir;
-start_test_server $tempdir;
+my $node = get_new_node();
+$node->init;
+$node->start;
+
+# clusterdb -a is not compatible with -d, hence enforce environment variable
+# correctly.
+$ENV{PGDATABASE} = 'postgres';
-issues_sql_like(
+$node->issues_sql_like(
[ 'clusterdb', '-a' ],
qr/statement: CLUSTER.*statement: CLUSTER/s,
'cluster all databases');
diff --git a/src/bin/scripts/t/020_createdb.pl b/src/bin/scripts/t/020_createdb.pl
index a44283c9458..e0cf8600585 100644
--- a/src/bin/scripts/t/020_createdb.pl
+++ b/src/bin/scripts/t/020_createdb.pl
@@ -1,5 +1,7 @@
use strict;
use warnings;
+
+use PostgresNode;
use TestLib;
use Test::More tests => 13;
@@ -7,16 +9,18 @@ program_help_ok('createdb');
program_version_ok('createdb');
program_options_handling_ok('createdb');
-my $tempdir = tempdir;
-start_test_server $tempdir;
+my $node = get_new_node();
+$node->init;
+$node->start;
-issues_sql_like(
+$node->issues_sql_like(
[ 'createdb', 'foobar1' ],
qr/statement: CREATE DATABASE foobar1/,
'SQL CREATE DATABASE run');
-issues_sql_like(
+$node->issues_sql_like(
[ 'createdb', '-l', 'C', '-E', 'LATIN1', '-T', 'template0', 'foobar2' ],
qr/statement: CREATE DATABASE foobar2 ENCODING 'LATIN1'/,
'create database with encoding');
-command_fails([ 'createdb', 'foobar1' ], 'fails if database already exists');
+$node->command_fails([ 'createdb', 'foobar1' ],
+ 'fails if database already exists');
diff --git a/src/bin/scripts/t/030_createlang.pl b/src/bin/scripts/t/030_createlang.pl
index 7ff0a3ed38d..4097f03da99 100644
--- a/src/bin/scripts/t/030_createlang.pl
+++ b/src/bin/scripts/t/030_createlang.pl
@@ -1,5 +1,7 @@
use strict;
use warnings;
+
+use PostgresNode;
use TestLib;
use Test::More tests => 14;
@@ -7,18 +9,17 @@ program_help_ok('createlang');
program_version_ok('createlang');
program_options_handling_ok('createlang');
-my $tempdir = tempdir;
-start_test_server $tempdir;
+my $node = get_new_node();
+$node->init;
+$node->start;
-command_fails(
- [ 'createlang', 'plpgsql', 'postgres' ],
+$node->command_fails([ 'createlang', 'plpgsql' ],
'fails if language already exists');
-psql 'postgres', 'DROP EXTENSION plpgsql';
-issues_sql_like(
- [ 'createlang', 'plpgsql', 'postgres' ],
+$node->psql('postgres', 'DROP EXTENSION plpgsql');
+$node->issues_sql_like(
+ [ 'createlang', 'plpgsql' ],
qr/statement: CREATE EXTENSION "plpgsql"/,
'SQL CREATE EXTENSION run');
-command_like([ 'createlang', '--list', 'postgres' ],
- qr/plpgsql/, 'list output');
+$node->command_like([ 'createlang', '--list' ], qr/plpgsql/, 'list output');
diff --git a/src/bin/scripts/t/040_createuser.pl b/src/bin/scripts/t/040_createuser.pl
index 4d44e14b7cf..fcada6338c6 100644
--- a/src/bin/scripts/t/040_createuser.pl
+++ b/src/bin/scripts/t/040_createuser.pl
@@ -1,5 +1,7 @@
use strict;
use warnings;
+
+use PostgresNode;
use TestLib;
use Test::More tests => 17;
@@ -7,24 +9,26 @@ program_help_ok('createuser');
program_version_ok('createuser');
program_options_handling_ok('createuser');
-my $tempdir = tempdir;
-start_test_server $tempdir;
+my $node = get_new_node();
+$node->init;
+$node->start;
-issues_sql_like(
+$node->issues_sql_like(
[ 'createuser', 'user1' ],
qr/statement: CREATE ROLE user1 NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN;/,
'SQL CREATE USER run');
-issues_sql_like(
+$node->issues_sql_like(
[ 'createuser', '-L', 'role1' ],
qr/statement: CREATE ROLE role1 NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT NOLOGIN;/,
'create a non-login role');
-issues_sql_like(
+$node->issues_sql_like(
[ 'createuser', '-r', 'user2' ],
qr/statement: CREATE ROLE user2 NOSUPERUSER NOCREATEDB CREATEROLE INHERIT LOGIN;/,
'create a CREATEROLE user');
-issues_sql_like(
+$node->issues_sql_like(
[ 'createuser', '-s', 'user3' ],
qr/statement: CREATE ROLE user3 SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;/,
'create a superuser');
-command_fails([ 'createuser', 'user1' ], 'fails if role already exists');
+$node->command_fails([ 'createuser', 'user1' ],
+ 'fails if role already exists');
diff --git a/src/bin/scripts/t/050_dropdb.pl b/src/bin/scripts/t/050_dropdb.pl
index 3065e5051df..2adc80a03f7 100644
--- a/src/bin/scripts/t/050_dropdb.pl
+++ b/src/bin/scripts/t/050_dropdb.pl
@@ -1,5 +1,7 @@
use strict;
use warnings;
+
+use PostgresNode;
use TestLib;
use Test::More tests => 11;
@@ -7,13 +9,15 @@ program_help_ok('dropdb');
program_version_ok('dropdb');
program_options_handling_ok('dropdb');
-my $tempdir = tempdir;
-start_test_server $tempdir;
+my $node = get_new_node();
+$node->init;
+$node->start;
-psql 'postgres', 'CREATE DATABASE foobar1';
-issues_sql_like(
+$node->psql('postgres', 'CREATE DATABASE foobar1');
+$node->issues_sql_like(
[ 'dropdb', 'foobar1' ],
qr/statement: DROP DATABASE foobar1/,
'SQL DROP DATABASE run');
-command_fails([ 'dropdb', 'nonexistent' ], 'fails with nonexistent database');
+$node->command_fails([ 'dropdb', 'nonexistent' ],
+ 'fails with nonexistent database');
diff --git a/src/bin/scripts/t/060_droplang.pl b/src/bin/scripts/t/060_droplang.pl
index 6a21d7e33d8..722804747ed 100644
--- a/src/bin/scripts/t/060_droplang.pl
+++ b/src/bin/scripts/t/060_droplang.pl
@@ -1,5 +1,7 @@
use strict;
use warnings;
+
+use PostgresNode;
use TestLib;
use Test::More tests => 11;
@@ -7,14 +9,15 @@ program_help_ok('droplang');
program_version_ok('droplang');
program_options_handling_ok('droplang');
-my $tempdir = tempdir;
-start_test_server $tempdir;
+my $node = get_new_node();
+$node->init;
+$node->start;
-issues_sql_like(
+$node->issues_sql_like(
[ 'droplang', 'plpgsql', 'postgres' ],
qr/statement: DROP EXTENSION "plpgsql"/,
'SQL DROP EXTENSION run');
-command_fails(
+$node->command_fails(
[ 'droplang', 'nonexistent', 'postgres' ],
'fails with nonexistent language');
diff --git a/src/bin/scripts/t/070_dropuser.pl b/src/bin/scripts/t/070_dropuser.pl
index bbb3b7922a4..0849f77ed67 100644
--- a/src/bin/scripts/t/070_dropuser.pl
+++ b/src/bin/scripts/t/070_dropuser.pl
@@ -1,5 +1,7 @@
use strict;
use warnings;
+
+use PostgresNode;
use TestLib;
use Test::More tests => 11;
@@ -7,13 +9,15 @@ program_help_ok('dropuser');
program_version_ok('dropuser');
program_options_handling_ok('dropuser');
-my $tempdir = tempdir;
-start_test_server $tempdir;
+my $node = get_new_node();
+$node->init;
+$node->start;
-psql 'postgres', 'CREATE ROLE foobar1';
-issues_sql_like(
+$node->psql('postgres', 'CREATE ROLE foobar1');
+$node->issues_sql_like(
[ 'dropuser', 'foobar1' ],
qr/statement: DROP ROLE foobar1/,
'SQL DROP ROLE run');
-command_fails([ 'dropuser', 'nonexistent' ], 'fails with nonexistent user');
+$node->command_fails([ 'dropuser', 'nonexistent' ],
+ 'fails with nonexistent user');
diff --git a/src/bin/scripts/t/080_pg_isready.pl b/src/bin/scripts/t/080_pg_isready.pl
index f432505d5cd..8f3f25cc364 100644
--- a/src/bin/scripts/t/080_pg_isready.pl
+++ b/src/bin/scripts/t/080_pg_isready.pl
@@ -1,5 +1,7 @@
use strict;
use warnings;
+
+use PostgresNode;
use TestLib;
use Test::More tests => 10;
@@ -9,7 +11,8 @@ program_options_handling_ok('pg_isready');
command_fails(['pg_isready'], 'fails with no server running');
-my $tempdir = tempdir;
-start_test_server $tempdir;
+my $node = get_new_node();
+$node->init;
+$node->start;
-command_ok(['pg_isready'], 'succeeds with server running');
+$node->command_ok(['pg_isready'], 'succeeds with server running');
diff --git a/src/bin/scripts/t/090_reindexdb.pl b/src/bin/scripts/t/090_reindexdb.pl
index 42628c25e2b..fd4eac347e1 100644
--- a/src/bin/scripts/t/090_reindexdb.pl
+++ b/src/bin/scripts/t/090_reindexdb.pl
@@ -1,5 +1,7 @@
use strict;
use warnings;
+
+use PostgresNode;
use TestLib;
use Test::More tests => 20;
@@ -7,35 +9,36 @@ program_help_ok('reindexdb');
program_version_ok('reindexdb');
program_options_handling_ok('reindexdb');
-my $tempdir = tempdir;
-start_test_server $tempdir;
+my $node = get_new_node();
+$node->init;
+$node->start;
$ENV{PGOPTIONS} = '--client-min-messages=WARNING';
-issues_sql_like(
+$node->issues_sql_like(
[ 'reindexdb', 'postgres' ],
qr/statement: REINDEX DATABASE postgres;/,
'SQL REINDEX run');
-psql 'postgres',
- 'CREATE TABLE test1 (a int); CREATE INDEX test1x ON test1 (a);';
-issues_sql_like(
+$node->psql('postgres',
+ 'CREATE TABLE test1 (a int); CREATE INDEX test1x ON test1 (a);');
+$node->issues_sql_like(
[ 'reindexdb', '-t', 'test1', 'postgres' ],
qr/statement: REINDEX TABLE test1;/,
'reindex specific table');
-issues_sql_like(
+$node->issues_sql_like(
[ 'reindexdb', '-i', 'test1x', 'postgres' ],
qr/statement: REINDEX INDEX test1x;/,
'reindex specific index');
-issues_sql_like(
+$node->issues_sql_like(
[ 'reindexdb', '-S', 'pg_catalog', 'postgres' ],
qr/statement: REINDEX SCHEMA pg_catalog;/,
'reindex specific schema');
-issues_sql_like(
+$node->issues_sql_like(
[ 'reindexdb', '-s', 'postgres' ],
qr/statement: REINDEX SYSTEM postgres;/,
'reindex system tables');
-issues_sql_like(
+$node->issues_sql_like(
[ 'reindexdb', '-v', '-t', 'test1', 'postgres' ],
qr/statement: REINDEX \(VERBOSE\) TABLE test1;/,
'reindex with verbose output');
diff --git a/src/bin/scripts/t/091_reindexdb_all.pl b/src/bin/scripts/t/091_reindexdb_all.pl
index ffadf29bc65..d47b18b9892 100644
--- a/src/bin/scripts/t/091_reindexdb_all.pl
+++ b/src/bin/scripts/t/091_reindexdb_all.pl
@@ -1,14 +1,16 @@
use strict;
use warnings;
-use TestLib;
+
+use PostgresNode;
use Test::More tests => 2;
-my $tempdir = tempdir;
-start_test_server $tempdir;
+my $node = get_new_node();
+$node->init;
+$node->start;
$ENV{PGOPTIONS} = '--client-min-messages=WARNING';
-issues_sql_like(
+$node->issues_sql_like(
[ 'reindexdb', '-a' ],
qr/statement: REINDEX.*statement: REINDEX/s,
'reindex all databases');
diff --git a/src/bin/scripts/t/100_vacuumdb.pl b/src/bin/scripts/t/100_vacuumdb.pl
index ac160ba8374..387d2b41e23 100644
--- a/src/bin/scripts/t/100_vacuumdb.pl
+++ b/src/bin/scripts/t/100_vacuumdb.pl
@@ -1,5 +1,7 @@
use strict;
use warnings;
+
+use PostgresNode;
use TestLib;
use Test::More tests => 18;
@@ -7,26 +9,27 @@ program_help_ok('vacuumdb');
program_version_ok('vacuumdb');
program_options_handling_ok('vacuumdb');
-my $tempdir = tempdir;
-start_test_server $tempdir;
+my $node = get_new_node();
+$node->init;
+$node->start;
-issues_sql_like(
+$node->issues_sql_like(
[ 'vacuumdb', 'postgres' ],
qr/statement: VACUUM;/,
'SQL VACUUM run');
-issues_sql_like(
+$node->issues_sql_like(
[ 'vacuumdb', '-f', 'postgres' ],
qr/statement: VACUUM \(FULL\);/,
'vacuumdb -f');
-issues_sql_like(
+$node->issues_sql_like(
[ 'vacuumdb', '-F', 'postgres' ],
qr/statement: VACUUM \(FREEZE\);/,
'vacuumdb -F');
-issues_sql_like(
+$node->issues_sql_like(
[ 'vacuumdb', '-z', 'postgres' ],
qr/statement: VACUUM \(ANALYZE\);/,
'vacuumdb -z');
-issues_sql_like(
+$node->issues_sql_like(
[ 'vacuumdb', '-Z', 'postgres' ],
qr/statement: ANALYZE;/,
'vacuumdb -Z');
diff --git a/src/bin/scripts/t/101_vacuumdb_all.pl b/src/bin/scripts/t/101_vacuumdb_all.pl
index e90f321d1eb..8f1536f44fe 100644
--- a/src/bin/scripts/t/101_vacuumdb_all.pl
+++ b/src/bin/scripts/t/101_vacuumdb_all.pl
@@ -1,12 +1,14 @@
use strict;
use warnings;
-use TestLib;
+
+use PostgresNode;
use Test::More tests => 2;
-my $tempdir = tempdir;
-start_test_server $tempdir;
+my $node = get_new_node();
+$node->init;
+$node->start;
-issues_sql_like(
+$node->issues_sql_like(
[ 'vacuumdb', '-a' ],
qr/statement: VACUUM.*statement: VACUUM/s,
'vacuum all databases');
diff --git a/src/bin/scripts/t/102_vacuumdb_stages.pl b/src/bin/scripts/t/102_vacuumdb_stages.pl
index 57b980ec6a5..4cb5b64877d 100644
--- a/src/bin/scripts/t/102_vacuumdb_stages.pl
+++ b/src/bin/scripts/t/102_vacuumdb_stages.pl
@@ -1,12 +1,14 @@
use strict;
use warnings;
-use TestLib;
+
+use PostgresNode;
use Test::More tests => 4;
-my $tempdir = tempdir;
-start_test_server $tempdir;
+my $node = get_new_node();
+$node->init;
+$node->start;
-issues_sql_like(
+$node->issues_sql_like(
[ 'vacuumdb', '--analyze-in-stages', 'postgres' ],
qr/.*statement:\ SET\ default_statistics_target=1;\ SET\ vacuum_cost_delay=0;
.*statement:\ ANALYZE.*
@@ -16,8 +18,7 @@ qr/.*statement:\ SET\ default_statistics_target=1;\ SET\ vacuum_cost_delay=0;
.*statement:\ ANALYZE/sx,
'analyze three times');
-
-issues_sql_like(
+$node->issues_sql_like(
[ 'vacuumdb', '--analyze-in-stages', '--all' ],
qr/.*statement:\ SET\ default_statistics_target=1;\ SET\ vacuum_cost_delay=0;
.*statement:\ ANALYZE.*