Debian Codegraph @

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

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