diff --git a/src/relations/async_replication.py b/src/relations/async_replication.py index 4518b7598e8..e9db11cd023 100644 --- a/src/relations/async_replication.py +++ b/src/relations/async_replication.py @@ -567,6 +567,13 @@ def _on_create_replication(self, event: ActionEvent) -> None: event.fail("There is already a replication set up.") return + if self._relation is None: + event.fail( + "No async-replication relation has been established." + " Create the offer and relate the two clusters before running this action." + ) + return + if self._relation.name == REPLICATION_CONSUMER_RELATION: event.fail("This action must be run in the cluster where the offer was created.") return diff --git a/tests/unit/test_async_replication.py b/tests/unit/test_async_replication.py index 0174a0080d1..379207dcfdd 100644 --- a/tests/unit/test_async_replication.py +++ b/tests/unit/test_async_replication.py @@ -571,6 +571,25 @@ def test_on_create_replication(): assert result is None + # 5. No async-replication relation established (regression for #1675). + mock_charm = MagicMock() + mock_event = MagicMock() + + relation = PostgreSQLAsyncReplication(mock_charm) + + relation._get_primary_cluster = MagicMock(return_value=None) + + with patch.object( + PostgreSQLAsyncReplication, "_relation", new_callable=PropertyMock, return_value=None + ): + result = relation._on_create_replication(mock_event) + + assert result is None + mock_event.fail.assert_called_once_with( + "No async-replication relation has been established." + " Create the offer and relate the two clusters before running this action." + ) + def test_promote_to_primary(): # 1.