Skip to content

Commit

Permalink
fix: Force re-config exporter when upgrade (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
jneo8 authored Dec 14, 2023
1 parent 9f081d9 commit 314c287
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,23 @@ def _on_install_or_upgrade(self, event: ops.InstallEvent) -> None:
self.model.unit.status = BlockedStatus(msg)
return

if self._stored.exporter_installed is not True:
self.model.unit.status = MaintenanceStatus("Installing exporter...")
success, err_msg = self.validate_exporter_configs()
if not success:
self.model.unit.status = BlockedStatus(err_msg)
return
# Install exporter
self.model.unit.status = MaintenanceStatus("Installing exporter...")
success, err_msg = self.validate_exporter_configs()
if not success:
self.model.unit.status = BlockedStatus(err_msg)
return

port = self.model.config.get("exporter-port", "10000")
level = self.model.config.get("exporter-log-level", "INFO")
redfish_creds = self._get_redfish_creds()
success = self.exporter.install(port, level, redfish_creds)
self._stored.exporter_installed = success
if not success:
msg = "Failed to install exporter, please refer to `juju debug-log`"
logger.error(msg)
self.model.unit.status = BlockedStatus(msg)
return
port = self.model.config.get("exporter-port", "10000")
level = self.model.config.get("exporter-log-level", "INFO")
redfish_creds = self._get_redfish_creds()
success = self.exporter.install(port, level, redfish_creds)
self._stored.exporter_installed = success
if not success:
msg = "Failed to install exporter, please refer to `juju debug-log`"
logger.error(msg)
self.model.unit.status = BlockedStatus(msg)
return
self._on_update_status(event)

def _on_remove(self, _: EventBase) -> None:
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,22 @@ def test_11_config_changed_before_install_complete(self, mock_exporter):

self.harness.charm.on.config_changed.emit()
self.assertEqual(self._get_notice_count("config_changed"), 1)

@mock.patch("charm.Exporter", return_value=mock.MagicMock())
@mock.patch("charm.HWToolHelper", return_value=mock.MagicMock())
def test_12_upgrade_force_reconfig_exporter(self, mock_hw_tool_helper, mock_exporter) -> None:
"""Test event install."""
mock_hw_tool_helper.return_value.install.return_value = (True, "")
mock_exporter.return_value.install.return_value = True
self.harness.begin()
self.harness.charm._stored.exporter_installed = True
print(dir(self.harness.charm.on))
self.harness.charm.on.upgrade_charm.emit()

self.assertTrue(self.harness.charm._stored.resource_installed)
self.assertTrue(self.harness.charm._stored.exporter_installed)

self.harness.charm.exporter.install.assert_called_once()
self.harness.charm.hw_tool_helper.install.assert_called_with(
self.harness.charm.model.resources
)

0 comments on commit 314c287

Please sign in to comment.