Skip to content

Commit cca9c6c

Browse files
SAY-5dragomirp
andauthored
fix: make create-replication action fail cleanly without async-replication relation (#1676)
* fix: make create-replication action fail cleanly without async-replication relation Signed-off-by: SAY-5 <saiasish.cnp@gmail.com> * fix(test): scope PropertyMock to prevent class-level leak Signed-off-by: SAY-5 <saiasish.cnp@gmail.com> --------- Signed-off-by: SAY-5 <saiasish.cnp@gmail.com> Co-authored-by: Dragomir Penev <dragomir.penev@canonical.com>
1 parent d0eab0f commit cca9c6c

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/relations/async_replication.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,13 @@ def _on_create_replication(self, event: ActionEvent) -> None:
567567
event.fail("There is already a replication set up.")
568568
return
569569

570+
if self._relation is None:
571+
event.fail(
572+
"No async-replication relation has been established."
573+
" Create the offer and relate the two clusters before running this action."
574+
)
575+
return
576+
570577
if self._relation.name == REPLICATION_CONSUMER_RELATION:
571578
event.fail("This action must be run in the cluster where the offer was created.")
572579
return

tests/unit/test_async_replication.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,25 @@ def test_on_create_replication():
571571

572572
assert result is None
573573

574+
# 5. No async-replication relation established (regression for #1675).
575+
mock_charm = MagicMock()
576+
mock_event = MagicMock()
577+
578+
relation = PostgreSQLAsyncReplication(mock_charm)
579+
580+
relation._get_primary_cluster = MagicMock(return_value=None)
581+
582+
with patch.object(
583+
PostgreSQLAsyncReplication, "_relation", new_callable=PropertyMock, return_value=None
584+
):
585+
result = relation._on_create_replication(mock_event)
586+
587+
assert result is None
588+
mock_event.fail.assert_called_once_with(
589+
"No async-replication relation has been established."
590+
" Create the offer and relate the two clusters before running this action."
591+
)
592+
574593

575594
def test_promote_to_primary():
576595
# 1.

0 commit comments

Comments
 (0)