Debian Codegraph @

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

raw
1 #
2 # Copyright (c) 2023, Sergei Antipov <greendayonfire at gmail.com>
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 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 AnsibleExitJson,
15 AnsibleFailJson,
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_vm_info
22
23 NODE1 = "pve"
24 NODE2 = "pve2"
25 RAW_CLUSTER_OUTPUT = [
26 {
27 "cpu": 0.174069059487628,
28 "disk": 0,
29 "diskread": 6656,
30 "diskwrite": 0,
31 "id": "qemu/100",
32 "maxcpu": 1,
33 "maxdisk": 34359738368,
34 "maxmem": 4294967296,
35 "mem": 35304543,
36 "name": "pxe.home.arpa",
37 "netin": 416956,
38 "netout": 17330,
39 "node": NODE1,
40 "status": "running",
41 "template": 0,
42 "type": "qemu",
43 "uptime": 669,
44 "vmid": 100,
45 },
46 {
47 "cpu": 0,
48 "disk": 0,
49 "diskread": 0,
50 "diskwrite": 0,
51 "id": "qemu/101",
52 "maxcpu": 1,
53 "maxdisk": 0,
54 "maxmem": 536870912,
55 "mem": 0,
56 "name": "test1",
57 "netin": 0,
58 "netout": 0,
59 "node": NODE2,
60 "pool": "pool1",
61 "status": "stopped",
62 "template": 0,
63 "type": "qemu",
64 "uptime": 0,
65 "vmid": 101,
66 },
67 {
68 "cpu": 0,
69 "disk": 352190464,
70 "diskread": 0,
71 "diskwrite": 0,
72 "id": "lxc/102",
73 "maxcpu": 2,
74 "maxdisk": 10737418240,
75 "maxmem": 536870912,
76 "mem": 28192768,
77 "name": "test-lxc.home.arpa",
78 "netin": 102757,
79 "netout": 446,
80 "node": NODE1,
81 "status": "running",
82 "template": 0,
83 "type": "lxc",
84 "uptime": 161,
85 "vmid": 102,
86 },
87 {
88 "cpu": 0,
89 "disk": 0,
90 "diskread": 0,
91 "diskwrite": 0,
92 "id": "lxc/103",
93 "maxcpu": 2,
94 "maxdisk": 10737418240,
95 "maxmem": 536870912,
96 "mem": 0,
97 "name": "test1-lxc.home.arpa",
98 "netin": 0,
99 "netout": 0,
100 "node": NODE2,
101 "pool": "pool1",
102 "status": "stopped",
103 "template": 0,
104 "type": "lxc",
105 "uptime": 0,
106 "vmid": 103,
107 },
108 {
109 "cpu": 0,
110 "disk": 0,
111 "diskread": 0,
112 "diskwrite": 0,
113 "id": "lxc/104",
114 "maxcpu": 2,
115 "maxdisk": 10737418240,
116 "maxmem": 536870912,
117 "mem": 0,
118 "name": "test-lxc.home.arpa",
119 "netin": 0,
120 "netout": 0,
121 "node": NODE2,
122 "pool": "pool1",
123 "status": "stopped",
124 "template": 0,
125 "type": "lxc",
126 "uptime": 0,
127 "vmid": 104,
128 },
129 {
130 "cpu": 0,
131 "disk": 0,
132 "diskread": 0,
133 "diskwrite": 0,
134 "id": "lxc/105",
135 "maxcpu": 2,
136 "maxdisk": 10737418240,
137 "maxmem": 536870912,
138 "mem": 0,
139 "name": "",
140 "netin": 0,
141 "netout": 0,
142 "node": NODE2,
143 "pool": "pool1",
144 "status": "stopped",
145 "template": 0,
146 "type": "lxc",
147 "uptime": 0,
148 "vmid": 105,
149 },
150 ]
151 RAW_LXC_OUTPUT = [
152 {
153 "cpu": 0,
154 "cpus": 2,
155 "disk": 0,
156 "diskread": 0,
157 "diskwrite": 0,
158 "maxdisk": 10737418240,
159 "maxmem": 536870912,
160 "maxswap": 536870912,
161 "mem": 0,
162 "name": "test1-lxc.home.arpa",
163 "netin": 0,
164 "netout": 0,
165 "status": "stopped",
166 "swap": 0,
167 "type": "lxc",
168 "uptime": 0,
169 "vmid": "103",
170 },
171 {
172 "cpu": 0,
173 "cpus": 2,
174 "disk": 352190464,
175 "diskread": 0,
176 "diskwrite": 0,
177 "maxdisk": 10737418240,
178 "maxmem": 536870912,
179 "maxswap": 536870912,
180 "mem": 28192768,
181 "name": "test-lxc.home.arpa",
182 "netin": 102757,
183 "netout": 446,
184 "pid": 4076752,
185 "status": "running",
186 "swap": 0,
187 "type": "lxc",
188 "uptime": 161,
189 "vmid": "102",
190 },
191 {
192 "cpu": 0,
193 "cpus": 2,
194 "disk": 0,
195 "diskread": 0,
196 "diskwrite": 0,
197 "maxdisk": 10737418240,
198 "maxmem": 536870912,
199 "maxswap": 536870912,
200 "mem": 0,
201 "name": "test-lxc.home.arpa",
202 "netin": 0,
203 "netout": 0,
204 "status": "stopped",
205 "swap": 0,
206 "type": "lxc",
207 "uptime": 0,
208 "vmid": "104",
209 },
210 {
211 "cpu": 0,
212 "cpus": 2,
213 "disk": 0,
214 "diskread": 0,
215 "diskwrite": 0,
216 "maxdisk": 10737418240,
217 "maxmem": 536870912,
218 "maxswap": 536870912,
219 "mem": 0,
220 "name": "",
221 "netin": 0,
222 "netout": 0,
223 "status": "stopped",
224 "swap": 0,
225 "type": "lxc",
226 "uptime": 0,
227 "vmid": "105",
228 },
229 ]
230 RAW_QEMU_OUTPUT = [
231 {
232 "cpu": 0,
233 "cpus": 1,
234 "disk": 0,
235 "diskread": 0,
236 "diskwrite": 0,
237 "maxdisk": 0,
238 "maxmem": 536870912,
239 "mem": 0,
240 "name": "test1",
241 "netin": 0,
242 "netout": 0,
243 "status": "stopped",
244 "uptime": 0,
245 "vmid": 101,
246 },
247 {
248 "cpu": 0.174069059487628,
249 "cpus": 1,
250 "disk": 0,
251 "diskread": 6656,
252 "diskwrite": 0,
253 "maxdisk": 34359738368,
254 "maxmem": 4294967296,
255 "mem": 35304543,
256 "name": "pxe.home.arpa",
257 "netin": 416956,
258 "netout": 17330,
259 "pid": 4076688,
260 "status": "running",
261 "uptime": 669,
262 "vmid": 100,
263 },
264 ]
265 EXPECTED_VMS_OUTPUT = [
266 {
267 "cpu": 0.174069059487628,
268 "cpus": 1,
269 "disk": 0,
270 "diskread": 6656,
271 "diskwrite": 0,
272 "id": "qemu/100",
273 "maxcpu": 1,
274 "maxdisk": 34359738368,
275 "maxmem": 4294967296,
276 "mem": 35304543,
277 "name": "pxe.home.arpa",
278 "netin": 416956,
279 "netout": 17330,
280 "node": NODE1,
281 "pid": 4076688,
282 "status": "running",
283 "template": False,
284 "type": "qemu",
285 "uptime": 669,
286 "vmid": 100,
287 },
288 {
289 "cpu": 0,
290 "cpus": 1,
291 "disk": 0,
292 "diskread": 0,
293 "diskwrite": 0,
294 "id": "qemu/101",
295 "maxcpu": 1,
296 "maxdisk": 0,
297 "maxmem": 536870912,
298 "mem": 0,
299 "name": "test1",
300 "netin": 0,
301 "netout": 0,
302 "node": NODE2,
303 "pool": "pool1",
304 "status": "stopped",
305 "template": False,
306 "type": "qemu",
307 "uptime": 0,
308 "vmid": 101,
309 },
310 {
311 "cpu": 0,
312 "cpus": 2,
313 "disk": 352190464,
314 "diskread": 0,
315 "diskwrite": 0,
316 "id": "lxc/102",
317 "maxcpu": 2,
318 "maxdisk": 10737418240,
319 "maxmem": 536870912,
320 "maxswap": 536870912,
321 "mem": 28192768,
322 "name": "test-lxc.home.arpa",
323 "netin": 102757,
324 "netout": 446,
325 "node": NODE1,
326 "pid": 4076752,
327 "status": "running",
328 "swap": 0,
329 "template": False,
330 "type": "lxc",
331 "uptime": 161,
332 "vmid": 102,
333 },
334 {
335 "cpu": 0,
336 "cpus": 2,
337 "disk": 0,
338 "diskread": 0,
339 "diskwrite": 0,
340 "id": "lxc/103",
341 "maxcpu": 2,
342 "maxdisk": 10737418240,
343 "maxmem": 536870912,
344 "maxswap": 536870912,
345 "mem": 0,
346 "name": "test1-lxc.home.arpa",
347 "netin": 0,
348 "netout": 0,
349 "node": NODE2,
350 "pool": "pool1",
351 "status": "stopped",
352 "swap": 0,
353 "template": False,
354 "type": "lxc",
355 "uptime": 0,
356 "vmid": 103,
357 },
358 {
359 "cpu": 0,
360 "cpus": 2,
361 "disk": 0,
362 "diskread": 0,
363 "diskwrite": 0,
364 "id": "lxc/104",
365 "maxcpu": 2,
366 "maxdisk": 10737418240,
367 "maxmem": 536870912,
368 "maxswap": 536870912,
369 "mem": 0,
370 "name": "test-lxc.home.arpa",
371 "netin": 0,
372 "netout": 0,
373 "node": NODE2,
374 "pool": "pool1",
375 "status": "stopped",
376 "swap": 0,
377 "template": False,
378 "type": "lxc",
379 "uptime": 0,
380 "vmid": 104,
381 },
382 {
383 "cpu": 0,
384 "cpus": 2,
385 "disk": 0,
386 "diskread": 0,
387 "diskwrite": 0,
388 "id": "lxc/105",
389 "maxcpu": 2,
390 "maxdisk": 10737418240,
391 "maxmem": 536870912,
392 "maxswap": 536870912,
393 "mem": 0,
394 "name": "",
395 "netin": 0,
396 "netout": 0,
397 "node": NODE2,
398 "pool": "pool1",
399 "status": "stopped",
400 "swap": 0,
401 "template": False,
402 "type": "lxc",
403 "uptime": 0,
404 "vmid": 105,
405 },
406 ]
407
408
409 def get_module_args(resource_type="all", node=None, vmid=None, name=None, config="none"):
410 return {
411 "api_host": "host",
412 "api_user": "user",
413 "api_password": "password",
414 "node": node,
415 "type": resource_type,
416 "vmid": vmid,
417 "name": name,
418 "config": config,
419 }
420
421
422 class TestProxmoxVmInfoModule(ModuleTestCase):
423 def setUp(self):
424 super().setUp()
425 proxmox_utils.HAS_PROXMOXER = True
426 self.module = proxmox_vm_info
427 self.connect_mock = patch(
428 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect",
429 ).start()
430 self.connect_mock.return_value.nodes.return_value.lxc.return_value.get.return_value = RAW_LXC_OUTPUT
431 self.connect_mock.return_value.nodes.return_value.qemu.return_value.get.return_value = RAW_QEMU_OUTPUT
432 self.connect_mock.return_value.cluster.return_value.resources.return_value.get.return_value = RAW_CLUSTER_OUTPUT
433 self.connect_mock.return_value.nodes.get.return_value = [{"node": NODE1}]
434
435 def tearDown(self):
436 self.connect_mock.stop()
437 super().tearDown()
438
439 def test_module_fail_when_required_args_missing(self):
440 with pytest.raises(AnsibleFailJson) as exc_info, set_module_args({}):
441 self.module.main()
442
443 result = exc_info.value.args[0]
444 assert result["msg"] == "missing required arguments: api_host, api_user"
445
446 def test_get_lxc_vms_information(self):
447 with pytest.raises(AnsibleExitJson) as exc_info, set_module_args(get_module_args(resource_type="lxc")):
448 expected_output = [vm for vm in EXPECTED_VMS_OUTPUT if vm["type"] == "lxc"]
449 self.module.main()
450
451 result = exc_info.value.args[0]
452 assert result["changed"] is False
453 assert result["proxmox_vms"] == expected_output
454
455 def test_get_qemu_vms_information(self):
456 with pytest.raises(AnsibleExitJson) as exc_info, set_module_args(get_module_args(resource_type="qemu")):
457 expected_output = [vm for vm in EXPECTED_VMS_OUTPUT if vm["type"] == "qemu"]
458 self.module.main()
459
460 result = exc_info.value.args[0]
461 assert result["proxmox_vms"] == expected_output
462
463 def test_get_all_vms_information(self):
464 with pytest.raises(AnsibleExitJson) as exc_info, set_module_args(get_module_args()):
465 self.module.main()
466
467 result = exc_info.value.args[0]
468 assert result["proxmox_vms"] == EXPECTED_VMS_OUTPUT
469
470 def test_vmid_is_converted_to_int(self):
471 with pytest.raises(AnsibleExitJson) as exc_info, set_module_args(get_module_args(resource_type="lxc")):
472 self.module.main()
473
474 result = exc_info.value.args[0]
475 assert isinstance(result["proxmox_vms"][0]["vmid"], int)
476
477 def test_get_specific_lxc_vm_information(self):
478 with pytest.raises(AnsibleExitJson) as exc_info:
479 vmid = 102
480 expected_output = [vm for vm in EXPECTED_VMS_OUTPUT if vm["vmid"] == vmid and vm["type"] == "lxc"]
481 with set_module_args(get_module_args(resource_type="lxc", vmid=vmid)):
482 self.module.main()
483
484 result = exc_info.value.args[0]
485 assert result["proxmox_vms"] == expected_output
486
487 def test_get_specific_qemu_vm_information(self):
488 with pytest.raises(AnsibleExitJson) as exc_info:
489 vmid = 100
490 expected_output = [vm for vm in EXPECTED_VMS_OUTPUT if vm["vmid"] == vmid and vm["type"] == "qemu"]
491 with set_module_args(get_module_args(resource_type="qemu", vmid=vmid)):
492 self.module.main()
493
494 result = exc_info.value.args[0]
495 assert result["proxmox_vms"] == expected_output
496
497 def test_get_specific_vm_information(self):
498 with pytest.raises(AnsibleExitJson) as exc_info:
499 vmid = 100
500 expected_output = [vm for vm in EXPECTED_VMS_OUTPUT if vm["vmid"] == vmid]
501 with set_module_args(get_module_args(resource_type="all", vmid=vmid)):
502 self.module.main()
503
504 result = exc_info.value.args[0]
505 assert result["proxmox_vms"] == expected_output
506
507 def test_get_specific_vm_information_by_using_name(self):
508 name = "test1-lxc.home.arpa"
509 self.connect_mock.return_value.cluster.resources.get.return_value = [{"name": name, "vmid": "103"}]
510
511 with pytest.raises(AnsibleExitJson) as exc_info:
512 expected_output = [vm for vm in EXPECTED_VMS_OUTPUT if vm["name"] == name]
513 with set_module_args(get_module_args(resource_type="all", name=name)):
514 self.module.main()
515
516 result = exc_info.value.args[0]
517 assert result["proxmox_vms"] == expected_output
518
519 def test_get_multiple_vms_with_the_same_name(self):
520 name = "test-lxc.home.arpa"
521 self.connect_mock.return_value.cluster.resources.get.return_value = [
522 {"name": name, "vmid": "102"},
523 {"name": name, "vmid": "104"},
524 ]
525
526 with pytest.raises(AnsibleExitJson) as exc_info:
527 expected_output = [vm for vm in EXPECTED_VMS_OUTPUT if vm["name"] == name]
528 with set_module_args(get_module_args(resource_type="all", name=name)):
529 self.module.main()
530
531 result = exc_info.value.args[0]
532 assert result["proxmox_vms"] == expected_output
533
534 def test_get_vm_with_an_empty_name(self):
535 name = ""
536 self.connect_mock.return_value.cluster.resources.get.return_value = [
537 {"name": name, "vmid": "105"},
538 ]
539
540 with pytest.raises(AnsibleExitJson) as exc_info:
541 expected_output = [vm for vm in EXPECTED_VMS_OUTPUT if vm["name"] == name]
542 with set_module_args(get_module_args(resource_type="all", name=name)):
543 self.module.main()
544
545 result = exc_info.value.args[0]
546 assert result["proxmox_vms"] == expected_output
547
548 def test_get_all_lxc_vms_from_specific_node(self):
549 with pytest.raises(AnsibleExitJson) as exc_info:
550 expected_output = [vm for vm in EXPECTED_VMS_OUTPUT if vm["node"] == NODE1 and vm["type"] == "lxc"]
551 with set_module_args(get_module_args(resource_type="lxc", node=NODE1)):
552 self.module.main()
553
554 result = exc_info.value.args[0]
555 assert result["proxmox_vms"] == expected_output
556
557 def test_get_all_qemu_vms_from_specific_node(self):
558 with pytest.raises(AnsibleExitJson) as exc_info:
559 expected_output = [vm for vm in EXPECTED_VMS_OUTPUT if vm["node"] == NODE1 and vm["type"] == "qemu"]
560 with set_module_args(get_module_args(resource_type="qemu", node=NODE1)):
561 self.module.main()
562
563 result = exc_info.value.args[0]
564 assert result["proxmox_vms"] == expected_output
565
566 def test_get_all_vms_from_specific_node(self):
567 with pytest.raises(AnsibleExitJson) as exc_info:
568 expected_output = [vm for vm in EXPECTED_VMS_OUTPUT if vm["node"] == NODE1]
569 with set_module_args(get_module_args(node=NODE1)):
570 self.module.main()
571
572 result = exc_info.value.args[0]
573 assert result["proxmox_vms"] == expected_output
574
575 def test_module_returns_empty_list_when_vm_does_not_exist(self):
576 with pytest.raises(AnsibleExitJson) as exc_info:
577 vmid = 200
578 with set_module_args(get_module_args(resource_type="all", vmid=vmid)):
579 self.module.main()
580
581 result = exc_info.value.args[0]
582 assert result["proxmox_vms"] == []
583
584 def test_module_fail_when_qemu_request_fails(self):
585 self.connect_mock.return_value.nodes.return_value.qemu.return_value.get.side_effect = OSError(
586 "Some mocked connection error."
587 )
588 with pytest.raises(AnsibleFailJson) as exc_info, set_module_args(get_module_args(resource_type="qemu")):
589 self.module.main()
590
591 result = exc_info.value.args[0]
592 assert "Failed to retrieve QEMU VMs information:" in result["msg"]
593
594 def test_module_fail_when_lxc_request_fails(self):
595 self.connect_mock.return_value.nodes.return_value.lxc.return_value.get.side_effect = OSError(
596 "Some mocked connection error."
597 )
598 with pytest.raises(AnsibleFailJson) as exc_info, set_module_args(get_module_args(resource_type="lxc")):
599 self.module.main()
600
601 result = exc_info.value.args[0]
602 assert "Failed to retrieve LXC VMs information:" in result["msg"]
603
604 def test_module_fail_when_cluster_resources_request_fails(self):
605 self.connect_mock.return_value.cluster.return_value.resources.return_value.get.side_effect = OSError(
606 "Some mocked connection error."
607 )
608 with pytest.raises(AnsibleFailJson) as exc_info, set_module_args(get_module_args()):
609 self.module.main()
610
611 result = exc_info.value.args[0]
612 assert "Failed to retrieve VMs information from cluster resources:" in result["msg"]
613
614 def test_module_fail_when_node_does_not_exist(self):
615 with pytest.raises(AnsibleFailJson) as exc_info, set_module_args(
616 get_module_args(resource_type="all", node="NODE3")
617 ):
618 self.module.main()
619
620 result = exc_info.value.args[0]
621 assert result["msg"] == "Node NODE3 doesn't exist in PVE cluster"
622
623 def test_call_to_get_vmid_is_not_used_when_vmid_provided(self):
624 with patch(
625 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible.get_vmid"
626 ) as get_vmid_mock, pytest.raises(AnsibleExitJson):
627 vmid = 100
628 with set_module_args(get_module_args(resource_type="all", vmid=vmid, name="something")):
629 self.module.main()
630
631 assert get_vmid_mock.call_count == 0
632
633 def test_config_returned_when_specified_qemu_vm_with_config(self):
634 config_vm_value = {
635 "scsi0": "local-lvm:vm-101-disk-0,iothread=1,size=32G",
636 "net0": "virtio=4E:79:9F:A8:EE:E4,bridge=vmbr0,firewall=1",
637 "scsihw": "virtio-scsi-single",
638 "cores": 1,
639 "name": "test1",
640 "ostype": "l26",
641 "boot": "order=scsi0;ide2;net0",
642 "memory": 2048,
643 "sockets": 1,
644 }
645 (
646 self.connect_mock.return_value.nodes.return_value.qemu.return_value.config.return_value.get.return_value
647 ) = config_vm_value
648
649 with pytest.raises(AnsibleExitJson) as exc_info:
650 vmid = 101
651 with set_module_args(
652 get_module_args(
653 resource_type="qemu",
654 vmid=vmid,
655 config="current",
656 )
657 ):
658 expected_output = [vm for vm in EXPECTED_VMS_OUTPUT if vm["vmid"] == vmid]
659 expected_output[0]["config"] = config_vm_value
660 self.module.main()
661
662 result = exc_info.value.args[0]
663 assert result["proxmox_vms"] == expected_output