|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | # Copyright 2024 Canonical Ltd. |
3 | 3 | # See LICENSE file for licensing details. |
4 | | -import contextlib |
5 | 4 | import logging |
6 | | -import subprocess |
7 | 5 | from asyncio import gather |
8 | 6 |
|
9 | 7 | import psycopg2 |
|
12 | 10 | from pytest_operator.plugin import OpsTest |
13 | 11 | from tenacity import Retrying, stop_after_delay, wait_fixed |
14 | 12 |
|
15 | | -from .. import architecture, markers |
| 13 | +from .. import markers |
16 | 14 | from ..helpers import ( |
17 | 15 | APPLICATION_NAME, |
18 | 16 | DATABASE_APP_NAME, |
|
23 | 21 | scale_application, |
24 | 22 | wait_for_relation_removed_between, |
25 | 23 | ) |
| 24 | +from .conftest import fast_forward |
26 | 25 | from .helpers import ( |
27 | 26 | app_name, |
28 | 27 | are_writes_increasing, |
|
43 | 42 | DATA_INTEGRATOR_APP_NAME = "data-integrator" |
44 | 43 |
|
45 | 44 |
|
46 | | -@contextlib.asynccontextmanager |
47 | | -async def fast_forward(model: Model, fast_interval: str = "10s", slow_interval: str | None = None): |
48 | | - """Adaptation of OpsTest.fast_forward to work with different models.""" |
49 | | - update_interval_key = "update-status-hook-interval" |
50 | | - interval_after = ( |
51 | | - slow_interval if slow_interval else (await model.get_config())[update_interval_key] |
52 | | - ) |
53 | | - |
54 | | - await model.set_config({update_interval_key: fast_interval}) |
55 | | - yield |
56 | | - await model.set_config({update_interval_key: interval_after}) |
57 | | - |
58 | | - |
59 | | -@pytest.fixture(scope="module") |
60 | | -def first_model(ops_test: OpsTest) -> Model: |
61 | | - """Return the first model.""" |
62 | | - first_model = ops_test.model |
63 | | - return first_model |
64 | | - |
65 | | - |
66 | | -@pytest.fixture(scope="module") |
67 | | -async def second_model(ops_test: OpsTest, first_model, request) -> Model: |
68 | | - """Create and return the second model.""" |
69 | | - second_model_name = f"{first_model.info.name}-other" |
70 | | - if second_model_name not in await ops_test._controller.list_models(): |
71 | | - await ops_test._controller.add_model(second_model_name) |
72 | | - subprocess.run(["juju", "switch", second_model_name], check=True) |
73 | | - subprocess.run( |
74 | | - ["juju", "set-model-constraints", f"arch={architecture.architecture}"], check=True |
75 | | - ) |
76 | | - subprocess.run(["juju", "switch", first_model.info.name], check=True) |
77 | | - second_model = Model() |
78 | | - await second_model.connect(model_name=second_model_name) |
79 | | - yield second_model |
80 | | - if request.config.getoption("--keep-models"): |
81 | | - return |
82 | | - logger.info("Destroying second model") |
83 | | - await ops_test._controller.destroy_model(second_model_name, destroy_storage=True) |
84 | | - |
85 | | - |
86 | 45 | @pytest.fixture |
87 | 46 | async def second_model_continuous_writes(second_model) -> None: |
88 | 47 | """Cleans up continuous writes on the second model after a test run.""" |
@@ -490,10 +449,13 @@ async def test_async_replication_failover_in_main_cluster( |
490 | 449 | ), |
491 | 450 | ) |
492 | 451 |
|
493 | | - # Check that the sync-standby unit is not the same as before. |
494 | | - new_sync_standby = await get_sync_standby(ops_test, first_model, DATABASE_APP_NAME) |
495 | | - logger.info(f"New sync-standby: {new_sync_standby}") |
496 | | - assert new_sync_standby != sync_standby, "Sync-standby is the same as before" |
| 452 | + # Check that the sync-standby unit is not the same as before (retry because |
| 453 | + # Patroni may take some time to update its cluster state after unit removal). |
| 454 | + for attempt in Retrying(stop=stop_after_delay(300), wait=wait_fixed(10), reraise=True): |
| 455 | + with attempt: |
| 456 | + new_sync_standby = await get_sync_standby(ops_test, first_model, DATABASE_APP_NAME) |
| 457 | + logger.info(f"New sync-standby: {new_sync_standby}") |
| 458 | + assert new_sync_standby != sync_standby, "Sync-standby is the same as before" |
497 | 459 |
|
498 | 460 | logger.info("Ensure continuous_writes after the crashed unit") |
499 | 461 | await are_writes_increasing(ops_test) |
|
0 commit comments