Debian Codegraph @

ansible_collections/community/proxmox/tests/unit/plugins/modules/test_proxmox_access_acl.py

raw
1 #
2 # Copyright (c) 2025, Ansible Project
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 from unittest.mock import patch
7
8 import pytest
9
10 proxmoxer = pytest.importorskip("proxmoxer")
11
12 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
13 AnsibleExitJson,
14 ModuleTestCase,
15 set_module_args,
16 )
17
18 from ansible_collections.community.proxmox.plugins.modules import proxmox_access_acl
19
20 ACE = {
21 "path": "/vms/100",
22 "propagate": 1,
23 "roleid": "PVEVMUser",
24 "type": "user",
25 "ugid": "a01mako@pam",
26 }
27
28 API = {
29 "api_user": "root@pam",
30 "api_password": "secret",
31 "api_host": "127.0.0.1",
32 }
33
34
35 class TestProxmoxAccessACLModule(ModuleTestCase):
36 def setUp(self):
37 super().setUp()
38
39 self.connect_mock = patch(
40 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect"
41 ).start()
42 self.mock_get = patch.object(proxmox_access_acl.ProxmoxAccessACLAnsible, "_get_acls").start()
43 self.mock_put = patch.object(proxmox_access_acl.ProxmoxAccessACLAnsible, "_put_acl").start()
44
45 self.mock_get.return_value = [ACE]
46
47 def tearDown(self):
48 self.connect_mock.stop()
49 self.mock_get.stop()
50 self.mock_put.stop()
51 super().tearDown()
52
53 def test_present_ace_already_exists(self):
54 with set_module_args({**API, "state": "present", **ACE}), pytest.raises(AnsibleExitJson) as exc_info:
55 proxmox_access_acl.main()
56
57 result = exc_info.value.args[0]
58 assert result["changed"] is False
59 assert self.mock_get.call_count == 1
60 assert self.mock_put.call_count == 0
61
62 def test_present_ace_does_not_exist(self):
63 with set_module_args({**API, "state": "present", **ACE, "path": "/vms/101"}), pytest.raises(
64 AnsibleExitJson
65 ) as exc_info:
66 proxmox_access_acl.main()
67
68 result = exc_info.value.args[0]
69 assert result["changed"] is True
70 assert self.mock_get.call_count == 2 # noqa: PLR2004
71 assert self.mock_put.call_count == 1
72
73 def test_absent_ace_exists(self):
74 with set_module_args({**API, "state": "absent", **ACE}), pytest.raises(AnsibleExitJson) as exc_info:
75 proxmox_access_acl.main()
76
77 result = exc_info.value.args[0]
78 assert result["changed"] is True
79 assert self.mock_get.call_count == 2 # noqa: PLR2004
80 assert self.mock_put.call_count == 1
81
82 def test_absent_ace_does_not_exist(self):
83 with set_module_args({**API, "state": "absent", **ACE, "path": "/vms/101"}), pytest.raises(
84 AnsibleExitJson
85 ) as exc_info:
86 proxmox_access_acl.main()
87
88 result = exc_info.value.args[0]
89 assert result["changed"] is False
90 assert self.mock_get.call_count == 1
91 assert self.mock_put.call_count == 0