Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
envsetup
Manage
Activity
Members
Plan
Redmine
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mediaserver
envsetup
Commits
d8b320ff
Commit
d8b320ff
authored
6 years ago
by
Nicolas KAROLAK
Browse files
Options
Downloads
Patches
Plain Diff
change(test_postgresql): return traceback message
parent
b21fb283
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/test_postgresql.py
+24
-24
24 additions, 24 deletions
tests/test_postgresql.py
with
24 additions
and
24 deletions
tests/test_postgresql.py
+
24
−
24
View file @
d8b320ff
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment