Debian Codegraph @

ansible_collections/community/proxmox/tests/unit/plugins/modules/test_proxmox_zone.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 Mock, patch
8
9 import pytest
10
11 proxmoxer = pytest.importorskip("proxmoxer")
12
13 from ansible.module_utils.compat.version import LooseVersion
14 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
15 ModuleTestCase,
16 set_module_args,
17 )
18
19 import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils
20 from ansible_collections.community.proxmox.plugins.modules import proxmox_zone
21
22 RAW_ZONES = [
23 {
24 "zone": "ans1",
25 "digest": "e3105246736ab2420104e34bca1dea68d152acc7",
26 "ipam": "pve",
27 "dhcp": "dnsmasq",
28 "type": "simple",
29 },
30 {
31 "type": "vlan",
32 "zone": "lab",
33 "digest": "e3105246736ab2420104e34bca1dea68d152acc7",
34 "ipam": "pve",
35 "bridge": "vmbr100",
36 },
37 {
38 "digest": "e3105246736ab2420104e34bca1dea68d152acc7",
39 "ipam": "pve",
40 "zone": "test1",
41 "type": "simple",
42 "dhcp": "dnsmasq",
43 },
44 {"zone": "vxlan-fabric-zone", "digest": "abc123def456", "type": "vxlan", "fabric": "my-fabric", "mtu": 1450},
45 {
46 "zone": "vxlan-peers-zone",
47 "digest": "def456abc123",
48 "type": "vxlan",
49 "peers": "192.168.1.10,192.168.1.11,192.168.1.12",
50 "vxlan-port": 4789,
51 },
52 {"zone": "simple-zone", "digest": "xyz789", "type": "simple"},
53 ]
54
55
56 def exit_json(*args, **kwargs):
57 """function to patch over exit_json; package return data into an exception"""
58 if "changed" not in kwargs:
59 kwargs["changed"] = False
60 raise SystemExit(kwargs)
61
62
63 def fail_json(*args, **kwargs):
64 """function to patch over fail_json; package return data into an exception"""
65 kwargs["failed"] = True
66 raise SystemExit(kwargs)
67
68
69 def get_module_args_state_none():
70 return {
71 "api_host": "host",
72 "api_user": "user",
73 "api_password": "password",
74 }
75
76
77 def get_module_args_zone(zone_type, zone, state="present", update=True, bridge=None):
78 return {
79 "api_host": "host",
80 "api_user": "user",
81 "api_password": "password",
82 "type": zone_type,
83 "zone": zone,
84 "state": state,
85 "update": update,
86 "bridge": bridge,
87 }
88
89
90 def get_module_args_vxlan_fabric(zone, fabric, state="present", update=True, **kwargs):
91 """Helper to build module args for VXLAN zones with fabric"""
92 args = {
93 "api_host": "host",
94 "api_user": "user",
95 "api_password": "password",
96 "type": "vxlan",
97 "zone": zone,
98 "fabric": fabric,
99 "state": state,
100 "update": update,
101 }
102 args.update(kwargs)
103 return args
104
105
106 def get_module_args_vxlan_peers(zone, peers, state="present", update=True, **kwargs):
107 """Helper to build module args for VXLAN zones with peers"""
108 args = {
109 "api_host": "host",
110 "api_user": "user",
111 "api_password": "password",
112 "type": "vxlan",
113 "zone": zone,
114 "peers": peers,
115 "state": state,
116 "update": update,
117 }
118 args.update(kwargs)
119 return args
120
121
122 class TestProxmoxZoneModule(ModuleTestCase):
123 def setUp(self):
124 super().setUp()
125 proxmox_utils.HAS_PROXMOXER = True
126 self.module = proxmox_zone
127 self.fail_json_patcher = patch(
128 "ansible.module_utils.basic.AnsibleModule.fail_json", new=Mock(side_effect=fail_json)
129 )
130 self.exit_json_patcher = patch("ansible.module_utils.basic.AnsibleModule.exit_json", new=exit_json)
131
132 self.fail_json_mock = self.fail_json_patcher.start()
133 self.exit_json_patcher.start()
134 self.connect_mock = patch(
135 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect",
136 ).start()
137 self.version_mock = patch.object(
138 proxmox_zone.ProxmoxZoneAnsible, "version", return_value=LooseVersion("9.0")
139 ).start()
140 self.connect_mock.return_value.cluster.return_value.sdn.return_value.zones.return_value.get.return_value = (
141 RAW_ZONES
142 )
143
144 def tearDown(self):
145 self.connect_mock.stop()
146 self.exit_json_patcher.stop()
147 self.fail_json_patcher.stop()
148 self.version_mock.stop()
149 super().tearDown()
150
151 def test_zone_present(self):
152 # Create new Zone
153 with pytest.raises(SystemExit) as exc_info, set_module_args(
154 get_module_args_zone(zone_type="simple", zone="test")
155 ):
156 self.module.main()
157 result = exc_info.value.args[0]
158 assert result["changed"] is True
159 assert result["msg"] == "Created new Zone - test"
160 assert result["zone"] == "test"
161
162 # Update the zone
163 with pytest.raises(SystemExit) as exc_info, set_module_args(
164 get_module_args_zone(zone_type="simple", zone="test1", state="present")
165 ):
166 self.module.main()
167 result = exc_info.value.args[0]
168 assert result["changed"] is True
169 assert result["msg"] == "Updated zone - test1"
170 assert result["zone"] == "test1"
171
172 # Zone Already exists update=False
173 with pytest.raises(SystemExit) as exc_info, set_module_args(
174 get_module_args_zone(zone_type="simple", zone="test1", update=False)
175 ):
176 self.module.main()
177 result = exc_info.value.args[0]
178 assert result["changed"] is False
179 assert result["msg"] == "Zone test1 already exists and update is false!"
180 assert result["zone"] == "test1"
181
182 # Zone Already exists with update=True
183 with pytest.raises(SystemExit) as exc_info, set_module_args(
184 get_module_args_zone(zone_type="vlan", zone="test1", update=True, bridge="test")
185 ):
186 self.module.main()
187 result = exc_info.value.args[0]
188 assert self.fail_json_mock.called
189 assert result["failed"] is True
190 assert result["msg"] == "zone test1 exists with different type and we cannot change type post fact."
191
192 def test_zone_absent(self):
193 with pytest.raises(SystemExit) as exc_info, set_module_args(
194 get_module_args_zone(zone_type="simple", zone="test1", state="absent")
195 ):
196 self.module.main()
197 result = exc_info.value.args[0]
198 assert result["changed"] is True
199 assert result["msg"] == "Successfully deleted zone test1"
200 assert result["zone"] == "test1"
201
202 def test_vxlan_zone_create_with_fabric(self):
203 """Test creating a new VXLAN zone with fabric"""
204 with pytest.raises(SystemExit) as exc_info, set_module_args(
205 get_module_args_vxlan_fabric(zone="new-vxlan-fabric", fabric="test-fabric")
206 ):
207 self.module.main()
208 result = exc_info.value.args[0]
209 assert result["changed"] is True
210 assert result["msg"] == "Created new Zone - new-vxlan-fabric"
211 assert result["zone"] == "new-vxlan-fabric"
212
213 def test_vxlan_zone_create_with_peers(self):
214 """Test creating a new VXLAN zone with peers"""
215 with pytest.raises(SystemExit) as exc_info, set_module_args(
216 get_module_args_vxlan_peers(zone="new-vxlan-peers", peers="10.0.0.1,10.0.0.2,10.0.0.3")
217 ):
218 self.module.main()
219 result = exc_info.value.args[0]
220 assert result["changed"] is True
221 assert result["msg"] == "Created new Zone - new-vxlan-peers"
222 assert result["zone"] == "new-vxlan-peers"
223
224 def test_vxlan_zone_create_without_fabric_or_peers(self):
225 """Test that creating VXLAN zone without fabric or peers fails"""
226 with pytest.raises(SystemExit) as exc_info, set_module_args(
227 {
228 "api_host": "host",
229 "api_user": "user",
230 "api_password": "password",
231 "type": "vxlan",
232 "zone": "invalid-vxlan",
233 "state": "present",
234 "update": True,
235 }
236 ):
237 self.module.main()
238 result = exc_info.value.args[0]
239 assert self.fail_json_mock.called
240 assert result["failed"] is True
241 assert "fabric or peers" in result["msg"]
242
243 def test_vxlan_zone_update_peers_zone(self):
244 """Test updating an existing VXLAN zone with peers"""
245 with pytest.raises(SystemExit) as exc_info, set_module_args(
246 get_module_args_vxlan_peers(
247 zone="vxlan-peers-zone",
248 peers="192.168.1.10,192.168.1.11,192.168.1.12,192.168.1.13", # Added peer
249 update=True,
250 )
251 ):
252 self.module.main()
253 result = exc_info.value.args[0]
254 assert result["changed"] is True
255 assert result["msg"] == "Updated zone - vxlan-peers-zone"
256 assert result["zone"] == "vxlan-peers-zone"
257
258 def test_vxlan_zone_delete_fabric_zone(self):
259 """Test deleting a VXLAN zone with fabric"""
260 with pytest.raises(SystemExit) as exc_info, set_module_args(
261 get_module_args_vxlan_fabric(zone="vxlan-fabric-zone", fabric="my-fabric", state="absent")
262 ):
263 self.module.main()
264 result = exc_info.value.args[0]
265 assert result["changed"] is True
266 assert result["msg"] == "Successfully deleted zone vxlan-fabric-zone"
267 assert result["zone"] == "vxlan-fabric-zone"
268
269 def test_vxlan_zone_delete_peers_zone(self):
270 """Test deleting a VXLAN zone with peers"""
271 with pytest.raises(SystemExit) as exc_info, set_module_args(
272 get_module_args_vxlan_peers(zone="vxlan-peers-zone", peers="192.168.1.10,192.168.1.11", state="absent")
273 ):
274 self.module.main()
275 result = exc_info.value.args[0]
276 assert result["changed"] is True
277 assert result["msg"] == "Successfully deleted zone vxlan-peers-zone"
278 assert result["zone"] == "vxlan-peers-zone"
279
280 def test_vxlan_zone_empty_peers_list(self):
281 """Test VXLAN zone with empty peers string"""
282 with pytest.raises(SystemExit) as exc_info, set_module_args(
283 get_module_args_vxlan_peers(zone="vxlan-empty-peers", peers="")
284 ):
285 self.module.main()
286 result = exc_info.value.args[0]
287 # Should fail validation since neither fabric nor valid peers provided
288 assert self.fail_json_mock.called or result["changed"] is False
289
290 def test_vxlan_zone_empty_fabric(self):
291 """Test VXLAN zone with empty fabric string"""
292 with pytest.raises(SystemExit) as exc_info, set_module_args(
293 get_module_args_vxlan_fabric(zone="vxlan-empty-fabric", fabric="")
294 ):
295 self.module.main()
296 result = exc_info.value.args[0]
297 # Should fail validation
298 assert self.fail_json_mock.called or result["changed"] is False