Skip to content
Snippets Groups Projects
Commit d8b320ff authored by Nicolas KAROLAK's avatar Nicolas KAROLAK
Browse files

change(test_postgresql): return traceback message

parent b21fb283
No related branches found
No related tags found
No related merge requests found
......@@ -197,7 +197,7 @@ def check_listen(host: str, port: int) -> bool:
return result == 0
def check_psql(db_conn: dict, query: str) -> bool:
def check_psql(db_conn: dict, query: str) -> tuple:
"""Check if we can write data on this node.
:param db_conn: Database connection parameters
......@@ -205,7 +205,7 @@ def check_psql(db_conn: dict, query: str) -> bool:
:param query: Query to execute
:type query: str
:return: Wether the query can be executed or not
:rtype: bool
:rtype: tuple
"""
# build database connection uri
......@@ -226,10 +226,10 @@ def check_psql(db_conn: dict, query: str) -> bool:
# execute
try:
subprocess.check_output(command, shell=True)
except subprocess.CalledProcessError:
return False
except subprocess.CalledProcessError as psql_error:
return False, str(psql_error).rstrip()
return True
return True, "OK"
def check_replication(primary: dict, standby: dict) -> tuple:
......@@ -247,9 +247,8 @@ def check_replication(primary: dict, standby: dict) -> tuple:
try:
primary_client = psycopg2.connect(**primary)
standby_client = psycopg2.connect(**standby)
except psycopg2.Error:
msg = "connection error"
return False, msg
except psycopg2.Error as repl_conn_error:
return False, str(repl_conn_error).rstrip()
# random id
rand = uuid.uuid4().hex
......@@ -261,9 +260,8 @@ def check_replication(primary: dict, standby: dict) -> tuple:
try:
primary_psql = primary_client.cursor()
primary_psql.execute(write_query)
except psycopg2.Error:
msg = "write error"
return False, msg
except psycopg2.Error as repl_write_error:
return False, str(repl_write_error).rstrip()
# read
max_time = 6.0
......@@ -276,10 +274,9 @@ def check_replication(primary: dict, standby: dict) -> tuple:
standby_psql.execute(read_query)
msg = "took ~{}s".format(str(timer))
break
except psycopg2.Error:
pass
except psycopg2.Error as repl_read_error:
msg = str(repl_read_error).rstrip()
else:
msg = "read error"
return False, msg
# delete
......@@ -355,10 +352,10 @@ def check_ha(db_conn: dict, errors: int = 0) -> int:
standby["port"] = 54322
status, info = check_replication(primary, standby)
if not status:
error("Cannot replicate data between primary/standby ({})".format(info))
error("Cannot replicate between primary/standby ({})".format(info))
errors += 1
else:
success("Can replicate data between primary/standby ({})".format(info))
success("Can replicate between primary/standby ({})".format(info))
return errors
......@@ -389,22 +386,25 @@ def check_local(db_conn: dict, errors: int = 0) -> int:
# check read
print("Checking read operation:")
read_query = "SELECT 1;"
if not check_psql(db_conn, read_query):
error("Cannot read data on {}@{}:{}".format(db_user, db_host, db_port))
status, info = check_psql(db_conn, read_query)
if not status:
error("Cannot read from {}@{}:{} ({})".format(db_user, db_host, db_port, info))
errors += 1
else:
success("Can read data on {}@{}:{}".format(db_user, db_host, db_port))
success("Can read from {}@{}:{}".format(db_user, db_host, db_port))
# check write
print("Checking write operation:")
write_query = "CREATE TABLE es_test (id serial PRIMARY KEY);"
if not check_psql(db_conn, write_query):
error("Cannot write data on {}@{}:{}".format(db_user, db_host, db_port))
rand = uuid.uuid4().hex
write_query = "CREATE TABLE es_test_{} (id serial PRIMARY KEY);".format(rand)
status, info = check_psql(db_conn, write_query)
if not status:
error("Cannot write on {}@{}:{} ({})".format(db_user, db_host, db_port, info))
errors += 1
else:
success("Can write data on {}@{}:{}".format(db_user, db_host, db_port))
success("Can write on {}@{}:{}".format(db_user, db_host, db_port))
# remove test table
check_psql(db_conn, "DROP TABLE es_test;")
check_psql(db_conn, "DROP TABLE es_test_{};".format(rand))
return errors
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment