fix(vm_provision_mixin): get_vm_status hit a non-existent guest-agent path

agent.network_get_interfaces.get() built the URL path segment literally
("network_get_interfaces"), but the real Proxmox REST endpoint uses
hyphens ("network-get-interfaces") and must be reached via agent(...) as
a callable resource — the underscored attribute path 404ed silently on
every poll, so wait_for_ip always ran out the full timeout even though
the guest agent was reporting the IP to Proxmox correctly the whole time.

Also stopped assuming interfaces[0] is the real NIC — the guest agent
commonly reports "lo" first, matching the working pattern already used
in vm_mixin.py (skip "lo", require ip-address-type == "ipv4").
This commit is contained in:
Christian Manivong
2026-07-08 14:09:40 +02:00
parent 0da4ca3c69
commit 7038494b49
2 changed files with 69 additions and 31 deletions
+45 -7
View File
@@ -335,9 +335,12 @@ def test_get_vm_status_with_wait_for_ip():
mock_node.qemu.return_value = mock_vm
mock_vm.config.get.return_value = {"net0": "virtio,bridge=vmbr0,tag=10"}
# Mock guest-agent: first no IP, then with IP
mock_agent = MagicMock()
mock_agent.network_get_interfaces.get.side_effect = [
# The real Proxmox REST path is "network-get-interfaces" (hyphens),
# reached via agent(...) as a callable resource, not dotted attribute
# access — mock that exact call shape.
mock_agent_call = MagicMock()
mock_vm.agent.return_value = mock_agent_call
mock_agent_call.get.side_effect = [
{"result": [{"name": "eth0", "hardware-address": "aa:bb:cc:dd:ee:00"}]},
{
"result": [
@@ -349,16 +352,51 @@ def test_get_vm_status_with_wait_for_ip():
]
},
]
mock_vm.agent = mock_agent
with patch("time.sleep"):
result = mixin.get_vm_status("101", wait_for_ip=True, timeout=30, poll_interval=1)
mock_vm.agent.assert_called_with("network-get-interfaces")
assert result["status"] == "running"
assert result["ip_address"] == "10.0.0.100"
assert result["mac_address"] == "aa:bb:cc:dd:ee:00"
def test_get_vm_status_skips_loopback_interface():
"""The guest agent commonly reports "lo" before the real NIC — it must
be skipped rather than mistaken for the VM's address."""
mixin = ProxmoxVMProvisionMixin()
mock_node = MagicMock()
mixin._node_api = MagicMock(return_value=mock_node)
mock_vm = MagicMock()
mock_node.qemu.return_value = mock_vm
mock_vm.config.get.return_value = {"net0": "virtio,bridge=vmbr0"}
mock_agent_call = MagicMock()
mock_vm.agent.return_value = mock_agent_call
mock_agent_call.get.return_value = {
"result": [
{
"name": "lo",
"hardware-address": "00:00:00:00:00:00",
"ip-addresses": [{"ip-address": "127.0.0.1", "ip-address-type": "ipv4"}],
},
{
"name": "eth0",
"hardware-address": "aa:bb:cc:dd:ee:00",
"ip-addresses": [{"ip-address": "10.0.0.101", "ip-address-type": "ipv4"}],
},
]
}
with patch("time.sleep"):
result = mixin.get_vm_status("101", wait_for_ip=True, timeout=30, poll_interval=1)
assert result["ip_address"] == "10.0.0.101"
def test_get_vm_status_timeout_waiting_for_ip():
"""get_vm_status raises RuntimeError if IP acquisition times out."""
mixin = ProxmoxVMProvisionMixin()
@@ -372,11 +410,11 @@ def test_get_vm_status_timeout_waiting_for_ip():
mock_vm.config.get.return_value = {"net0": "virtio,bridge=vmbr0"}
# Mock guest-agent that never returns IP
mock_agent = MagicMock()
mock_agent.network_get_interfaces.get.return_value = {
mock_agent_call = MagicMock()
mock_vm.agent.return_value = mock_agent_call
mock_agent_call.get.return_value = {
"result": [{"name": "eth0", "hardware-address": "aa:bb:cc:dd:ee:00"}]
}
mock_vm.agent = mock_agent
with pytest.raises(RuntimeError, match="timeout|IP"):
with patch("time.sleep"):