1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/vrtManager/tests/test_interface_addresses.py
catborise 79f3b906dd fix(instances): Guest Info fits the tab, reads odd agent answers and says when usage is unknown
Audit and review (Codex, agy) of the file system list:
- the table had a side margin on a full-width table and ran past the tab's
  edge (on a phone its Used column was cut off); it now sits in a box that
  scrolls a long mount point, and on a phone the labels above it wrap by word;
- an agent answer that is not a list, an entry that is not an object, a
  negative, boolean or oversized size, or a null mount point or type no longer
  fails the tab or shows "None"; a "filesystems" key of another command is not
  taken for the list; the total is the whole file system's when the agent
  tells it (total-bytes-privileged), so used no longer passes it;
- when the agent does not give the file systems, a note says so;
- Turkish labels: "Kernel Version" was "Çekirdek Sürümü" like "Kernel Release",
  "OS Pretty-Name" read as a short name, "Timezone / Offset" as a location;
- the bulk migration live test's name no longer claims two at once, which its
  unit test checks.
2026-11-20 10:28:35 +03:00

121 lines
6.6 KiB
Python

"""A running VM's addresses come from the guest agent when it answers and
from the host's ARP table, so a VM without an agent shows its IPv4 address.
The agent is asked with a timeout: interfaceAddresses() would wait for a
hung agent indefinitely; the VM page asks for them after it loads."""
import json
import unittest
from unittest.mock import MagicMock, patch
from libvirt import VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP, libvirtError
from vrtManager import instance as module
from vrtManager.instance import wvmInstance
MAC = "52:54:00:00:00:01"
ARP = {"vnet0": {"hwaddr": MAC, "addrs": [{"type": 0, "addr": "192.0.2.10", "prefix": 0}]}}
# guest-network-get-interfaces: the loopback has no hardware address
AGENT = [{"name": "lo", "ip-addresses": [{"ip-address-type": "ipv4", "ip-address": "127.0.0.1", "prefix": 8}]},
{"name": "eth0", "hardware-address": MAC, "ip-addresses": [
{"ip-address-type": "ipv4", "ip-address": "192.0.2.20", "prefix": 24},
{"ip-address-type": "ipv6", "ip-address": "2001:db8::20", "prefix": 64}]}]
def vm(agent):
inst = wvmInstance.__new__(wvmInstance)
inst.instance = MagicMock()
inst.instance.interfaceAddresses.side_effect = lambda source: {VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP: ARP}[source]
patch.object(inst, "get_status", return_value=1).start()
patch.object(inst, "is_agent_ready", return_value=agent).start()
return inst
class InterfaceAddressesTestCase(unittest.TestCase):
def setUp(self):
self.agent = patch.object(module, "qemuAgentCommand", return_value=json.dumps({"return": AGENT})).start()
def tearDown(self):
patch.stopall()
def test_a_vm_without_an_agent_shows_its_arp_address(self):
inst = vm(agent=False)
self.assertEqual(inst.get_addresses(), {MAC: (["192.0.2.10"], [])})
self.agent.assert_not_called()
def test_the_agents_address_comes_first_and_is_asked_with_a_timeout(self):
inst = vm(agent=True)
self.assertEqual(inst.get_addresses(), {MAC: (["192.0.2.20"], ["2001:db8::20/64"])})
self.agent.assert_called_once_with(inst.instance, '{"execute": "guest-network-get-interfaces"}', 10, 0)
self.assertEqual([c.args for c in inst.instance.interfaceAddresses.call_args_list],
[(VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_ARP,)])
def test_the_vm_s_definition_does_not_read_them(self):
inst = vm(agent=True)
inst._XMLDesc = lambda flags: f"<domain><devices><interface type='network'><mac address='{MAC}'/></interface></devices></domain>"
self.assertEqual([nic["mac"] for nic in inst.get_net_devices()], [MAC])
self.agent.assert_not_called()
inst.instance.interfaceAddresses.assert_not_called()
def test_a_vm_that_does_not_run_has_none(self):
inst = vm(agent=True)
inst.get_status.return_value = 5
self.assertEqual(inst.get_addresses(), {})
def test_an_agent_that_does_not_answer_leaves_the_arp_address(self):
self.agent.side_effect = libvirtError("guest agent didn't respond to command within '10' seconds")
self.assertEqual(vm(agent=True).get_addresses(), {MAC: (["192.0.2.10"], [])})
class OsInfoTestCase(unittest.TestCase):
def test_the_os_details_are_asked_with_a_timeout(self):
answers = {"guest-get-osinfo": {"kernel-release": "6.12"}, "guest-get-timezone": {"zone": "UTC"},
"guest-get-host-name": {"host-name": "web"}, "guest-get-fsinfo": []}
inst = wvmInstance.__new__(wvmInstance)
inst.instance = MagicMock()
with patch.object(module, "qemuAgentCommand",
side_effect=lambda dom, cmd, timeout, flags: json.dumps(
{"return": answers[json.loads(cmd)["execute"]]})) as agent:
self.assertEqual(inst.osinfo(), {"kernel-release": "6.12", "zone": "UTC", "host-name": "web",
"filesystems": []})
self.assertEqual({c.args[2] for c in agent.call_args_list}, {10})
def test_a_command_the_agent_lacks_leaves_out_only_its_details(self):
inst = wvmInstance.__new__(wvmInstance)
inst.instance = MagicMock()
def agent(dom, cmd, timeout, flags):
if json.loads(cmd)["execute"] in ("guest-get-timezone", "guest-get-fsinfo"):
raise libvirtError("The command has not been found")
# a "filesystems" of another command is not taken for guest_filesystems'
return json.dumps({"return": {"guest-get-osinfo": {"id": "alma", "filesystems": "x"},
"guest-get-host-name": {"host-name": "web"}}[json.loads(cmd)["execute"]]})
with patch.object(module, "qemuAgentCommand", side_effect=agent):
self.assertEqual(inst.osinfo(), {"id": "alma", "host-name": "web"})
def test_the_file_systems_with_a_size_by_mount_point(self):
fsinfo = [
{"name": "vda2", "mountpoint": "/boot", "type": "xfs", "used-bytes": 2**28, "total-bytes": 2**30},
{"name": "vda1", "mountpoint": "/var/lib/containers/overlay", "type": "overlay"}, # no size: left out
{"name": "vdb1", "mountpoint": "/data", "type": "xfs", "total-bytes": 2**30}, # no used size: left out
None, "/", 1, # not entries
{"mountpoint": "/neg", "used-bytes": -5, "total-bytes": 100}, # garbled sizes: left out
{"mountpoint": "/bool", "used-bytes": True, "total-bytes": True},
{"mountpoint": None, "used-bytes": 1, "total-bytes": 2}, {"mountpoint": ["/x"], "used-bytes": 1, "total-bytes": 2},
{"mountpoint": "/srv", "type": None, "used-bytes": 92, "total-bytes": 90, "total-bytes-privileged": 100},
{"name": "vda1", "mountpoint": "/", "type": "xfs", "used-bytes": 3 * 2**30, "total-bytes": 10 * 2**30},
]
inst = wvmInstance.__new__(wvmInstance)
inst.instance = MagicMock()
with patch.object(module, "qemuAgentCommand", return_value=json.dumps({"return": fsinfo})):
self.assertEqual(inst.guest_filesystems(), [
{"mountpoint": "/", "type": "xfs", "used": 3 * 2**30, "total": 10 * 2**30},
{"mountpoint": "/boot", "type": "xfs", "used": 2**28, "total": 2**30},
{"mountpoint": "/srv", "type": "", "used": 92, "total": 100}, # the whole file system's total
])
for answer in ({"error": "x"}, 1, None): # not a list: none
with patch.object(module, "qemuAgentCommand", return_value=json.dumps({"return": answer})):
self.assertEqual(inst.guest_filesystems(), [])
if __name__ == "__main__":
unittest.main()