Skip to content

Commit bebd8b9

Browse files
authored
[MISC] Don't set tls flag if relation isn't initialised (#933)
* Don't set tls flag if relation isn't initialised * Unit test
1 parent c246fae commit bebd8b9

File tree

7 files changed

+31
-8
lines changed

7 files changed

+31
-8
lines changed

src/relations/postgresql_provider.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,9 @@ def update_tls_flag(self, tls: str) -> None:
265265
ca = ""
266266

267267
for relation in relations:
268-
self.database_provides.set_tls(relation.id, tls)
269-
self.database_provides.set_tls_ca(relation.id, ca)
268+
if self.database_provides.fetch_relation_field(relation.id, "database"):
269+
self.database_provides.set_tls(relation.id, tls)
270+
self.database_provides.set_tls_ca(relation.id, ca)
270271

271272
def _check_multiple_endpoints(self) -> bool:
272273
"""Checks if there are relations with other endpoints."""

tests/integration/ha_tests/test_rollback_to_master_label.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async def test_deploy_stable(ops_test: OpsTest) -> None:
6161
logger.info("Wait for applications to become active")
6262
async with ops_test.fast_forward():
6363
await ops_test.model.wait_for_idle(
64-
apps=[DATABASE_APP_NAME, APPLICATION_NAME], status="active", raise_on_error=False
64+
apps=[DATABASE_APP_NAME, APPLICATION_NAME], status="active"
6565
)
6666
assert len(ops_test.model.applications[DATABASE_APP_NAME].units) == 3
6767
instances_roles = await get_instances_roles(ops_test)

tests/integration/ha_tests/test_upgrade.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ async def test_deploy_latest(ops_test: OpsTest) -> None:
5858
await ops_test.model.wait_for_idle(
5959
apps=[DATABASE_APP_NAME, APPLICATION_NAME],
6060
status="active",
61-
raise_on_error=False,
6261
timeout=1000,
6362
)
6463
assert len(ops_test.model.applications[DATABASE_APP_NAME].units) == 3

tests/integration/ha_tests/test_upgrade_from_stable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async def test_deploy_stable(ops_test: OpsTest) -> None:
5252
logger.info("Wait for applications to become active")
5353
async with ops_test.fast_forward():
5454
await ops_test.model.wait_for_idle(
55-
apps=[DATABASE_APP_NAME, APPLICATION_NAME], status="active", raise_on_error=False
55+
apps=[DATABASE_APP_NAME, APPLICATION_NAME], status="active"
5656
)
5757
assert len(ops_test.model.applications[DATABASE_APP_NAME].units) == 3
5858

tests/integration/ha_tests/test_upgrade_to_primary_label.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async def test_deploy_stable(ops_test: OpsTest) -> None:
6464
logger.info("Wait for applications to become active")
6565
async with ops_test.fast_forward():
6666
await ops_test.model.wait_for_idle(
67-
apps=[DATABASE_APP_NAME, APPLICATION_NAME], status="active", raise_on_error=False
67+
apps=[DATABASE_APP_NAME, APPLICATION_NAME], status="active"
6868
)
6969
assert len(ops_test.model.applications[DATABASE_APP_NAME].units) == 3
7070
instances_roles = await get_instances_roles(ops_test)

tests/integration/helpers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ async def build_and_deploy(
108108
apps=[database_app_name],
109109
status=status,
110110
raise_on_blocked=True,
111-
raise_on_error=False,
112111
timeout=1000,
113112
wait_for_exact_units=num_units,
114113
)

tests/unit/test_postgresql_provider.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2022 Canonical Ltd.
22
# See LICENSE file for licensing details.
33

4-
from unittest.mock import Mock, PropertyMock, patch
4+
from unittest.mock import Mock, PropertyMock, patch, sentinel
55

66
import pytest
77
from charms.postgresql_k8s.v0.postgresql import (
@@ -216,3 +216,27 @@ def test_on_relation_broken(harness):
216216
)
217217
harness.charm.postgresql_client_relation._on_relation_broken(event)
218218
postgresql_mock.delete_user.assert_not_called()
219+
220+
221+
def test_update_tls_flag(harness):
222+
with (
223+
patch("charm.PostgreSQLTLS.get_tls_files", return_value=(None, sentinel.ca, None)),
224+
patch(
225+
"relations.postgresql_provider.new_password", return_value="test-password"
226+
) as _new_password,
227+
patch(
228+
"relations.postgresql_provider.DatabaseProvides.fetch_relation_field",
229+
side_effect=[None, "db"],
230+
),
231+
patch(
232+
"relations.postgresql_provider.DatabaseProvides.set_tls",
233+
) as _set_tls,
234+
patch(
235+
"relations.postgresql_provider.DatabaseProvides.set_tls_ca",
236+
) as _set_tls_ca,
237+
):
238+
with harness.hooks_disabled():
239+
second_rel = harness.add_relation(RELATION_NAME, "second_app")
240+
harness.charm.postgresql_client_relation.update_tls_flag("True")
241+
_set_tls.assert_called_once_with(second_rel, "True")
242+
_set_tls_ca.assert_called_once_with(second_rel, sentinel.ca)

0 commit comments

Comments
 (0)