|
3 | 3 | # See LICENSE file for licensing details. |
4 | 4 | import pytest as pytest |
5 | 5 | from pytest_operator.plugin import OpsTest |
| 6 | +from tenacity import Retrying, stop_after_delay, wait_exponential |
6 | 7 |
|
7 | 8 | from tests.helpers import METADATA |
8 | 9 | from tests.integration.helpers import ( |
|
13 | 14 | check_tls_patroni_api, |
14 | 15 | deploy_and_relate_application_with_postgresql, |
15 | 16 | enable_connections_logging, |
| 17 | + get_password, |
16 | 18 | get_primary, |
| 19 | + get_unit_address, |
17 | 20 | primary_changed, |
18 | 21 | run_command_on_unit, |
19 | 22 | ) |
| 23 | +from tests.integration.test_charm import db_connect |
20 | 24 |
|
21 | 25 | MATTERMOST_APP_NAME = "mattermost" |
22 | 26 | TLS_CERTIFICATES_APP_NAME = "tls-certificates-operator" |
@@ -70,12 +74,38 @@ async def test_mattermost_db(ops_test: OpsTest) -> None: |
70 | 74 | # being used in a later step. |
71 | 75 | await enable_connections_logging(ops_test, primary) |
72 | 76 |
|
73 | | - # Promote the replica to primary. |
74 | | - await run_command_on_unit( |
75 | | - ops_test, |
76 | | - replica, |
77 | | - 'su postgres -c "/usr/lib/postgresql/14/bin/pg_ctl -D /var/lib/postgresql/data/pgdata promote"', |
78 | | - ) |
| 77 | + for attempt in Retrying( |
| 78 | + stop=stop_after_delay(60), wait=wait_exponential(multiplier=1, min=2, max=30) |
| 79 | + ): |
| 80 | + with attempt: |
| 81 | + # Promote the replica to primary. |
| 82 | + await run_command_on_unit( |
| 83 | + ops_test, |
| 84 | + replica, |
| 85 | + 'su postgres -c "/usr/lib/postgresql/14/bin/pg_ctl -D /var/lib/postgresql/data/pgdata promote"', |
| 86 | + ) |
| 87 | + |
| 88 | + # Check that the replica was promoted. |
| 89 | + host = await get_unit_address(ops_test, replica) |
| 90 | + password = await get_password(ops_test) |
| 91 | + with db_connect(host, password) as connection, connection.cursor() as cursor: |
| 92 | + cursor.execute("SELECT pg_is_in_recovery();") |
| 93 | + in_recovery = cursor.fetchone()[0] |
| 94 | + assert ( |
| 95 | + not in_recovery |
| 96 | + ) # If the instance is not in recovery mode anymore it was successfully promoted. |
| 97 | + connection.close() |
| 98 | + |
| 99 | + # Write some data to the initial primary (this causes a divergence |
| 100 | + # in the instances' timelines). |
| 101 | + host = await get_unit_address(ops_test, primary) |
| 102 | + password = await get_password(ops_test) |
| 103 | + with db_connect(host, password) as connection: |
| 104 | + connection.autocommit = True |
| 105 | + with connection.cursor() as cursor: |
| 106 | + cursor.execute("CREATE TABLE pgrewindtest (testcol INT);") |
| 107 | + cursor.execute("INSERT INTO pgrewindtest SELECT generate_series(1,1000);") |
| 108 | + connection.close() |
79 | 109 |
|
80 | 110 | # Stop the initial primary. |
81 | 111 | await run_command_on_unit(ops_test, primary, "/charm/bin/pebble stop postgresql") |
|
0 commit comments