Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2e916df

Browse files
author
v.shepard
committed
PBCKP-588 fixes after review
1 parent 1b4f74a commit 2e916df

File tree

9 files changed

+30
-44
lines changed

9 files changed

+30
-44
lines changed

testgres/logger.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,6 @@
55
import threading
66
import time
77

8-
# create logger
9-
log = logging.getLogger('Testgres')
10-
11-
if not log.handlers:
12-
log.setLevel(logging.WARN)
13-
# create console handler and set level to debug
14-
ch = logging.StreamHandler()
15-
ch.setLevel(logging.WARN)
16-
# create formatter
17-
formatter = logging.Formatter('\n%(asctime)s - %(name)s[%(levelname)s]: %(message)s')
18-
# add formatter to ch
19-
ch.setFormatter(formatter)
20-
# add ch to logger
21-
log.addHandler(ch)
22-
238

249
class TestgresLogger(threading.Thread):
2510
"""

testgres/node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def child_processes(self):
245245
"""
246246

247247
# get a list of postmaster's children
248-
children = self.os_ops.get_remote_children(self.pid)
248+
children = self.os_ops.get_process_children(self.pid)
249249

250250
return [ProcessProxy(p) for p in children]
251251

testgres/operations/local_ops.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import psutil
99

1010
from testgres.exceptions import ExecUtilException
11-
from testgres.logger import log
1211

