Skip to content

Commit

Permalink
refactor: remove multiple relation check in charm code.
Browse files Browse the repository at this point in the history
The `metadata.yaml` support `limit` for interface, this can limits how
many relation the interface can be connected to, and we can handle the
relation checks in juju level.
  • Loading branch information
chanchiwai-ray committed May 2, 2024
1 parent 33ff8a3 commit 9a8333e
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 27 deletions.
11 changes: 1 addition & 10 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,6 @@ def _on_update_status(self, _: EventBase) -> None: # noqa: C901
self.model.unit.status = BlockedStatus("Missing relation: [cos-agent]")
return

if self.too_many_cos_agent_relations:
self.model.unit.status = BlockedStatus("Cannot relate to more than one grafana-agent")
return

config_valid, config_valid_message = self.validate_exporter_configs()
if not config_valid:
self.model.unit.status = BlockedStatus(config_valid_message)
Expand Down Expand Up @@ -322,12 +318,7 @@ def get_num_cos_agent_relations(self, relation_name: str) -> int:
@property
def exporter_enabled(self) -> bool:
"""Return True if cos-agent relation is present."""
return self.num_cos_agent_relations != 0

@property
def too_many_cos_agent_relations(self) -> bool:
"""Return True if there're more than one cos-agent relation."""
return self.num_cos_agent_relations > 1
return self.num_cos_agent_relations == 1

@property
def redfish_conn_params_valid(self) -> Optional[bool]:
Expand Down
17 changes: 0 additions & 17 deletions tests/unit/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,6 @@ def test_check_relation_not_exists(self, mock_service_installed):
self.harness.charm.unit.status, BlockedStatus("Missing relation: [cos-agent]")
)

@mock.patch.object(pathlib.Path, "exists", return_value=True)
def test_too_many_relations(self, mock_service_installed):
"""Test there too many relations."""
rid_1 = self.harness.add_relation(EXPORTER_RELATION_NAME, "grafana-agent")
rid_2 = self.harness.add_relation(EXPORTER_RELATION_NAME, "grafana-agent")
self.harness.begin()
with mock.patch("builtins.open", new_callable=mock.mock_open) as _:
self.harness.charm.on.install.emit()
self.harness.add_relation_unit(rid_1, "grafana-agent/0")
self.harness.add_relation_unit(rid_2, "grafana-agent/1")
self.mock_systemd.service_failed.return_value = False
self.harness.charm.on.update_status.emit()
self.assertEqual(
self.harness.charm.unit.status,
BlockedStatus("Cannot relate to more than one grafana-agent"),
)

@mock.patch.object(pathlib.Path, "exists", return_value=True)
def test_config_changed_log_level_okay(self, mock_service_installed):
"""Test on_config_change function when exporter-log-level is changed."""
Expand Down

0 comments on commit 9a8333e

Please sign in to comment.