From 48a8c2053ed157558a9f658331ef493a159e180e Mon Sep 17 00:00:00 2001 From: Dragomir Penev <6687393+dragomirp@users.noreply.github.com> Date: Wed, 10 Jul 2024 20:44:42 +0300 Subject: [PATCH] [MISC] Suppress alias creation error (#284) * Suppress alias creation error * Add unit test --- src/charm.py | 6 ++---- tests/unit/test_charm.py | 9 +++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/charm.py b/src/charm.py index f723b9b36..c90a3e49d 100755 --- a/src/charm.py +++ b/src/charm.py @@ -216,14 +216,12 @@ def _on_install(self, _) -> None: self.unit.status = BlockedStatus("failed to install snap packages") return - # Try to disable pgbackrest service try: cache = snap.SnapCache() selected_snap = cache[PGBOUNCER_SNAP_NAME] selected_snap.alias("psql") - except snap.SnapError as e: - error_message = "Failed to stop and disable pgbackrest snap service" - logger.exception(error_message, exc_info=e) + except snap.SnapError: + logger.warning("Unable to create alias") self.create_instance_directories() self.render_pgb_config() diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index 8e2317d1e..f3eac7e16 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -57,6 +57,7 @@ def setUp(self): def use_caplog(self, caplog): self._caplog = caplog + @patch("charm.logger.warning") @patch("builtins.open", unittest.mock.mock_open()) @patch("charm.snap.SnapCache") @patch("charm.PgBouncerCharm._install_snap_packages") @@ -80,6 +81,7 @@ def test_on_install( _stop, _install, _snap_cache, + _warning, ): pg_snap = _snap_cache.return_value["charmed-pgbouncer"] self.charm.on.install.emit() @@ -98,6 +100,13 @@ def test_on_install( self.assertIsInstance(self.harness.model.unit.status, WaitingStatus) + # Log warning if alias fails + pg_snap.alias.side_effect = snap.SnapError + + self.charm.on.install.emit() + + _warning.assert_called_once_with("Unable to create alias") + @patch( "relations.backend_database.BackendDatabaseRequires.ready", new_callable=PropertyMock,