diff --git a/src/relations/postgresql_provider.py b/src/relations/postgresql_provider.py index ebb77d4b19..a828d046ae 100644 --- a/src/relations/postgresql_provider.py +++ b/src/relations/postgresql_provider.py @@ -265,8 +265,9 @@ def update_tls_flag(self, tls: str) -> None: ca = "" for relation in relations: - self.database_provides.set_tls(relation.id, tls) - self.database_provides.set_tls_ca(relation.id, ca) + if self.database_provides.fetch_relation_field(relation.id, "database"): + self.database_provides.set_tls(relation.id, tls) + self.database_provides.set_tls_ca(relation.id, ca) def _check_multiple_endpoints(self) -> bool: """Checks if there are relations with other endpoints.""" diff --git a/tests/integration/ha_tests/test_rollback_to_master_label.py b/tests/integration/ha_tests/test_rollback_to_master_label.py index f30aea9e8c..f4d7cbe9f3 100644 --- a/tests/integration/ha_tests/test_rollback_to_master_label.py +++ b/tests/integration/ha_tests/test_rollback_to_master_label.py @@ -61,7 +61,7 @@ async def test_deploy_stable(ops_test: OpsTest) -> None: logger.info("Wait for applications to become active") async with ops_test.fast_forward(): await ops_test.model.wait_for_idle( - apps=[DATABASE_APP_NAME, APPLICATION_NAME], status="active", raise_on_error=False + apps=[DATABASE_APP_NAME, APPLICATION_NAME], status="active" ) assert len(ops_test.model.applications[DATABASE_APP_NAME].units) == 3 instances_roles = await get_instances_roles(ops_test) diff --git a/tests/integration/ha_tests/test_upgrade.py b/tests/integration/ha_tests/test_upgrade.py index 8ce47757b1..e49a9b6dd0 100644 --- a/tests/integration/ha_tests/test_upgrade.py +++ b/tests/integration/ha_tests/test_upgrade.py @@ -58,7 +58,6 @@ async def test_deploy_latest(ops_test: OpsTest) -> None: await ops_test.model.wait_for_idle( apps=[DATABASE_APP_NAME, APPLICATION_NAME], status="active", - raise_on_error=False, timeout=1000, ) assert len(ops_test.model.applications[DATABASE_APP_NAME].units) == 3 diff --git a/tests/integration/ha_tests/test_upgrade_from_stable.py b/tests/integration/ha_tests/test_upgrade_from_stable.py index 92767cd7d4..46fa2850dc 100644 --- a/tests/integration/ha_tests/test_upgrade_from_stable.py +++ b/tests/integration/ha_tests/test_upgrade_from_stable.py @@ -52,7 +52,7 @@ async def test_deploy_stable(ops_test: OpsTest) -> None: logger.info("Wait for applications to become active") async with ops_test.fast_forward(): await ops_test.model.wait_for_idle( - apps=[DATABASE_APP_NAME, APPLICATION_NAME], status="active", raise_on_error=False + apps=[DATABASE_APP_NAME, APPLICATION_NAME], status="active" ) assert len(ops_test.model.applications[DATABASE_APP_NAME].units) == 3 diff --git a/tests/integration/ha_tests/test_upgrade_to_primary_label.py b/tests/integration/ha_tests/test_upgrade_to_primary_label.py index 01a8507c84..661f509092 100644 --- a/tests/integration/ha_tests/test_upgrade_to_primary_label.py +++ b/tests/integration/ha_tests/test_upgrade_to_primary_label.py @@ -64,7 +64,7 @@ async def test_deploy_stable(ops_test: OpsTest) -> None: logger.info("Wait for applications to become active") async with ops_test.fast_forward(): await ops_test.model.wait_for_idle( - apps=[DATABASE_APP_NAME, APPLICATION_NAME], status="active", raise_on_error=False + apps=[DATABASE_APP_NAME, APPLICATION_NAME], status="active" ) assert len(ops_test.model.applications[DATABASE_APP_NAME].units) == 3 instances_roles = await get_instances_roles(ops_test) diff --git a/tests/integration/helpers.py b/tests/integration/helpers.py index 2ef30e6bfe..e67ad47583 100644 --- a/tests/integration/helpers.py +++ b/tests/integration/helpers.py @@ -108,7 +108,6 @@ async def build_and_deploy( apps=[database_app_name], status=status, raise_on_blocked=True, - raise_on_error=False, timeout=1000, wait_for_exact_units=num_units, ) diff --git a/tests/unit/test_postgresql_provider.py b/tests/unit/test_postgresql_provider.py index f51b7adb3a..9b00f62528 100644 --- a/tests/unit/test_postgresql_provider.py +++ b/tests/unit/test_postgresql_provider.py @@ -1,7 +1,7 @@ # Copyright 2022 Canonical Ltd. # See LICENSE file for licensing details. -from unittest.mock import Mock, PropertyMock, patch +from unittest.mock import Mock, PropertyMock, patch, sentinel import pytest from charms.postgresql_k8s.v0.postgresql import ( @@ -216,3 +216,27 @@ def test_on_relation_broken(harness): ) harness.charm.postgresql_client_relation._on_relation_broken(event) postgresql_mock.delete_user.assert_not_called() + + +def test_update_tls_flag(harness): + with ( + patch("charm.PostgreSQLTLS.get_tls_files", return_value=(None, sentinel.ca, None)), + patch( + "relations.postgresql_provider.new_password", return_value="test-password" + ) as _new_password, + patch( + "relations.postgresql_provider.DatabaseProvides.fetch_relation_field", + side_effect=[None, "db"], + ), + patch( + "relations.postgresql_provider.DatabaseProvides.set_tls", + ) as _set_tls, + patch( + "relations.postgresql_provider.DatabaseProvides.set_tls_ca", + ) as _set_tls_ca, + ): + with harness.hooks_disabled(): + second_rel = harness.add_relation(RELATION_NAME, "second_app") + harness.charm.postgresql_client_relation.update_tls_flag("True") + _set_tls.assert_called_once_with(second_rel, "True") + _set_tls_ca.assert_called_once_with(second_rel, sentinel.ca)