generated from canonical/template-operator
-
Notifications
You must be signed in to change notification settings - Fork 16
bug : replace event.set_results(success=False) with event.fail() in get-cluster-status #663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arjun11-malik
wants to merge
3
commits into
canonical:main
Choose a base branch
from
arjun11-malik:bugfix/get-cluster-status-fail
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# Copyright 2025 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
from unittest.mock import Mock, PropertyMock, patch | ||
|
||
import pytest | ||
from ops.charm import ActionEvent | ||
from ops.testing import Harness | ||
|
||
from charm import MySQLOperatorCharm | ||
|
||
|
||
class FakeMySQLBackend: | ||
"""Simulates the real MySQL backend, either returning a dict or raising.""" | ||
|
||
def __init__(self, response=None, error=None): | ||
self._response = response | ||
self._error = error | ||
|
||
def get_cluster_status(self): | ||
"""Return the preset response or raise the preset error.""" | ||
if self._error: | ||
raise self._error | ||
return self._response | ||
|
||
|
||
@pytest.fixture | ||
def harness(): | ||
"""Start the charm so harness.charm exists and peer databag works.""" | ||
h = Harness(MySQLOperatorCharm) | ||
h.begin() | ||
return h | ||
|
||
|
||
def make_event(): | ||
"""Create a dummy ActionEvent with spies on set_results() and fail().""" | ||
evt = Mock(spec=ActionEvent) | ||
evt.set_results = Mock() | ||
evt.fail = Mock() | ||
evt.params = {} # ensure .params.get() won't AttributeError | ||
return evt | ||
|
||
|
||
def test_get_cluster_status_action_success(harness): | ||
"""On success, the action wraps and forwards the status dict.""" | ||
# Prepare peer-databag so handler finds a cluster-name | ||
rel = harness.add_relation("database-peers", "database-peers") | ||
harness.update_relation_data(rel, harness.charm.app.name, {"cluster-name": "my-cluster"}) | ||
|
||
# Patch out the MySQL backend to return a known dict | ||
sample = {"clusterrole": "primary", "status": "ok"} | ||
fake = FakeMySQLBackend(response=sample) | ||
with patch.object(MySQLOperatorCharm, "_mysql", new_callable=PropertyMock, return_value=fake): | ||
evt = make_event() | ||
|
||
# Invoke the action | ||
harness.charm._get_cluster_status(evt) | ||
|
||
# Expect set_results called once with {'success': True, 'status': sample} | ||
evt.set_results.assert_called_once_with({"success": True, "status": sample}) | ||
evt.fail.assert_not_called() | ||
|
||
|
||
def test_get_cluster_status_action_failure(harness): | ||
"""On backend error, the action calls event.fail() and does not set_results().""" | ||
# Seed peer-databag for cluster-name lookup | ||
rel = harness.add_relation("database-peers", "database-peers") | ||
harness.update_relation_data(rel, harness.charm.app.name, {"cluster-name": "my-cluster"}) | ||
|
||
# Patch MySQL backend to always raise | ||
fake = FakeMySQLBackend(error=RuntimeError("boom")) | ||
with patch.object(MySQLOperatorCharm, "_mysql", new_callable=PropertyMock, return_value=fake): | ||
evt = make_event() | ||
|
||
# Invoke the action | ||
harness.charm._get_cluster_status(evt) | ||
|
||
# It should report failure and never set_results | ||
evt.fail.assert_called_once() | ||
args, _ = evt.fail.call_args | ||
assert "Failed to read cluster status" in args[0] | ||
|
||
evt.set_results.assert_not_called() | ||
|
||
|
||
def test_get_cluster_status_action_none_return(harness): | ||
"""When the backend returns None (no error), the action should fail.""" | ||
rel = harness.add_relation("database-peers", "database-peers") | ||
harness.update_relation_data(rel, harness.charm.app.name, {"cluster-name": "my-cluster"}) | ||
|
||
fake = FakeMySQLBackend(response=None) # Simulate silent failure | ||
with patch.object(MySQLOperatorCharm, "_mysql", new_callable=PropertyMock, return_value=fake): | ||
evt = make_event() | ||
harness.charm._get_cluster_status(evt) | ||
|
||
evt.fail.assert_called_once_with( | ||
"Failed to read cluster status. See logs for more information." | ||
) | ||
evt.set_results.assert_not_called() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@arjun11-malik make sure that the output change is not being misinterpreted on the the failing tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve double-checked every integration test that invokes get-cluster-status—they only wait for the action to complete and then read out results["status"]. None of them inspect the old success=False payload or rely on its exact shape, so switching to event.fail() on empty status won’t change their behavior.