Debian Codegraph @

ansible_collections/community/proxmox/tests/unit/plugins/inventory/test_proxmox.py

raw
1 # Copyright (c) 2020, Jeffrey van Pelt <[email protected]>
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4 #
5 # The API responses used in these tests were recorded from PVE version 6.2.
6
7
8 import pytest
9 from ansible.inventory.data import InventoryData
10
11 from ansible_collections.community.proxmox.plugins.inventory.proxmox import InventoryModule
12
13
14 @pytest.fixture(scope="module")
15 def inventory():
16 r = InventoryModule()
17 r.inventory = InventoryData()
18 return r
19
20
21 def test_verify_file(tmp_path, inventory):
22 file = tmp_path / "foobar.proxmox.yml"
23 file.touch()
24 assert inventory.verify_file(str(file)) is True
25
26
27 def test_verify_file_bad_config(inventory):
28 assert inventory.verify_file("foobar.proxmox.yml") is False
29
30
31 def get_auth():
32 return True
33
34
35 API_RESPONSES = {
36 "https://localhost:8006/api2/json/nodes": [
37 {
38 "type": "node",
39 "cpu": 0.01,
40 "maxdisk": 500,
41 "mem": 500,
42 "node": "testnode",
43 "id": "node/testnode",
44 "maxcpu": 1,
45 "status": "online",
46 "ssl_fingerprint": "xx",
47 "disk": 1000,
48 "maxmem": 1000,
49 "uptime": 10000,
50 "level": "",
51 },
52 {"type": "node", "node": "testnode2", "id": "node/testnode2", "status": "offline", "ssl_fingerprint": "yy"},
53 ],
54 "https://localhost:8006/api2/json/pools": [{"poolid": "test"}],
55 "https://localhost:8006/api2/json/nodes/testnode/lxc": [
56 {
57 "cpus": 1,
58 "name": "test-lxc",
59 "cpu": 0.01,
60 "diskwrite": 0,
61 "lock": "",
62 "maxmem": 1000,
63 "template": "",
64 "diskread": 0,
65 "mem": 1000,
66 "swap": 0,
67 "type": "lxc",
68 "maxswap": 0,
69 "maxdisk": "1000",
70 "netout": 1000,
71 "pid": "1000",
72 "netin": 1000,
73 "status": "running",
74 "vmid": "100",
75 "disk": "1000",
76 "uptime": 1000,
77 },
78 ],
79 "https://localhost:8006/api2/json/nodes/testnode/qemu": [
80 {
81 "name": "test-qemu",
82 "cpus": 1,
83 "mem": 1000,
84 "template": "",
85 "diskread": 0,
86 "cpu": 0.01,
87 "maxmem": 1000,
88 "diskwrite": 0,
89 "netout": 1000,
90 "pid": "1001",
91 "netin": 1000,
92 "maxdisk": 1000,
93 "vmid": "101",
94 "uptime": 1000,
95 "disk": 0,
96 "status": "running",
97 },
98 {
99 "name": "test-qemu-windows",
100 "cpus": 1,
101 "mem": 1000,
102 "template": "",
103 "diskread": 0,
104 "cpu": 0.01,
105 "maxmem": 1000,
106 "diskwrite": 0,
107 "netout": 1000,
108 "pid": "1001",
109 "netin": 1000,
110 "maxdisk": 1000,
111 "vmid": "102",
112 "uptime": 1000,
113 "disk": 0,
114 "status": "running",
115 },
116 {
117 "name": "test-qemu-multi-nic",
118 "cpus": 1,
119 "mem": 1000,
120 "template": "",
121 "diskread": 0,
122 "cpu": 0.01,
123 "maxmem": 1000,
124 "diskwrite": 0,
125 "netout": 1000,
126 "pid": "1001",
127 "netin": 1000,
128 "maxdisk": 1000,
129 "vmid": "103",
130 "uptime": 1000,
131 "disk": 0,
132 "status": "running",
133 },
134 {
135 "name": "test-qemu-template",
136 "cpus": 1,
137 "mem": 0,
138 "template": 1,
139 "diskread": 0,
140 "cpu": 0,
141 "maxmem": 1000,
142 "diskwrite": 0,
143 "netout": 0,
144 "pid": "1001",
145 "netin": 0,
146 "maxdisk": 1000,
147 "vmid": "9001",
148 "uptime": 0,
149 "disk": 0,
150 "status": "stopped",
151 },
152 ],
153 "https://localhost:8006/api2/json/pools/test": {
154 "members": [
155 {
156 "uptime": 1000,
157 "template": 0,
158 "id": "qemu/101",
159 "mem": 1000,
160 "status": "running",
161 "cpu": 0.01,
162 "maxmem": 1000,
163 "diskwrite": 1000,
164 "name": "test-qemu",
165 "netout": 1000,
166 "netin": 1000,
167 "vmid": 101,
168 "node": "testnode",
169 "maxcpu": 1,
170 "type": "qemu",
171 "maxdisk": 1000,
172 "disk": 0,
173 "diskread": 1000,
174 },
175 ],
176 },
177 "https://localhost:8006/api2/json/nodes/testnode/network": [
178 {
179 "families": ["inet"],
180 "priority": 3,
181 "active": 1,
182 "cidr": "10.1.1.2/24",
183 "iface": "eth0",
184 "method": "static",
185 "exists": 1,
186 "type": "eth",
187 "netmask": "24",
188 "gateway": "10.1.1.1",
189 "address": "10.1.1.2",
190 "method6": "manual",
191 "autostart": 1,
192 },
193 {
194 "method6": "manual",
195 "autostart": 1,
196 "type": "OVSPort",
197 "exists": 1,
198 "method": "manual",
199 "iface": "eth1",
200 "ovs_bridge": "vmbr0",
201 "active": 1,
202 "families": ["inet"],
203 "priority": 5,
204 "ovs_type": "OVSPort",
205 },
206 {
207 "type": "OVSBridge",
208 "method": "manual",
209 "iface": "vmbr0",
210 "families": ["inet"],
211 "priority": 4,
212 "ovs_ports": "eth1",
213 "ovs_type": "OVSBridge",
214 "method6": "manual",
215 "autostart": 1,
216 "active": 1,
217 },
218 ],
219 "https://localhost:8006/api2/json/nodes/testnode/lxc/100/config": {
220 "console": 1,
221 "rootfs": "local-lvm:vm-100-disk-0,size=4G",
222 "cmode": "tty",
223 "description": "A testnode",
224 "cores": 1,
225 "hostname": "test-lxc",
226 "arch": "amd64",
227 "tty": 2,
228 "swap": 0,
229 "cpulimit": "0",
230 "net0": "name=eth0,bridge=vmbr0,gw=10.1.1.1,hwaddr=FF:FF:FF:FF:FF:FF,ip=10.1.1.3/24,type=veth",
231 "ostype": "ubuntu",
232 "digest": "123456789abcdef0123456789abcdef01234567890",
233 "protection": 0,
234 "memory": 1000,
235 "onboot": 0,
236 "cpuunits": 1024,
237 "tags": "one, two, three",
238 },
239 "https://localhost:8006/api2/json/nodes/testnode/qemu/101/config": {
240 "tags": "one, two, three",
241 "cores": 1,
242 "ide2": "none,media=cdrom",
243 "memory": 1000,
244 "kvm": 1,
245 "digest": "0123456789abcdef0123456789abcdef0123456789",
246 "description": "A test qemu",
247 "sockets": 1,
248 "onboot": 1,
249 "vmgenid": "ffffffff-ffff-ffff-ffff-ffffffffffff",
250 "numa": 0,
251 "bootdisk": "scsi0",
252 "cpu": "host",
253 "name": "test-qemu",
254 "ostype": "l26",
255 "hotplug": "network,disk,usb",
256 "scsi0": "local-lvm:vm-101-disk-0,size=8G",
257 "net0": "virtio=ff:ff:ff:ff:ff:ff,bridge=vmbr0,firewall=1",
258 "agent": "1,fstrim_cloned_disks=1",
259 "bios": "seabios",
260 "ide0": "local-lvm:vm-101-cloudinit,media=cdrom,size=4M",
261 "boot": "cdn",
262 "scsihw": "virtio-scsi-pci",
263 "smbios1": "uuid=ffffffff-ffff-ffff-ffff-ffffffffffff",
264 },
265 "https://localhost:8006/api2/json/nodes/testnode/qemu/102/config": {
266 "numa": 0,
267 "digest": "460add1531a7068d2ae62d54f67e8fb9493dece9",
268 "ide2": "none,media=cdrom",
269 "bootdisk": "sata0",
270 "name": "test-qemu-windows",
271 "balloon": 0,
272 "cpulimit": "4",
273 "agent": "1",
274 "cores": 6,
275 "sata0": "storage:vm-102-disk-0,size=100G",
276 "memory": 10240,
277 "smbios1": "uuid=127301fc-0122-48d5-8fc5-c04fa78d8146",
278 "scsihw": "virtio-scsi-pci",
279 "sockets": 1,
280 "ostype": "win8",
281 "net0": "virtio=ff:ff:ff:ff:ff:ff,bridge=vmbr0",
282 "onboot": 1,
283 },
284 "https://localhost:8006/api2/json/nodes/testnode/qemu/103/config": {
285 "scsi1": "storage:vm-103-disk-3,size=30G",
286 "sockets": 1,
287 "memory": 8192,
288 "ostype": "l26",
289 "scsihw": "virtio-scsi-pci",
290 "net0": "virtio=ff:ff:ff:ff:ff:ff,bridge=vmbr0",
291 "net1": "virtio=ff:ff:ff:ff:ff:ff,bridge=vmbr1",
292 "bootdisk": "scsi0",
293 "scsi0": "storage:vm-103-disk-0,size=10G",
294 "name": "test-qemu-multi-nic",
295 "cores": 4,
296 "digest": "51b7599f869b9a3f564804a0aed290f3de803292",
297 "smbios1": "uuid=863b31c3-42ca-4a92-aed7-4111f342f70a",
298 "agent": "1,type=virtio",
299 "ide2": "none,media=cdrom",
300 "balloon": 0,
301 "numa": 0,
302 "scsi2": "storage:vm-103-disk-2,size=10G",
303 "serial0": "socket",
304 "vmgenid": "ddfb79b2-b484-4d66-88e7-6e76f2d1be77",
305 "onboot": 1,
306 "tablet": 0,
307 },
308 "https://localhost:8006/api2/json/nodes/testnode/qemu/101/agent/network-get-interfaces": {
309 "result": [
310 {
311 "hardware-address": "00:00:00:00:00:00",
312 "ip-addresses": [
313 {"prefix": 8, "ip-address-type": "ipv4", "ip-address": "127.0.0.1"},
314 {"ip-address-type": "ipv6", "ip-address": "::1", "prefix": 128},
315 ],
316 "statistics": {
317 "rx-errs": 0,
318 "rx-bytes": 163244,
319 "rx-packets": 1623,
320 "rx-dropped": 0,
321 "tx-dropped": 0,
322 "tx-packets": 1623,
323 "tx-bytes": 163244,
324 "tx-errs": 0,
325 },
326 "name": "lo",
327 },
328 {
329 "statistics": {
330 "rx-packets": 4025,
331 "rx-dropped": 12,
332 "rx-bytes": 324105,
333 "rx-errs": 0,
334 "tx-errs": 0,
335 "tx-bytes": 368860,
336 "tx-packets": 3479,
337 "tx-dropped": 0,
338 },
339 "name": "eth0",
340 "ip-addresses": [
341 {"prefix": 24, "ip-address-type": "ipv4", "ip-address": "10.1.2.3"},
342 {
343 "prefix": 64,
344 "ip-address": "fd8c:4687:e88d:1be3:5b70:7b88:c79c:293",
345 "ip-address-type": "ipv6",
346 },
347 ],
348 "hardware-address": "ff:ff:ff:ff:ff:ff",
349 },
350 {
351 "hardware-address": "ff:ff:ff:ff:ff:ff",
352 "ip-addresses": [{"prefix": 16, "ip-address": "10.10.2.3", "ip-address-type": "ipv4"}],
353 "name": "docker0",
354 "statistics": {
355 "rx-bytes": 0,
356 "rx-errs": 0,
357 "rx-dropped": 0,
358 "rx-packets": 0,
359 "tx-packets": 0,
360 "tx-dropped": 0,
361 "tx-errs": 0,
362 "tx-bytes": 0,
363 },
364 },
365 ],
366 },
367 "https://localhost:8006/api2/json/nodes/testnode/qemu/102/agent/network-get-interfaces": {
368 "result": {"error": {"desc": "this feature or command is not currently supported", "class": "Unsupported"}},
369 },
370 "https://localhost:8006/api2/json/nodes/testnode/qemu/103/agent/network-get-interfaces": {
371 "result": [
372 {
373 "statistics": {
374 "tx-errs": 0,
375 "rx-errs": 0,
376 "rx-dropped": 0,
377 "tx-bytes": 48132932372,
378 "tx-dropped": 0,
379 "rx-bytes": 48132932372,
380 "tx-packets": 178578980,
381 "rx-packets": 178578980,
382 },
383 "hardware-address": "ff:ff:ff:ff:ff:ff",
384 "ip-addresses": [{"ip-address-type": "ipv4", "prefix": 8, "ip-address": "127.0.0.1"}],
385 "name": "lo",
386 },
387 {
388 "name": "eth0",
389 "ip-addresses": [{"ip-address-type": "ipv4", "prefix": 24, "ip-address": "172.16.0.143"}],
390 "statistics": {
391 "rx-errs": 0,
392 "tx-errs": 0,
393 "rx-packets": 660028,
394 "tx-packets": 304599,
395 "tx-dropped": 0,
396 "rx-bytes": 1846743499,
397 "tx-bytes": 1287844926,
398 "rx-dropped": 0,
399 },
400 "hardware-address": "ff:ff:ff:ff:ff:ff",
401 },
402 {
403 "name": "eth1",
404 "hardware-address": "ff:ff:ff:ff:ff:ff",
405 "statistics": {
406 "rx-bytes": 235717091946,
407 "tx-dropped": 0,
408 "rx-dropped": 0,
409 "tx-bytes": 123411636251,
410 "rx-packets": 540431277,
411 "tx-packets": 468411864,
412 "rx-errs": 0,
413 "tx-errs": 0,
414 },
415 "ip-addresses": [{"ip-address": "10.0.0.133", "prefix": 24, "ip-address-type": "ipv4"}],
416 },
417 {
418 "name": "docker0",
419 "ip-addresses": [{"ip-address": "172.17.0.1", "prefix": 16, "ip-address-type": "ipv4"}],
420 "hardware-address": "ff:ff:ff:ff:ff:ff",
421 "statistics": {
422 "rx-errs": 0,
423 "tx-errs": 0,
424 "rx-packets": 0,
425 "tx-packets": 0,
426 "tx-dropped": 0,
427 "rx-bytes": 0,
428 "rx-dropped": 0,
429 "tx-bytes": 0,
430 },
431 },
432 {"hardware-address": "ff:ff:ff:ff:ff:ff", "name": "datapath"},
433 {
434 "name": "weave",
435 "ip-addresses": [{"ip-address": "10.42.0.1", "ip-address-type": "ipv4", "prefix": 16}],
436 "hardware-address": "ff:ff:ff:ff:ff:ff",
437 "statistics": {
438 "rx-bytes": 127289123306,
439 "tx-dropped": 0,
440 "rx-dropped": 0,
441 "tx-bytes": 43827573343,
442 "rx-packets": 132750542,
443 "tx-packets": 74218762,
444 "rx-errs": 0,
445 "tx-errs": 0,
446 },
447 },
448 {"name": "vethwe-datapath", "hardware-address": "ff:ff:ff:ff:ff:ff"},
449 {"name": "vethwe-bridge", "hardware-address": "ff:ff:ff:ff:ff:ff"},
450 {"hardware-address": "ff:ff:ff:ff:ff:ff", "name": "vxlan-6784"},
451 {"name": "vethwepl0dfe1fe", "hardware-address": "ff:ff:ff:ff:ff:ff"},
452 {"name": "vethweplf1e7715", "hardware-address": "ff:ff:ff:ff:ff:ff"},
453 {"hardware-address": "ff:ff:ff:ff:ff:ff", "name": "vethwepl9d244a1"},
454 {"hardware-address": "ff:ff:ff:ff:ff:ff", "name": "vethwepl2ca477b"},
455 {
456 "name": "nomacorip",
457 },
458 ],
459 },
460 "https://localhost:8006/api2/json/nodes/testnode/lxc/100/status/current": {
461 "swap": 0,
462 "name": "test-lxc",
463 "diskread": 0,
464 "vmid": 100,
465 "diskwrite": 0,
466 "pid": 9000,
467 "mem": 89980928,
468 "netin": 1950776396424,
469 "disk": 4998168576,
470 "cpu": 0.00163430613110039,
471 "type": "lxc",
472 "uptime": 6793736,
473 "maxmem": 1073741824,
474 "status": "running",
475 "cpus": "1",
476 "ha": {"group": "null", "state": "started", "managed": 1},
477 "maxdisk": 3348329267200,
478 "netout": 1947793356037,
479 "maxswap": 1073741824,
480 },
481 "https://localhost:8006/api2/json/nodes/testnode/qemu/101/status/current": {
482 "status": "stopped",
483 "uptime": 0,
484 "maxmem": 5364514816,
485 "maxdisk": 34359738368,
486 "netout": 0,
487 "cpus": 2,
488 "ha": {"managed": 0},
489 "diskread": 0,
490 "vmid": 101,
491 "diskwrite": 0,
492 "name": "test-qemu",
493 "cpu": 0,
494 "disk": 0,
495 "netin": 0,
496 "mem": 0,
497 "qmpstatus": "stopped",
498 },
499 "https://localhost:8006/api2/json/nodes/testnode/qemu/102/status/current": {
500 "status": "stopped",
501 "uptime": 0,
502 "maxmem": 5364514816,
503 "maxdisk": 34359738368,
504 "netout": 0,
505 "cpus": 2,
506 "ha": {"managed": 0},
507 "diskread": 0,
508 "vmid": 102,
509 "diskwrite": 0,
510 "name": "test-qemu-windows",
511 "cpu": 0,
512 "disk": 0,
513 "netin": 0,
514 "mem": 0,
515 "qmpstatus": "prelaunch",
516 },
517 "https://localhost:8006/api2/json/nodes/testnode/qemu/103/status/current": {
518 "status": "stopped",
519 "uptime": 0,
520 "maxmem": 5364514816,
521 "maxdisk": 34359738368,
522 "netout": 0,
523 "cpus": 2,
524 "ha": {"managed": 0},
525 "diskread": 0,
526 "vmid": 103,
527 "diskwrite": 0,
528 "name": "test-qemu-multi-nic",
529 "cpu": 0,
530 "disk": 0,
531 "netin": 0,
532 "mem": 0,
533 "qmpstatus": "paused",
534 },
535 "https://localhost:8006/api2/json/nodes/testnode/qemu/9001/status/current": {
536 "status": "stopped",
537 "qmpstatus": "stopped",
538 },
539 "https://localhost:8006/api2/json/nodes/testnode/qemu/9001/config": {},
540 }
541
542
543 # NOTE: when updating/adding replies to this function,
544 # be sure to only add only the _contents_ of the 'data' dict in the API reply
545 def get_json(url, ignore_errors=None):
546 return API_RESPONSES.get(url)
547
548
549 def get_vm_snapshots(node, properties, vmtype, vmid, name):
550 return [
551 {"description": "", "name": "clean", "snaptime": 1000, "vmstate": 0},
552 {"name": "current", "digest": "1234689abcdf", "running": 0, "description": "You are here!", "parent": "clean"},
553 ]
554
555
556 def get_option(opts):
557 def fn(option):
558 default = opts.get("default", False)
559 return opts.get(option, default)
560
561 return fn
562
563
564 def test_populate(inventory, mocker):
565 # module settings
566 inventory.proxmox_user = "root@pam"
567 inventory.proxmox_password = "password"
568 inventory.proxmox_url = "https://localhost:8006"
569 inventory.group_prefix = "proxmox_"
570 inventory.facts_prefix = "proxmox_"
571 inventory.strict = False
572 inventory.exclude_nodes = False
573
574 opts = {
575 "group_prefix": "proxmox_",
576 "facts_prefix": "proxmox_",
577 "want_facts": True,
578 "want_proxmox_nodes_ansible_host": True,
579 "qemu_extended_statuses": True,
580 "exclude_nodes": False,
581 }
582
583 # bypass authentication and API fetch calls
584 inventory._get_auth = mocker.MagicMock(side_effect=get_auth)
585 inventory._get_json = mocker.MagicMock(side_effect=get_json)
586 inventory._get_vm_snapshots = mocker.MagicMock(side_effect=get_vm_snapshots)
587 inventory.get_option = mocker.MagicMock(side_effect=get_option(opts))
588 inventory._can_add_host = mocker.MagicMock(return_value=True)
589 inventory._populate()
590
591 # get different hosts
592 host_qemu = inventory.inventory.get_host("test-qemu")
593 host_qemu_windows = inventory.inventory.get_host("test-qemu-windows")
594 host_qemu_multi_nic = inventory.inventory.get_host("test-qemu-multi-nic")
595 host_qemu_template = inventory.inventory.get_host("test-qemu-template")
596 host_lxc = inventory.inventory.get_host("test-lxc")
597
598 # check if qemu-test is in the proxmox_pool_test group
599 assert "proxmox_pool_test" in inventory.inventory.groups
600 group_qemu = inventory.inventory.groups["proxmox_pool_test"]
601 assert group_qemu.hosts == [host_qemu]
602
603 # check if qemu-test has eth0 interface in agent_interfaces fact
604 assert "eth0" in [d["name"] for d in host_qemu.get_vars()["proxmox_agent_interfaces"]]
605
606 # check if qemu-multi-nic has multiple network interfaces
607 for iface_name in ["eth0", "eth1", "weave"]:
608 assert iface_name in [d["name"] for d in host_qemu_multi_nic.get_vars()["proxmox_agent_interfaces"]]
609
610 # check if interface with no mac-address or ip-address defaults correctly
611 assert [
612 iface
613 for iface in host_qemu_multi_nic.get_vars()["proxmox_agent_interfaces"]
614 if iface["name"] == "nomacorip" and iface["mac-address"] == "" and iface["ip-addresses"] == []
615 ]
616
617 # check to make sure qemu-windows doesn't have proxmox_agent_interfaces
618 assert "proxmox_agent_interfaces" not in host_qemu_windows.get_vars()
619
620 # check if lxc-test has been discovered correctly
621 group_lxc = inventory.inventory.groups["proxmox_all_lxc"]
622 assert group_lxc.hosts == [host_lxc]
623
624 # check if qemu template is present and only in templates group
625 assert host_qemu_template is not None
626 assert host_qemu_template in inventory.inventory.groups["proxmox_all_templates"].hosts
627 assert host_qemu_template not in inventory.inventory.groups["proxmox_all_qemu"].hosts
628 assert host_qemu_template not in inventory.inventory.groups["proxmox_all_stopped"].hosts
629
630 # check that offline node is in inventory
631 assert inventory.inventory.get_host("testnode2")
632
633 # make sure that ['prelaunch', 'paused'] are in the group list
634 for group in ["paused", "prelaunch"]:
635 assert (f"{inventory.group_prefix}all_{group}") in inventory.inventory.groups
636
637 # check if qemu-windows is in the prelaunch group
638 group_prelaunch = inventory.inventory.groups["proxmox_all_prelaunch"]
639 assert group_prelaunch.hosts == [host_qemu_windows]
640
641 # check if qemu-multi-nic is in the paused group
642 group_paused = inventory.inventory.groups["proxmox_all_paused"]
643 assert group_paused.hosts == [host_qemu_multi_nic]
644
645
646 def test_populate_missing_qemu_extended_groups(inventory, mocker):
647 # module settings
648 inventory.proxmox_user = "root@pam"
649 inventory.proxmox_password = "password"
650 inventory.proxmox_url = "https://localhost:8006"
651 inventory.group_prefix = "proxmox_"
652 inventory.facts_prefix = "proxmox_"
653 inventory.strict = False
654 inventory.exclude_nodes = False
655
656 opts = {
657 "group_prefix": "proxmox_",
658 "facts_prefix": "proxmox_",
659 "want_facts": True,
660 "want_proxmox_nodes_ansible_host": True,
661 "qemu_extended_statuses": False,
662 "exclude_nodes": False,
663 }
664
665 # bypass authentication and API fetch calls
666 inventory._get_auth = mocker.MagicMock(side_effect=get_auth)
667 inventory._get_json = mocker.MagicMock(side_effect=get_json)
668 inventory._get_vm_snapshots = mocker.MagicMock(side_effect=get_vm_snapshots)
669 inventory.get_option = mocker.MagicMock(side_effect=get_option(opts))
670 inventory._can_add_host = mocker.MagicMock(return_value=True)
671 inventory._populate()
672
673 # make sure that ['prelaunch', 'paused'] are not in the group list
674 for group in ["paused", "prelaunch"]:
675 assert (f"{inventory.group_prefix}all_{group}") not in inventory.inventory.groups
676
677
678 def test_populate_exclude_nodes(inventory, mocker):
679 # module settings
680 inventory.proxmox_user = "root@pam"
681 inventory.proxmox_password = "password"
682 inventory.proxmox_url = "https://localhost:8006"
683 inventory.group_prefix = "proxmox_"
684 inventory.facts_prefix = "proxmox_"
685 inventory.strict = False
686 inventory.exclude_nodes = True
687
688 opts = {
689 "group_prefix": "proxmox_",
690 "facts_prefix": "proxmox_",
691 "want_facts": True,
692 "want_proxmox_nodes_ansible_host": True,
693 "qemu_extended_statuses": False,
694 "exclude_nodes": True,
695 }
696
697 # bypass authentication and API fetch calls
698 inventory._get_auth = mocker.MagicMock(side_effect=get_auth)
699 inventory._get_json = mocker.MagicMock(side_effect=get_json)
700 inventory._get_vm_snapshots = mocker.MagicMock(side_effect=get_vm_snapshots)
701 inventory.get_option = mocker.MagicMock(side_effect=get_option(opts))
702 inventory._can_add_host = mocker.MagicMock(return_value=True)
703 inventory._populate()
704
705 # make sure that nodes are not in the inventory
706 for node in ["testnode", "testnode2"]:
707 assert node not in inventory.inventory.hosts
708 # make sure that nodes group is absent
709 assert (f"{inventory.group_prefix}_nodes") not in inventory.inventory.groups
710 # make sure that nodes are not in the "ungrouped" group
711 for node in ["testnode", "testnode2"]:
712 assert node not in inventory.inventory.get_groups_dict()["ungrouped"]