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
7eaf7828
Commit
7eaf7828
authored
6 years ago
by
Nicolas KAROLAK
Browse files
Options
Downloads
Patches
Plain Diff
change(test_postgresql): clean, comment, format
parent
6bef8835
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
+35
-99
35 additions, 99 deletions
tests/test_postgresql.py
with
35 additions
and
99 deletions
tests/test_postgresql.py
+
35
−
99
View file @
7eaf7828
...
@@ -12,7 +12,10 @@ import subprocess
...
@@ -12,7 +12,10 @@ import subprocess
import
sys
import
sys
import
time
import
time
import
psycopg2
try
:
import
psycopg2
except
ImportError
:
sys
.
exit
(
2
)
GREEN
=
"
\033
[92m
"
GREEN
=
"
\033
[92m
"
YELLOW
=
"
\033
[93m
"
YELLOW
=
"
\033
[93m
"
...
@@ -134,20 +137,6 @@ def get_nodes(conf: dict) -> dict:
...
@@ -134,20 +137,6 @@ def get_nodes(conf: dict) -> dict:
return
servers
return
servers
def
check_odd_number
(
number
:
int
)
->
bool
:
"""
Check if we have an odd number of nodes, ensuring we can have a quorum.
:param number: The number of nodes in the cluster
:type number: int
:return: Wether it as an odd number or not
:rtype: bool
"""
modulo
=
number
%
2
return
modulo
!=
0
def
get_node_state
(
host
:
str
,
port
:
int
)
->
str
:
def
get_node_state
(
host
:
str
,
port
:
int
)
->
str
:
"""
Get the curent state of node from its RepHACheck daemon.
"""
Get the curent state of node from its RepHACheck daemon.
...
@@ -170,87 +159,55 @@ def get_node_state(host: str, port: int) -> str:
...
@@ -170,87 +159,55 @@ def get_node_state(host: str, port: int) -> str:
return
state
return
state
def
check_primary
(
nodes
:
dict
)
->
tuple
:
def
check_fenced
(
nodes
:
dict
)
->
tuple
:
"""
Check if we have a primary in the nodes.
"""
Check if the cluster have a fenced node.
:param nodes: The dictionary containing nodes and their informations
:type nodes: dict
:return: Wether the nodes list contains a primary server
:rtype: tuple
"""
for
node
in
nodes
.
keys
():
host
=
nodes
[
node
][
"
host
"
]
port
=
int
(
nodes
[
node
][
"
rephacheck
"
])
if
get_node_state
(
host
,
port
)
==
"
primary
"
:
return
True
,
node
return
False
,
None
def
check_standby
(
nodes
:
dict
)
->
tuple
:
"""
Check if we have a standby in the nodes.
:param nodes: The dictionary containing nodes and their informations
:type nodes: dict
:return: Wether the nodes list contains a standby server
:rtype: tuple
"""
for
node
in
nodes
.
keys
():
host
=
nodes
[
node
][
"
host
"
]
port
=
int
(
nodes
[
node
][
"
rephacheck
"
])
if
get_node_state
(
host
,
port
)
==
"
standby
"
:
return
True
,
node
return
False
,
None
def
check_witness
(
nodes
:
dict
)
->
tuple
:
"""
Check if we have a witness in the nodes.
:param nodes: The dictionary containing nodes and their informations
:param nodes: The dictionary containing nodes and their informations
:type nodes: dict
:type nodes: dict
:return: Wether the nodes list contains a
witness
server
:return: Wether the nodes list contains a
fenced
server
:rtype: tuple
:rtype: tuple
"""
"""
for
node
in
nodes
.
keys
():
for
node
in
nodes
.
keys
():
host
=
nodes
[
node
][
"
host
"
]
host
=
nodes
[
node
][
"
host
"
]
port
=
int
(
nodes
[
node
][
"
rephacheck
"
])
port
=
int
(
nodes
[
node
][
"
rephacheck
"
])
if
get_node_state
(
host
,
port
)
==
"
witness
"
:
if
get_node_state
(
host
,
port
)
==
"
fenced
"
:
return
True
,
node
return
True
,
node
return
False
,
None
return
False
,
None
def
check_
fenced
(
nodes
:
dic
t
)
->
tuple
:
def
check_
listen
(
host
:
str
,
port
:
in
t
)
->
bool
:
"""
Check if
the cluster have a fenced node
.
"""
Check if
server is listening (TCP only)
.
:param nodes: The dictionary containing nodes and their informations
:param host: The hostname or IP address to bind
:type nodes: dict
:param port: The port number to bind
:return: Wether the nodes list contains a fenced server
:type host: str
:rtype: tuple
:type port: int
:return: Wether the `host` is listening on TCP/`port`
:rtype: bool
"""
"""
for
node
in
nodes
.
keys
():
# try to connect to the port used by psql-primary frontend
host
=
nodes
[
node
][
"
host
"
]
client
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
port
=
int
(
nodes
[
node
][
"
rephacheck
"
])
result
=
client
.
connect_ex
((
host
,
port
))
if
get_node_state
(
host
,
port
)
==
"
fenced
"
:
client
.
close
()
return
True
,
node
return
False
,
None
return
result
==
0
def
check_psql
(
db_conn
:
dict
,
query
:
str
)
->
bool
:
def
check_psql
(
db_conn
:
dict
,
query
:
str
)
->
bool
:
"""
Check if we can write data on this node.
"""
Check if we can write data on this node.
:param db_conn: Connection details
:param db_conn: Database connection parameters
:type db_conn: str
:type db_conn: dict
:param query: Query to execute
:type query: str
:return: Wether the query can be executed or not
:return: Wether the query can be executed or not
:rtype: bool
:rtype: bool
"""
"""
# build database connection uri
if
"
password
"
in
db_conn
:
if
"
password
"
in
db_conn
:
uri
=
"
postgresql://{}:{}@{}:{}/{}
"
.
format
(
uri
=
"
postgresql://{}:{}@{}:{}/{}
"
.
format
(
db_conn
[
"
user
"
],
db_conn
[
"
user
"
],
...
@@ -262,8 +219,10 @@ def check_psql(db_conn: dict, query: str) -> bool:
...
@@ -262,8 +219,10 @@ def check_psql(db_conn: dict, query: str) -> bool:
else
:
else
:
uri
=
"
postgresql:///{}
"
.
format
(
db_conn
[
"
dbname
"
])
uri
=
"
postgresql:///{}
"
.
format
(
db_conn
[
"
dbname
"
])
# format command
command
=
[
"
su -l postgres -c
\"
psql {} -c
'
{}
'
\"
"
.
format
(
uri
,
query
)]
command
=
[
"
su -l postgres -c
\"
psql {} -c
'
{}
'
\"
"
.
format
(
uri
,
query
)]
# execute
try
:
try
:
subprocess
.
check_output
(
command
,
shell
=
True
)
subprocess
.
check_output
(
command
,
shell
=
True
)
except
subprocess
.
CalledProcessError
:
except
subprocess
.
CalledProcessError
:
...
@@ -275,9 +234,9 @@ def check_psql(db_conn: dict, query: str) -> bool:
...
@@ -275,9 +234,9 @@ def check_psql(db_conn: dict, query: str) -> bool:
def
check_replication
(
primary
:
dict
,
standby
:
dict
)
->
bool
:
def
check_replication
(
primary
:
dict
,
standby
:
dict
)
->
bool
:
"""
Check if replication is working between the primary and standby servers.
"""
Check if replication is working between the primary and standby servers.
:param primary:
C
onnection
detail
s for primary server
:param primary:
Database c
onnection
parameter
s for primary server
:type primary: dict
:type primary: dict
:param standby:
C
onnection
detail
s for standby server
:param standby:
Database c
onnection
parameter
s for standby server
:type standby: dict
:type standby: dict
:return: Wether replication between primary/stanbdy is working or not
:return: Wether replication between primary/stanbdy is working or not
:rtype: bool
:rtype: bool
...
@@ -311,25 +270,6 @@ def check_replication(primary: dict, standby: dict) -> bool:
...
@@ -311,25 +270,6 @@ def check_replication(primary: dict, standby: dict) -> bool:
return
True
return
True
def
check_listen
(
host
:
str
,
port
:
int
)
->
bool
:
"""
Check if server is listening (TCP only).
:param host: The hostname or IP address to bind
:param port: The port number to bind
:type host: str
:type port: int
:return: Wether the `host` is listening on TCP/`port`
:rtype: bool
"""
# try to connect to the port used by psql-primary frontend
client
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
result
=
client
.
connect_ex
((
host
,
port
))
client
.
close
()
return
result
==
0
def
check_ha
(
db_conn
:
dict
,
errors
:
int
=
0
)
->
int
:
def
check_ha
(
db_conn
:
dict
,
errors
:
int
=
0
)
->
int
:
"""
Run all tests for a highly-available setup.
"""
Run all tests for a highly-available setup.
...
@@ -402,8 +342,6 @@ def check_local(db_conn: dict, errors: int = 0) -> int:
...
@@ -402,8 +342,6 @@ def check_local(db_conn: dict, errors: int = 0) -> int:
:type db_conn: dict
:type db_conn: dict
:param errors: Error counter, defaults to 0
:param errors: Error counter, defaults to 0
:param errors: int, optional
:param errors: int, optional
:param warnings: Warning counter, defaults to 0
:param warnings: int, optional
:return: Number of errors
:return: Number of errors
:rtype: int
:rtype: int
"""
"""
...
@@ -422,8 +360,8 @@ def check_local(db_conn: dict, errors: int = 0) -> int:
...
@@ -422,8 +360,8 @@ def check_local(db_conn: dict, errors: int = 0) -> int:
# check read
# check read
print
(
"
Checking read operation:
"
)
print
(
"
Checking read operation:
"
)
rquery
=
"
SELECT 1;
"
r
ead_
query
=
"
SELECT 1;
"
if
not
check_psql
(
db_conn
,
rquery
):
if
not
check_psql
(
db_conn
,
r
ead_
query
):
error
(
"
Cannot read data on {}@{}:{}
"
.
format
(
db_user
,
db_host
,
db_port
))
error
(
"
Cannot read data on {}@{}:{}
"
.
format
(
db_user
,
db_host
,
db_port
))
errors
+=
1
errors
+=
1
else
:
else
:
...
@@ -431,8 +369,8 @@ def check_local(db_conn: dict, errors: int = 0) -> int:
...
@@ -431,8 +369,8 @@ def check_local(db_conn: dict, errors: int = 0) -> int:
# check write
# check write
print
(
"
Checking write operation:
"
)
print
(
"
Checking write operation:
"
)
wquery
=
"
CREATE TABLE es_test (id serial PRIMARY KEY);
"
w
rite_
query
=
"
CREATE TABLE es_test (id serial PRIMARY KEY);
"
if
not
check_psql
(
db_conn
,
wquery
):
if
not
check_psql
(
db_conn
,
w
rite_
query
):
error
(
"
Cannot write data on {}@{}:{}
"
.
format
(
db_user
,
db_host
,
db_port
))
error
(
"
Cannot write data on {}@{}:{}
"
.
format
(
db_user
,
db_host
,
db_port
))
errors
+=
1
errors
+=
1
else
:
else
:
...
@@ -470,14 +408,12 @@ def main():
...
@@ -470,14 +408,12 @@ def main():
if
db_pass
:
if
db_pass
:
db_conn
.
update
({
"
password
"
:
db_pass
})
db_conn
.
update
({
"
password
"
:
db_pass
})
# determine if HA setup
# determine if HA setup
and run according tests
if
is_ha
(
db_port
):
if
is_ha
(
db_port
):
print
(
"
This setup is using a HA database
"
)
print
(
"
This setup is using a HA database
"
)
errors
=
check_ha
(
db_conn
)
errors
=
check_ha
(
db_conn
)
else
:
else
:
print
(
"
This setup is using a local database
"
)
print
(
"
This setup is using a local database
"
)
errors
=
check_local
(
db_conn
)
errors
=
check_local
(
db_conn
)
if
errors
:
if
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