Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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).
4 changes: 4 additions & 0 deletions plugins/modules/github_deploy_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ def main():
module.exit_json(changed=True)
elif deploy_key.state == "present" and key_id is not None:
module.exit_json(changed=False)
elif deploy_key.state == "absent" and key_id is not None:
module.exit_json(changed=True, msg="Deploy key successfully deleted", id=key_id)
elif deploy_key.state == "absent":
module.exit_json(changed=False, msg="Deploy key does not exist")

# to forcefully modify an existing key, the existing key must be deleted first
if deploy_key.state == 'absent' or deploy_key.force:
Expand Down
96 changes: 96 additions & 0 deletions tests/unit/plugins/modules/test_github_deploy_key.py
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
Comment thread
felixfontein marked this conversation as resolved.
Resolved
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")
Loading