diff --git a/src/charm.py b/src/charm.py index 9cc2b2a85..9c22409b8 100755 --- a/src/charm.py +++ b/src/charm.py @@ -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. @@ -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() diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index 230bb1c7b..eada6e6b5 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -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.