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