feat(slice-3): implement Proxmox VE REST API collector with guest agent IP resolution

This commit is contained in:
soda64 committed 2026-10-03 08:59:00 +02:00
1 parent 951be389a1
commit ca9163530f
4 files changed
+361 -1

No files matched your search

+125
View File
@@ -0,0 +1,125 @@
import unittest
from unittest.mock import patch, MagicMock
from inventory_collector.collectors.proxmox import ProxmoxCollector
class TestProxmoxCollector(unittest.TestCase):
def setUp(self):
self.collector = ProxmoxCollector(
url="https://pve-01.wired.intra:8006",
token_id="root@pam!collector",
token_secret="12345678-abcd-1234-abcd-123456789abc",
verify_ssl=False
)
@patch("inventory_collector.collectors.proxmox.urllib.request.urlopen")
def test_collect_nodes_and_vms(self, mock_urlopen):
# Mock responses for different API endpoints
def mock_api_router(req, *args, **kwargs):
url = req.full_url if hasattr(req, "full_url") else str(req)
resp = MagicMock()
resp.status = 200
resp.__enter__.return_value = resp
if url.endswith("/api2/json/nodes"):
# Node listing
payload = {
"data": [
{"node": "node0", "status": "online", "ip": "192.168.0.101"}
]
}
elif "/qemu" in url and not "/agent" in url and not "/config" in url:
# QEMU listing on node0
payload = {
"data": [
{
"vmid": 100,
"name": "k3s-master-0",
"status": "running",
"tags": "k3s_master;control-plane",
"template": 0
},
{
"vmid": 900,
"name": "debian-12-template",
"status": "stopped",
"template": 1 # Should be skipped!
}
]
}
elif "/lxc" in url and not "/config" in url:
# LXC listing on node0
payload = {
"data": [
{
"vmid": 200,
"name": "pihole-ct",
"status": "running",
"tags": "dns;network",
"template": 0
}
]
}
elif "/agent/network-get-interfaces" in url:
# Guest agent IP
payload = {
"data": {
"result": [
{
"name": "lo",
"ip-addresses": [{"ip-address-type": "ipv4", "ip-address": "127.0.0.1"}]
},
{
"name": "eth0",
"ip-addresses": [{"ip-address-type": "ipv4", "ip-address": "192.168.5.70"}]
}
]
}
}
elif "/lxc/200/config" in url:
# LXC config with IP
payload = {
"data": {
"net0": "name=eth0,bridge=vmbr0,ip=192.168.5.20/24,gw=192.168.5.1"
}
}
else:
payload = {"data": {}}
import json
resp.read.return_value = json.dumps(payload).encode("utf-8")
return resp
mock_urlopen.side_effect = mock_api_router
hosts = self.collector.collect()
hosts_by_name = {h.name: h for h in hosts}
# 1. Hypervisor node should be collected
self.assertIn("node0", hosts_by_name)
self.assertEqual(hosts_by_name["node0"].ansible_host, "192.168.0.101")
self.assertEqual(hosts_by_name["node0"].ansible_user, "root")
self.assertIn("proxmox_nodes", hosts_by_name["node0"].groups)
# 2. Template should be skipped
self.assertNotIn("debian-12-template", hosts_by_name)
# 3. QEMU VM with Guest Agent IP
self.assertIn("k3s-master-0", hosts_by_name)
k3s = hosts_by_name["k3s-master-0"]
self.assertEqual(k3s.ansible_host, "192.168.5.70")
self.assertIn("k3s_master", k3s.groups)
self.assertIn("tag_k3s_master", k3s.groups)
self.assertIn("proxmox_vms", k3s.groups)
self.assertIn("pve_node_node0", k3s.groups)
self.assertIn("status_running", k3s.groups)
self.assertEqual(k3s.variables.get("proxmox_vmid"), 100)
# 4. LXC container with net0 config IP
self.assertIn("pihole-ct", hosts_by_name)
pihole = hosts_by_name["pihole-ct"]
self.assertEqual(pihole.ansible_host, "192.168.5.20")
self.assertIn("proxmox_lxc", pihole.groups)
self.assertIn("dns", pihole.groups)
if __name__ == "__main__":
unittest.main()