Skip to content

Couple of new methods and minor fixes(again!) #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 3, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions testgres/testgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ class QueryException(Exception):
pass


class PgcontroldataException(Exception):
def __init__(self, error_text, cmd):
self.error_text = error_text
self.cmd = cmd

def __str__(self):
self.error_text = repr(self.error_text)
return '\n ERROR: {0}\n CMD: {1}'.format(self.error_text, self.cmd)


class NodeConnection(object):

"""
Expand Down Expand Up @@ -277,14 +287,15 @@ def append_conf(self, filename, string):

return self

def pg_ctl(self, command, params, command_options=[]):
def pg_ctl(self, command, params={}, command_options=[]):
"""Runs pg_ctl with specified params

This function is a workhorse for start(), stop() and reload()
This function is a workhorse for start(), stop(), reload() and status()
functions"""
pg_ctl = self.get_bin_path("pg_ctl")

arguments = [pg_ctl]
arguments.append(command)
for key, value in six.iteritems(params):
arguments.append(key)
if value:
Expand All @@ -294,7 +305,7 @@ def pg_ctl(self, command, params, command_options=[]):
open(self.error_filename, "a") as file_err:

res = subprocess.call(
arguments + [command] + command_options,
arguments + command_options,
stdout=file_out,
stderr=file_err
)
Expand All @@ -318,6 +329,55 @@ def start(self, params={}):

return self

def status(self):
""" Check cluster status
If Running - return True
If not Running - return False
If there is no data in data_dir or data_dir do not exists - return None
"""
try:
res = subprocess.check_output([
self.get_bin_path("pg_ctl"), 'status', '-D', '{0}'.format(self.data_dir)
])
return True
except subprocess.CalledProcessError as e:
if e.returncode == 3:
# Not Running
self.working = False
return False
elif e.returncode == 4:
# No data or directory do not exists
self.working = False
return None

def get_pid(self):
""" Check that node is running and return postmaster pid
Otherwise return None
"""
if self.status():
file = open(os.path.join(self.data_dir, 'postmaster.pid'))
pid = int(file.readline())
return pid
else:
return None

def get_control_data(self):
""" Return pg_control content """
out_data = {}
pg_controldata = self.get_bin_path("pg_controldata")
try:
lines = subprocess.check_output(
[pg_controldata] + ["-D", self.data_dir],
stderr=subprocess.STDOUT
).splitlines()
except subprocess.CalledProcessError as e:
raise PgcontroldataException(e.output, e.cmd)

for line in lines:
key, value = line.partition(':')[::2]
out_data[key.strip()] = value.strip()
return out_data

def stop(self, params={}):
""" Stops cluster """
_params = {
Expand Down Expand Up @@ -375,7 +435,7 @@ def psql(self, dbname, query=None, filename=None, username=None):
"""
psql = self.get_bin_path("psql")
psql_params = [
psql, "-XAtq", "-p {}".format(self.port), dbname
psql, "-XAtq", "-h{}".format(self.host), "-p {}".format(self.port), dbname
]

if query:
Expand Down