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