Skip to content

Commit 1b4f74a

Browse files
author
v.shepard
committed
PBCKP-588 test partially fixed test_simple_remote.py 41/43
1 parent 2c2d2c5 commit 1b4f74a

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

testgres/node.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,8 @@ def get_auth_method(t):
519519
u"local\treplication\tall\t\t\t{}\n".format(auth_local),
520520
u"host\treplication\tall\t127.0.0.1/32\t{}\n".format(auth_host),
521521
u"host\treplication\tall\t::1/128\t\t{}\n".format(auth_host),
522-
u"host\treplication\t{}\t{}/24\t\t{}\n".format(self.os_ops.username, subnet_base, auth_host),
523-
u"host\tall\t{}\t{}/24\t\t{}\n".format(self.os_ops.username, subnet_base, auth_host)
522+
u"host\treplication\tall\t{}/24\t\t{}\n".format(subnet_base, auth_host),
523+
u"host\tall\tall\t{}/24\t\t{}\n".format(subnet_base, auth_host)
524524
] # yapf: disable
525525

526526
# write missing lines
@@ -790,7 +790,9 @@ def restart(self, params=[]):
790790
] + params # yapf: disable
791791

792792
try:
793-
execute_utility(_params, self.utils_log_file)
793+
error_code, out, error = execute_utility(_params, self.utils_log_file, verbose=True)
794+
if 'could not start server' in error:
795+
raise ExecUtilException
794796
except ExecUtilException as e:
795797
msg = 'Cannot restart node'
796798
files = self._collect_special_files()

testgres/operations/remote_ops.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import tempfile
3+
import time
34
from typing import Optional
45

56
import sshtunnel
@@ -46,11 +47,29 @@ def __init__(self, host="127.0.0.1", hostname='localhost', port=None, ssh_key=No
4647
self.remote = True
4748
self.ssh = self.ssh_connect()
4849
self.username = username or self.get_user()
50+
self.tunnel = None
51+
52+
def __enter__(self):
53+
return self
54+
55+
def __exit__(self, exc_type, exc_val, exc_tb):
56+
self.close_tunnel()
57+
if getattr(self, 'ssh', None):
58+
self.ssh.close()
4959

5060
def __del__(self):
51-
if self.ssh:
61+
if getattr(self, 'ssh', None):
5262
self.ssh.close()
5363

64+
def close_tunnel(self):
65+
if getattr(self, 'tunnel', None):
66+
self.tunnel.stop(force=True)
67+
start_time = time.time()
68+
while self.tunnel.is_active:
69+
if time.time() - start_time > sshtunnel.TUNNEL_TIMEOUT:
70+
break
71+
time.sleep(0.5)
72+
5473
def ssh_connect(self) -> Optional[SSHClient]:
5574
if not self.remote:
5675
return None
@@ -402,26 +421,27 @@ def db_connect(self, dbname, user, password=None, host="127.0.0.1", port=5432, s
402421
This function establishes a connection to a PostgreSQL database on the remote system using the specified
403422
parameters. It returns a connection object that can be used to interact with the database.
404423
"""
405-
tunnel = sshtunnel.open_tunnel(
424+
self.close_tunnel()
425+
self.tunnel = sshtunnel.open_tunnel(
406426
(host, 22), # Remote server IP and SSH port
407427
ssh_username=user or self.username,
408428
ssh_pkey=ssh_key or self.ssh_key,
409429
remote_bind_address=(host, port), # PostgreSQL server IP and PostgreSQL port
410430
local_bind_address=('localhost', port) # Local machine IP and available port
411431
)
412432

413-
tunnel.start()
433+
self.tunnel.start()
414434

415435
try:
416436
conn = pglib.connect(
417437
host=host, # change to 'localhost' because we're connecting through a local ssh tunnel
418-
port=tunnel.local_bind_port, # use the local bind port set up by the tunnel
438+
port=self.tunnel.local_bind_port, # use the local bind port set up by the tunnel
419439
dbname=dbname,
420440
user=user or self.username,
421441
password=password
422442
)
423443

424444
return conn
425445
except Exception as e:
426-
tunnel.stop()
446+
self.tunnel.stop()
427447
raise e

testgres/pubsub.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,4 @@ def catchup(self, username=None):
214214
username=username or self.pub.username,
215215
max_attempts=LOGICAL_REPL_MAX_CATCHUP_ATTEMPTS)
216216
except Exception as e:
217-
raise_from(CatchUpException("Failed to catch up", query), e)
217+
raise_from(CatchUpException("Failed to catch up"), e)

tests/test_simple_remote.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import subprocess
77
import tempfile
88

9-
109
import testgres
1110
import time
1211
import six
@@ -138,6 +137,7 @@ def test_init_after_cleanup(self):
138137
@unittest.skipUnless(util_exists('pg_resetwal'), 'might be missing')
139138
@unittest.skipUnless(pg_version_ge('9.6'), 'requires 9.6+')
140139
def test_init_unique_system_id(self):
140+
# FAIL
141141
# this function exists in PostgreSQL 9.6+
142142
query = 'select system_identifier from pg_control_system()'
143143

@@ -291,7 +291,7 @@ def test_psql(self):
291291
node.safe_psql('copy horns from stdin (format csv)',
292292
input=b"1\n2\n3\n\\.\n")
293293
_sum = node.safe_psql('select sum(w) from horns')
294-
self.assertEqual(b'6\n', _sum)
294+
self.assertEqual(_sum, b'6\n')
295295

296296
# check psql's default args, fails
297297
with self.assertRaises(QueryException):
@@ -688,6 +688,7 @@ def test_poll_query_until(self):
688688
node.poll_query_until('select true')
689689

690690
def test_logging(self):
691+
# FAIL
691692
logfile = tempfile.NamedTemporaryFile('w', delete=True)
692693

693694
log_conf = {
@@ -747,14 +748,11 @@ def test_pgbench(self):
747748
options=['-q']).pgbench_run(time=2)
748749

749750
# run TPC-B benchmark
750-
proc = node.pgbench(stdout=subprocess.PIPE,
751+
out = node.pgbench(stdout=subprocess.PIPE,
751752
stderr=subprocess.STDOUT,
752753
options=['-T3'])
753754

754-
out, _ = proc.communicate()
755-
out = out.decode('utf-8')
756-
757-
self.assertTrue('tps' in out)
755+
self.assertTrue(b'tps = ' in out)
758756

759757
def test_pg_config(self):
760758
# check same instances
@@ -764,7 +762,6 @@ def test_pg_config(self):
764762

765763
# save right before config change
766764
c1 = get_pg_config()
767-
768765
# modify setting for this scope
769766
with scoped_config(cache_pg_config=False) as config:
770767

@@ -819,12 +816,16 @@ def test_unix_sockets(self):
819816
node.init(unix_sockets=False, allow_streaming=True)
820817
node.start()
821818

822-
node.execute('select 1')
823-
node.safe_psql('select 1')
819+
res_exec = node.execute('select 1')
820+
res_psql = node.safe_psql('select 1')
821+
self.assertEqual(res_exec, [(1,)])
822+
self.assertEqual(res_psql, b'1\n')
824823

825824
with node.replicate().start() as r:
826-
r.execute('select 1')
827-
r.safe_psql('select 1')
825+
res_exec = r.execute('select 1')
826+
res_psql = r.safe_psql('select 1')
827+
self.assertEqual(res_exec, [(1,)])
828+
self.assertEqual(res_psql, b'1\n')
828829

829830
def test_auto_name(self):
830831
with get_remote_node().init(allow_streaming=True).start() as m:

0 commit comments

Comments
 (0)