diff options
author | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2015-12-02 18:46:16 -0300 |
---|---|---|
committer | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2015-12-02 18:46:16 -0300 |
commit | 1caef31d9e550408d0cbc5788a422dcb69736df5 (patch) | |
tree | 451c4745e315c8dba59415a83eb2c4963fded212 /src/test/ssl/ServerSetup.pm | |
parent | c7485a82c3e29103757db75bb9ff8dac597387dc (diff) | |
download | postgresql-1caef31d9e550408d0cbc5788a422dcb69736df5.tar.gz postgresql-1caef31d9e550408d0cbc5788a422dcb69736df5.zip |
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/test/ssl/ServerSetup.pm')
-rw-r--r-- | src/test/ssl/ServerSetup.pm | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/src/test/ssl/ServerSetup.pm b/src/test/ssl/ServerSetup.pm index a6c77b5c80a..4e93184eb03 100644 --- a/src/test/ssl/ServerSetup.pm +++ b/src/test/ssl/ServerSetup.pm @@ -18,6 +18,7 @@ package ServerSetup; use strict; use warnings; +use PostgresNode; use TestLib; use File::Basename; use File::Copy; @@ -45,17 +46,19 @@ sub copy_files sub configure_test_server_for_ssl { - my $tempdir = $_[0]; + my $node = $_[0]; my $serverhost = $_[1]; + my $pgdata = $node->data_dir; + # Create test users and databases - psql 'postgres', "CREATE USER ssltestuser"; - psql 'postgres', "CREATE USER anotheruser"; - psql 'postgres', "CREATE DATABASE trustdb"; - psql 'postgres', "CREATE DATABASE certdb"; + $node->psql('postgres', "CREATE USER ssltestuser"); + $node->psql('postgres', "CREATE USER anotheruser"); + $node->psql('postgres', "CREATE DATABASE trustdb"); + $node->psql('postgres', "CREATE DATABASE certdb"); # enable logging etc. - open CONF, ">>$tempdir/pgdata/postgresql.conf"; + open CONF, ">>$pgdata/postgresql.conf"; print CONF "fsync=off\n"; print CONF "log_connections=on\n"; print CONF "log_hostname=on\n"; @@ -68,17 +71,17 @@ sub configure_test_server_for_ssl close CONF; # Copy all server certificates and keys, and client root cert, to the data dir - copy_files("ssl/server-*.crt", "$tempdir/pgdata"); - copy_files("ssl/server-*.key", "$tempdir/pgdata"); - chmod(0600, glob "$tempdir/pgdata/server-*.key") or die $!; - copy_files("ssl/root+client_ca.crt", "$tempdir/pgdata"); - copy_files("ssl/root+client.crl", "$tempdir/pgdata"); + copy_files("ssl/server-*.crt", $pgdata); + copy_files("ssl/server-*.key", $pgdata); + chmod(0600, glob "$pgdata/server-*.key") or die $!; + copy_files("ssl/root+client_ca.crt", $pgdata); + copy_files("ssl/root+client.crl", $pgdata); # Only accept SSL connections from localhost. Our tests don't depend on this # but seems best to keep it as narrow as possible for security reasons. # # When connecting to certdb, also check the client certificate. - open HBA, ">$tempdir/pgdata/pg_hba.conf"; + open HBA, ">$pgdata/pg_hba.conf"; print HBA "# TYPE DATABASE USER ADDRESS METHOD\n"; print HBA @@ -96,12 +99,13 @@ sub configure_test_server_for_ssl # the server so that the configuration takes effect. sub switch_server_cert { - my $tempdir = $_[0]; + my $node = $_[0]; my $certfile = $_[1]; + my $pgdata = $node->data_dir; diag "Restarting server with certfile \"$certfile\"..."; - open SSLCONF, ">$tempdir/pgdata/sslconfig.conf"; + open SSLCONF, ">$pgdata/sslconfig.conf"; print SSLCONF "ssl=on\n"; print SSLCONF "ssl_ca_file='root+client_ca.crt'\n"; print SSLCONF "ssl_cert_file='$certfile.crt'\n"; @@ -110,5 +114,5 @@ sub switch_server_cert close SSLCONF; # Stop and restart server to reload the new config. - restart_test_server(); + $node->restart; } |