feat(agent): vms/lxc collectors for runtime and detail with fixtures

This commit is contained in:
Carsten 2026-04-21 22:34:45 +02:00
parent ec7f08dfda
commit da5ed6cd08
6 changed files with 299 additions and 0 deletions

12
agent/test/fixtures/pvesh/lxc.json vendored Normal file
View file

@ -0,0 +1,12 @@
[
{
"vmid": 200,
"name": "minecraft",
"status": "running",
"uptime": 3600,
"cpu": 0.15,
"mem": 2147483648,
"maxmem": 4294967296,
"tags": ""
}
]

22
agent/test/fixtures/pvesh/qemu.json vendored Normal file
View file

@ -0,0 +1,22 @@
[
{
"vmid": 100,
"name": "nginx-proxy",
"status": "running",
"uptime": 86400,
"cpu": 0.05,
"mem": 536870912,
"maxmem": 2147483648,
"tags": "web;production"
},
{
"vmid": 101,
"name": "db-backup",
"status": "stopped",
"uptime": 0,
"cpu": 0,
"mem": 0,
"maxmem": 4294967296,
"tags": "db"
}
]

View file

@ -0,0 +1,17 @@
{
"result": [
{
"name": "lo",
"ip-addresses": [
{ "ip-address": "127.0.0.1", "ip-address-type": "ipv4" }
]
},
{
"name": "eth0",
"ip-addresses": [
{ "ip-address": "192.168.1.10", "ip-address-type": "ipv4" },
{ "ip-address": "fe80::a", "ip-address-type": "ipv6" }
]
}
]
}

View file

@ -0,0 +1,8 @@
{
"name": "nginx-proxy",
"cores": 2,
"memory": 2048,
"onboot": 1,
"scsi0": "local-zfs:vm-100-disk-0,size=32G",
"net0": "virtio=AA:BB:CC:DD:EE:FF,bridge=vmbr0"
}

View file

@ -0,0 +1,76 @@
defmodule ProxmoxAgent.Collectors.VmsTest do
use ExUnit.Case, async: true
alias ProxmoxAgent.Collectors.Vms
@fixtures Path.expand("../../fixtures/pvesh", __DIR__)
defp read!(name), do: File.read!(Path.join(@fixtures, name))
defp fake_runner do
fn
"pvesh", ["get", "/nodes/" <> rest, "--output-format", "json"] ->
cond do
String.ends_with?(rest, "/qemu") ->
{:ok, read!("qemu.json")}
String.ends_with?(rest, "/lxc") ->
{:ok, read!("lxc.json")}
String.ends_with?(rest, "/qemu/100/config") ->
{:ok, read!("qemu_100_config.json")}
String.ends_with?(rest, "/qemu/100/agent/network-get-interfaces") ->
{:ok, read!("qemu_100_agent_interfaces.json")}
String.ends_with?(rest, "/qemu/101/config") ->
{:ok, ~s({"name":"db-backup","cores":4,"memory":4096})}
String.ends_with?(rest, "/qemu/101/agent/network-get-interfaces") ->
{:error, {:nonzero_exit, 1, "QEMU guest agent is not running"}}
String.ends_with?(rest, "/lxc/200/config") ->
{:ok,
~s({"hostname":"minecraft","cores":4,"memory":4096,"net0":"name=eth0,ip=10.0.0.5/24"})}
end
end
end
describe "collect_runtime/1" do
test "returns qemu + lxc runtime info" do
sample = Vms.collect_runtime(node: "pve-01", runner: fake_runner())
assert length(sample.vms) == 3
nginx = Enum.find(sample.vms, &(&1.vmid == 100))
assert nginx.type == "qemu"
assert nginx.name == "nginx-proxy"
assert nginx.status == "running"
assert nginx.cpu_usage == 0.05
assert nginx.mem_bytes == 536_870_912
assert nginx.max_mem_bytes == 2_147_483_648
assert nginx.tags == ["web", "production"]
mc = Enum.find(sample.vms, &(&1.vmid == 200))
assert mc.type == "lxc"
end
end
describe "collect_detail/1" do
test "returns per-VM config + IPs" do
sample = Vms.collect_detail(node: "pve-01", runner: fake_runner())
nginx = Enum.find(sample.vms, &(&1.vmid == 100))
assert nginx.config["cores"] == 2
assert nginx.config["memory"] == 2048
assert "192.168.1.10" in nginx.ips
db = Enum.find(sample.vms, &(&1.vmid == 101))
assert db.config["cores"] == 4
assert db.ips == []
mc = Enum.find(sample.vms, &(&1.vmid == 200))
assert mc.config["hostname"] == "minecraft"
assert "10.0.0.5" in mc.ips
end
end
end