Skip to content

Commit 4816eb5

Browse files
authored
[DPE-4533] Pause Patroni in the TLS test (#534)
* Increase Patroni's loop_wait * Allow setting and getting configs using tls * Wait for loop to clear * Mover loop_wait restore * Pause patroni * Skip on fail * Increase update status interval * Further increase the update hook interval * Different ffwd context * Switch back ffwding * Retry grep * Reraise and log retries * Union signature
1 parent da7ab85 commit 4816eb5

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

tests/integration/ha_tests/helpers.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import subprocess
88
from pathlib import Path
99
from tempfile import mkstemp
10-
from typing import Dict, Optional, Set, Tuple
10+
from typing import Dict, Optional, Set, Tuple, Union
1111

1212
import psycopg2
1313
import requests
@@ -137,7 +137,11 @@ async def app_name(
137137

138138

139139
async def change_patroni_setting(
140-
ops_test: OpsTest, setting: str, value: int, use_random_unit: bool = False
140+
ops_test: OpsTest,
141+
setting: str,
142+
value: Union[int, bool],
143+
use_random_unit: bool = False,
144+
tls: bool = False,
141145
) -> None:
142146
"""Change the value of one of the Patroni settings.
143147
@@ -146,8 +150,13 @@ async def change_patroni_setting(
146150
setting: the name of the setting.
147151
value: the value to assign to the setting.
148152
use_random_unit: whether to use a random unit (default is False,
149-
so it uses the primary)
153+
so it uses the primary).
154+
tls: if Patroni is serving using tls.
150155
"""
156+
if tls:
157+
schema = "https"
158+
else:
159+
schema = "http"
151160
for attempt in Retrying(stop=stop_after_delay(30 * 2), wait=wait_fixed(3)):
152161
with attempt:
153162
app = await app_name(ops_test)
@@ -158,8 +167,9 @@ async def change_patroni_setting(
158167
primary_name = await get_primary(ops_test, app)
159168
unit_ip = get_unit_address(ops_test, primary_name)
160169
requests.patch(
161-
f"http://{unit_ip}:8008/config",
170+
f"{schema}://{unit_ip}:8008/config",
162171
json={setting: value},
172+
verify=not tls,
163173
)
164174

165175

@@ -399,22 +409,27 @@ async def get_ip_from_inside_the_unit(ops_test: OpsTest, unit_name: str) -> str:
399409
return stdout.splitlines()[0].strip()
400410

401411

402-
async def get_patroni_setting(ops_test: OpsTest, setting: str) -> Optional[int]:
412+
async def get_patroni_setting(ops_test: OpsTest, setting: str, tls: bool = False) -> Optional[int]:
403413
"""Get the value of one of the integer Patroni settings.
404414
405415
Args:
406416
ops_test: ops_test instance.
407417
setting: the name of the setting.
418+
tls: if Patroni is serving using tls.
408419
409420
Returns:
410421
the value of the configuration or None if it's using the default value.
411422
"""
423+
if tls:
424+
schema = "https"
425+
else:
426+
schema = "http"
412427
for attempt in Retrying(stop=stop_after_delay(30 * 2), wait=wait_fixed(3)):
413428
with attempt:
414429
app = await app_name(ops_test)
415430
primary_name = await get_primary(ops_test, app)
416431
unit_ip = get_unit_address(ops_test, primary_name)
417-
configuration_info = requests.get(f"http://{unit_ip}:8008/config")
432+
configuration_info = requests.get(f"{schema}://{unit_ip}:8008/config", verify=not tls)
418433
value = configuration_info.json().get(setting)
419434
return int(value) if value is not None else None
420435

tests/integration/test_tls.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
import pytest as pytest
88
from pytest_operator.plugin import OpsTest
9-
from tenacity import Retrying, stop_after_attempt, stop_after_delay, wait_exponential
9+
from tenacity import Retrying, stop_after_attempt, stop_after_delay, wait_exponential, wait_fixed
1010

1111
from . import architecture
12+
from .ha_tests.helpers import (
13+
change_patroni_setting,
14+
)
1215
from .helpers import (
1316
CHARM_SERIES,
1417
DATABASE_APP_NAME,
@@ -65,6 +68,7 @@ async def test_deploy_active(ops_test: OpsTest):
6568

6669

6770
@pytest.mark.group(1)
71+
@pytest.mark.abort_on_fail
6872
async def test_tls_enabled(ops_test: OpsTest) -> None:
6973
"""Test that TLS is enabled when relating to the TLS Certificates Operator."""
7074
async with ops_test.fast_forward():
@@ -106,6 +110,10 @@ async def test_tls_enabled(ops_test: OpsTest) -> None:
106110
)
107111
change_primary_start_timeout(ops_test, primary, 0)
108112

113+
# Pause Patroni so it doesn't wipe the custom changes
114+
await change_patroni_setting(ops_test, "pause", True, use_random_unit=True, tls=True)
115+
116+
async with ops_test.fast_forward("24h"):
109117
for attempt in Retrying(
110118
stop=stop_after_delay(60 * 5), wait=wait_exponential(multiplier=1, min=2, max=30)
111119
):
@@ -158,12 +166,18 @@ async def test_tls_enabled(ops_test: OpsTest) -> None:
158166

159167
# Check the logs to ensure TLS is being used by pg_rewind.
160168
primary = await get_primary(ops_test, primary)
161-
await run_command_on_unit(
162-
ops_test,
163-
primary,
164-
"grep 'connection authorized: user=rewind database=postgres SSL enabled' /var/snap/charmed-postgresql/common/var/log/postgresql/postgresql-*.log",
165-
)
169+
for attempt in Retrying(stop=stop_after_attempt(10), wait=wait_fixed(5), reraise=True):
170+
with attempt:
171+
logger.info("Trying to grep for rewind logs.")
172+
await run_command_on_unit(
173+
ops_test,
174+
primary,
175+
"grep 'connection authorized: user=rewind database=postgres SSL enabled' /var/snap/charmed-postgresql/common/var/log/postgresql/postgresql-*.log",
176+
)
177+
178+
await change_patroni_setting(ops_test, "pause", False, use_random_unit=True, tls=True)
166179

180+
async with ops_test.fast_forward():
167181
# Remove the relation.
168182
await ops_test.model.applications[DATABASE_APP_NAME].remove_relation(
169183
f"{DATABASE_APP_NAME}:certificates", f"{tls_certificates_app_name}:certificates"

0 commit comments

Comments
 (0)