Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up logic in _on_config_changed event #285

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,22 +231,22 @@ def _on_config_changed(self, event: EventBase) -> None:
event.handle,
)
event.defer()
return

if self.cos_agent_related:
jneo8 marked this conversation as resolved.
Show resolved Hide resolved
success, message = self.validate_configs()
if not success:
success, message = self.validate_configs()
if not success:
self.model.unit.status = BlockedStatus(message)
return
for exporter in self.exporters:
success = exporter.render_config()
if success:
exporter.restart()
else:
message = (
f"Failed to configure {exporter.exporter_name}, "
"please check if the server is healthy."
)
self.model.unit.status = BlockedStatus(message)
return
for exporter in self.exporters:
success = exporter.render_config()
if success:
exporter.restart()
else:
message = (
f"Failed to configure {exporter.exporter_name}, "
"please check if the server is healthy."
)
self.model.unit.status = BlockedStatus(message)

self._on_update_status(event)

Expand Down
47 changes: 25 additions & 22 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,29 +552,32 @@ 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_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))
else:
zxhdaze marked this conversation as resolved.
Show resolved Hide resolved
if not cos_agent_related:
self.harness.charm.validate_configs.assert_called()
self.harness.charm._on_update_status.assert_called()
return
zxhdaze marked this conversation as resolved.
Show resolved Hide resolved
gabrielcocenza marked this conversation as resolved.
Show resolved Hide resolved
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()
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
Loading