first commit
This commit is contained in:
commit
dd50a0d05e
31 files changed
+5299
No files matched your search
Executable
+174
@@ -0,0 +1,174 @@
|
||||
"""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), ())
|
||||
|
||||
|
||||
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()
|
||||
Reference in new issue
Block a user