1312
from .os_ops import OsOperations
1413
from .os_ops import pglib
@@ -53,7 +52,6 @@ def exec_command(self, cmd, wait_exit=False, verbose=False,
5352
"""
5453
if isinstance(cmd, list):
5554
cmd = ' '.join(item.decode('utf-8') if isinstance(item, bytes) else item for item in cmd)
56-
log.debug(f"Executing command: `{cmd}`")
5755

5856
if os.name == 'nt':
5957
with tempfile.NamedTemporaryFile() as buf:
@@ -252,7 +250,7 @@ def get_pid(self):
252250
# Get current process id
253251
return os.getpid()
254252

255-
def get_remote_children(self, pid):
253+
def get_process_children(self, pid):
256254
return psutil.Process(pid).children()
257255

258256
# Database control

testgres/operations/os_ops.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ def get_pid(self):
8888
# Get current process id
8989
raise NotImplementedError()
9090

91+
def get_process_children(self, pid):
92+
raise NotImplementedError()
93+
9194
# Database control
9295
def db_connect(self, dbname, user, password=None, host="localhost", port=5432):
9396
raise NotImplementedError()

testgres/operations/remote_ops.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from paramiko import SSHClient
1010

1111
from testgres.exceptions import ExecUtilException
12-
from testgres.logger import log
1312

1413
from .os_ops import OsOperations
1514
from .os_ops import pglib
@@ -90,9 +89,9 @@ def _read_ssh_key(self):
9089
key = paramiko.RSAKey.from_private_key_file(self.ssh_key)
9190
return key
9291
except FileNotFoundError:
93-
log.error(f"No such file or directory: '{self.ssh_key}'")
92+
raise ExecUtilException(message=f"No such file or directory: '{self.ssh_key}'")
9493
except Exception as e:
95-
log.error(f"An error occurred while reading the ssh key: {e}")
94+
ExecUtilException(message=f"An error occurred while reading the ssh key: {e}")
9695

9796
def exec_command(self, cmd: str, wait_exit=False, verbose=False, expect_error=False,
9897
encoding=None, shell=True, text=False, input=None, stdout=None,
@@ -400,7 +399,7 @@ def get_pid(self):
400399
# Get current process id
401400
return int(self.exec_command("echo $$", encoding='utf-8'))
402401

403-
def get_remote_children(self, pid):
402+
def get_process_children(self, pid):
404403
command = f"pgrep -P {pid}"
405404
stdin, stdout, stderr = self.ssh.exec_command(command)
406405
children = stdout.readlines()
@@ -414,8 +413,7 @@ def db_connect(self, dbname, user, password=None, host="127.0.0.1", port=5432, s
414413
- dbname (str): The name of the database to connect to.
415414
- user (str): The username for the database connection.
416415
- password (str, optional): The password for the database connection. Defaults to None.
417-
- host (str, optional): The IP address of the remote system. Defaults to "127.0.0.1".
418-
- hostname (str, optional): The hostname of the remote system. Defaults to "localhost".
416+
- host (str, optional): The IP address of the remote system. Defaults to "localhost".
419417
- port (int, optional): The port number of the PostgreSQL service. Defaults to 5432.
420418
421419
This function establishes a connection to a PostgreSQL database on the remote system using the specified
@@ -444,4 +442,4 @@ def db_connect(self, dbname, user, password=None, host="127.0.0.1", port=5432, s
444442
return conn
445443
except Exception as e:
446444
self.tunnel.stop()
447-
raise e
445+
raise ExecUtilException("Could not create db tunnel.")

testgres/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212

1313
from six import iteritems
1414

15-
15+
from .exceptions import ExecUtilException
1616
from .config import testgres_config as tconf
17-
from .logger import log
1817

1918
# rows returned by PG_CONFIG
2019
_pg_config_data = {}
@@ -73,7 +72,7 @@ def execute_utility(args, logfile=None, verbose=False):
7372
lines = [u'\n'] + ['# ' + line for line in out.splitlines()] + [u'\n']
7473
tconf.os_ops.write(filename=logfile, data=lines)
7574
except IOError:
76-
log.warn(f"Problem with writing to logfile `{logfile}` during run command `{args}`")
75+
raise ExecUtilException(f"Problem with writing to logfile `{logfile}` during run command `{args}`")
7776
if verbose:
7877
return exit_status, out, error
7978
else:

tests/test_remote.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def setup(self):
1111
self.operations = RemoteOperations(
1212
host="172.18.0.3",
1313
username="dev",
14-
ssh_key='/home/vika/Desktop/work/probackup/dev-ee-probackup/container_files/postgres/ssh/id_ed25519'
14+
ssh_key='../../container_files/postgres/ssh/id_ed25519'
1515
)
1616

1717
yield

tests/test_simple.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ def test_init_unique_system_id(self):
151151
self.assertGreater(id2, id1)
152152

153153
def test_node_exit(self):
154+
base_dir = None
155+
154156
with self.assertRaises(QueryException):
155157
with get_new_node().init() as node:
156158
base_dir = node.base_dir
@@ -252,27 +254,27 @@ def test_psql(self):
252254

253255
# check returned values (1 arg)
254256
res = node.psql('select 1')
255-
self.assertEqual((0, b'1\n', b''), res)
257+
self.assertEqual(res, (0, b'1\n', b''))
256258

257259
# check returned values (2 args)
258260
res = node.psql('postgres', 'select 2')
259-
self.assertEqual((0, b'2\n', b''), res)
261+
self.assertEqual(res, (0, b'2\n', b''))
260262

261263
# check returned values (named)
262264
res = node.psql(query='select 3', dbname='postgres')
263-
self.assertEqual((0, b'3\n', b''), res)
265+
self.assertEqual(res, (0, b'3\n', b''))
264266

265267
# check returned values (1 arg)
266268
res = node.safe_psql('select 4')
267-
self.assertEqual(b'4\n', res)
269+
self.assertEqual(res, b'4\n')
268270

269271
# check returned values (2 args)
270272
res = node.safe_psql('postgres', 'select 5')
271-
self.assertEqual(b'5\n', res)
273+
self.assertEqual(res, b'5\n')
272274

273275
# check returned values (named)
274276
res = node.safe_psql(query='select 6', dbname='postgres')
275-
self.assertEqual(b'6\n', res)
277+
self.assertEqual(res, b'6\n')
276278

277279
# check feeding input
278280
node.safe_psql('create table horns (w int)')
@@ -612,7 +614,7 @@ def test_users(self):
612614
with get_new_node().init().start() as node:
613615
node.psql('create role test_user login')
614616
value = node.safe_psql('select 1', username='test_user')
615-
self.assertEqual(b'1\n', value)
617+
self.assertEqual(value, b'1\n')
616618

617619
def test_poll_query_until(self):
618620
with get_new_node() as node:

tests/test_simple_remote.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
NodeStatus, \
3636
ProcessType, \
3737
IsolationLevel, \
38-
get_new_node, RemoteOperations
38+
get_new_node, \
39+
RemoteOperations
3940

4041
from testgres import \
4142
get_bin_path, \
@@ -54,7 +55,7 @@
5455

5556
os_ops = RemoteOperations(host='172.18.0.3',
5657
username='dev',
57-
ssh_key='/home/vika/Desktop/work/probackup/dev-ee-probackup/container_files/postgres/ssh/id_ed25519')
58+
ssh_key='../../container_files/postgres/ssh/id_ed25519')
5859
testgres_config.set_os_ops(os_ops=os_ops)
5960

6061

@@ -92,8 +93,8 @@ def removing(f):
9293
os_ops.rmdirs(f, ignore_errors=True)
9394

9495

95-
def get_remote_node():
96-
return get_new_node(host=os_ops.host, username=os_ops.username, ssh_key=os_ops.ssh_key)
96+
def get_remote_node(name=None):
97+
return get_new_node(name=name, host=os_ops.host, username=os_ops.username, ssh_key=os_ops.ssh_key)
9798

9899

99100
class TestgresRemoteTests(unittest.TestCase):
@@ -696,7 +697,7 @@ def test_logging(self):
696697
'handlers': {
697698
'file': {
698699
'class': 'logging.FileHandler',
699-
'filename': logfile.name,
700+
'filename': logfile,
700701
'formatter': 'base_format',
701702
'level': logging.DEBUG,
702703
},
@@ -717,7 +718,7 @@ def test_logging(self):
717718
with scoped_config(use_python_logging=True):
718719
node_name = 'master'
719720

720-
with get_new_node(name=node_name) as master:
721+
with get_remote_node(name=node_name) as master:
721722
master.init().start()
722723

723724
# execute a dummy query a few times
@@ -729,7 +730,7 @@ def test_logging(self):
729730
time.sleep(0.1)
730731

731732
# check that master's port is found
732-
with open(logfile.name, 'r') as log:
733+
with open(logfile, 'r') as log:
733734
lines = log.readlines()
734735
self.assertTrue(any(node_name in s for s in lines))
735736

0 commit comments

Comments
 (0)