From 47d9a8b9bee1764c9c7e9c9f3f861cca5e1dd7c7 Mon Sep 17 00:00:00 2001 From: stalkerg Date: Thu, 15 Dec 2016 13:47:28 +0300 Subject: [PATCH 1/4] Add additional params for stop, start, restart, reload node. --- testgres/testgres.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/testgres/testgres.py b/testgres/testgres.py index 72af64b2..7ac07162 100644 --- a/testgres/testgres.py +++ b/testgres/testgres.py @@ -303,46 +303,50 @@ def pg_ctl(self, command, params): with open(self.error_filename, "r") as errfile: raise ClusterException(errfile.readlines()[-1]) - def start(self): + def start(self, params={}): """ Starts cluster """ logfile = os.path.join(self.logs_dir, "postgresql.log") - params = { + _params = { "-D": self.data_dir, "-w": None, "-l": logfile, } - self.pg_ctl("start", params) + _params.update(params) + self.pg_ctl("start", _params) self.working = True return self - def stop(self): + def stop(self, params={}): """ Stops cluster """ - params = { + _params = { "-D": self.data_dir, "-w": None } - self.pg_ctl("stop", params) + _params.update(params) + self.pg_ctl("stop", _params) self.working = False return self - def restart(self): + def restart(self, params={}): """ Restarts cluster """ - params = { + _params = { "-D": self.data_dir, "-w": None } - self.pg_ctl("restart", params) + _params.update(params) + self.pg_ctl("restart", _params) return self - def reload(self): + def reload(self, params={}): """Reloads config files""" - params = {"-D": self.data_dir} - self.pg_ctl("reload", params) + _params = {"-D": self.data_dir} + _params.update(params) + self.pg_ctl("reload", _params) return self From 1426718ee195bf873fb1772e4ec434bc62112d1d Mon Sep 17 00:00:00 2001 From: stalkerg Date: Thu, 15 Dec 2016 13:52:20 +0300 Subject: [PATCH 2/4] Add command options for pg_ctl (for implement KILL). --- testgres/testgres.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testgres/testgres.py b/testgres/testgres.py index 7ac07162..ff79a04b 100644 --- a/testgres/testgres.py +++ b/testgres/testgres.py @@ -277,7 +277,7 @@ def append_conf(self, filename, string): return self - def pg_ctl(self, command, params): + def pg_ctl(self, command, params, command_options=[]): """Runs pg_ctl with specified params This function is a workhorse for start(), stop() and reload() @@ -294,7 +294,7 @@ def pg_ctl(self, command, params): open(self.error_filename, "a") as file_err: res = subprocess.call( - arguments + [command], + arguments + [command] + command_options, stdout=file_out, stderr=file_err ) From 8eb59069f732bc1730dc8974a467ffde9fdd378d Mon Sep 17 00:00:00 2001 From: stalkerg Date: Thu, 15 Dec 2016 13:57:36 +0300 Subject: [PATCH 3/4] Add to execute func commit param. --- testgres/testgres.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/testgres/testgres.py b/testgres/testgres.py index ff79a04b..c0c97954 100644 --- a/testgres/testgres.py +++ b/testgres/testgres.py @@ -452,10 +452,13 @@ def poll_query_until(self, dbname, query): attemps += 1 raise QueryException("Timeout while waiting for query to return True") - def execute(self, dbname, query, username=None): + def execute(self, dbname, query, username=None, commit=False): """Executes the query and returns all rows""" with self.connect(dbname, username) as node_con: - return node_con.execute(query) + res = node_con.execute(query) + if commit: + node_con.commit() + return res def backup(self, name): """Performs pg_basebackup""" From 203f394a758e0a2d33d13ec792beca7297b53514 Mon Sep 17 00:00:00 2001 From: stalkerg Date: Thu, 15 Dec 2016 14:05:28 +0300 Subject: [PATCH 4/4] If server itself down then pg_ctl stop return error. We must catch exception. --- testgres/testgres.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/testgres/testgres.py b/testgres/testgres.py index c0c97954..af784b91 100644 --- a/testgres/testgres.py +++ b/testgres/testgres.py @@ -355,7 +355,10 @@ def cleanup(self): # stop server if it still working if self.working: - self.stop() + try: + self.stop() + except: + pass # remove data directory shutil.rmtree(self.data_dir)