Skip to content

Commit 0d1a334

Browse files
committed
Move some client-specific routines from SSLServer to PostgresNode
test_connect_ok() and test_connect_fails() have always been part of the SSL tests, and check if a connection to the backend should work or not, and there are sanity checks done on specific error patterns dropped by libpq if the connection fails. This was fundamentally wrong on two aspects. First, SSLServer.pm works mostly on setting up and changing the SSL configuration of a PostgresNode, and has really nothing to do with the client. Second, the situation became worse in light of b34ca59, where the SSL tests would finish by using a psql command that may not come from the same installation as the node set up. This commit moves those client routines into PostgresNode, making easier the refactoring of SSLServer to become more SSL-implementation aware. This can also be reused by the ldap, kerberos and authentication test suites for connection checks, and a follow-up patch should extend those interfaces to match with backend log patterns. Author: Michael Paquier Reviewed-by: Andrew Dunstan, Daniel Gustafsson, Álvaro Herrera Discussion: https://postgr.es/m/YGLKNBf9zyh6+WSt@paquier.xyz
1 parent 28b3e39 commit 0d1a334

File tree

4 files changed

+207
-251
lines changed

4 files changed

+207
-251
lines changed

src/test/perl/PostgresNode.pm

+64-8
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,11 @@ the B<timed_out> parameter is also given.
15111511
If B<timeout> is set and this parameter is given, the scalar it references
15121512
is set to true if the psql call times out.
15131513
1514+
=item connstr => B<value>
1515+
1516+
If set, use this as the connection string for the connection to the
1517+
backend.
1518+
15141519
=item replication => B<value>
15151520
15161521
If set, add B<replication=value> to the conninfo string.
@@ -1550,14 +1555,20 @@ sub psql
15501555
my $replication = $params{replication};
15511556
my $timeout = undef;
15521557
my $timeout_exception = 'psql timed out';
1553-
my @psql_params = (
1554-
'psql',
1555-
'-XAtq',
1556-
'-d',
1557-
$self->connstr($dbname)
1558-
. (defined $replication ? " replication=$replication" : ""),
1559-
'-f',
1560-
'-');
1558+
1559+
# Build the connection string.
1560+
my $psql_connstr;
1561+
if (defined $params{connstr})
1562+
{
1563+
$psql_connstr = $params{connstr};
1564+
}
1565+
else
1566+
{
1567+
$psql_connstr = $self->connstr($dbname);
1568+
}
1569+
$psql_connstr .= defined $replication ? " replication=$replication" : "";
1570+
1571+
my @psql_params = ('psql', '-XAtq', '-d', $psql_connstr, '-f', '-');
15611572

15621573
# If the caller wants an array and hasn't passed stdout/stderr
15631574
# references, allocate temporary ones to capture them so we
@@ -1849,6 +1860,51 @@ sub interactive_psql
18491860

18501861
=pod
18511862
1863+
=item $node->connect_ok($connstr, $test_name)
1864+
1865+
Attempt a connection with a custom connection string. This is expected
1866+
to succeed.
1867+
1868+
=cut
1869+
1870+
sub connect_ok
1871+
{
1872+
local $Test::Builder::Level = $Test::Builder::Level + 1;
1873+
my ($self, $connstr, $test_name) = @_;
1874+
my ($ret, $stdout, $stderr) = $self->psql(
1875+
'postgres',
1876+
"SELECT \$\$connected with $connstr\$\$",
1877+
connstr => "$connstr",
1878+
on_error_stop => 0);
1879+
1880+
ok($ret == 0, $test_name);
1881+
}
1882+
1883+
=pod
1884+
1885+
=item $node->connect_fails($connstr, $expected_stderr, $test_name)
1886+
1887+
Attempt a connection with a custom connection string. This is expected
1888+
to fail with a message that matches the regular expression
1889+
$expected_stderr.
1890+
1891+
=cut
1892+
1893+
sub connect_fails
1894+
{
1895+
local $Test::Builder::Level = $Test::Builder::Level + 1;
1896+
my ($self, $connstr, $expected_stderr, $test_name) = @_;
1897+
my ($ret, $stdout, $stderr) = $self->psql(
1898+
'postgres',
1899+
"SELECT \$\$connected with $connstr\$\$",
1900+
connstr => "$connstr");
1901+
1902+
ok($ret != 0, $test_name);
1903+
like($stderr, $expected_stderr, "$test_name: matches");
1904+
}
1905+
1906+
=pod
1907+
18521908
=item $node->poll_query_until($dbname, $query [, $expected ])
18531909
18541910
Run B<$query> repeatedly, until it returns the B<$expected> result

0 commit comments

Comments
 (0)