Skip to content

Commit b6f2d8a

Browse files
committed
Improve integration test
1 parent 16dce63 commit b6f2d8a

File tree

2 files changed

+14
-31
lines changed

2 files changed

+14
-31
lines changed

tests/integration/helpers.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright 2022 Canonical Ltd.
33
# See LICENSE file for licensing details.
44
import itertools
5+
from datetime import datetime
56
from pathlib import Path
67
from typing import List
78

@@ -90,20 +91,23 @@ async def check_database_creation(ops_test: OpsTest, database: str) -> None:
9091
stop=stop_after_attempt(10),
9192
wait=wait_exponential(multiplier=1, min=2, max=30),
9293
)
93-
async def check_patroni(ops_test: OpsTest, unit_name: str) -> bool:
94+
async def check_patroni(ops_test: OpsTest, unit_name: str, restart_time: float) -> bool:
9495
"""Check if Patroni is running correctly on a specific unit.
9596
9697
Args:
9798
ops_test: The ops test framework instance
9899
unit_name: The name of the unit
100+
restart_time: Point in time before the unit was restarted.
99101
100102
Returns:
101103
whether Patroni is running correctly.
102104
"""
103105
unit_ip = await get_unit_address(ops_test, unit_name)
104-
health_info = requests.get(f"http://{unit_ip}:8008/health")
105-
print(health_info.json())
106-
return health_info.json()["state"] == "running"
106+
health_info = requests.get(f"http://{unit_ip}:8008/health").json()
107+
postmaster_start_time = datetime.strptime(
108+
health_info["postmaster_start_time"], "%Y-%m-%d %H:%M:%S.%f%z"
109+
).timestamp()
110+
return postmaster_start_time > restart_time and health_info["state"] == "running"
107111

108112

109113
def convert_records_to_dict(records: List[tuple]) -> dict:
@@ -226,22 +230,6 @@ async def get_password(ops_test: OpsTest, username: str = "operator"):
226230
return result.results[f"{username}-password"]
227231

228232

229-
async def get_postgresql_start_time(ops_test: OpsTest, unit_name: str) -> bool:
230-
"""Get PostgreSQL start time.
231-
232-
Args:
233-
ops_test: The ops test framework instance
234-
unit_name: The name of the unit
235-
236-
Returns:
237-
PostgreSQL start time.
238-
"""
239-
unit_ip = await get_unit_address(ops_test, unit_name)
240-
health_info = requests.get(f"http://{unit_ip}:8008/health")
241-
print(health_info.json())
242-
return health_info.json()["postmaster_start_time"]
243-
244-
245233
async def get_primary(ops_test: OpsTest, unit_id=0) -> str:
246234
"""Get the primary unit.
247235

tests/integration/test_password_rotation.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
# Copyright 2022 Canonical Ltd.
33
# See LICENSE file for licensing details.
4+
import time
45

56
import pytest
67
from pytest_operator.plugin import OpsTest
@@ -64,14 +65,8 @@ async def test_password_rotation(ops_test: OpsTest):
6465

6566
# Restart Patroni on any non-leader unit and check that
6667
# Patroni and PostgreSQL continue to work.
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)
68+
restart_time = time.time()
69+
for unit in ops_test.model.applications[APP_NAME].units:
70+
if not await unit.is_leader_from_status():
71+
await restart_patroni(ops_test, unit.name)
72+
assert await check_patroni(ops_test, unit.name, restart_time)

0 commit comments

Comments
 (0)