From 314c28757f3ffdac0b010a1a82e6de613ca585b8 Mon Sep 17 00:00:00 2001 From: james_lin Date: Thu, 14 Dec 2023 17:04:38 +0800 Subject: [PATCH] fix: Force re-config exporter when upgrade (#140) --- src/charm.py | 32 ++++++++++++++++---------------- tests/unit/test_charm.py | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/charm.py b/src/charm.py index 962369f8..981813b9 100755 --- a/src/charm.py +++ b/src/charm.py @@ -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: diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index fcb379c7..72c5cc56 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -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 + )