generated from canonical/template-operator
-
Notifications
You must be signed in to change notification settings - Fork 18
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
Closed
arjun11-malik
wants to merge
5
commits into
canonical:main
from
arjun11-malik:bugfix/get-cluster-status-fail
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c6e73c7
Fix: use event.fail() instead of set_results(success=False)
arjun11-malik 3da6a34
test: increase coverage for get_cluster_status action edge case
arjun11-malik 897026c
tests: fix lint (ASCII apostrophe) in get-cluster-status unit tests
arjun11-malik 3c11c63
feat: use different failure state depending on condition
astrojuanlu 4f9ad9d
style: lint
astrojuanlu 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,86 @@ | ||
| # 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() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "backend_result", | ||
| [ | ||
| {"error": RuntimeError("boom")}, | ||
| {"response": None}, # silent failure | ||
| ], | ||
| ) | ||
| def test_get_cluster_status_action_failure(backend_result, 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"}) | ||
|
|
||
| fake = FakeMySQLBackend(**backend_result) | ||
| 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() | ||
| evt.set_results.assert_not_called() |
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.