From e7e2a4e5db3d1b8a9bc0591397e0cafda117d8c1 Mon Sep 17 00:00:00 2001 From: Dragomir Penev Date: Tue, 12 Sep 2023 19:13:25 +0300 Subject: [PATCH] Start the exporter in on peer relation changed --- src/charm.py | 8 ++++---- tests/unit/test_backups.py | 7 +++++++ tests/unit/test_charm.py | 2 ++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/charm.py b/src/charm.py index 06bce479de..36e8f6ac17 100755 --- a/src/charm.py +++ b/src/charm.py @@ -539,6 +539,9 @@ def _on_peer_relation_changed(self, event: HookEvent): event.defer() return + if "exporter-started" not in self.unit_peer_data: + self._setup_exporter() + self._update_new_unit_status() def _update_new_unit_status(self) -> None: @@ -1391,10 +1394,7 @@ def update_config(self, is_creating_backup: bool = False) -> bool: "Early exit update_config: Trying to reset metrics service with no configuration set" ) return True - if ( - snap_password != self.get_secret(APP_SCOPE, MONITORING_PASSWORD_KEY) - or "exporter-started" not in self.unit_peer_data - ): + if snap_password != self.get_secret(APP_SCOPE, MONITORING_PASSWORD_KEY): self._setup_exporter() return True diff --git a/tests/unit/test_backups.py b/tests/unit/test_backups.py index ae98b1b7c4..b31c2e7f01 100644 --- a/tests/unit/test_backups.py +++ b/tests/unit/test_backups.py @@ -595,9 +595,16 @@ def test_is_primary_pgbackrest_service_running( self.assertFalse(self.charm.backup._is_primary_pgbackrest_service_running) _execute_command.assert_called_once() + # Test when the endpoint is not generated. + _execute_command.reset_mock() + _primary_endpoint.return_value = None + self.assertFalse(self.charm.backup._is_primary_pgbackrest_service_running) + _execute_command.assert_not_called() + # Test when the pgBackRest succeeds on contacting the primary server. _execute_command.reset_mock() _execute_command.return_value = (0, "fake stdout", "") + _primary_endpoint.return_value = "fake_endpoint" self.assertTrue(self.charm.backup._is_primary_pgbackrest_service_running) _execute_command.assert_called_once() diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index 75f2cecb8c..65d3b25e0b 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -1126,6 +1126,7 @@ def test_on_cluster_topology_change_clear_blocked( self.assertTrue(isinstance(self.harness.model.unit.status, ActiveStatus)) @patch_network_get(private_address="1.1.1.1") + @patch("charm.snap.SnapCache") @patch("charm.PostgresqlOperatorCharm._update_relation_endpoints") @patch("charm.PostgresqlOperatorCharm.primary_endpoint", new_callable=PropertyMock) @patch("charm.Patroni.member_started", new_callable=PropertyMock) @@ -1142,6 +1143,7 @@ def test_on_peer_relation_changed( _member_started, _primary_endpoint, _update_relation_endpoints, + _, ): # Test an uninitialized cluster. mock_event = Mock()