Skip to content

Commit 9eb9730

Browse files
Fix list of SANs and pg_rewind test (canonical#57)
* Fix SANS and pg_rewind test * Improve code
1 parent 03d8baa commit 9eb9730

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

lib/charms/postgresql_k8s/v0/postgresql_tls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
# Increment this PATCH version before using `charmcraft publish-lib` or reset
4646
# to 0 if you are raising the major API version.
47-
LIBPATCH = 2
47+
LIBPATCH = 3
4848

4949
logger = logging.getLogger(__name__)
5050
SCOPE = "unit"
@@ -167,6 +167,7 @@ def _get_sans(self) -> List[str]:
167167
unit_id = self.charm.unit.name.split("/")[1]
168168
return [
169169
f"{self.charm.app.name}-{unit_id}",
170+
self.charm.get_hostname_by_unit(self.charm.unit.name),
170171
socket.getfqdn(),
171172
str(self.charm.model.get_binding(self.peer_relation).network.bind_address),
172173
]

tests/integration/test_tls.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# See LICENSE file for licensing details.
44
import pytest as pytest
55
from pytest_operator.plugin import OpsTest
6+
from tenacity import Retrying, stop_after_delay, wait_exponential
67

78
from tests.helpers import METADATA
89
from tests.integration.helpers import (
@@ -13,10 +14,13 @@
1314
check_tls_patroni_api,
1415
deploy_and_relate_application_with_postgresql,
1516
enable_connections_logging,
17+
get_password,
1618
get_primary,
19+
get_unit_address,
1720
primary_changed,
1821
run_command_on_unit,
1922
)
23+
from tests.integration.test_charm import db_connect
2024

2125
MATTERMOST_APP_NAME = "mattermost"
2226
TLS_CERTIFICATES_APP_NAME = "tls-certificates-operator"
@@ -70,12 +74,38 @@ async def test_mattermost_db(ops_test: OpsTest) -> None:
7074
# being used in a later step.
7175
await enable_connections_logging(ops_test, primary)
7276

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()
79109

80110
# Stop the initial primary.
81111
await run_command_on_unit(ops_test, primary, "/charm/bin/pebble stop postgresql")

tests/unit/test_postgresql_tls.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,15 @@ def test_on_certificate_expiring(self, _request_certificate_renewal):
185185
@patch_network_get(private_address="1.1.1.1")
186186
def test_get_sans(self):
187187
sans = self.charm.tls._get_sans()
188-
self.assertEqual(sans, ["postgresql-k8s-0", socket.getfqdn(), "1.1.1.1"])
188+
self.assertEqual(
189+
sans,
190+
[
191+
"postgresql-k8s-0",
192+
"postgresql-k8s-0.postgresql-k8s-endpoints",
193+
socket.getfqdn(),
194+
"1.1.1.1",
195+
],
196+
)
189197

190198
def test_get_tls_extensions(self):
191199
extensions = self.charm.tls._get_tls_extensions()

0 commit comments

Comments
 (0)