Skip to content

Commit 95ea517

Browse files
committed
test: increase coverage for get_cluster_status action edge case
1 parent a837dca commit 95ea517

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

lib/charms/mysql/v0/mysql.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -579,19 +579,18 @@ def _get_cluster_status(self, event: ActionEvent) -> None:
579579
logger.debug("Getting cluster status")
580580
status = self._mysql.get_cluster_status()
581581

582+
if not status:
583+
event.fail("Failed to read cluster status. See logs for more information.")
584+
return
585+
586+
event.set_results({
587+
"success": True,
588+
"status": status,
589+
})
590+
582591
except Exception:
583592
logger.exception("Failed to read cluster status")
584593
event.fail("Failed to read cluster status. See logs for more information.")
585-
return
586-
587-
if not status:
588-
event.fail("Failed to read cluster status. See logs for more information.")
589-
return
590-
591-
event.set_results({
592-
"success": True,
593-
"status": status,
594-
})
595594

596595
def _on_promote_to_primary(self, event: ActionEvent) -> None:
597596
"""Action for setting this unit as the cluster primary."""

tests/unit/test_action_get_cluster_status.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ def test_get_cluster_status_action_failure(harness):
7676
harness.charm._get_cluster_status(evt)
7777

7878
# It should report failure and never set_results
79+
evt.fail.assert_called_once()
80+
args, _ = evt.fail.call_args
81+
assert "Failed to read cluster status" in args[0]
82+
83+
evt.set_results.assert_not_called()
84+
85+
86+
def test_get_cluster_status_action_none_return(harness):
87+
"""When the backend returns None (no error), the action should fail."""
88+
rel = harness.add_relation("database-peers", "database-peers")
89+
harness.update_relation_data(rel, harness.charm.app.name, {"cluster-name": "my-cluster"})
90+
91+
fake = FakeMySQLBackend(response=None) # Simulate silent failure
92+
with patch.object(MySQLOperatorCharm, "_mysql", new_callable=PropertyMock, return_value=fake):
93+
evt = make_event()
94+
harness.charm._get_cluster_status(evt)
95+
7996
evt.fail.assert_called_once_with(
8097
"Failed to read cluster status. See logs for more information."
8198
)

0 commit comments

Comments
 (0)