Skip to content

Commit c8b8273

Browse files
committed
improve function get_control_data()
1 parent 394b1cc commit c8b8273

File tree

2 files changed

+12
-26
lines changed

2 files changed

+12
-26
lines changed

testgres/testgres.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,6 @@ class InitNodeException(Exception):
8484
pass
8585

8686

87-
class PgControlDataException(Exception):
88-
def __init__(self, error_text, cmd):
89-
self.error_text = error_text
90-
self.cmd = cmd
91-
92-
def __str__(self):
93-
self.error_text = repr(self.error_text)
94-
return '\n ERROR: {0}\n CMD: {1}'.format(self.error_text, self.cmd)
95-
96-
9787
class TestgresLogger(threading.Thread):
9888
"""
9989
Helper class to implement reading from postgresql.log
@@ -434,14 +424,10 @@ def get_control_data(self):
434424
Return contents of pg_control file
435425
"""
436426

427+
_params = ["-D", self.data_dir]
428+
lines = _execute_utility("pg_controldata", _params, self.utils_logname)
429+
437430
out_data = {}
438-
pg_controldata = get_bin_path("pg_controldata")
439-
try:
440-
lines = subprocess.check_output(
441-
[pg_controldata] + ["-D", self.data_dir],
442-
stderr=subprocess.STDOUT).decode("utf-8").splitlines()
443-
except subprocess.CalledProcessError as e:
444-
raise PgControlDataException(e.output, e.cmd)
445431

446432
for line in lines:
447433
key, value = line.partition(':')[::2]
@@ -751,23 +737,23 @@ def _execute_utility(util, args, logfile):
751737

752738
# get result of pg_ctl
753739
out, _ = process.communicate()
754-
755-
# decode logs
756-
log_text = out.decode('utf-8')
740+
out = out.decode('utf-8')
757741

758742
# write new log entry
759743
file_out.write(''.join(map(lambda x: str(x) + ' ', [util] + args)))
760744
file_out.write('\n')
761-
file_out.write(log_text)
745+
file_out.write(out)
762746

763747
if process.returncode:
764748
error_text = (
765749
"{} failed\n"
766750
"log:\n----\n{}\n"
767-
).format(util, out.decode('utf-8'))
751+
).format(util, out)
768752

769753
raise ExecUtilException(error_text, process.returncode)
770754

755+
return out
756+
771757

772758
def get_bin_path(filename):
773759
"""

testgres/tests/test_simple.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from distutils.version import LooseVersion
1111

12-
from testgres import InitNodeException, StartNodeException, PgControlDataException
12+
from testgres import InitNodeException, StartNodeException, ExecUtilException
1313
from testgres import get_new_node, get_pg_config
1414
from testgres import bound_ports
1515
from testgres import NodeStatus
@@ -134,7 +134,7 @@ def test_control_data(self):
134134

135135
try:
136136
node.get_control_data()
137-
except PgControlDataException as e:
137+
except ExecUtilException as e:
138138
got_exception = True
139139
except Exception as e:
140140
pass
@@ -146,8 +146,8 @@ def test_control_data(self):
146146
node.init()
147147
data = node.get_control_data()
148148
self.assertIsNotNode(data)
149-
except PgControlDataException as e:
150-
print(e)
149+
except ExecUtilException as e:
150+
print(e.message)
151151
got_exception = True
152152
except Exception as e:
153153
pass

0 commit comments

Comments
 (0)