1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/vrtManager/tests/test_backup.py
catborise 823d34cc18 feat(vmbackups): restore a backup as a new VM
A ready backup has a Restore button (Backups page and the VM's tab) and
POST /api/v1/backups/<uuid>/restore/. The form takes the new VM's name
(default <vm>-restored), a compute that has the target's pool, the pool
for its disks, Connect the network (off by default: the restored guest
keeps the VM's addresses and host name) and Start after restore. The job
page follows the restore.

The backup.restore job holds what a new VM's job holds, the backup
(shared) and the target. Before anything is made it checks again: the
host is not in maintenance; the name is free in the panel and on the
host (a VM made with virsh too); the backup's files are in the target,
the disks at their size and the small files with their SHA-256; the
host offers the definition's emulator, machine type and firmware, its
networks, bridges and network filters; the destination pool has room
and none of the copies' names. The new VM's UUID, MACs, volume names and
NVRAM path are recorded first. Then the disks are copied from the
target, the NVRAM file uploaded, maintenance checked once more, and the
VM defined last and given to the superuser who restored it.

The definition is the kept one rewritten as a clone's (rewrite_for_new_vm,
taken out of _clone_definition for both): new name, UUID and SMBIOS
UUID, MACs and filter parameters, a VNC password, no cloud-init seed of
the VM's; with a new generation ID, each disk's source the restored file
without the old chain, the NVRAM path, and links down unless connected.

Recovery records a VM defined by the recorded UUID; otherwise it removes
the copies and the NVRAM file by their recorded names.
2026-11-17 09:14:09 +03:00

321 lines
16 KiB
Python

"""What a VM's backup holds (wvmBackup.backup_plan), the target's file
names and space, and the small files' upload read back."""
import unittest
from unittest.mock import MagicMock
import libvirt
from vrtManager import util
from vrtManager.backup import backup_file_names, check_target_space, put_file, wvmBackup
from vrtManager.instance import PERSISTENT_XML
GIB = 1024**3
QCOW = "<disk type='file' device='disk'><driver name='qemu' type='qcow2'/><source file='/pool/a.qcow2'/><target dev='vda'/></disk>"
RAW = "<disk type='file' device='disk'><driver name='qemu' type='raw'/><source file='/pool/b.img'/><target dev='vdb'/></disk>"
CDROM = "<disk type='file' device='cdrom'><source file='/iso/x.iso'/><target dev='sda'/></disk>"
def error(code):
err = libvirt.libvirtError("x")
err.err = (code,)
return err
def domain(devices=QCOW + RAW + CDROM, os_extra=""):
return (
f"<domain><name>vm1</name><uuid>u</uuid><os><type>hvm</type>{os_extra}</os>"
f"<devices>{devices}</devices></domain>"
)
def proxy(xml=None, live=None, state=libvirt.VIR_DOMAIN_SHUTOFF, managed_save=False):
p = wvmBackup.__new__(wvmBackup) # no connection: only these calls
p.wvm = MagicMock()
p.instance = MagicMock()
p.instance.name.return_value = "vm1"
p.instance.state.return_value = [state, 0]
p.instance.hasManagedSaveImage.return_value = managed_save
persistent = xml or domain()
p._XMLDesc = lambda flags: persistent if flags == PERSISTENT_XML else (live or persistent)
pool = MagicMock()
pool.name.return_value = "default"
def volume(path):
vol = MagicMock()
vol.info.return_value = [0, (10 if path.endswith("qcow2") else 20) * GIB, GIB]
vol.storagePoolLookupByVolume.return_value = pool
return vol
p.get_volume_by_path = MagicMock(side_effect=volume)
return p
class BackupPlanTestCase(unittest.TestCase):
def refused(self, p, *texts):
with self.assertRaises(util.OperationError) as caught:
p.backup_plan()
for text in texts:
self.assertIn(text, str(caught.exception))
def test_a_vm_with_qcow2_and_raw_disks_and_a_cdrom(self):
plan = proxy().backup_plan()
self.assertEqual(
plan["disks"],
[
{"dev": "vda", "path": "/pool/a.qcow2", "pool": "default", "format": "qcow2", "capacity": 10 * GIB},
{"dev": "vdb", "path": "/pool/b.img", "pool": "default", "format": "raw", "capacity": 20 * GIB},
],
)
self.assertEqual(plan["left_out"], [{"dev": "sda", "why": "CD-ROM: not copied"}])
self.assertIsNone(plan["nvram"])
self.assertIn("<name>vm1</name>", plan["definition"])
def test_the_nvram_is_named(self):
xml = domain(os_extra="<nvram>/var/lib/libvirt/qemu/nvram/vm1_VARS.fd</nvram>")
self.assertEqual(proxy(xml).backup_plan()["nvram"], "/var/lib/libvirt/qemu/nvram/vm1_VARS.fd")
xml = domain(os_extra="<nvram><source file='/var/lib/libvirt/qemu/nvram/vm1_VARS.fd'/></nvram>")
self.assertEqual(proxy(xml).backup_plan()["nvram"], "/var/lib/libvirt/qemu/nvram/vm1_VARS.fd")
def test_an_nvram_outside_libvirts_directory_is_refused(self):
self.refused(proxy(domain(os_extra="<nvram>/srv/vm1_VARS.fd</nvram>")), "/srv/vm1_VARS.fd")
def test_refusals_name_the_reason_and_the_device(self):
cases = {
"encrypted": (
"<disk type='file' device='disk'><driver type='qcow2'/><source file='/pool/a.qcow2'>"
"<encryption format='luks'/></source><target dev='vda'/></disk>",
("vda", "encrypted"),
),
"block": ("<disk type='block' device='disk'><source dev='/dev/sdb'/><target dev='vda'/></disk>", ("vda", "not a file")),
"network": ("<disk type='network' device='disk'><source protocol='rbd' name='p/i'/><target dev='vda'/></disk>", ("vda", "not a file")),
"volume": ("<disk type='volume' device='disk'><source pool='p' volume='v'/><target dev='vda'/></disk>", ("vda", "not a file")),
"dir": ("<disk type='dir' device='disk'><source dir='/srv'/><target dev='vda'/></disk>", ("vda", "not a file")),
"shareable": (
"<disk type='file' device='disk'><driver type='raw'/><source file='/pool/b.img'/><target dev='vda'/><shareable/></disk>",
("vda", "shareable"),
),
"other format": (
"<disk type='file' device='disk'><driver type='vmdk'/><source file='/pool/c.vmdk'/><target dev='vda'/></disk>",
("vda", "vmdk"),
),
"filesystem": (QCOW + "<filesystem type='mount'><source dir='/srv'/><target dir='t'/></filesystem>", ("host directory",)),
"host device": (QCOW + "<hostdev mode='subsystem' type='pci'/>", ("host device",)),
"host device NIC": (QCOW + "<interface type='hostdev'><mac address='52:54:00:00:00:09'/></interface>",
("host device",)),
"tpm": (QCOW + "<tpm model='tpm-crb'><backend type='emulator'/></tpm>", ("vm1", "TPM")),
}
for name, (devices, texts) in cases.items():
with self.subTest(name):
self.refused(proxy(domain(devices)), *texts)
for tag in ("kernel", "initrd", "dtb"):
with self.subTest(tag):
self.refused(proxy(domain(os_extra=f"<{tag}>/boot/x</{tag}>")), "kernel")
def test_a_saved_state_is_refused(self):
self.refused(proxy(managed_save=True), "saved state")
def test_a_disk_in_no_pool_is_refused_by_name(self):
p = proxy()
p.get_volume_by_path.side_effect = error(libvirt.VIR_ERR_NO_STORAGE_VOL)
self.refused(p, "vda", "/pool/a.qcow2", "no storage pool")
def test_another_lookup_error_is_raised(self):
p = proxy()
p.get_volume_by_path.side_effect = error(libvirt.VIR_ERR_INTERNAL_ERROR)
with self.assertRaises(libvirt.libvirtError):
p.backup_plan()
def test_live_and_persistent_disks_must_match(self):
changed = QCOW.replace("/pool/a.qcow2", "/pool/new.qcow2") + RAW
p = proxy(domain(changed), live=domain(), state=libvirt.VIR_DOMAIN_RUNNING)
self.refused(p, "vda", "next start")
p = proxy(domain(QCOW), live=domain(QCOW + RAW), state=libvirt.VIR_DOMAIN_RUNNING)
self.refused(p, "next start")
p = proxy(domain(), live=domain(), state=libvirt.VIR_DOMAIN_RUNNING)
self.assertEqual(len(p.backup_plan()["disks"]), 2)
class FilesTestCase(unittest.TestCase):
def test_file_names(self):
uuid = "00000000-0000-4000-8000-000000000001"
disks = [{"dev": "vda", "format": "qcow2"}, {"dev": "vdb", "format": "raw"}]
self.assertEqual(
backup_file_names(uuid, disks, nvram=True),
{
"disks": {"vda": f"wvc-{uuid}-vda.qcow2", "vdb": f"wvc-{uuid}-vdb.raw"},
"nvram": f"wvc-{uuid}-nvram.fd",
"domain": f"wvc-{uuid}-domain.xml",
"manifest": f"wvc-{uuid}-manifest.json",
},
)
self.assertIsNone(backup_file_names(uuid, disks, nvram=False)["nvram"])
def test_space_counts_virtual_capacity_and_the_margin(self):
# 2 x 10 GiB plus max(10 % of it, 1 GiB) = 22 GiB
pool = MagicMock()
pool.name.return_value = "b1"
disks = [{"capacity": 10 * GIB}, {"capacity": 10 * GIB}]
pool.info.return_value = [2, 100 * GIB, 0, int(21.9 * GIB)]
with self.assertRaisesRegex(util.OperationError, "Storage pool b1 has 21.9 GiB free; the backup needs 22.0 GiB"):
check_target_space(pool, disks, 10, 1)
pool.info.return_value = [2, 100 * GIB, 0, 22 * GIB]
check_target_space(pool, disks, 10, 1)
pool.refresh.assert_called_with(0)
def test_put_file_refuses_a_different_read_back(self):
conn, pool = MagicMock(), MagicMock()
pool.storageVolLookupByName.side_effect = error(libvirt.VIR_ERR_NO_STORAGE_VOL)
conn.download_volume.return_value = b"other"
with self.assertRaisesRegex(util.OperationError, "wvc-x-domain.xml"):
put_file(conn, pool, "wvc-x-domain.xml", b"data", (107, 107))
conn.upload_volume.return_value.delete.assert_called_once_with(0)
def test_put_file_returns_the_sha256_and_replaces_a_volume(self):
conn, pool = MagicMock(), MagicMock()
old = pool.storageVolLookupByName.return_value
conn.download_volume.return_value = b"data"
self.assertEqual(
put_file(conn, pool, "wvc-x-domain.xml", b"data", (107, 108)),
"3a6eb0790f39ac87c94f3856b2dd2c5d110e6811602261a9a923d3bb23adc8b7",
)
old.delete.assert_called_once_with(0)
# written as QEMU's user: an NFS export that squashes root refuses root's files
conn.upload_volume.assert_called_once_with(pool, "wvc-x-domain.xml", b"data", owner=(107, 108))
def test_upload_volume_sets_the_owner(self):
from lxml import etree
from vrtManager.connection import wvmConnect
conn = wvmConnect.__new__(wvmConnect)
conn.wvm = MagicMock()
pool = MagicMock()
conn.upload_volume(pool, "f", b"ab", owner=(107, 108), mode="0600")
volume = etree.fromstring(pool.createXML.call_args.args[0])
self.assertEqual(volume.findtext("target/permissions/owner"), "107")
self.assertEqual(volume.findtext("target/permissions/group"), "108")
self.assertEqual(volume.findtext("target/permissions/mode"), "0600")
conn.upload_volume(pool, "g", b"ab")
self.assertIsNone(etree.fromstring(pool.createXML.call_args.args[0]).find("target/permissions"))
SOURCE = """<domain type='kvm'>
<name>vm1</name><uuid>11111111-1111-4111-8111-111111111111</uuid><genid>22222222-2222-4222-8222-222222222222</genid>
<title>web</title>
<sysinfo type='smbios'><system><entry name='uuid'>11111111-1111-4111-8111-111111111111</entry></system></sysinfo>
<os><type arch='x86_64' machine='pc-q35-10.1'>hvm</type><loader readonly='yes' type='pflash'>/usr/share/edk2/ovmf/OVMF_CODE.fd</loader>
<nvram>/var/lib/libvirt/qemu/nvram/vm1_VARS.fd</nvram></os>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'><driver name='qemu' type='qcow2'/>
<source file='/pool/a.qcow2'><seclabel model='dac' relabel='no'/></source>
<backingStore type='file'><format type='qcow2'/><source file='/pool/base.qcow2'/></backingStore>
<target dev='vda' bus='virtio'/></disk>
<disk type='file' device='disk'><driver name='qemu' type='raw'/><source file='/pool/b.img'/><target dev='vdb' bus='virtio'/></disk>
<disk type='file' device='cdrom'><source file='/iso/cidata-11111111-1111-4111-8111-111111111111.iso'/><target dev='sda'/></disk>
<disk type='file' device='cdrom'><source file='/iso/install.iso'/><target dev='sdb'/></disk>
<disk type='file' device='floppy'><source file='/iso/drivers.vfd'/><target dev='fda'/></disk>
<interface type='network'><mac address='52:54:00:00:00:01'/><source network='default'/>
<filterref filter='clean-traffic'><parameter name='MAC' value='52:54:00:00:00:01'/><parameter name='IP' value='192.0.2.5'/></filterref></interface>
<interface type='bridge'><mac address='52:54:00:00:00:02'/><source bridge='br0'/><link state='up'/></interface>
<graphics type='vnc' passwd='secret1'/>
</devices>
</domain>"""
class RestoreDefinitionTestCase(unittest.TestCase):
def restored(self, connect=False):
from lxml import etree
from vrtManager.backup import restore_definition
xml = restore_definition(
SOURCE, name="vm1-restored", uuid="33333333-3333-4333-8333-333333333333",
macs=["52:54:00:aa:00:01", "52:54:00:aa:00:02"],
disks={"vda": ("/dest/vm1-restored-vda.qcow2", "qcow2"), "vdb": ("/dest/vm1-restored-vdb.raw", "raw")},
nvram="/var/lib/libvirt/qemu/nvram/vm1-restored_VARS.fd", connect=connect,
)
return etree.fromstring(xml)
def test_restore_definition(self):
tree = self.restored()
self.assertEqual(tree.findtext("name"), "vm1-restored")
self.assertEqual(tree.findtext("uuid"), "33333333-3333-4333-8333-333333333333")
self.assertEqual(tree.xpath("string(sysinfo/system/entry[@name='uuid'])"), "33333333-3333-4333-8333-333333333333")
self.assertEqual(tree.findtext("genid") or "", "") # libvirt makes a new one
self.assertIsNotNone(tree.find("genid"))
self.assertEqual(tree.xpath("devices/interface/mac/@address"), ["52:54:00:aa:00:01", "52:54:00:aa:00:02"])
self.assertEqual(tree.xpath("devices/interface/filterref/parameter[@name='MAC']/@value"), ["52:54:00:aa:00:01"])
self.assertEqual(tree.xpath("devices/interface/filterref/parameter[@name='IP']"), [])
self.assertEqual(tree.xpath("devices/disk[@device='disk']/source/@file"),
["/dest/vm1-restored-vda.qcow2", "/dest/vm1-restored-vdb.raw"])
self.assertEqual(tree.xpath("devices/disk[@device='disk']/driver/@type"), ["qcow2", "raw"])
self.assertEqual(tree.xpath("devices/disk//backingStore|devices/disk//seclabel|devices/disk//dataStore"), [])
self.assertEqual(tree.xpath("devices/disk[@device='cdrom']/source"), []) # the old VM's cloud-init seed
self.assertEqual(len(tree.xpath("devices/disk[@device='cdrom']")), 2) # the drives stay
self.assertEqual(tree.findtext("os/nvram"), "/var/lib/libvirt/qemu/nvram/vm1-restored_VARS.fd")
self.assertEqual(tree.xpath("devices/interface/link/@state"), ["down", "down"])
self.assertNotEqual(tree.xpath("string(devices/graphics/@passwd)"), "secret1")
self.assertEqual(tree.findtext("title"), "web")
def test_media_are_ejected(self):
# not in the backup: the restore host may not have them, and the VM would not start
tree = self.restored()
self.assertEqual(tree.xpath("devices/disk[@device='cdrom' or @device='floppy']/source"), [])
self.assertEqual(len(tree.xpath("devices/disk[@device='cdrom' or @device='floppy']")), 3)
def test_links_kept_when_connected(self):
self.assertEqual(self.restored(connect=True).xpath("devices/interface/link/@state"), ["up"])
class RestoreHostTestCase(unittest.TestCase):
def conn(self):
conn = MagicMock()
conn.get_networks.return_value = ["default"]
conn.get_ifaces.return_value = ["br0", "eth0"]
conn.get_nwfilters.return_value = ["clean-traffic"]
conn.wvm.getDomainCapabilities.return_value = (
"<domainCapabilities><os><loader><value>/usr/share/edk2/ovmf/OVMF_CODE.fd</value></loader></os></domainCapabilities>")
return conn
def check(self, conn, *texts):
from vrtManager.backup import check_restore_host
if not texts:
check_restore_host(conn, SOURCE)
return
with self.assertRaises(util.OperationError) as caught:
check_restore_host(conn, SOURCE)
for text in texts:
self.assertIn(text, str(caught.exception))
def test_a_host_that_offers_everything(self):
conn = self.conn()
self.check(conn)
conn.wvm.getDomainCapabilities.assert_called_once_with("/usr/libexec/qemu-kvm", "x86_64", "pc-q35-10.1", "kvm", 0)
def test_a_missing_network_is_refused(self):
conn = self.conn()
conn.get_networks.return_value = []
self.check(conn, "network default")
def test_a_missing_bridge_is_refused(self):
conn = self.conn()
conn.get_ifaces.return_value = ["eth0"]
self.check(conn, "br0")
def test_a_missing_filter_is_refused(self):
conn = self.conn()
conn.get_nwfilters.return_value = []
self.check(conn, "clean-traffic")
def test_an_emulator_or_machine_the_host_lacks_is_refused(self):
conn = self.conn()
conn.wvm.getDomainCapabilities.side_effect = error(libvirt.VIR_ERR_INVALID_ARG)
self.check(conn, "/usr/libexec/qemu-kvm", "pc-q35-10.1")
def test_a_firmware_the_host_lacks_is_refused(self):
conn = self.conn()
conn.wvm.getDomainCapabilities.return_value = "<domainCapabilities><os><loader/></os></domainCapabilities>"
self.check(conn, "OVMF_CODE.fd")