Avoid using internal test methods in SSL tests

The SSL tests for pg_ctl restart with an incorrect key passphrase used
the internal _update_pid method to set the pidfile after running pg_ctl
manually instead of using the supplied ->restart method. This refactors
the ->restart method to accept a fail_ok parameter like how ->start and
->stop does, and changes the SSL tests to use this instead. This removes
the need to call internal test module functions.

Reviewed-by: Melih Mutlu <m.melihmutlu@gmail.com>
Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi>
Discussion: https://postgr.es/m/F81643C4-D7B8-4C6B-AF18-B73839966279@yesql.se
This commit is contained in:
Daniel Gustafsson 2023-09-22 13:35:37 +02:00
parent 5f3aa309a8
commit 33774978c7
2 changed files with 32 additions and 22 deletions

View File

@ -1035,17 +1035,18 @@ sub reload
=item $node->restart() =item $node->restart()
Wrapper for pg_ctl restart Wrapper for pg_ctl restart.
With optional extra param fail_ok => 1, returns 0 for failure
instead of bailing out.
=cut =cut
sub restart sub restart
{ {
my ($self) = @_; my ($self, %params) = @_;
my $port = $self->port;
my $pgdata = $self->data_dir;
my $logfile = $self->logfile;
my $name = $self->name; my $name = $self->name;
my $ret;
local %ENV = $self->_get_env(PGAPPNAME => undef); local %ENV = $self->_get_env(PGAPPNAME => undef);
@ -1053,11 +1054,25 @@ sub restart
# -w is now the default but having it here does no harm and helps # -w is now the default but having it here does no harm and helps
# compatibility with older versions. # compatibility with older versions.
PostgreSQL::Test::Utils::system_or_bail('pg_ctl', '-w', '-D', $pgdata, $ret = PostgreSQL::Test::Utils::system_log(
'-l', $logfile, 'restart'); 'pg_ctl', '-w', '-D', $self->data_dir,
'-l', $self->logfile, 'restart');
if ($ret != 0)
{
print "# pg_ctl restart failed; logfile:\n";
print PostgreSQL::Test::Utils::slurp_file($self->logfile);
# pg_ctl could have timed out, so check to see if there's a pid file;
# otherwise our END block will fail to shut down the new postmaster.
$self->_update_pid(-1);
BAIL_OUT("pg_ctl restart failed") unless $params{fail_ok};
return 0;
}
$self->_update_pid(1); $self->_update_pid(1);
return; return 1;
} }
=pod =pod

View File

@ -85,10 +85,8 @@ switch_server_cert(
passphrase_cmd => 'echo wrongpassword', passphrase_cmd => 'echo wrongpassword',
restart => 'no'); restart => 'no');
command_fails( $result = $node->restart(fail_ok => 1);
[ 'pg_ctl', '-D', $node->data_dir, '-l', $node->logfile, 'restart' ], is($result, 0, 'restart fails with password-protected key file with wrong password');
'restart fails with password-protected key file with wrong password');
$node->_update_pid(0);
switch_server_cert( switch_server_cert(
$node, $node,
@ -98,10 +96,8 @@ switch_server_cert(
passphrase_cmd => 'echo secret1', passphrase_cmd => 'echo secret1',
restart => 'no'); restart => 'no');
command_ok( $result = $node->restart(fail_ok => 1);
[ 'pg_ctl', '-D', $node->data_dir, '-l', $node->logfile, 'restart' ], is($result, 1, 'restart succeeds with password-protected key file');
'restart succeeds with password-protected key file');
$node->_update_pid(1);
# Test compatibility of SSL protocols. # Test compatibility of SSL protocols.
# TLSv1.1 is lower than TLSv1.2, so it won't work. # TLSv1.1 is lower than TLSv1.2, so it won't work.
@ -109,17 +105,16 @@ $node->append_conf(
'postgresql.conf', 'postgresql.conf',
qq{ssl_min_protocol_version='TLSv1.2' qq{ssl_min_protocol_version='TLSv1.2'
ssl_max_protocol_version='TLSv1.1'}); ssl_max_protocol_version='TLSv1.1'});
command_fails( $result = $node->restart(fail_ok => 1);
[ 'pg_ctl', '-D', $node->data_dir, '-l', $node->logfile, 'restart' ], is($result, 0, 'restart fails with incorrect SSL protocol bounds');
'restart fails with incorrect SSL protocol bounds');
# Go back to the defaults, this works. # Go back to the defaults, this works.
$node->append_conf( $node->append_conf(
'postgresql.conf', 'postgresql.conf',
qq{ssl_min_protocol_version='TLSv1.2' qq{ssl_min_protocol_version='TLSv1.2'
ssl_max_protocol_version=''}); ssl_max_protocol_version=''});
command_ok( $result = $node->restart(fail_ok => 1);
[ 'pg_ctl', '-D', $node->data_dir, '-l', $node->logfile, 'restart' ], is($result, 1, 'restart succeeds with correct SSL protocol bounds');
'restart succeeds with correct SSL protocol bounds');
### Run client-side tests. ### Run client-side tests.
### ###