Skip to content

Commit 59e8c81

Browse files
committed
Renamed inp to input, added test for it, fixed restart logging.
node.restart() logging was previously broken: -l option was not supplied, so its stdout/err wasn't redirected -- this is option of pg_ctl, not of postgres itself, and pg_ctl didn't reused it. That was also the reason of 'write_to_pipe' existence: if pg_ctl restart's stdout was fed to pipe, communicate() hanged, reading stdout of postgres infinitely. Now we ride on 'start' routine for restart and get the same behaviour, including logging. 'inp' param of safe_psql and psql was renamed to input for consistency (we don't use here standard input() function quite often, right?), test for it was added.
1 parent 43cb1d1 commit 59e8c81

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

testgres/testgres.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -677,11 +677,12 @@ def get_control_data(self):
677677

678678
return out_dict
679679

680-
def start(self, params=[]):
680+
def start(self, restart=False, params=[]):
681681
"""
682-
Start this node using pg_ctl.
682+
(Re)start this node using pg_ctl.
683683
684684
Args:
685+
restart: restart or start?
685686
params: additional arguments for _execute_utility().
686687
687688
Returns:
@@ -706,8 +707,9 @@ def start(self, params=[]):
706707
# choose recovery_filename
707708
recovery_filename = os.path.join(self.data_dir, "recovery.conf")
708709

710+
action = "restart" if restart else "start"
709711
_params = [
710-
"start",
712+
action,
711713
"-D{}".format(self.data_dir),
712714
"-l{}".format(log_filename),
713715
"-w"
@@ -727,7 +729,7 @@ def print_node_file(node_file):
727729
return "### file not found ###\n"
728730

729731
error_text = (
730-
u"Cannot start node\n"
732+
u"Cannot {} node\n".format(action) +
731733
u"{}\n" # pg_ctl log
732734
u"{}:\n----\n{}\n" # postgresql.log
733735
u"{}:\n----\n{}\n" # postgresql.conf
@@ -773,10 +775,10 @@ def restart(self, params=[]):
773775
This instance of PostgresNode.
774776
"""
775777

776-
_params = ["restart", "-D", self.data_dir, "-w"] + params
777-
_execute_utility("pg_ctl", _params,
778-
self.utils_logname,
779-
write_to_pipe=True)
778+
if self.logger:
779+
self.logger.stop()
780+
781+
self.start(restart=True, params=params)
780782

781783
return self
782784

@@ -845,7 +847,7 @@ def cleanup(self, max_attempts=3):
845847

846848
return self
847849

848-
def psql(self, dbname, query=None, filename=None, username=None, inp=None):
850+
def psql(self, dbname, query=None, filename=None, username=None, input=None):
849851
"""
850852
Execute a query using psql.
851853
@@ -886,10 +888,10 @@ def psql(self, dbname, query=None, filename=None, username=None, inp=None):
886888
stderr=subprocess.PIPE)
887889

888890
# wait until it finishes and get stdout and stderr
889-
out, err = process.communicate(input=inp)
891+
out, err = process.communicate(input=input)
890892
return process.returncode, out, err
891893

892-
def safe_psql(self, dbname, query, username=None, inp=None):
894+
def safe_psql(self, dbname, query, username=None, input=None):
893895
"""
894896
Execute a query using psql.
895897
@@ -902,7 +904,7 @@ def safe_psql(self, dbname, query, username=None, inp=None):
902904
psql's output as str.
903905
"""
904906

905-
ret, out, err = self.psql(dbname, query, username=username, inp=inp)
907+
ret, out, err = self.psql(dbname, query, username=username, input=input)
906908
if ret:
907909
err = '' if not err else err.decode('utf-8')
908910
raise QueryException(err)
@@ -1201,29 +1203,24 @@ def rm_cached_data_dir(rm_dir):
12011203
raise InitNodeException(_explain_exception(e))
12021204

12031205

1204-
def _execute_utility(util, args, logfile, write_to_pipe=True):
1206+
def _execute_utility(util, args, logfile):
12051207
"""
12061208
Execute utility (pg_ctl, pg_dump etc) using get_bin_path().
12071209
12081210
Args:
12091211
util: utility to be executed.
12101212
args: arguments for utility (list).
12111213
logfile: path to file to store stdout and stderr.
1212-
write_to_pipe: do we care about stdout?
12131214
12141215
Returns:
12151216
stdout of executed utility.
12161217
"""
12171218

12181219
# we can't use subprocess.DEVNULL on 2.7
12191220
with open(os.devnull, "w") as devnull:
1220-
1221-
# choose file according to options
1222-
stdout_file = subprocess.PIPE if write_to_pipe else devnull
1223-
12241221
# run utility
12251222
process = subprocess.Popen([get_bin_path(util)] + args,
1226-
stdout=stdout_file,
1223+
stdout=subprocess.PIPE,
12271224
stderr=subprocess.STDOUT)
12281225

12291226
# get result

tests/test_simple.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ def test_psql(self):
102102
res = node.safe_psql('postgres', 'select 1')
103103
self.assertEqual(res, b'1\n')
104104

105+
# check feeding input
106+
node.safe_psql('postgres', 'create table horns (w int)')
107+
node.safe_psql('postgres', 'copy horns from stdin (format csv)',
108+
input=
109+
b"""1
110+
2
111+
3
112+
\.
113+
""")
114+
sum = node.safe_psql('postgres', 'select sum(w) from horns')
115+
self.assertEqual(sum, b'6\n')
116+
node.safe_psql('postgres', 'drop table horns')
117+
105118
node.stop()
106119

107120
# check psql on stopped node

0 commit comments

Comments
 (0)