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 Apr 29, 2024
1 parent ad0520e commit 80ea19f
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 @@ -126,10 +126,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 @@ -262,12 +258,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 @@ -252,23 +252,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 80ea19f

Please sign in to comment.