2026-11-20 15:16:41 +02:00
|
|
|
"""Proxmox guest discovery and IP selection."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
from proxdomain.errors import ApiError, ProxdomainError
|
|
|
|
|
from proxdomain.proxmox import Guest, ProxmoxClient, _split_tags
|
|
|
|
|
from proxdomain.http import JsonClient
|
|
|
|
|
from tests.support import FakeTransport, add_proxmox_routes, make_config, pve
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def client(transport: FakeTransport, **proxmox) -> ProxmoxClient:
|
|
|
|
|
config = make_config(proxmox=proxmox or None)
|
|
|
|
|
return ProxmoxClient(config.proxmox, transport=transport)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GuestParsingTests(unittest.TestCase):
|
|
|
|
|
def test_list_guests(self) -> None:
|
|
|
|
|
transport = FakeTransport()
|
|
|
|
|
add_proxmox_routes(
|
|
|
|
|
transport,
|
|
|
|
|
guests=[
|
|
|
|
|
{
|
|
|
|
|
"vmid": 101,
|
|
|
|
|
"node": "pve",
|
|
|
|
|
"type": "qemu",
|
|
|
|
|
"name": "web01",
|
|
|
|
|
"status": "running",
|
|
|
|
|
"tags": "proxdomain;web",
|
|
|
|
|
},
|
|
|
|
|
{"vmid": 102, "node": "pve2", "type": "lxc", "name": "db", "status": "stopped"},
|
|
|
|
|
{"id": "storage/local", "type": "storage"},
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
guests = client(transport).list_guests()
|
|
|
|
|
self.assertEqual([g.vmid for g in guests], [101, 102])
|
|
|
|
|
self.assertEqual(guests[0].tags, ("proxdomain", "web"))
|
|
|
|
|
self.assertTrue(guests[0].is_running)
|
|
|
|
|
self.assertFalse(guests[1].is_running)
|
|
|
|
|
|
|
|
|
|
def test_find_by_name_exact_then_substring(self) -> None:
|
|
|
|
|
transport = FakeTransport()
|
|
|
|
|
add_proxmox_routes(
|
|
|
|
|
transport,
|
|
|
|
|
guests=[
|
|
|
|
|
{"vmid": 101, "node": "pve", "type": "qemu", "name": "web01", "status": "running"},
|
|
|
|
|
{"vmid": 102, "node": "pve", "type": "lxc", "name": "web02", "status": "running"},
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
proxmox = client(transport)
|
|
|
|
|
self.assertEqual(proxmox.find_guest(name="web01").vmid, 101)
|
|
|
|
|
self.assertEqual(proxmox.find_guest(name="WEB02").vmid, 102)
|
|
|
|
|
|
|
|
|
|
def test_ambiguous_name(self) -> None:
|
|
|
|
|
transport = FakeTransport()
|
|
|
|
|
add_proxmox_routes(
|
|
|
|
|
transport,
|
|
|
|
|
guests=[
|
|
|
|
|
{"vmid": 101, "node": "pve", "type": "qemu", "name": "web01", "status": "running"},
|
|
|
|
|
{"vmid": 102, "node": "pve", "type": "lxc", "name": "web02", "status": "running"},
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
with self.assertRaises(ProxdomainError) as ctx:
|
|
|
|
|
client(transport).find_guest(name="web")
|
|
|
|
|
self.assertIn("use --vmid", str(ctx.exception))
|
|
|
|
|
|
|
|
|
|
def test_unknown_guest(self) -> None:
|
|
|
|
|
transport = FakeTransport()
|
|
|
|
|
add_proxmox_routes(transport)
|
|
|
|
|
with self.assertRaises(ProxdomainError):
|
|
|
|
|
client(transport).find_guest(vmid=999)
|
|
|
|
|
|
|
|
|
|
def test_tag_parsing(self) -> None:
|
|
|
|
|
self.assertEqual(_split_tags("a;b"), ("a", "b"))
|
|
|
|
|
self.assertEqual(_split_tags("a,b"), ("a", "b"))
|
|
|
|
|
self.assertEqual(_split_tags(None), ())
|
|
|
|
|
|
|
|
|
|
|
2026-11-23 00:05:50 +02:00
|
|
|
class PortDeclarationTests(unittest.TestCase):
|
|
|
|
|
"""A VM/CT can declare its own backend port."""
|
|
|
|
|
|
|
|
|
|
def _guest(self, tags: tuple = ()) -> Guest:
|
|
|
|
|
return Guest(
|
|
|
|
|
vmid=101, node="pve", name="web01", kind="qemu", status="running", tags=tags
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _client(self, *, description: str = "", tags: tuple = ()) -> ProxmoxClient:
|
|
|
|
|
transport = FakeTransport()
|
|
|
|
|
add_proxmox_routes(
|
|
|
|
|
transport,
|
|
|
|
|
guests=[
|
|
|
|
|
{
|
|
|
|
|
"vmid": 101,
|
|
|
|
|
"node": "pve",
|
|
|
|
|
"type": "qemu",
|
|
|
|
|
"name": "web01",
|
|
|
|
|
"status": "running",
|
|
|
|
|
"tags": ",".join(tags),
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
guest_config={"description": description},
|
|
|
|
|
)
|
|
|
|
|
return client(transport)
|
|
|
|
|
|
|
|
|
|
def test_port_from_notes(self) -> None:
|
|
|
|
|
proxmox = self._client(description="proxdomain-port=8080\n")
|
|
|
|
|
self.assertEqual(proxmox.guest_port(self._guest()), (8080, "guest notes"))
|
|
|
|
|
|
|
|
|
|
def test_port_in_a_sentence(self) -> None:
|
|
|
|
|
proxmox = self._client(description="App server. proxdomain-port: 3000 for the UI")
|
|
|
|
|
self.assertEqual(proxmox.guest_port(self._guest())[0], 3000)
|
|
|
|
|
|
|
|
|
|
def test_port_from_tag(self) -> None:
|
|
|
|
|
proxmox = self._client(tags=("proxdomain", "proxdomain-port-8096"))
|
|
|
|
|
self.assertEqual(proxmox.guest_port(self._guest(tags=("proxdomain", "proxdomain-port-8096"))), (8096, "guest tag"))
|
|
|
|
|
|
|
|
|
|
def test_notes_win_over_tag(self) -> None:
|
|
|
|
|
proxmox = self._client(description="proxdomain-port=8080", tags=("proxdomain-port-9000",))
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
proxmox.guest_port(self._guest(tags=("proxdomain-port-9000",)))[0], 8080
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_no_declaration(self) -> None:
|
|
|
|
|
proxmox = self._client(description="just a web server")
|
|
|
|
|
self.assertEqual(proxmox.guest_port(self._guest()), (None, ""))
|
|
|
|
|
|
|
|
|
|
def test_unrelated_tag_is_ignored(self) -> None:
|
|
|
|
|
proxmox = self._client(tags=("proxdomain", "prod"))
|
|
|
|
|
self.assertEqual(proxmox.guest_port(self._guest(tags=("proxdomain", "prod"))), (None, ""))
|
|
|
|
|
|
|
|
|
|
def test_out_of_range_port_is_rejected(self) -> None:
|
|
|
|
|
proxmox = self._client(description="proxdomain-port=70000")
|
|
|
|
|
with self.assertRaises(ProxdomainError) as ctx:
|
|
|
|
|
proxmox.guest_port(self._guest())
|
|
|
|
|
self.assertIn("not a valid TCP port", str(ctx.exception))
|
|
|
|
|
|
|
|
|
|
def test_config_read_failure_means_no_declaration(self) -> None:
|
|
|
|
|
transport = FakeTransport()
|
|
|
|
|
transport.add(
|
|
|
|
|
"GET",
|
|
|
|
|
"/api2/json/nodes/pve/qemu/101/config",
|
|
|
|
|
{"errors": [{"message": "unavailable"}]},
|
|
|
|
|
status=500,
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(client(transport).guest_port(self._guest()), (None, ""))
|
|
|
|
|
|
|
|
|
|
|
2026-11-20 15:16:41 +02:00
|
|
|
class AddressTests(unittest.TestCase):
|
|
|
|
|
def _guest(self, kind: str = "qemu", vmid: int = 101) -> Guest:
|
|
|
|
|
return Guest(vmid=vmid, node="pve", name="web01", kind=kind, status="running")
|
|
|
|
|
|
|
|
|
|
def test_agent_addresses_filtered(self) -> None:
|
|
|
|
|
transport = FakeTransport()
|
|
|
|
|
add_proxmox_routes(
|
|
|
|
|
transport,
|
|
|
|
|
agent_ips=["127.0.0.1", "169.254.10.1", "192.168.1.50"],
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(client(transport).guest_ip(self._guest()), "192.168.1.50")
|
|
|
|
|
|
|
|
|
|
def test_excluded_network_is_skipped(self) -> None:
|
|
|
|
|
transport = FakeTransport()
|
|
|
|
|
add_proxmox_routes(transport, agent_ips=["172.17.0.5"])
|
|
|
|
|
self.assertIsNone(client(transport).guest_ip(self._guest()))
|
|
|
|
|
|
|
|
|
|
def test_agent_failure_falls_back_to_static_config(self) -> None:
|
|
|
|
|
transport = FakeTransport()
|
|
|
|
|
transport.add(
|
|
|
|
|
"GET",
|
|
|
|
|
"/api2/json/nodes/pve/qemu/101/agent/network-get-interfaces",
|
|
|
|
|
{"errors": [{"message": "guest agent not running"}]},
|
|
|
|
|
status=500,
|
|
|
|
|
)
|
|
|
|
|
transport.add(
|
|
|
|
|
"GET",
|
|
|
|
|
"/api2/json/nodes/pve/qemu/101/config",
|
|
|
|
|
pve({"ipconfig0": "ip=10.0.0.9/24,gw=10.0.0.1"}),
|
|
|
|
|
)
|
|
|
|
|
self.assertEqual(client(transport).guest_ip(self._guest()), "10.0.0.9")
|
|
|
|
|
|
|
|
|
|
def test_auth_error_propagates(self) -> None:
|
|
|
|
|
transport = FakeTransport()
|
|
|
|
|
transport.add(
|
|
|
|
|
"GET",
|
|
|
|
|
"/api2/json/nodes/pve/qemu/101/agent/network-get-interfaces",
|
|
|
|
|
{"errors": [{"message": "denied"}]},
|
|
|
|
|
status=403,
|
|
|
|
|
)
|
|
|
|
|
with self.assertRaises(ApiError) as ctx:
|
|
|
|
|
client(transport).guest_ip(self._guest())
|
|
|
|
|
self.assertTrue(ctx.exception.is_auth_error)
|
|
|
|
|
|
|
|
|
|
def test_lxc_static_ip_from_net0(self) -> None:
|
|
|
|
|
transport = FakeTransport()
|
|
|
|
|
transport.add(
|
|
|
|
|
"GET",
|
|
|
|
|
"/api2/json/nodes/pve/lxc/102/config",
|
|
|
|
|
pve({"net0": "name=eth0,bridge=vmbr0,ip=10.0.0.6/24,gw=10.0.0.1"}),
|
|
|
|
|
)
|
|
|
|
|
guest = Guest(vmid=102, node="pve", name="db", kind="lxc", status="running")
|
|
|
|
|
self.assertEqual(client(transport).guest_ip(guest), "10.0.0.6")
|
|
|
|
|
|
|
|
|
|
def test_lxc_runtime_interfaces(self) -> None:
|
|
|
|
|
transport = FakeTransport()
|
|
|
|
|
transport.add(
|
|
|
|
|
"GET",
|
|
|
|
|
"/api2/json/nodes/pve/lxc/102/interfaces",
|
|
|
|
|
pve([{"name": "eth0", "inet": "10.0.0.7/24", "inet6": "fe80::1/64"}]),
|
|
|
|
|
)
|
|
|
|
|
guest = Guest(vmid=102, node="pve", name="db", kind="lxc", status="running")
|
|
|
|
|
self.assertEqual(client(transport).guest_ip(guest), "10.0.0.7")
|
|
|
|
|
|
|
|
|
|
def test_ipv6_preference(self) -> None:
|
|
|
|
|
transport = FakeTransport()
|
|
|
|
|
add_proxmox_routes(transport, agent_ips=["192.168.1.50"])
|
|
|
|
|
transport.add(
|
|
|
|
|
"GET",
|
|
|
|
|
"/api2/json/nodes/pve/qemu/101/agent/network-get-interfaces",
|
|
|
|
|
pve(
|
|
|
|
|
{
|
|
|
|
|
"result": [
|
|
|
|
|
{
|
|
|
|
|
"name": "eth0",
|
|
|
|
|
"ip-addresses": [
|
|
|
|
|
{"ip-address": "192.168.1.50", "ip-address-type": "ipv4"},
|
|
|
|
|
{"ip-address": "2001:db8::10", "ip-address-type": "ipv6"},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
proxmox = client(transport, ip_preference="ipv6")
|
|
|
|
|
self.assertEqual(proxmox.guest_ip(self._guest()), "2001:db8::10")
|
|
|
|
|
|
|
|
|
|
def test_loopback_only_returns_none(self) -> None:
|
|
|
|
|
transport = FakeTransport()
|
|
|
|
|
add_proxmox_routes(transport, agent_ips=["127.0.0.1", "::1"])
|
|
|
|
|
self.assertIsNone(client(transport).guest_ip(self._guest()))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|