Skip to content

Commit

Permalink
Clean up logic in _on_config_changed event (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
zxhdaze authored Aug 8, 2024
1 parent 4f41afd commit cd1055d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ def _on_config_changed(self, event: EventBase) -> None:
event.handle,
)
event.defer()
return

if self.cos_agent_related:
success, message = self.validate_configs()
Expand Down
47 changes: 26 additions & 21 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,29 +552,34 @@ def test_config_changed(

if not resource_installed:
mock_logger.info.assert_called()
if not cos_agent_related:
self.harness.charm.validate_configs.assert_not_called()
self.harness.charm._on_update_status.assert_not_called()
else:
if not cos_agent_related:
self.harness.charm.validate_configs.assert_not_called()
self.harness.charm._on_update_status.assert_called()
return
if not validate_configs_return[0]:
self.assertEqual(self.harness.charm.unit.status, BlockedStatus("invalid msg"))
self.harness.charm.exporters[0].render_config.assert_not_called()
return
if not all(mock_exporters_render_config_returns):
for mock_exporter, render_config_return in zip(
mock_exporters,
mock_exporters_render_config_returns,
):
if render_config_return:
mock_exporter.restart.assert_called()
else:
message = (
f"Failed to configure {mock_exporter.exporter_name}, "
f"please check if the server is healthy."
)
self.assertEqual(
self.harness.charm.unit.status, BlockedStatus(message)
)
self.harness.charm._on_update_status.assert_called()
self.harness.charm._on_update_status.assert_called()
return
if not validate_configs_return[0]:
self.assertEqual(self.harness.charm.unit.status, BlockedStatus("invalid msg"))
self.harness.charm.exporters[0].render_config.assert_not_called()
return
if not all(mock_exporters_render_config_returns):
for mock_exporter, render_config_return in zip(
mock_exporters,
mock_exporters_render_config_returns,
):
if render_config_return:
mock_exporter.restart.assert_called()
else:
message = (
f"Failed to configure {mock_exporter.exporter_name}, "
f"please check if the server is healthy."
)
self.assertEqual(self.harness.charm.unit.status, BlockedStatus(message))
self.harness.charm._on_update_status.assert_called()
self.harness.charm._on_update_status.assert_called()

def test_config_changed_update_alert_rules(self):
"""Test config changed will update alert rule."""
Expand Down

0 comments on commit cd1055d

Please sign in to comment.