community.general/tests/unit/plugins/modules/test_rundeck_acl_policy.py
Felix Fontein 8f8a0e1d7c
Some checks are pending
EOL CI / EOL Sanity (Ⓐ2.17) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.17+py3.10) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.17+py3.12) (push) Waiting to run
EOL CI / EOL Units (Ⓐ2.17+py3.7) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+alpine319+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+alpine319+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+alpine319+py:azp/posix/3/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+fedora39+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+fedora39+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+fedora39+py:azp/posix/3/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+ubuntu2004+py:azp/posix/1/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+ubuntu2004+py:azp/posix/2/) (push) Waiting to run
EOL CI / EOL I (Ⓐ2.17+ubuntu2004+py:azp/posix/3/) (push) Waiting to run
nox / Run extra sanity tests (push) Waiting to run
Fix __future__ imports, __metaclass__ = type, and remove explicit UTF-8 encoding statement for Python files (#10886)
* Adjust all __future__ imports:

for i in $(grep -REl "__future__.*absolute_import" plugins/ tests/); do
  sed -e 's/from __future__ import .*/from __future__ import annotations/g' -i $i;
done

* Remove all UTF-8 encoding specifications for Python source files:

for i in $(grep -REl '[-][*]- coding: utf-8 -[*]-' plugins/ tests/); do
  sed -e '/^# -\*- coding: utf-8 -\*-/d' -i $i;
done

* Remove __metaclass__ = type:

for i in $(grep -REl '__metaclass__ = type' plugins/ tests/); do
  sed -e '/^__metaclass__ = type/d' -i $i;
done
2025-10-10 19:52:04 +02:00

154 lines
4.8 KiB
Python

# 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
from __future__ import annotations
import pytest
from ansible_collections.community.general.plugins.modules import rundeck_acl_policy
from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import patch
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
set_module_args,
AnsibleExitJson,
exit_json,
fail_json
)
@pytest.fixture(autouse=True)
def module():
with patch.multiple(
"ansible.module_utils.basic.AnsibleModule",
exit_json=exit_json,
fail_json=fail_json,
):
yield
# define our two table entries: system ACL vs. project ACL
PROJECT_TABLE = [
(None, "system/acl"),
("test_project", "project/test_project/acl"),
]
@pytest.mark.parametrize("project, prefix", PROJECT_TABLE)
@patch.object(rundeck_acl_policy, 'api_request')
def test_acl_create(api_request_mock, project, prefix):
"""Test creating a new ACL, both system-level and project-level."""
name = "my_policy"
policy = "test_policy_yaml"
# simulate: GET→404, POST→201, final GET→200
api_request_mock.side_effect = [
(None, {'status': 404}),
(None, {'status': 201}),
({"contents": policy}, {'status': 200}),
]
args = {
'name': name,
'url': "https://rundeck.example.org",
'api_token': "mytoken",
'policy': policy,
}
if project:
args['project'] = project
with pytest.raises(AnsibleExitJson):
with set_module_args(args):
rundeck_acl_policy.main()
# should have done GET → POST → GET
assert api_request_mock.call_count == 3
args, kwargs = api_request_mock.call_args_list[1]
assert kwargs['endpoint'] == "%s/%s.aclpolicy" % (prefix, name)
assert kwargs['method'] == 'POST'
@pytest.mark.parametrize("project, prefix", PROJECT_TABLE)
@patch.object(rundeck_acl_policy, 'api_request')
def test_acl_unchanged(api_request_mock, project, prefix):
"""Test no-op when existing ACL contents match the desired policy."""
name = "unchanged_policy"
policy = "same_policy_yaml"
# first GET returns matching contents
api_request_mock.return_value = ({"contents": policy}, {'status': 200})
args = {
'name': name,
'url': "https://rundeck.example.org",
'api_token': "mytoken",
'policy': policy,
}
if project:
args['project'] = project
with pytest.raises(AnsibleExitJson):
with set_module_args(args):
rundeck_acl_policy.main()
# only a single GET
assert api_request_mock.call_count == 1
args, kwargs = api_request_mock.call_args
assert kwargs['endpoint'] == "%s/%s.aclpolicy" % (prefix, name)
# default method is GET
assert kwargs.get('method', 'GET') == 'GET'
@pytest.mark.parametrize("project, prefix", PROJECT_TABLE)
@patch.object(rundeck_acl_policy, 'api_request')
def test_acl_remove(api_request_mock, project, prefix):
"""Test removing an existing ACL, both system- and project-level."""
name = "remove_me"
# GET finds it, DELETE removes it
api_request_mock.side_effect = [
({"contents": "old_yaml"}, {'status': 200}),
(None, {'status': 204}),
]
args = {
'name': name,
'url': "https://rundeck.example.org",
'api_token': "mytoken",
'state': 'absent',
}
if project:
args['project'] = project
with pytest.raises(AnsibleExitJson):
with set_module_args(args):
rundeck_acl_policy.main()
# GET → DELETE
assert api_request_mock.call_count == 2
args, kwargs = api_request_mock.call_args_list[1]
assert kwargs['endpoint'] == "%s/%s.aclpolicy" % (prefix, name)
assert kwargs['method'] == 'DELETE'
@pytest.mark.parametrize("project, prefix", PROJECT_TABLE)
@patch.object(rundeck_acl_policy, 'api_request')
def test_acl_remove_nonexistent(api_request_mock, project, prefix):
"""Test removing a non-existent ACL results in no change."""
name = "not_there"
# GET returns 404
api_request_mock.return_value = (None, {'status': 404})
args = {
'name': name,
'url': "https://rundeck.example.org",
'api_token': "mytoken",
'state': 'absent',
}
if project:
args['project'] = project
with pytest.raises(AnsibleExitJson):
with set_module_args(args):
rundeck_acl_policy.main()
# only the initial GET
assert api_request_mock.call_count == 1
args, kwargs = api_request_mock.call_args
assert kwargs['endpoint'] == "%s/%s.aclpolicy" % (prefix, name)
assert kwargs.get('method', 'GET') == 'GET'