Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit72c2f36

Browse files
committed
libpq: Add TAP tests for service files and names
This commit adds a set of regression tests that checks various patternswith service names and service files, with:- Service file with no contents, used as default for PGSERVICEFILE toprevent any lookups at the HOME directory of an environment where thetest is run.- Service file with valid service name and its section.- Service file at the root of PGSYSCONFDIR, named pg_service.conf.- Missing service file.- Service name defined as a connection parameter or as PGSERVICE.Note that PGSYSCONFDIR is set to always point at a temporary directorycreated by the test, so as we never try to look at SYSCONFDIR.This set of tests has come up as a useful independent addition whilediscussing a patch that adds an equivalent of PGSERVICEFILE as aconnection parameter as there have never been any tests for servicefiles and service names. Torsten Foertsch and Ryo Kanbayashi haveprovided a basic implementation, that I have expanded to what isintroduced in this commit.Author: Torsten Foertsch <tfoertsch123@gmail.com>Author: Ryo Kanbayashi <kanbayashi.dev@gmail.com>Author: Michael Paquier <michael@paquier.xyz>Discussion:https://postgr.es/m/CAKkG4_nCjx3a_F3gyXHSPWxD8Sd8URaM89wey7fG_9g7KBkOCQ@mail.gmail.com
1 parentad9a23b commit72c2f36

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

‎src/interfaces/libpq/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ tests += {
122122
't/003_load_balance_host_list.pl',
123123
't/004_load_balance_dns.pl',
124124
't/005_negotiate_encryption.pl',
125+
't/006_service.pl',
125126
],
126127
'env': {
127128
'with_ssl': ssl_library,

‎src/interfaces/libpq/t/006_service.pl

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Copyright (c) 2025, PostgreSQL Global Development Group
2+
use strict;
3+
use warningsFATAL=>'all';
4+
use File::Copy;
5+
use PostgreSQL::Test::Utils;
6+
use PostgreSQL::Test::Cluster;
7+
use Test::More;
8+
9+
# This tests scenarios related to the service name and the service file,
10+
# for the connection options and their environment variables.
11+
12+
my$node = PostgreSQL::Test::Cluster->new('node');
13+
$node->init;
14+
$node->start;
15+
16+
my$td = PostgreSQL::Test::Utils::tempdir;
17+
18+
# Windows vs non-Windows: CRLF vs LF for the file's newline, relying on
19+
# the fact that libpq uses fgets() when reading the lines of a service file.
20+
my$newline =$windows_os ?"\r\n" :"\n";
21+
22+
# Create the set of service files used in the tests.
23+
# File that includes a valid service name, that uses a decomposed connection
24+
# string for its contents, split on spaces.
25+
my$srvfile_valid ="$td/pg_service_valid.conf";
26+
append_to_file($srvfile_valid,"[my_srv]",$newline);
27+
append_to_file($srvfile_valid,split(/\s+/,$node->connstr) .$newline);
28+
29+
# File defined with no contents, used as default value for PGSERVICEFILE,
30+
# so as no lookup is attempted in the user's home directory.
31+
my$srvfile_empty ="$td/pg_service_empty.conf";
32+
append_to_file($srvfile_empty,'');
33+
34+
# Default service file in PGSYSCONFDIR.
35+
my$srvfile_default ="$td/pg_service.conf";
36+
37+
# Missing service file.
38+
my$srvfile_missing ="$td/pg_service_missing.conf";
39+
40+
# Set the fallback directory lookup of the service file to the temporary
41+
# directory of this test. PGSYSCONFDIR is used if the service file
42+
# defined in PGSERVICEFILE cannot be found, or when a service file is
43+
# found but not the service name.
44+
local$ENV{PGSYSCONFDIR} =$td;
45+
# Force PGSERVICEFILE to a default location, so as this test never
46+
# tries to look at a home directory. This value needs to remain
47+
# at the top of this script before running any tests, and should never
48+
# be changed.
49+
local$ENV{PGSERVICEFILE} ="$srvfile_empty";
50+
51+
# Checks combinations of service name and a valid service file.
52+
{
53+
local$ENV{PGSERVICEFILE} =$srvfile_valid;
54+
$node->connect_ok(
55+
'service=my_srv',
56+
'connection with correct "service" string and PGSERVICEFILE',
57+
sql=>"SELECT 'connect1_1'",
58+
expected_stdout=>qr/connect1_1/);
59+
60+
$node->connect_ok(
61+
'postgres://?service=my_srv',
62+
'connection with correct "service" URI and PGSERVICEFILE',
63+
sql=>"SELECT 'connect1_2'",
64+
expected_stdout=>qr/connect1_2/);
65+
66+
$node->connect_fails(
67+
'service=undefined-service',
68+
'connection with incorrect "service" string and PGSERVICEFILE',
69+
expected_stderr=>
70+
qr/definition of service "undefined-service" not found/);
71+
72+
local$ENV{PGSERVICE} ='my_srv';
73+
$node->connect_ok(
74+
'',
75+
'connection with correct PGSERVICE and PGSERVICEFILE',
76+
sql=>"SELECT 'connect1_3'",
77+
expected_stdout=>qr/connect1_3/);
78+
79+
local$ENV{PGSERVICE} ='undefined-service';
80+
$node->connect_fails(
81+
'',
82+
'connection with incorrect PGSERVICE and PGSERVICEFILE',
83+
expected_stdout=>
84+
qr/definition of service "undefined-service" not found/);
85+
}
86+
87+
# Checks case of incorrect service file.
88+
{
89+
local$ENV{PGSERVICEFILE} =$srvfile_missing;
90+
$node->connect_fails(
91+
'service=my_srv',
92+
'connection with correct "service" string and incorrect PGSERVICEFILE',
93+
expected_stderr=>
94+
qr/service file ".*pg_service_missing.conf" not found/);
95+
}
96+
97+
# Checks case of service file named "pg_service.conf" in PGSYSCONFDIR.
98+
{
99+
# Create copy of valid file
100+
my$srvfile_default ="$td/pg_service.conf";
101+
copy($srvfile_valid,$srvfile_default);
102+
103+
$node->connect_ok(
104+
'service=my_srv',
105+
'connection with correct "service" string and pg_service.conf',
106+
sql=>"SELECT 'connect2_1'",
107+
expected_stdout=>qr/connect2_1/);
108+
109+
$node->connect_ok(
110+
'postgres://?service=my_srv',
111+
'connection with correct "service" URI and default pg_service.conf',
112+
sql=>"SELECT 'connect2_2'",
113+
expected_stdout=>qr/connect2_2/);
114+
115+
$node->connect_fails(
116+
'service=undefined-service',
117+
'connection with incorrect "service" string and default pg_service.conf',
118+
expected_stderr=>
119+
qr/definition of service "undefined-service" not found/);
120+
121+
local$ENV{PGSERVICE} ='my_srv';
122+
$node->connect_ok(
123+
'',
124+
'connection with correct PGSERVICE and default pg_service.conf',
125+
sql=>"SELECT 'connect2_3'",
126+
expected_stdout=>qr/connect2_3/);
127+
128+
local$ENV{PGSERVICE} ='undefined-service';
129+
$node->connect_fails(
130+
'',
131+
'connection with incorrect PGSERVICE and default pg_service.conf',
132+
expected_stdout=>
133+
qr/definition of service "undefined-service" not found/);
134+
135+
# Remove default pg_service.conf.
136+
unlink($srvfile_default);
137+
}
138+
139+
$node->teardown_node;
140+
141+
done_testing();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp