From 550b6ba53ae4ad2d189c253ae2d48e86e126f705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinclert=20P=C3=A9rez?= Date: Tue, 8 Apr 2025 11:00:44 +0200 Subject: [PATCH 1/2] Fix cluster-initialize on primary --- src/charm.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/charm.py b/src/charm.py index accd6844be..12aecbf443 100755 --- a/src/charm.py +++ b/src/charm.py @@ -1324,10 +1324,10 @@ def _start_primary(self, event: StartEvent) -> None: self.unit.status = BlockedStatus("failed to start Patroni") return - # Assert the member is up and running before marking it as initialised. - if not self._patroni.member_started: - logger.debug("Deferring on_start: awaiting for member to start") - self.unit.status = WaitingStatus("awaiting for member to start") + # Assert the primary is up and running before marking it as initialised. + if not self.primary_endpoint: + logger.debug("Deferring on_start: awaiting for primary endpoint to be ready") + self.unit.status = WaitingStatus("awaiting for primary endpoint to be ready") event.defer() return From 693d97d4919ece953680cf006efeb18a1da9b6ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sinclert=20P=C3=A9rez?= Date: Tue, 8 Apr 2025 11:07:46 +0200 Subject: [PATCH 2/2] Adapt cluster-initialize tests --- tests/unit/test_charm.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index e500a8d099..47c668c191 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -615,9 +615,9 @@ def test_on_start(harness): patch("charm.PostgreSQLProvider.update_endpoints"), patch("charm.PostgresqlOperatorCharm.update_config"), patch( - "charm.Patroni.member_started", + "charm.PostgresqlOperatorCharm.primary_endpoint", new_callable=PropertyMock, - ) as _member_started, + ) as _primary_endpoint, patch("charm.Patroni.bootstrap_cluster") as _bootstrap_cluster, patch("charm.PostgresqlOperatorCharm._replication_password") as _replication_password, patch("charm.PostgresqlOperatorCharm._get_password") as _get_password, @@ -637,7 +637,7 @@ def test_on_start(harness): _reboot_on_detached_storage.assert_called_once() # Test before the passwords are generated. - _member_started.return_value = False + _primary_endpoint.return_value = None _get_password.return_value = None harness.charm.on.start.emit() _bootstrap_cluster.assert_not_called() @@ -672,7 +672,7 @@ def test_on_start(harness): # Test the event of an error happening when trying to create the default postgres user. _restart_services_after_reboot.reset_mock() - _member_started.return_value = True + _primary_endpoint.return_value = "endpoint" harness.charm.on.start.emit() _postgresql.create_user.assert_called_once() _oversee_users.assert_not_called() @@ -756,7 +756,7 @@ def test_on_start_replica(harness): assert isinstance(harness.model.unit.status, WaitingStatus) -def test_on_start_no_patroni_member(harness): +def test_on_start_no_primary_endpoint(harness): with ( patch("subprocess.check_output", return_value=b"C"), patch("charm.snap.SnapCache") as _snap_cache, @@ -767,10 +767,13 @@ def test_on_start_no_patroni_member(harness): patch( "charm.PostgresqlOperatorCharm._is_storage_attached", return_value=True ) as _is_storage_attached, + patch( + "charm.PostgresqlOperatorCharm.primary_endpoint", new_callable=PropertyMock + ) as _primary_endpoint, patch("charm.PostgresqlOperatorCharm.get_available_memory") as _get_available_memory, ): # Mock the passwords. - patroni.return_value.member_started = False + _primary_endpoint.return_value = None _get_password.return_value = "fake-operator-password" bootstrap_cluster = patroni.return_value.bootstrap_cluster bootstrap_cluster.return_value = True @@ -782,7 +785,7 @@ def test_on_start_no_patroni_member(harness): bootstrap_cluster.assert_called_once() _postgresql.create_user.assert_not_called() assert isinstance(harness.model.unit.status, WaitingStatus) - assert harness.model.unit.status.message == "awaiting for member to start" + assert harness.model.unit.status.message == "awaiting for primary endpoint to be ready" def test_on_start_after_blocked_state(harness):