-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathtest_connect.py
More file actions
576 lines (505 loc) · 15.3 KB
/
Copy pathtest_connect.py
File metadata and controls
576 lines (505 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
##
# .test.test_connect
##
import sys
import os
import unittest
import atexit
import socket
import errno
from ..python.socket import find_available_port
from .. import installation
from .. import cluster as pg_cluster
from .. import exceptions as pg_exc
from ..driver import dbapi20 as dbapi20
from .. import driver as pg_driver
from .. import open as pg_open
default_installation = installation.default()
def check_for_ipv6():
result = False
if socket.has_ipv6:
try:
socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
result = True
except socket.error as e:
errs = [errno.EAFNOSUPPORT]
WSAEAFNOSUPPORT = getattr(errno, 'WSAEAFNOSUPPORT', None)
if WSAEAFNOSUPPORT is not None:
errs.append(WSAEAFNOSUPPORT)
if e.errno not in errs:
raise
return result
msw = sys.platform in ('win32', 'win64')
# win32 binaries don't appear to be built with ipv6
has_ipv6 = check_for_ipv6() and not msw
has_unix_sock = not msw
class TestCaseWithCluster(unittest.TestCase):
"""
postgresql.driver *interface* tests.
"""
installation = default_installation
@property
def _crt(self):
return self.params.get('sslrootcrtfile') or None
def __init__(self, *args, **kw):
super().__init__(*args, **kw)
self.cluster_path = \
'pypg_test_' \
+ str(os.getpid()) + getattr(self, 'cluster_path_suffix', '')
self.cluster = pg_cluster.Cluster(
self.installation,
self.cluster_path,
)
@property
def disable_replication(self):
"""
Whether replication settings should be disabled.
"""
return self.installation.version_info[:2] > (9, 6)
def configure_cluster(self):
self.cluster_port = find_available_port()
if self.cluster_port is None:
pg_exc.ClusterError(
'failed to find a port for the test cluster on localhost',
creator = self.cluster
).raise_exception()
listen_addresses = '127.0.0.1'
if has_ipv6:
listen_addresses += ',::1'
if self.cluster.installation.version_info >= (10, 0):
pwe = 'md5'
else:
pwe = 'on'
self.cluster.settings.update(dict(
port = str(self.cluster_port),
max_connections = '6',
shared_buffers = '24',
listen_addresses = listen_addresses,
log_destination = 'stderr',
log_min_messages = 'FATAL',
password_encryption = pwe,
))
if self.disable_replication:
self.cluster.settings.update({
'max_wal_senders': '0',
})
if self.cluster.installation.version_info[:2] < (9, 3):
self.cluster.settings.update(dict(
unix_socket_directory = self.cluster.data_directory,
))
else:
self.cluster.settings.update(dict(
unix_socket_directories = self.cluster.data_directory,
))
# 8.4 turns prepared transactions off by default.
if self.cluster.installation.version_info >= (8,1):
self.cluster.settings.update(dict(
max_prepared_transactions = '3',
))
def initialize_database(self):
c = self.cluster.connection(
user = 'test',
database = 'template1',
sslrootcrtfile = self._crt,
)
with c:
if c.prepare(
"select true from pg_catalog.pg_database " \
"where datname = 'test'"
).first() is None:
c.execute('create database test')
def connection(self, *args, **kw):
return self.cluster.connection(*args, user = 'test', **self.params, **kw)
def drop_cluster(self):
if self.cluster.initialized():
self.cluster.drop()
def run(self, *args, **kw):
self.params = {}
if 'PGINSTALLATION' not in os.environ:
# Expect tests to show skipped.
return super().run(*args, **kw)
# From prior test run?
if self.cluster.initialized():
self.cluster.drop()
self.cluster.encoding = 'utf-8'
self.cluster.init(
user = 'test',
encoding = self.cluster.encoding,
logfile = None,
)
sys.stderr.write('*')
atexit.register(self.drop_cluster)
self.configure_cluster()
self.cluster.start(logfile = sys.stdout)
self.cluster.wait_until_started()
self.initialize_database()
if not self.cluster.running():
self.cluster.start()
self.cluster.wait_until_started()
db = self.connection()
with db:
self.db = db
return super().run(*args, **kw)
self.db = None
class test_connect(TestCaseWithCluster):
"""
postgresql.driver connection tests
"""
ip6 = '::1'
ip4 = '127.0.0.1'
host = 'localhost'
cluster_path_suffix = '_test_connect'
mk_common_users = """
CREATE USER md5 WITH ENCRYPTED PASSWORD 'md5_password';
CREATE USER password WITH ENCRYPTED PASSWORD 'password_password';
CREATE USER trusted;
"""
mk_crypt_user = """
-- crypt doesn't work with encrypted passwords:
-- http://www.postgresql.org/docs/8.2/interactive/auth-methods.html#AUTH-PASSWORD
CREATE USER crypt WITH UNENCRYPTED PASSWORD 'crypt_password';
"""
def __init__(self, *args, **kw):
super().__init__(*args,**kw)
@property
def check_crypt_user(self):
return (self.cluster.installation.version_info < (8,4))
def configure_cluster(self):
super().configure_cluster()
self.cluster.settings['log_min_messages'] = 'log'
# Configure the hba file with the supported methods.
with open(self.cluster.hba_file, 'w') as hba:
hosts = ['0.0.0.0/0',]
if has_ipv6:
hosts.append('0::0/0')
methods = ['md5', 'password'] + (['crypt'] if self.check_crypt_user else [])
for h in hosts:
for m in methods:
# user and method are the same name.
hba.writelines(['host test {m} {h} {m}\n'.format(
h = h,
m = m
)])
# trusted
hba.writelines(["local all all trust\n"])
hba.writelines(["host test trusted 0.0.0.0/0 trust\n"])
if has_ipv6:
hba.writelines(["host test trusted 0::0/0 trust\n"])
# admin lines
hba.writelines(["host all test 0.0.0.0/0 trust\n"])
if has_ipv6:
hba.writelines(["host all test 0::0/0 trust\n"])
def initialize_database(self):
super().initialize_database()
with self.connection() as db:
db.execute(self.mk_common_users)
if self.check_crypt_user:
db.execute(self.mk_crypt_user)
@unittest.skipIf(default_installation is None, "no installation provided by environment")
def test_pg_open_SQL_ASCII(self):
host, port = self.cluster.address()
dbctx = self.params
# test simple locators..
with pg_open(
'pq://' + 'md5:' + 'md5_password@' + host + ':' + str(port) \
+ '/test?client_encoding=SQL_ASCII',
**dbctx
) as db:
self.assertEqual(db.prepare('select 1')(), [(1,)])
self.assertEqual(db.settings['client_encoding'], 'SQL_ASCII')
self.assertTrue(db.closed)
@unittest.skipIf(default_installation is None, "no installation provided by environment")
def test_pg_open_keywords(self):
host, port = self.cluster.address()
dbctx = self.params
# Keywords only, no indicator.
with pg_open(
user = 'md5',
password = 'md5_password',
host = host,
port = port,
database = 'test',
**dbctx,
) as db:
self.assertEqual(db.prepare('select 1')(), [(1,)])
# Keyword and indicator source.
with pg_open(
"pq://md5:md5_password@",
host = host,
port = port,
database = 'test',
**dbctx,
) as db:
self.assertEqual(db.prepare('select 1')(), [(1,)])
# Keyword override.
with pg_open(
"pq://md5:foobar@",
password = 'md5_password',
host = host,
port = port,
database = 'test',
**dbctx,
) as db:
self.assertEqual(db.prepare('select 1')(), [(1,)])
# Settings override.
with pg_open(
"pq://md5:foobar@?search_path=ieeee",
password = 'md5_password',
host = host,
port = port,
database = 'test',
settings = {'search_path' : 'public'},
**dbctx,
) as db:
self.assertEqual(db.prepare('select 1')(), [(1,)])
self.assertEqual(db.settings['search_path'], 'public')
@unittest.skipIf(default_installation is None, "no installation provided by environment")
def test_pg_open(self):
host, port = self.cluster.address()
dbctx = self.params
# test simple locators..
with pg_open(
'pq://' + 'md5:' + 'md5_password@' + host + ':' + str(port) \
+ '/test',
**dbctx,
) as db:
self.assertEqual(db.prepare('select 1')(), [(1,)])
self.assertTrue(db.closed)
with pg_open(
'pq://' + 'password:' + 'password_password@' + host + ':' + str(port) \
+ '/test',
**dbctx,
) as db:
self.assertEqual(db.prepare('select 1')(), [(1,)])
self.assertTrue(db.closed)
with pg_open(
'pq://' + 'trusted@' + host + ':' + str(port) + '/test',
**dbctx,
) as db:
self.assertEqual(db.prepare('select 1')(), [(1,)])
self.assertTrue(db.closed)
# test environment collection
pgenv = ('PGUSER', 'PGPORT', 'PGHOST', 'PGSERVICE', 'PGPASSWORD', 'PGDATABASE')
stored = list(map(os.environ.get, pgenv))
try:
os.environ.pop('PGSERVICE', None)
os.environ['PGUSER'] = 'md5'
os.environ['PGPASSWORD'] = 'md5_password'
os.environ['PGHOST'] = host
os.environ['PGPORT'] = str(port)
os.environ['PGDATABASE'] = 'test'
# No arguments, the environment provided everything.
with pg_open(**dbctx) as db:
self.assertEqual(db.prepare('select 1')(), [(1,)])
self.assertEqual(db.prepare('select current_user').first(), 'md5')
self.assertTrue(db.closed)
finally:
i = 0
for x in stored:
env = pgenv[i]
if x is None:
os.environ.pop(env, None)
else:
os.environ[env] = x
oldservice = os.environ.get('PGSERVICE')
oldsysconfdir = os.environ.get('PGSYSCONFDIR')
try:
with open('pg_service.conf', 'w') as sf:
sf.write('''
[myserv]
user = password
password = password_password
host = {host}
port = {port}
dbname = test
search_path = public
'''.format(host = host, port = port))
sf.flush()
try:
os.environ['PGSERVICE'] = 'myserv'
os.environ['PGSYSCONFDIR'] = os.getcwd()
with pg_open(**dbctx) as db:
self.assertEqual(db.prepare('select 1')(), [(1,)])
self.assertEqual(db.prepare('select current_user').first(), 'password')
self.assertEqual(db.settings['search_path'], 'public')
finally:
if oldservice is None:
os.environ.pop('PGSERVICE', None)
else:
os.environ['PGSERVICE'] = oldservice
if oldsysconfdir is None:
os.environ.pop('PGSYSCONFDIR', None)
else:
os.environ['PGSYSCONFDIR'] = oldsysconfdir
finally:
if os.path.exists('pg_service.conf'):
os.remove('pg_service.conf')
@unittest.skipIf(default_installation is None, "no installation provided by environment")
def test_dbapi_connect(self):
host, port = self.cluster.address()
MD5 = dbapi20.connect(
user = 'md5',
database = 'test',
password = 'md5_password',
host = host, port = port,
**self.params
)
self.assertEqual(MD5.cursor().execute('select 1').fetchone()[0], 1)
MD5.close()
self.assertRaises(pg_exc.ConnectionDoesNotExistError,
MD5.cursor().execute, 'select 1'
)
if self.check_crypt_user:
CRYPT = dbapi20.connect(
user = 'crypt',
database = 'test',
password = 'crypt_password',
host = host, port = port,
**self.params
)
self.assertEqual(CRYPT.cursor().execute('select 1').fetchone()[0], 1)
CRYPT.close()
self.assertRaises(pg_exc.ConnectionDoesNotExistError,
CRYPT.cursor().execute, 'select 1'
)
PASSWORD = dbapi20.connect(
user = 'password',
database = 'test',
password = 'password_password',
host = host, port = port,
**self.params
)
self.assertEqual(PASSWORD.cursor().execute('select 1').fetchone()[0], 1)
PASSWORD.close()
self.assertRaises(pg_exc.ConnectionDoesNotExistError,
PASSWORD.cursor().execute, 'select 1'
)
TRUST = dbapi20.connect(
user = 'trusted',
database = 'test',
password = '',
host = host, port = port,
**self.params
)
self.assertEqual(TRUST.cursor().execute('select 1').fetchone()[0], 1)
TRUST.close()
self.assertRaises(pg_exc.ConnectionDoesNotExistError,
TRUST.cursor().execute, 'select 1'
)
@unittest.skipIf(default_installation is None, "no installation provided by environment")
def test_dbapi_connect_failure(self):
host, port = self.cluster.address()
badlogin = (lambda: dbapi20.connect(
user = '--',
database = '--',
password = '...',
host = host, port = port,
**self.params
))
self.assertRaises(pg_exc.ClientCannotConnectError, badlogin)
@unittest.skipIf(default_installation is None, "no installation provided by environment")
def test_IP4_connect(self):
C = pg_driver.default.ip4(
user = 'test',
host = '127.0.0.1',
database = 'test',
port = self.cluster.address()[1],
**self.params
)
with C() as c:
self.assertEqual(c.prepare('select 1').first(), 1)
@unittest.skipIf(default_installation is None, "no installation provided by environment")
@unittest.skipIf(not has_ipv6, "platform may not support IPv6")
def test_IP6_connect(self):
C = pg_driver.default.ip6(
user = 'test',
host = '::1',
database = 'test',
port = self.cluster.address()[1],
**self.params
)
with C() as c:
self.assertEqual(c.prepare('select 1').first(), 1)
@unittest.skipIf(default_installation is None, "no installation provided by environment")
def test_Host_connect(self):
C = pg_driver.default.host(
user = 'test',
host = 'localhost',
database = 'test',
port = self.cluster.address()[1],
**self.params
)
with C() as c:
self.assertEqual(c.prepare('select 1').first(), 1)
@unittest.skipIf(default_installation is None, "no installation provided by environment")
def test_md5_connect(self):
c = self.cluster.connection(
user = 'md5',
password = 'md5_password',
database = 'test',
**self.params
)
with c:
self.assertEqual(c.prepare('select current_user').first(), 'md5')
@unittest.skipIf(default_installation is None, "no installation provided by environment")
def test_crypt_connect(self):
if self.check_crypt_user:
c = self.cluster.connection(
user = 'crypt',
password = 'crypt_password',
database = 'test',
**self.params
)
with c:
self.assertEqual(c.prepare('select current_user').first(), 'crypt')
@unittest.skipIf(default_installation is None, "no installation provided by environment")
def test_password_connect(self):
c = self.cluster.connection(
user = 'password',
password = 'password_password',
database = 'test',
sslrootcrtfile = self._crt,
)
with c:
self.assertEqual(c.prepare('select current_user').first(), 'password')
@unittest.skipIf(default_installation is None, "no installation provided by environment")
def test_trusted_connect(self):
c = self.cluster.connection(
user = 'trusted',
password = '',
database = 'test',
**self.params
)
with c:
self.assertEqual(c.prepare('select current_user').first(), 'trusted')
@unittest.skipIf(default_installation is None, "no installation provided by environment")
def test_Unix_connect(self):
if not has_unix_sock:
return
unix_domain_socket = os.path.join(
self.cluster.data_directory,
'.s.PGSQL.' + self.cluster.settings['port']
)
C = pg_driver.default.unix(
user = 'test',
unix = unix_domain_socket,
)
with C() as c:
self.assertEqual(c.prepare('select 1').first(), 1)
self.assertEqual(c.client_address, None)
@unittest.skipIf(default_installation is None, "no installation provided by environment")
def test_pg_open_unix(self):
if not has_unix_sock:
return
unix_domain_socket = os.path.join(
self.cluster.data_directory,
'.s.PGSQL.' + self.cluster.settings['port']
)
with pg_open(unix = unix_domain_socket, user = 'test') as c:
self.assertEqual(c.prepare('select 1').first(), 1)
self.assertEqual(c.client_address, None)
with pg_open('pq://test@[unix:' + unix_domain_socket.replace('/',':') + ']') as c:
self.assertEqual(c.prepare('select 1').first(), 1)
self.assertEqual(c.client_address, None)
if __name__ == '__main__':
unittest.main()