"""Live checks that each domain edit does what it says and nothing else.
For every edit the persistent definition (inactive, with secrets) is read
before and after. The edit must show its effect, and once the nodes it is
allowed to touch are removed from both sides, the rest of the definition
must be identical: unknown elements, secrets and pending changes survive.
On a running VM, edits of the persistent definition must leave the live
one alone. Runs only with TEST_LIBVIRT_HOST set (see instances/livetest.py).
"""
import os
import unittest
import libvirt
from django.test import SimpleTestCase
from lxml import etree
from vrtManager.connection import connection_manager
from vrtManager.instance import wvmInstance
from . import livetest
P = livetest.PREFIX
MAC = "52:54:00:aa:02:01"
PERSISTENT = libvirt.VIR_DOMAIN_XML_INACTIVE | libvirt.VIR_DOMAIN_XML_SECURE
PARSER = etree.XMLParser(remove_blank_text=True)
def domain_xml(name, disk, iso):
"""A VM with elements WebVirtCloud does not manage, which edits must keep."""
return f"""
{name}
original title
keep-metadata
128
128
1
2048
hvm
restart
keep-serial
10485760
/dev/urandom
"""
def first(tree, path):
found = tree.xpath(path)
return found[0] if found else None
# name -> (edit, check of the new persistent tree, paths the edit may change,
# allowed while running, changes the running VM too)
EDITS = {
"set_bootmenu": (
lambda vm: vm.set_bootmenu(1),
lambda t: first(t, "os/bootmenu/@enable") == "yes",
["os/bootmenu"], True, False,
),
"set_bootorder": (
lambda vm: vm.set_bootorder({0: {"type": "disk", "dev": "vda"}}),
lambda t: first(t, "devices/disk[target/@dev='vda']/boot/@order") == "1" and not t.xpath("os/boot"),
["os/boot", "devices/*/boot"], True, False,
),
"set_vcpu_hotplug": (
lambda vm: vm.set_vcpu_hotplug(True, 1),
lambda t: bool(t.xpath("vcpus/vcpu")),
["vcpus"], False, False,
),
"set_console_keymap": (
lambda vm: vm.set_console_keymap("de"),
lambda t: first(t, "devices/graphics/@keymap") == "de",
["devices/graphics/@keymap"], True, False,
),
"set_console_listener_addr": (
lambda vm: vm.set_console_listener_addr("127.0.0.1"),
lambda t: first(t, "devices/graphics/listen/@address") == "127.0.0.1",
["devices/graphics/@listen", "devices/graphics/listen/@address"], True, False,
),
"set_console_passwd": (
lambda vm: vm.set_console_passwd("new12345"),
lambda t: first(t, "devices/graphics/@passwd") == "new12345",
["devices/graphics/@passwd"], True, False,
),
"set_video_model": (
lambda vm: vm.set_video_model("virtio"),
lambda t: first(t, "devices/video/model/@type") == "virtio",
["devices/video"], True, False,
),
"resize_cpu": (
lambda vm: vm.resize_cpu("1", "2"),
lambda t: t.findtext("vcpu") == "2" and first(t, "vcpu/@current") == "1",
["vcpu"], True, False,
),
"resize_mem": (
lambda vm: vm.resize_mem(128, 160),
lambda t: t.findtext("memory") == str(160 * 1024),
["memory", "currentMemory"], False, False,
),
"set_options": (
lambda vm: vm.set_options({"title": "new title", "description": "new description"}),
lambda t: t.findtext("title") == "new title" and t.findtext("description") == "new description",
["title", "description"], True, False,
),
"set_qos": (
lambda vm: vm.set_qos(MAC, "inbound", 100, 200, 300),
lambda t: first(t, "devices/interface/bandwidth/inbound/@average") == "100"
and first(t, "devices/interface/bandwidth/outbound/@average") == "1000",
["devices/interface/bandwidth/inbound"], True, False,
),
"unset_qos": (
lambda vm: vm.unset_qos(MAC, "outbound"),
lambda t: not t.xpath("devices/interface/bandwidth/outbound"),
["devices/interface/bandwidth"], True, False,
),
"umount_iso": (
lambda vm: vm.umount_iso("sda"),
lambda t: not t.xpath("devices/disk[target/@dev='sda']/source"),
["devices/disk[target/@dev='sda']/source"], True, True,
),
"set_link_state": (
lambda vm: vm.set_link_state(MAC, "down"),
lambda t: first(t, "devices/interface/link/@state") == "down",
["devices/interface/link"], True, True,
),
"change_network (same MAC, new model)": (
lambda vm: vm.change_network(MAC, MAC, "default", "net", "e1000e", ""),
lambda t: first(t, "devices/interface/model/@type") == "e1000e"
and first(t, "devices/interface/bandwidth/outbound/@average") == "1000",
["devices/interface/model"], True, False,
),
"add_network": (
lambda vm: vm.add_network("52:54:00:aa:02:09", "default", "net", "virtio"),
lambda t: len(t.xpath("devices/interface")) == 2,
["devices/interface[mac/@address='52:54:00:aa:02:09']"], True, True,
),
"delete_network": (
lambda vm: vm.delete_network(MAC),
lambda t: not t.xpath("devices/interface"),
["devices/interface"], True, True,
),
"edit_disk (cache mode)": (
lambda vm: vm.edit_disk("vda", vm.get_disk_devices(config=True)[0]["path"], False, False, "virtio",
"keep-serial", "qcow2", "writeback", None, None, None),
lambda t: first(t, "devices/disk[target/@dev='vda']/driver/@cache") == "writeback",
["devices/disk[target/@dev='vda']/driver/@cache"], True, False,
),
}
def canonical(tree, paths):
tree = etree.fromstring(etree.tostring(tree), PARSER)
for path in paths:
for node in tree.xpath(path):
if isinstance(node, etree._ElementUnicodeResult):
node.getparent().attrib.pop(node.attrname, None)
else:
node.getparent().remove(node)
return etree.tostring(tree, method="c14n").decode()
class LiveXmlEffectsTestCase(SimpleTestCase):
@classmethod
def setUpClass(cls):
if not livetest.enabled():
raise unittest.SkipTest("Set TEST_LIBVIRT_HOST to run the live libvirt tests")
cls.args = (
os.environ["TEST_LIBVIRT_HOST"],
os.environ.get("TEST_LIBVIRT_LOGIN", ""),
os.environ.get("TEST_LIBVIRT_PASSWORD", ""),
int(os.environ.get("TEST_LIBVIRT_TYPE", 4)),
)
cls.conn = connection_manager.get_connection(*cls.args)
livetest.cleanup(cls.conn)
livetest.ensure_pool(cls.conn)
cls.inventory = livetest.inventory(cls.conn)
super().setUpClass()
@classmethod
def tearDownClass(cls):
try:
livetest.remove_pool(cls.conn)
finally:
super().tearDownClass()
if livetest.inventory(cls.conn) != cls.inventory:
raise AssertionError("Live tests changed objects outside the test namespace")
def vm(self, running, paused=False):
disk = livetest.create_volume(self.conn, P + "fx")
iso = self.conn.storagePoolLookupByName(livetest.POOL).createXML(
f"{P}fx.iso1", 0
).path()
dom = self.conn.defineXML(domain_xml(P + "fx", disk, iso))
if running:
dom.create()
# A pending change only in the persistent definition
tree = self.persistent(dom)
tree.find("on_crash").text = "destroy"
self.conn.defineXML(etree.tostring(tree).decode())
if paused:
dom.suspend()
return dom, wvmInstance(*self.args, None, uuid=dom.UUIDString())
def persistent(self, dom):
return etree.fromstring(dom.XMLDesc(PERSISTENT), PARSER)
def live(self, dom):
return etree.fromstring(dom.XMLDesc(libvirt.VIR_DOMAIN_XML_SECURE), PARSER)
def check(self, running, paused=False):
for name, (edit, effect, touched, allowed_running, changes_live) in EDITS.items():
if running and not allowed_running:
continue
with self.subTest(name):
try:
dom, vm = self.vm(running, paused)
before, live_before = self.persistent(dom), dom.isActive() and self.live(dom)
edit(vm)
after = self.persistent(dom)
self.assertTrue(effect(after), "edit had no effect")
self.assertEqual(canonical(after, touched), canonical(before, touched), "unrelated change")
if running and not changes_live:
self.assertEqual(
canonical(self.live(dom), ["devices/graphics/@port"]),
canonical(live_before, ["devices/graphics/@port"]),
"persistent-only edit changed the running VM",
)
finally:
livetest.cleanup(self.conn)
def test_edits_of_a_stopped_vm(self):
self.check(running=False)
def test_edits_of_a_running_vm(self):
self.check(running=True)
def test_edits_of_a_paused_vm(self):
self.check(running=True, paused=True)