Debian Codegraph @

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

raw
1 #
2 # Copyright (c) 2025, Jana Hoch <[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 MagicMock, patch
8
9 import pytest
10
11 proxmoxer = pytest.importorskip("proxmoxer")
12
13 from ansible.module_utils import basic
14 from ansible.module_utils.compat.version import LooseVersion
15 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
16 ModuleTestCase,
17 set_module_args,
18 )
19
20 import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils
21 from ansible_collections.community.proxmox.plugins.modules import proxmox_firewall
22
23 RAW_FIREWALL_RULES = [
24 {
25 "ipversion": 4,
26 "digest": "245f9fb31d5f59543dedc5a84ba7cd6afa4dbcc0",
27 "log": "nolog",
28 "action": "ACCEPT",
29 "enable": 1,
30 "type": "out",
31 "source": "1.1.1.1",
32 "pos": 0,
33 },
34 {
35 "enable": 1,
36 "pos": 1,
37 "source": "1.0.0.1",
38 "type": "out",
39 "action": "ACCEPT",
40 "digest": "245f9fb31d5f59543dedc5a84ba7cd6afa4dbcc0",
41 "ipversion": 4,
42 },
43 ]
44
45 RAW_GROUPS = [
46 {"digest": "fdb62dec01018d4f35c83ecc2ae3f110a8b3bd62", "group": "test1"},
47 {"group": "test2", "digest": "fdb62dec01018d4f35c83ecc2ae3f110a8b3bd62"},
48 ]
49
50 RAW_IPSET = [{"digest": "48671c29c6503157990fc99354b78f32e8654c78", "name": "test_ipset"}]
51
52 RAW_IPSET_CIDR = [{"digest": "dce088809f001ca83c39c8dcfc2a5e4892bf3d1b", "cidr": "192.168.1.10", "nomatch": True}]
53
54
55 def exit_json(*args, **kwargs):
56 """function to patch over exit_json; package return data into an exception"""
57 if "changed" not in kwargs:
58 kwargs["changed"] = False
59 raise SystemExit(kwargs)
60
61
62 def fail_json(*args, **kwargs):
63 """function to patch over fail_json; package return data into an exception"""
64 kwargs["failed"] = True
65 raise SystemExit(kwargs)
66
67
68 def get_module_args_group_conf(group, level="cluster", state="present"):
69 return {
70 "api_host": "host",
71 "api_user": "user",
72 "api_password": "password",
73 "level": level,
74 "group": group,
75 "group_conf": True,
76 "state": state,
77 }
78
79
80 def get_module_args_rules(state, pos=1, level="cluster", source_ip="1.1.1.1"):
81 return {
82 "api_host": "host",
83 "api_user": "user",
84 "api_password": "password",
85 "level": level,
86 "state": state,
87 "rules": [{"type": "out", "action": "ACCEPT", "source": source_ip, "pos": pos, "enable": True}],
88 }
89
90
91 def get_module_args_ipset(state, name, cidr=False, nomatch=False):
92 if cidr:
93 return {
94 "api_host": "host",
95 "api_user": "user",
96 "api_password": "password",
97 "level": "cluster",
98 "state": state,
99 "ip_sets": [{"name": name, "cidrs": [{"cidr": "192.168.1.10", "nomatch": nomatch}]}],
100 }
101 return {
102 "api_host": "host",
103 "api_user": "user",
104 "api_password": "password",
105 "level": "cluster",
106 "state": state,
107 "ip_sets": [
108 {
109 "name": name,
110 }
111 ],
112 }
113
114
115 def get_module_args_fw_delete(pos, level="cluster", state="absent"):
116 return {
117 "api_host": "host",
118 "api_user": "user",
119 "api_password": "password",
120 "level": level,
121 "state": state,
122 "pos": pos,
123 }
124
125
126 class TestProxmoxFirewallModule(ModuleTestCase):
127 def setUp(self):
128 super().setUp()
129 proxmox_utils.HAS_PROXMOXER = True
130 self.module = proxmox_firewall
131 self.mock_module_helper = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json)
132 self.mock_module_helper.start()
133 self.connect_mock = patch(
134 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect",
135 ).start()
136 self.version_mock = patch.object(
137 proxmox_firewall.ProxmoxFirewallAnsible, "version", return_value=LooseVersion("9.0")
138 ).start()
139
140 mock_cluster = self.connect_mock.return_value.cluster.return_value.firewall
141 # return the mock object as return value to enable passing it on as firewall_obj
142 mock_cluster.return_value = mock_cluster
143 mock_cluster.rules.return_value.get.return_value = RAW_FIREWALL_RULES
144 mock_cluster.groups.return_value.get.return_value = RAW_GROUPS
145 # while rules and groups are currently accessed with their full paths,
146 # ipset output needs to change if it receives an argument
147 mock_cluster.ipset.side_effect = lambda name=None: MagicMock(
148 get=MagicMock(return_value=RAW_IPSET_CIDR if name else RAW_IPSET)
149 )
150
151 def tearDown(self):
152 self.connect_mock.stop()
153 self.mock_module_helper.stop()
154 self.version_mock.stop()
155 super().tearDown()
156
157 def test_create_group(self):
158 with pytest.raises(SystemExit) as exc_info, set_module_args(get_module_args_group_conf(group="test")):
159 self.module.main()
160 result = exc_info.value.args[0]
161 assert result["changed"] is True
162 assert result["msg"] == "successfully created security group test"
163 assert result["group"] == "test"
164
165 def test_delete_group(self):
166 with pytest.raises(SystemExit) as exc_info, set_module_args(
167 get_module_args_group_conf(group="test1", state="absent")
168 ):
169 self.module.main()
170 result = exc_info.value.args[0]
171 assert result["changed"] is True
172 assert result["msg"] == "successfully deleted security group test1"
173 assert result["group"] == "test1"
174
175 def test_create_fw_rules(self):
176 with pytest.raises(SystemExit) as exc_info, set_module_args(get_module_args_rules(state="present", pos=2)):
177 self.module.main()
178 result = exc_info.value.args[0]
179 assert result["changed"] is True
180 assert result["msg"] == "successfully created/updated firewall rules"
181
182 def test_delete_fw_rule(self):
183 with pytest.raises(SystemExit) as exc_info, set_module_args(get_module_args_fw_delete(state="absent", pos=0)):
184 self.module.main()
185 result = exc_info.value.args[0]
186 assert result["changed"] is True
187 assert result["msg"] == "successfully deleted firewall rules"
188
189 def test_ipset_present(self):
190 # New Ipset name
191 with pytest.raises(SystemExit) as exc_info, set_module_args(
192 get_module_args_ipset(state="present", name="new_ipset", cidr=True)
193 ):
194 self.module.main()
195 result = exc_info.value.args[0]
196 assert result["changed"] is True
197 assert result["msg"] == "All ipsets present."
198
199 # Same as existing ipset (no change)
200 with pytest.raises(SystemExit) as exc_info, set_module_args(
201 get_module_args_ipset(state="present", name="test_ipset", cidr=True, nomatch=True)
202 ):
203 self.module.main()
204 result = exc_info.value.args[0]
205 assert result["changed"] is False
206 assert result["msg"] == "All ipsets present."
207
208 def test_ipset_absent(self):
209 # Delete full ipset
210 with pytest.raises(SystemExit) as exc_info, set_module_args(
211 get_module_args_ipset(state="absent", name="test_ipset", cidr=False)
212 ):
213 self.module.main()
214 result = exc_info.value.args[0]
215 assert result["changed"] is True
216 assert result["msg"] == "Ipsets are absent."
217
218 # Delete only 1 CIDR
219 with pytest.raises(SystemExit) as exc_info, set_module_args(
220 get_module_args_ipset(state="absent", name="test_ipset", cidr=True)
221 ):
222 self.module.main()
223 result = exc_info.value.args[0]
224 assert result["changed"] is True
225 assert result["msg"] == "Ipsets are absent."
226
227 # try to delete non existent ipset (changed=False)
228 with pytest.raises(SystemExit) as exc_info, set_module_args(
229 get_module_args_ipset(state="absent", name="ipset_doesnt_exist")
230 ):
231 self.module.main()
232 result = exc_info.value.args[0]
233 assert result["changed"] is False
234 assert result["msg"] == "Ipsets are absent."