Repository navigation
[PR #13132/f7b4249f backport][stable-11] github_deploy_key: do not delete the key with state=absent in check mode #13161
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
Merged
felixfontein
merged 4 commits into
stable-11
from
patchback/backports/stable-11/f7b4249fffef6563ada9be3de076ef0a095bc3b2/pr-13132
Oct 30, 2026
Merged
Changes from 2 commits
Commits
Show all changes
4 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
2 changes: 2 additions & 0 deletions
2
changelogs/fragments/12348-github_deploy_key-check-mode-absent.yml
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,2 @@ | ||
| bugfixes: | ||
| - github_deploy_key - do not delete an existing deploy key when running with ``state=absent`` in check mode (https://github.com/ansible-collections/community.general/issues/12348, https://github.com/ansible-collections/community.general/pull/13132). |
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,96 @@ | ||
| # Copyright (c) Ansible Project | ||
| # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
| # SPDX-License-Identifier: GPL-3.0-or-later | ||
|
|
||
| import json | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
| from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( | ||
| AnsibleExitJson, | ||
| exit_json, | ||
| fail_json, | ||
| set_module_args, | ||
| ) | ||
|
|
||
| from ansible_collections.community.general.plugins.modules import github_deploy_key | ||
|
|
||
| KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyMaterial user@example" | ||
|
|
||
| EXISTING_KEYS = [ | ||
| {"id": 42, "key": "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyMaterial", "title": "deploy"}, | ||
| ] | ||
|
|
||
|
|
||
| def make_response(body, status=200): | ||
| response = MagicMock() | ||
| response.read.return_value = json.dumps(body).encode("utf-8") | ||
| return (response, {"status": status}) | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def patch_module(): | ||
| with patch.multiple( | ||
| "ansible.module_utils.basic.AnsibleModule", | ||
| exit_json=exit_json, | ||
| fail_json=fail_json, | ||
| ): | ||
| yield | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def fetch_url_mock(): | ||
| with patch.object(github_deploy_key, "fetch_url") as mock: | ||
| yield mock | ||
|
|
||
|
|
||
| def module_args(**kwargs): | ||
| args = { | ||
| "owner": "ansible", | ||
| "repo": "example", | ||
| "name": "deploy", | ||
| "key": KEY, | ||
| "token": "secret", | ||
| } | ||
| args.update(kwargs) | ||
| return args | ||
|
|
||
|
|
||
| def test_absent_check_mode_existing_key_is_not_deleted(fetch_url_mock): | ||
| fetch_url_mock.return_value = make_response(EXISTING_KEYS) | ||
|
|
||
| with set_module_args(module_args(state="absent", _ansible_check_mode=True)): | ||
| with pytest.raises(AnsibleExitJson) as exc: | ||
| github_deploy_key.main() | ||
|
|
||
| assert exc.value.args[0]["changed"] is True | ||
| assert exc.value.args[0]["id"] == "42" | ||
| methods = [call.kwargs.get("method") for call in fetch_url_mock.call_args_list] | ||
| assert "DELETE" not in methods | ||
| assert methods == ["GET"] | ||
|
|
||
|
|
||
| def test_absent_check_mode_missing_key(fetch_url_mock): | ||
| fetch_url_mock.return_value = make_response([]) | ||
|
|
||
| with set_module_args(module_args(state="absent", _ansible_check_mode=True)): | ||
| with pytest.raises(AnsibleExitJson) as exc: | ||
| github_deploy_key.main() | ||
|
|
||
| assert exc.value.args[0]["changed"] is False | ||
| assert "id" not in exc.value.args[0] | ||
| methods = [call.kwargs.get("method") for call in fetch_url_mock.call_args_list] | ||
| assert "DELETE" not in methods | ||
|
|
||
|
|
||
| def test_absent_deletes_existing_key(fetch_url_mock): | ||
| fetch_url_mock.side_effect = [make_response(EXISTING_KEYS), make_response(None, status=204)] | ||
|
|
||
| with set_module_args(module_args(state="absent")): | ||
| with pytest.raises(AnsibleExitJson) as exc: | ||
| github_deploy_key.main() | ||
|
|
||
| assert exc.value.args[0]["changed"] is True | ||
| assert exc.value.args[0]["id"] == "42" | ||
| assert fetch_url_mock.call_args_list[-1].kwargs["method"] == "DELETE" | ||
| assert fetch_url_mock.call_args_list[-1].args[1].endswith("/repos/ansible/example/keys/42") | ||
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.
Uh oh!
There was an error while loading. Please reload this page.