Skip to content

Commit 8b6b813

Browse files
demonolockvshepard
and
vshepard
authored
Remove polling (#137)
* Change print on logging * Normalize error --------- Co-authored-by: vshepard <[email protected]>
1 parent 1d68e91 commit 8b6b813

File tree

9 files changed

+40
-24
lines changed

9 files changed

+40
-24
lines changed

testgres/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import atexit
44
import copy
5+
import logging
6+
import os
57
import tempfile
68

79
from contextlib import contextmanager
@@ -10,6 +12,10 @@
1012
from .operations.os_ops import OsOperations
1113
from .operations.local_ops import LocalOperations
1214

15+
log_level = os.getenv('LOGGING_LEVEL', 'WARNING').upper()
16+
log_format = os.getenv('LOGGING_FORMAT', '%(asctime)s - %(levelname)s - %(message)s').upper()
17+
logging.basicConfig(level=log_level, format=log_format)
18+
1319

1420
class GlobalConfig(object):
1521
"""

testgres/connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# coding: utf-8
2+
import logging
23

34
# we support both pg8000 and psycopg2
45
try:
@@ -110,7 +111,7 @@ def execute(self, query, *args):
110111
except ProgrammingError:
111112
return None
112113
except Exception as e:
113-
print("Error executing query: {}\n {}".format(repr(e), query))
114+
logging.error("Error executing query: {}\n {}".format(repr(e), query))
114115
return None
115116

116117
def close(self):

testgres/node.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# coding: utf-8
2-
2+
import logging
33
import os
44
import random
55
import signal
@@ -736,11 +736,10 @@ def start(self, params=[], wait=True):
736736
if any(len(file) > 1 and 'Is another postmaster already '
737737
'running on port' in file[1].decode() for
738738
file in files):
739-
print("Detected an issue with connecting to port {0}. "
740-
"Trying another port after a 5-second sleep...".format(self.port))
739+
logging.warning("Detected an issue with connecting to port {0}. "
740+
"Trying another port after a 5-second sleep...".format(self.port))
741741
self.port = reserve_port()
742-
options = {}
743-
options['port'] = str(self.port)
742+
options = {'port': str(self.port)}
744743
self.set_auto_conf(options)
745744
startup_retries -= 1
746745
time.sleep(5)
@@ -1166,7 +1165,6 @@ def poll_query_until(self,
11661165
assert sleep_time > 0
11671166
attempts = 0
11681167
while max_attempts == 0 or attempts < max_attempts:
1169-
print(f"Pooling {attempts}")
11701168
try:
11711169
res = self.execute(dbname=dbname,
11721170
query=query,
@@ -1190,6 +1188,7 @@ def poll_query_until(self,
11901188
return # done
11911189

11921190
except tuple(suppress or []):
1191+
logging.info(f"Trying execute, attempt {attempts + 1}.\nQuery: {query}")
11931192
pass # we're suppressing them
11941193

11951194
time.sleep(sleep_time)

testgres/operations/local_ops.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import getpass
2+
import logging
23
import os
34
import shutil
45
import stat
@@ -165,9 +166,9 @@ def rmdirs(self, path, ignore_errors=True, retries=3, delay=1):
165166
except FileNotFoundError:
166167
return True
167168
except Exception as e:
168-
print(f"Error: Failed to remove directory {path} on attempt {attempt + 1}: {e}")
169+
logging.error(f"Error: Failed to remove directory {path} on attempt {attempt + 1}: {e}")
169170
time.sleep(delay)
170-
print(f"Error: Failed to remove directory {path} after {retries} attempts.")
171+
logging.error(f"Error: Failed to remove directory {path} after {retries} attempts.")
171172
return False
172173

173174
def listdir(self, path):

testgres/operations/remote_ops.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
9393
if not error:
9494
error_found = 0
9595
else:
96+
error = normalize_error(error)
9697
error_found = exit_status != 0 or any(
97-
marker in error for marker in [b'error', b'Permission denied', b'fatal', b'No such file or directory'])
98+
marker in error for marker in ['error', 'Permission denied', 'fatal', 'No such file or directory']
99+
)
98100

99101
if error_found:
100102
if isinstance(error, bytes):
@@ -369,3 +371,9 @@ def db_connect(self, dbname, user, password=None, host="localhost", port=5432):
369371
password=password,
370372
)
371373
return conn
374+
375+
376+
def normalize_error(error):
377+
if isinstance(error, bytes):
378+
return error.decode()
379+
return error

testgres/plugins/pg_probackup2/pg_probackup2/app.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import contextlib
22
import importlib
33
import json
4+
import logging
45
import os
56
import re
67
import subprocess
@@ -74,7 +75,7 @@ def run(self, command, gdb=False, old_binary=False, return_id=True, env=None,
7475
command = [command[0], *self.backup_dir.pb_args, *command[1:]]
7576

7677
if not self.probackup_old_path and old_binary:
77-
print('PGPROBACKUPBIN_OLD is not set')
78+
logging.error('PGPROBACKUPBIN_OLD is not set')
7879
exit(1)
7980

8081
if old_binary:
@@ -107,12 +108,11 @@ def run(self, command, gdb=False, old_binary=False, return_id=True, env=None,
107108
return GDBobj(cmdline, self.test_class)
108109

109110
try:
110-
result = None
111111
if type(gdb) is tuple and gdb[0] == 'suspend':
112112
# special test flow for manually debug probackup
113113
gdb_port = gdb[1]
114114
cmdline = ['gdbserver'] + ['localhost:' + str(gdb_port)] + cmdline
115-
print("pg_probackup gdb suspended, waiting gdb connection on localhost:{0}".format(gdb_port))
115+
logging.warning("pg_probackup gdb suspended, waiting gdb connection on localhost:{0}".format(gdb_port))
116116

117117
start_time = time.time()
118118
self.test_class.output = subprocess.check_output(
@@ -233,7 +233,7 @@ def backup_node(
233233
if options is None:
234234
options = []
235235
if not node and not data_dir:
236-
print('You must provide ether node or data_dir for backup')
236+
logging.error('You must provide ether node or data_dir for backup')
237237
exit(1)
238238

239239
if not datname:
@@ -502,7 +502,7 @@ def show(
502502
if i == '':
503503
backup_record_split.remove(i)
504504
if len(header_split) != len(backup_record_split):
505-
print(warning.format(
505+
logging.error(warning.format(
506506
header=header, body=body,
507507
header_split=header_split,
508508
body_split=backup_record_split)
@@ -581,7 +581,7 @@ def show_archive(
581581
else:
582582
show_splitted = self.run(cmd_list + options, old_binary=old_binary,
583583
expect_error=expect_error).splitlines()
584-
print(show_splitted)
584+
logging.error(show_splitted)
585585
exit(1)
586586

587587
def validate(
@@ -769,7 +769,7 @@ def load_backup_class(fs_type):
769769
if fs_type:
770770
implementation = fs_type
771771

772-
print("Using ", implementation)
772+
logging.info("Using ", implementation)
773773
module_name, class_name = implementation.rsplit(sep='.', maxsplit=1)
774774

775775
module = importlib.import_module(module_name)

testgres/plugins/pg_probackup2/pg_probackup2/init_helpers.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from functools import reduce
23
import getpass
34
import os
@@ -31,7 +32,7 @@
3132
cached_initdb_dir=False,
3233
node_cleanup_full=delete_logs)
3334
except Exception as e:
34-
print("Can't configure testgres: {0}".format(e))
35+
logging.warning("Can't configure testgres: {0}".format(e))
3536

3637

3738
class Init(object):
@@ -104,7 +105,7 @@ def __init__(self):
104105

105106
if os.path.isfile(probackup_path_tmp):
106107
if not os.access(probackup_path_tmp, os.X_OK):
107-
print('{0} is not an executable file'.format(
108+
logging.warning('{0} is not an executable file'.format(
108109
probackup_path_tmp))
109110
else:
110111
self.probackup_path = probackup_path_tmp
@@ -114,13 +115,13 @@ def __init__(self):
114115

115116
if os.path.isfile(probackup_path_tmp):
116117
if not os.access(probackup_path_tmp, os.X_OK):
117-
print('{0} is not an executable file'.format(
118+
logging.warning('{0} is not an executable file'.format(
118119
probackup_path_tmp))
119120
else:
120121
self.probackup_path = probackup_path_tmp
121122

122123
if not self.probackup_path:
123-
print('pg_probackup binary is not found')
124+
logging.error('pg_probackup binary is not found')
124125
exit(1)
125126

126127
if os.name == 'posix':
@@ -207,7 +208,7 @@ def __init__(self):
207208
if self.probackup_version.split('.')[0].isdigit():
208209
self.major_version = int(self.probackup_version.split('.')[0])
209210
else:
210-
print('Can\'t process pg_probackup version \"{}\": the major version is expected to be a number'.format(self.probackup_version))
211+
logging.error('Can\'t process pg_probackup version \"{}\": the major version is expected to be a number'.format(self.probackup_version))
211212
sys.exit(1)
212213

213214
def test_env(self):

testgres/plugins/pg_probackup2/pg_probackup2/tests/basic_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import os
23
import shutil
34
import unittest
@@ -14,7 +15,7 @@ def get_module_and_function_name(test_id):
1415
module_name = test_id.split('.')[-2]
1516
fname = test_id.split('.')[-1]
1617
except IndexError:
17-
print(f"Couldn't get module name and function name from test_id: `{test_id}`")
18+
logging.warning(f"Couldn't get module name and function name from test_id: `{test_id}`")
1819
module_name, fname = test_id.split('(')[1].split('.')[1], test_id.split('(')[0]
1920
return module_name, fname
2021

testgres/utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ def eprint(*args, **kwargs):
228228
"""
229229
Print stuff to stderr.
230230
"""
231-
232231
print(*args, file=sys.stderr, **kwargs)
233232

234233

0 commit comments

Comments
 (0)