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
08bf31f7
Commit
08bf31f7
authored
3 years ago
by
Stéphane Diemer
Browse files
Options
Downloads
Patches
Plain Diff
Test PostgreSQL standby node only if available | refs
#33870
parent
30cc77cd
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/scripts/test_postgresql.py
+38
-36
38 additions, 36 deletions
tests/scripts/test_postgresql.py
with
38 additions
and
36 deletions
tests/scripts/test_postgresql.py
+
38
−
36
View file @
08bf31f7
...
...
@@ -82,15 +82,16 @@ def get_haproxy_conf(path: str = '/etc/haproxy/haproxy.cfg') -> dict:
return
conf
def
get_nodes
(
conf
:
dict
)
->
dict
:
def
get_nodes
()
->
dict
:
'''
Get the list of nodes from HAProxy configuration.
:param conf: The HAProxy configuration file content
:type conf: dict
:return: The list of nodes found in HAProxy configuration
:rtype: dict
'''
# get haproxy conf
conf
=
get_haproxy_conf
()
servers
=
{}
for
item
in
conf
.
keys
():
...
...
@@ -263,24 +264,22 @@ def check_ha(db_conn: dict, errors: int = 0) -> int:
:rtype: int
'''
# get haproxy conf
ha_conf
=
get_haproxy_conf
()
# get nodes data
nodes
=
get_nodes
(
ha_conf
)
nodes
=
get_nodes
()
# check haproxy
lg
.
log
(
'
Checking local HAProxy frontends:
'
)
if
not
check_listen
(
db_conn
[
'
host
'
],
54321
)
:
lg
.
error
(
'
HAProxy pgsql-primary frontend is not
listening
'
)
errors
+=
1
primary_listening
=
check_listen
(
db_conn
[
'
host
'
],
54321
)
if
primary_
listening
:
lg
.
success
(
'
HAProxy pgsql-primary frontend is listening.
'
)
else
:
lg
.
success
(
'
HAProxy pgsql-primary frontend is listening
'
)
if
not
check_listen
(
db_conn
[
'
host
'
],
54322
):
lg
.
error
(
'
HAProxy pgsql-standby frontend is not listening
'
)
lg
.
error
(
'
HAProxy pgsql-primary frontend is not listening.
'
)
errors
+=
1
standby_listening
=
check_listen
(
db_conn
[
'
host
'
],
54322
)
if
standby_listening
:
lg
.
info
(
'
HAProxy pgsql-standby frontend is listening.
'
)
else
:
lg
.
success
(
'
HAProxy pgsql-standby frontend is listening
'
)
lg
.
info
(
'
HAProxy pgsql-standby frontend is
not
listening
.
'
)
# check remotes
lg
.
log
(
'
Checking remote PostgreSQL nodes:
'
)
...
...
@@ -288,10 +287,10 @@ def check_ha(db_conn: dict, errors: int = 0) -> int:
node_host
=
nodes
[
node
][
'
host
'
]
node_port
=
nodes
[
node
][
'
port
'
]
if
not
check_listen
(
node_host
,
node_port
):
lg
.
error
(
'
c
annot bind {}:{}
'
.
format
(
node_host
,
node_port
))
lg
.
error
(
'
C
annot bind {}:{}
.
'
.
format
(
node_host
,
node_port
))
errors
+=
1
else
:
lg
.
success
(
'
c
an bind {}:{}
'
.
format
(
node_host
,
node_port
))
lg
.
success
(
'
C
an bind {}:{}
.
'
.
format
(
node_host
,
node_port
))
# check fenced
lg
.
log
(
'
Checking cluster state:
'
)
...
...
@@ -300,20 +299,23 @@ def check_ha(db_conn: dict, errors: int = 0) -> int:
lg
.
error
(
'
Node `{}` is fenced
'
.
format
(
node
))
errors
+=
1
else
:
lg
.
success
(
'
No fenced node found
'
)
lg
.
success
(
'
No fenced node found
.
'
)
# check replication
lg
.
log
(
'
Checking replication state:
'
)
primary
=
db_conn
.
copy
()
primary
[
'
port
'
]
=
54321
standby
=
db_conn
.
copy
()
standby
[
'
port
'
]
=
54322
status
,
info
=
check_replication
(
primary
,
standby
)
if
not
status
:
lg
.
error
(
'
cannot replicate between primary/standby ({})
'
.
format
(
info
))
errors
+=
1
if
primary_listening
and
standby_listening
:
primary
=
db_conn
.
copy
()
primary
[
'
port
'
]
=
54321
standby
=
db_conn
.
copy
()
standby
[
'
port
'
]
=
54322
status
,
info
=
check_replication
(
primary
,
standby
)
if
not
status
:
lg
.
error
(
'
Cannot replicate between primary/standby ({}).
'
.
format
(
info
))
errors
+=
1
else
:
lg
.
success
(
'
Can replicate between primary/standby ({}).
'
.
format
(
info
))
else
:
lg
.
success
(
'
c
an
replicate between
primary
/
standby
({})
'
.
format
(
info
)
)
lg
.
info
(
'
C
an
not check replication because
primary
and
standby
are not both accessible.
'
)
return
errors
...
...
@@ -336,20 +338,20 @@ def check_local(db_conn: dict, errors: int = 0) -> int:
# check listen
lg
.
log
(
'
Checking local PostgreSQL node:
'
)
if
not
check_listen
(
host
,
port
):
lg
.
error
(
'
c
annot connect to {}:{}
'
.
format
(
host
,
port
))
lg
.
error
(
'
C
annot connect to {}:{}
.
'
.
format
(
host
,
port
))
errors
+=
1
else
:
lg
.
success
(
'
c
an connect to {}:{}
'
.
format
(
host
,
port
))
lg
.
success
(
'
C
an connect to {}:{}
.
'
.
format
(
host
,
port
))
# check read
lg
.
log
(
'
Checking read operation:
'
)
read_query
=
'
SELECT 1;
'
status
,
info
=
check_psql
(
db_conn
,
read_query
)
if
not
status
:
lg
.
error
(
'
c
annot read from {}@{}:{} ({})
'
.
format
(
user
,
host
,
port
,
info
))
lg
.
error
(
'
C
annot read from {}@{}:{} ({})
.
'
.
format
(
user
,
host
,
port
,
info
))
errors
+=
1
else
:
lg
.
success
(
'
c
an read from {}@{}:{}
'
.
format
(
user
,
host
,
port
))
lg
.
success
(
'
C
an read from {}@{}:{}
.
'
.
format
(
user
,
host
,
port
))
# get replication state if available
if
check_listen
(
'
127.0.0.1
'
,
8543
):
...
...
@@ -360,16 +362,16 @@ def check_local(db_conn: dict, errors: int = 0) -> int:
# check write
lg
.
log
(
'
Checking write operation:
'
)
if
state
!=
'
primary
'
:
lg
.
info
(
'
t
his database is in {} state
'
.
format
(
state
))
lg
.
info
(
'
T
his database is in {} state
.
'
.
format
(
state
))
else
:
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
:
lg
.
error
(
'
c
annot write on {}@{}:{} ({})
'
.
format
(
user
,
host
,
port
,
info
))
lg
.
error
(
'
C
annot write on {}@{}:{} ({})
.
'
.
format
(
user
,
host
,
port
,
info
))
errors
+=
1
else
:
lg
.
success
(
'
c
an write on {}@{}:{}
'
.
format
(
user
,
host
,
port
))
lg
.
success
(
'
C
an write on {}@{}:{}
.
'
.
format
(
user
,
host
,
port
))
# remove test table
check_psql
(
db_conn
,
'
DROP TABLE es_test_{};
'
.
format
(
rand
))
...
...
@@ -398,11 +400,11 @@ def main():
# determine if HA setup and run according tests
lg
.
log
(
'
Checking availibility mode:
'
)
if
check_listen
(
'
127.0.0.1
'
,
54321
)
and
check_listen
(
'
127.0.0.1
'
,
54322
)
:
lg
.
info
(
'
t
his setup is using a primary and standby database
'
)
if
check_listen
(
'
127.0.0.1
'
,
54321
):
lg
.
info
(
'
T
his setup is using a primary and standby database
.
'
)
errors
=
check_ha
(
db_conn
)
else
:
lg
.
info
(
'
t
his setup is using a single database
'
)
lg
.
info
(
'
T
his setup is using a single database
.
'
)
errors
=
check_local
(
db_conn
)
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