| 1 |
# |
| 2 |
# Copyright (c) 2026, Clément Cruau (@PendaGTP) <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
|
| 7 |
from unittest.mock import Mock, patch |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
proxmoxer = pytest.importorskip("proxmoxer") |
| 12 |
|
| 13 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 14 |
ModuleTestCase, |
| 15 |
set_module_args, |
| 16 |
) |
| 17 |
|
| 18 |
import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils |
| 19 |
from ansible_collections.community.proxmox.plugins.modules import proxmox_role |
| 20 |
|
| 21 |
ROLE_ID = "role-test" |
| 22 |
|
| 23 |
|
| 24 |
def exit_json(*args, **kwargs): |
| 25 |
"""function to patch over exit_json; package return data into an exception""" |
| 26 |
if "changed" not in kwargs: |
| 27 |
kwargs["changed"] = False |
| 28 |
raise SystemExit(kwargs) |
| 29 |
|
| 30 |
|
| 31 |
def fail_json(*args, **kwargs): |
| 32 |
"""function to patch over fail_json; package return data into an exception""" |
| 33 |
kwargs["failed"] = True |
| 34 |
raise SystemExit(kwargs) |
| 35 |
|
| 36 |
|
| 37 |
def get_module_args(roleid, privs=None, state="present"): |
| 38 |
return { |
| 39 |
"api_host": "host", |
| 40 |
"api_user": "user", |
| 41 |
"api_password": "password", |
| 42 |
"roleid": roleid, |
| 43 |
"privs": privs, |
| 44 |
"state": state, |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
class TestProxmoxRoleModule(ModuleTestCase): |
| 49 |
def setUp(self): |
| 50 |
super().setUp() |
| 51 |
proxmox_utils.HAS_PROXMOXER = True |
| 52 |
self.module = proxmox_role |
| 53 |
self.fail_json_patcher = patch( |
| 54 |
"ansible.module_utils.basic.AnsibleModule.fail_json", new=Mock(side_effect=fail_json) |
| 55 |
) |
| 56 |
self.exit_json_patcher = patch("ansible.module_utils.basic.AnsibleModule.exit_json", new=exit_json) |
| 57 |
self.fail_json_mock = self.fail_json_patcher.start() |
| 58 |
self.exit_json_patcher.start() |
| 59 |
self.connect_mock = patch( |
| 60 |
"ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect" |
| 61 |
).start() |
| 62 |
self.mock_get_role = patch.object(proxmox_role.ProxmoxRoleAnsible, "_get_role").start() |
| 63 |
|
| 64 |
def tearDown(self): |
| 65 |
self.connect_mock.stop() |
| 66 |
self.exit_json_patcher.stop() |
| 67 |
self.fail_json_patcher.stop() |
| 68 |
self.mock_get_role.stop() |
| 69 |
super().tearDown() |
| 70 |
|
| 71 |
def _run_module(self, args): |
| 72 |
with pytest.raises(SystemExit) as exc_info, set_module_args(args): |
| 73 |
self.module.main() |
| 74 |
return exc_info.value.args[0] |
| 75 |
|
| 76 |
def _check_mode_args(self, **kwargs): |
| 77 |
return {**get_module_args(**kwargs), "_ansible_check_mode": True} |
| 78 |
|
| 79 |
def test_role_present(self): |
| 80 |
# Role does not exist, then is created with no privileges |
| 81 |
self.mock_get_role.side_effect = [None, {}] |
| 82 |
result = self._run_module(get_module_args(roleid=ROLE_ID)) |
| 83 |
assert result["changed"] is True |
| 84 |
assert result["msg"] == f"Role {ROLE_ID} successfully created" |
| 85 |
assert result["roleid"] == ROLE_ID |
| 86 |
assert result["privs"] == [] |
| 87 |
|
| 88 |
# Role exists already with desired configuration (no privileges) |
| 89 |
self.mock_get_role.side_effect = [{}] |
| 90 |
result = self._run_module(get_module_args(roleid=ROLE_ID)) |
| 91 |
assert result["changed"] is False |
| 92 |
assert result["msg"] == f"Role {ROLE_ID} already exists with desired configuration" |
| 93 |
assert result["roleid"] == ROLE_ID |
| 94 |
assert result["privs"] == [] |
| 95 |
|
| 96 |
# Role exists already with privileges but privs omitted - privileges are kept unchanged |
| 97 |
self.mock_get_role.side_effect = [{"VM.Console": 1}] |
| 98 |
result = self._run_module(get_module_args(roleid=ROLE_ID)) |
| 99 |
assert result["changed"] is False |
| 100 |
assert result["msg"] == f"Role {ROLE_ID} already exists with desired configuration" |
| 101 |
assert result["roleid"] == ROLE_ID |
| 102 |
assert result["privs"] == ["VM.Console"] |
| 103 |
|
| 104 |
# Role exists without privileges, then is updated to have the requested privileges |
| 105 |
self.mock_get_role.side_effect = [{}, {"VM.Console": 1}] |
| 106 |
result = self._run_module(get_module_args(roleid=ROLE_ID, privs=["VM.Console"])) |
| 107 |
assert result["changed"] is True |
| 108 |
assert result["msg"] == f"Role {ROLE_ID} successfully updated" |
| 109 |
assert result["roleid"] == ROLE_ID |
| 110 |
assert result["privs"] == ["VM.Console"] |
| 111 |
|
| 112 |
def test_role_absent(self): |
| 113 |
self.mock_get_role.return_value = {} |
| 114 |
result = self._run_module(get_module_args(roleid=ROLE_ID, state="absent")) |
| 115 |
assert result["changed"] is True |
| 116 |
assert result["msg"] == f"Role {ROLE_ID} successfully deleted" |
| 117 |
assert result["roleid"] == ROLE_ID |
| 118 |
|
| 119 |
self.mock_get_role.return_value = None |
| 120 |
result = self._run_module(get_module_args(roleid=ROLE_ID, state="absent")) |
| 121 |
assert result["changed"] is False |
| 122 |
assert result["msg"] == f"Role {ROLE_ID} does not exist" |
| 123 |
assert result["roleid"] == ROLE_ID |
| 124 |
|
| 125 |
def test_role_present_check_mode(self): |
| 126 |
self.mock_get_role.return_value = None |
| 127 |
result = self._run_module(self._check_mode_args(roleid=ROLE_ID)) |
| 128 |
assert result["changed"] is True |
| 129 |
assert result["msg"] == f"Role {ROLE_ID} would be created" |
| 130 |
assert result["roleid"] == ROLE_ID |
| 131 |
assert result["privs"] == [] |
| 132 |
|
| 133 |
self.mock_get_role.return_value = {} |
| 134 |
result = self._run_module(self._check_mode_args(roleid=ROLE_ID)) |
| 135 |
assert result["changed"] is False |
| 136 |
assert result["msg"] == f"Role {ROLE_ID} already exists with desired configuration" |
| 137 |
assert result["roleid"] == ROLE_ID |
| 138 |
assert result["privs"] == [] |
| 139 |
|
| 140 |
self.mock_get_role.return_value = {"VM.Console": True} |
| 141 |
result = self._run_module(self._check_mode_args(roleid=ROLE_ID)) |
| 142 |
assert result["changed"] is False |
| 143 |
assert result["msg"] == f"Role {ROLE_ID} already exists with desired configuration" |
| 144 |
assert result["roleid"] == ROLE_ID |
| 145 |
assert result["privs"] == ["VM.Console"] |
| 146 |
|
| 147 |
self.mock_get_role.return_value = {} |
| 148 |
result = self._run_module(self._check_mode_args(roleid=ROLE_ID, privs=["VM.Console"])) |
| 149 |
assert result["changed"] is True |
| 150 |
assert result["msg"] == f"Role {ROLE_ID} would be updated" |
| 151 |
assert result["roleid"] == ROLE_ID |
| 152 |
assert result["privs"] == ["VM.Console"] |
| 153 |
|
| 154 |
def test_role_absent_check_mode(self): |
| 155 |
self.mock_get_role.return_value = {} |
| 156 |
result = self._run_module(self._check_mode_args(roleid=ROLE_ID, state="absent")) |
| 157 |
assert result["changed"] is True |
| 158 |
assert result["msg"] == f"Role {ROLE_ID} would be deleted" |
| 159 |
assert result["roleid"] == ROLE_ID |
| 160 |
|
| 161 |
self.mock_get_role.return_value = None |
| 162 |
result = self._run_module(self._check_mode_args(roleid=ROLE_ID, state="absent")) |
| 163 |
assert result["changed"] is False |
| 164 |
assert result["msg"] == f"Role {ROLE_ID} does not exist" |
| 165 |
assert result["roleid"] == ROLE_ID |