|
2 | 2 | # Copyright 2022 Canonical Ltd. |
3 | 3 | # See LICENSE file for licensing details. |
4 | 4 | import itertools |
| 5 | +from datetime import datetime |
5 | 6 | from pathlib import Path |
6 | 7 | from typing import List |
7 | 8 |
|
@@ -90,20 +91,23 @@ async def check_database_creation(ops_test: OpsTest, database: str) -> None: |
90 | 91 | stop=stop_after_attempt(10), |
91 | 92 | wait=wait_exponential(multiplier=1, min=2, max=30), |
92 | 93 | ) |
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: |
94 | 95 | """Check if Patroni is running correctly on a specific unit. |
95 | 96 |
|
96 | 97 | Args: |
97 | 98 | ops_test: The ops test framework instance |
98 | 99 | unit_name: The name of the unit |
| 100 | + restart_time: Point in time before the unit was restarted. |
99 | 101 |
|
100 | 102 | Returns: |
101 | 103 | whether Patroni is running correctly. |
102 | 104 | """ |
103 | 105 | 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" |
107 | 111 |
|
108 | 112 |
|
109 | 113 | def convert_records_to_dict(records: List[tuple]) -> dict: |
@@ -226,22 +230,6 @@ async def get_password(ops_test: OpsTest, username: str = "operator"): |
226 | 230 | return result.results[f"{username}-password"] |
227 | 231 |
|
228 | 232 |
|
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 | | - |
245 | 233 | async def get_primary(ops_test: OpsTest, unit_id=0) -> str: |
246 | 234 | """Get the primary unit. |
247 | 235 |
|
|
0 commit comments