Skip to content

Commit 34421e5

Browse files
committed
Add test checks
1 parent 7c75e45 commit 34421e5

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

tests/integration/helpers.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,22 @@ async def check_database_creation(ops_test: OpsTest, database: str) -> None:
8484
assert len(output)
8585

8686

87+
async def check_patroni(ops_test: OpsTest, unit_name: str) -> bool:
88+
"""Check if Patroni is running correctly on a specific unit.
89+
90+
Args:
91+
ops_test: The ops test framework instance
92+
unit_name: The name of the unit
93+
94+
Returns:
95+
whether Patroni is running correctly.
96+
"""
97+
unit_ip = await get_unit_address(ops_test, unit_name)
98+
health_info = requests.get(f"http://{unit_ip}:8008/health")
99+
print(health_info.json())
100+
return health_info.json()["state"] == "running"
101+
102+
87103
def convert_records_to_dict(records: List[tuple]) -> dict:
88104
"""Converts psycopg2 records list to a dict."""
89105
records_dict = {}
@@ -204,6 +220,22 @@ async def get_password(ops_test: OpsTest, username: str = "operator"):
204220
return result.results[f"{username}-password"]
205221

206222

223+
async def get_postgresql_start_time(ops_test: OpsTest, unit_name: str) -> bool:
224+
"""Get PostgreSQL start time.
225+
226+
Args:
227+
ops_test: The ops test framework instance
228+
unit_name: The name of the unit
229+
230+
Returns:
231+
PostgreSQL start time.
232+
"""
233+
unit_ip = await get_unit_address(ops_test, unit_name)
234+
health_info = requests.get(f"http://{unit_ip}:8008/health")
235+
print(health_info.json())
236+
return health_info.json()["postmaster_start_time"]
237+
238+
207239
async def get_primary(ops_test: OpsTest, unit_id=0) -> str:
208240
"""Get the primary unit.
209241
@@ -236,7 +268,7 @@ async def get_unit_address(ops_test: OpsTest, unit_name: str) -> str:
236268

237269

238270
async def restart_patroni(ops_test: OpsTest, unit_name: str) -> None:
239-
"""Restart patroni on a specific unit.
271+
"""Restart Patroni on a specific unit.
240272
241273
Args:
242274
ops_test: The ops test framework instance

tests/integration/test_password_rotation.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
from pytest_operator.plugin import OpsTest
77

88
from tests.helpers import METADATA
9-
from tests.integration.helpers import get_password, restart_patroni, set_password
9+
from tests.integration.helpers import (
10+
check_patroni,
11+
get_password,
12+
restart_patroni,
13+
set_password,
14+
)
1015

1116
APP_NAME = METADATA["name"]
1217

@@ -59,6 +64,14 @@ async def test_password_rotation(ops_test: OpsTest):
5964

6065
# Restart Patroni on any non-leader unit and check that
6166
# Patroni and PostgreSQL continue to work.
62-
for unit in ops_test.model.applications[APP_NAME].units:
63-
if not await unit.is_leader_from_status():
64-
await restart_patroni(ops_test, unit.name)
67+
non_leader_units = [
68+
unit.name:
69+
for unit in ops_test.model.applications[APP_NAME].units
70+
if not await unit.is_leader_from_status()
71+
]
72+
73+
for unit in non_leader_units:
74+
await restart_patroni(ops_test, unit)
75+
76+
for unit in non_leader_units:
77+
assert await check_patroni(ops_test, unit)

0 commit comments

Comments
 (0)