Skip to content

Commit

Permalink
observe mongodb relation events
Browse files Browse the repository at this point in the history
  • Loading branch information
cbartz committed Jul 3, 2024
1 parent 1e70e19 commit 38ee335
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self.on.update_dependencies_action, self._on_update_dependencies_action
)
self.framework.observe(self.on.update_status, self._on_update_status)
self.framework.observe(self.on.mongodb_relation_joined, self._on_mongodb_relation_joined)
self.framework.observe(
self.on.mongodb_relation_departed, self._on_mongodb_relation_departed
)
self.framework.observe(self.on.mongodb_relation_changed, self._on_mongodb_relation_changed)

def _setup_state(self) -> CharmState:
"""Set up the charm state.
Expand Down Expand Up @@ -686,7 +691,26 @@ def _check_and_update_local_lxd_dependencies(

@catch_charm_errors
def _on_reconcile_runners(self, _: ReconcileRunnersEvent) -> None:
"""Handle the reconciliation of runners."""
"""Event handler for reconciling runners."""
self._trigger_reconciliation()

@catch_charm_errors
def _on_mongodb_relation_joined(self, _: ops.RelationEvent) -> None:
"""Handle the MongoDB relation joined event."""
self._trigger_reconciliation()

@catch_charm_errors
def _on_mongodb_relation_changed(self, _: ops.RelationEvent) -> None:
"""Handle the MongoDB relation changed event."""
self._trigger_reconciliation()

@catch_charm_errors
def _on_mongodb_relation_departed(self, _: ops.RelationEvent) -> None:
"""Handle the departure of the MongoDB relation."""
self._trigger_reconciliation()

def _trigger_reconciliation(self) -> None:
"""Trigger the reconciliation of runners."""
self.unit.status = MaintenanceStatus("Reconciling runners")
state = self._setup_state()

Expand Down
25 changes: 25 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,31 @@ def test_charm_goes_into_waiting_state_on_missing_integration_data(
assert "mock error" in harness.charm.unit.status.message


@pytest.mark.parametrize(
"hook",
[
pytest.param("mongodb_relation_joined", id="Relation Joined"),
pytest.param("mongodb_relation_changed", id="Relation Changed"),
pytest.param("mongodb_relation_departed", id="Relation Departed"),
],
)
def test_mongodb_integration_events_trigger_reconciliation(
hook: str, monkeypatch: pytest.MonkeyPatch, harness: Harness
):
"""
arrange: Mock charm._trigger_reconciliation.
act: Fire mongodb relation events.
assert: _trigger_reconciliation has been called.
"""
reconcliation_mock = MagicMock()
relation_mock = MagicMock()
relation_mock.name = "mongodb"
relation_mock.id = 0
monkeypatch.setattr("charm.GithubRunnerCharm._trigger_reconciliation", reconcliation_mock)
getattr(harness.charm.on, hook).emit(relation=relation_mock)
reconcliation_mock.assert_called_once()


# New tests should not be added here. This should be refactored to pytest over time.
# New test should be written with pytest, similar to the above tests.
# Consider to rewrite test with pytest if the tests below needs to be changed.
Expand Down

0 comments on commit 38ee335

Please sign in to comment.