Skip to content

Commit

Permalink
Return if exporter not installed, unit test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dashmage committed Mar 21, 2024
1 parent 8f934bb commit 47a403a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,14 @@ def _on_update_status(self, _: EventBase) -> None: # noqa: C901
self.model.unit.status = BlockedStatus(error_msg)
return

if self._stored.exporter_installed: # type: ignore[truthy-function]
if not self.exporter.check_active():
logger.warning("Exporter health check - failed.")
# if restart isn't successful, an ExporterError exception will be raised here
self.restart_exporter()
if not self._stored.exporter_installed: # type: ignore[truthy-function]
logger.warning("Exporter not installed") # type: ignore[unreachable]
return

if not self.exporter.check_health():
logger.warning("Exporter health check - failed.")
# if restart isn't successful, an ExporterError exception will be raised here
self.restart_exporter()

self.model.unit.status = ActiveStatus("Unit is ready")

Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ def test_09_update_status_exporter_crashed(self, mock_hw_tool_helper, mock_expor
self.harness.add_relation("cos-agent", "grafana-agent")
self.harness.begin()
self.harness.charm._stored.resource_installed = True
self.harness.charm._stored.exporter_installed = False

# no exception should be raised
self.harness.charm.on.update_status.emit()

# exception raised, exporter should crash
self.harness.charm._stored.exporter_installed = True
with self.assertRaises(ExporterError):
self.harness.charm.on.update_status.emit()

Expand Down

0 comments on commit 47a403a

Please sign in to comment.