1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/instances/tests/test_duplicate_devices.py
catborise 84d633af08 fix: a downloading volume is not put in a new VM or a CD-ROM; slow downloads read as bytes come
A download writes its volume under its final name, so the volume was
offered while half written. #685 kept a copy and an attach away from
it; a new VM and a CD-ROM still took it. The VM create job now claims
the volumes it is made from (a writable one exclusive, a template or a
CD-ROM image shared), and adding a CD-ROM or inserting media holds the
ISO's volume shared and reads the ISO again in that pool under the
claim (iso_location, iso_path_in). Each is refused with the download
named.

A claim's key holds a pool's and a volume's names: longer than 255
characters on PostgreSQL it failed the request; Claim.resource is text.

A pool that belongs to more than one backup target is not stopped while
a backup to any of them runs (it took the first target only).

The download read 1 MiB at a time: on a slow server a cancel and the
progress waited for a whole MiB. It reads what has come (read1).
2026-11-20 16:59:01 +03:00

129 lines
7.2 KiB
Python

"""A device form sent twice (a double click, a resent request) adds the
device once: libvirt takes the same disk file or MAC twice in the
persistent definition."""
from unittest.mock import MagicMock, patch
from django.contrib.auth import get_user_model
from django.contrib.messages import get_messages
from django.test import TestCase
from django.urls import reverse
from computes.models import Compute
from instances.models import Instance
from operations.claims import admit
from operations.models import Claim
from vrtManager import util
PATH = "/var/lib/libvirt/images/data.qcow2"
MAC = "52:54:00:aa:bb:cc"
class DuplicateDeviceTests(TestCase):
def setUp(self):
self.client.force_login(get_user_model().objects.create_superuser("dup-admin", "d@example.com", "pw"))
compute = Compute.objects.create(name="dup-c", hostname="192.0.2.40", login="root", password="", type=1)
self.vm = Instance.objects.create(compute=compute, name="dup-vm", uuid="12121212-0000-1111-2222-333333333333")
self.proxy = MagicMock()
self.proxy.get_disk_devices.return_value = []
self.proxy.get_media_devices.return_value = []
self.proxy.get_net_devices.return_value = []
self.proxy.get_disk_bus_types.return_value = ["virtio"]
self.proxy.get_cache_modes.return_value = {"default": "Default"}
patcher = patch("instances.models.wvmInstance", return_value=self.proxy)
patcher.start()
self.addCleanup(patcher.stop)
def post(self, name, data):
response = self.client.post(reverse(f"instances:{name}", args=[self.vm.pk]), data)
return [str(m) for m in get_messages(response.wsgi_request)]
@patch("instances.views.wvmStorage")
def test_a_disk_of_the_vm_is_not_attached_again(self, storage):
pool = storage.return_value
pool.get_volumes.return_value = ["data.qcow2"]
pool.get_type.return_value = "dir"
pool.get_volume_format_type.return_value = "qcow2"
pool.get_volume_type.return_value = "file"
pool.get_volume.return_value.path.return_value = PATH
form = {"selected_storage": "default", "vols": "data.qcow2", "bus": "virtio", "cache": "default"}
self.post("add_existing_vol", form)
self.proxy.attach_disk.assert_called_once()
# the first attach is in the persistent definition only (a shut-off VM)
self.proxy.get_disk_devices.side_effect = lambda config=False: [{"path": PATH, "dev": "vdb"}] if config else []
self.assertEqual(self.post("add_existing_vol", form), ["data.qcow2 is already a disk of this VM"])
self.proxy.attach_disk.assert_called_once()
@patch("instances.views.wvmStorage")
def test_a_volume_a_job_writes_is_not_attached(self, storage):
# a download holds the volume it writes: the VM would get a half-written disk; nothing is read before the claim
pool = storage.return_value
pool.get_volumes.return_value = ["data.qcow2"]
pool.get_volume_format_type.return_value = "qcow2"
pool.get_type.return_value = "dir"
pool.get_volume_type.return_value = "file"
pool.get_volume.return_value.path.return_value = PATH
# a copy reads it (shared): a running VM would write it meanwhile
admit({f"volume:{self.vm.compute_id}:default/data.qcow2": Claim.SHARED}, holder="other-node:1:1")
form = {"selected_storage": "default", "vols": "data.qcow2", "bus": "virtio", "cache": "default"}
self.assertEqual(self.post("add_existing_vol", form),
["A short operation is in progress on this resource; try again in a moment."])
self.proxy.attach_disk.assert_not_called()
pool.get_volume_format_type.assert_not_called()
@patch("instances.views.wvmStorage")
def test_a_backup_volume_is_not_attached(self, storage):
# the VM would write into the backup, and its deletion would remove the VM's disk
from vmbackups.models import Backup, BackupTarget, BackupTargetPool
target = BackupTarget.objects.create(name="nfs1")
BackupTargetPool.objects.create(target=target, compute=self.vm.compute, pool="b1", pool_uuid="u1")
backup = Backup.objects.create(target=target, vm_uuid="u", vm_name="vm1", compute_name="dup-c")
name = f"{backup.prefix}-vda.qcow2"
storage.return_value.get_volumes.return_value = [name]
form = {"selected_storage": "b1", "vols": name, "bus": "virtio", "cache": "default"}
self.assertEqual(self.post("add_existing_vol", form),
[f"Volume {name} belongs to a backup of vm1; delete the backup on the Backups page"])
self.proxy.attach_disk.assert_not_called()
def test_a_nic_with_the_mac_of_another_is_not_added(self):
form = {"add-net-network": "net:default", "add-net-mac": MAC, "add-net-model": "virtio"}
self.post("add_network", form)
self.proxy.add_network.assert_called_once()
self.proxy.get_net_devices.side_effect = lambda config=False: [{"mac": MAC}] if config else []
form["add-net-mac"] = MAC.upper()
self.assertEqual(self.post("add_network", form),
[f"The VM already has a network device with MAC {MAC.upper()}"])
self.proxy.add_network.assert_called_once()
def test_an_iso_a_download_writes_is_not_added_or_inserted(self):
self.proxy.iso_location.return_value = ("isos", "/var/lib/libvirt/isos/alma.iso")
admit({f"volume:{self.vm.compute_id}:isos/alma.iso": Claim.EXCLUSIVE}, holder="other-node:1:1")
busy = ["A short operation is in progress on this resource; try again in a moment."]
with patch("instances.views.cdrom_bus_choices", return_value=["sata"]):
self.assertEqual(self.post("add_cdrom", {"bus": "sata", "media": "alma.iso"}), busy)
self.assertEqual(self.post("mount_iso", {"mount_iso": "sda", "media": "alma.iso"})[-1:], busy)
self.proxy.attach_disk.assert_not_called()
self.proxy.mount_iso.assert_not_called()
def test_an_iso_is_read_again_in_the_pool_claimed(self):
# another pool's image of that name (a download there) is not inserted under this pool's claim
from vrtManager.instance import wvmInstance
vm = wvmInstance.__new__(wvmInstance)
with patch.object(wvmInstance, "iso_location", return_value=("other", "/other/alma.iso")), \
self.assertRaisesMessage(util.OperationError, "no longer in pool isos"):
vm.iso_path_in("alma.iso", "isos")
def test_an_iso_in_a_cdrom_of_the_vm_is_not_added_again(self):
iso = "/var/lib/libvirt/isos/alma.iso"
self.proxy.iso_location.return_value = ("isos", iso)
with patch("instances.views.cdrom_bus_choices", return_value=["sata"]):
form = {"bus": "sata", "media": "alma.iso"}
self.post("add_cdrom", form)
self.proxy.attach_disk.assert_called_once()
self.proxy.get_media_devices.side_effect = lambda config=False: [{"path": iso, "dev": "sdb"}] if config else []
self.assertEqual(self.post("add_cdrom", form), ["alma.iso is already in a CD-ROM of this VM"])
# an empty drive may be added more than once
self.post("add_cdrom", {"bus": "sata", "media": ""})
self.assertEqual(self.proxy.attach_disk.call_count, 2)