Skip to content

Commit

Permalink
[MISC] Suppress alias creation error (#284)
Browse files Browse the repository at this point in the history
* Suppress alias creation error

* Add unit test
  • Loading branch information
dragomirp authored Jul 10, 2024
1 parent fd6f8fa commit 48a8c20
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
Expand All @@ -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,
Expand Down

0 comments on commit 48a8c20

Please sign in to comment.