1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-09 03:36:48 +00:00

fix(instances): allow editing disks that have no driver format

A disk without driver/@type could not be edited: the empty format was
rejected, and edit_disk would have written type=''. An empty format is
now accepted and edit_disk leaves the type attribute out.
This commit is contained in:
catborise 2026-10-02 23:58:55 +03:00
parent 0f611e6383
commit 479faaa4ca
4 changed files with 36 additions and 5 deletions

View file

@ -304,3 +304,14 @@ class DiskViewsTenantIsolationTestCase(TestCase):
"bus": "virtio", "cache": "default"},
)
mock_create.return_value.create_volume.assert_not_called()
def test_edit_volume_accepts_a_disk_without_any_driver_format(self):
self.client.force_login(self.superuser)
no_format_disk = dict(VM_DISK, format=None)
with patch("instances.models.wvmInstance") as mock_wvm:
proxy = mock_wvm.return_value
self._mock_disk_options(proxy)
proxy.get_disk_devices.return_value = [no_format_disk]
self._edit_volume_post(vol_format="None")
proxy.edit_disk.assert_called_once()
self.assertEqual(proxy.edit_disk.call_args[0][6], "")

View file

@ -985,7 +985,8 @@ def edit_volume(request, pk):
io=io,
discard=discard,
zeroes=zeroes,
format=format,
# "": the disk has no driver type and keeps having none.
format=format or None,
serial=serial,
)
if target_dev not in [disk["dev"] for disk in instance.disks]:

View file

@ -913,12 +913,12 @@ class wvmInstance(wvmConnect):
additionals += f"detect_zeroes='{detect_zeroes_mode}' "
xml_disk = f"<disk type='{old_disk_type}' device='{old_disk_device}'>"
# A disk without a driver type keeps having none.
type_attr = f"type='{format}' " if format else ""
if old_disk_device == "cdrom":
xml_disk += f"<driver name='{old_driver_name}' type='{format}'/>"
xml_disk += f"<driver name='{old_driver_name}' {type_attr}/>"
elif old_disk_device == "disk":
xml_disk += (
f"<driver name='{old_driver_name}' type='{format}' {additionals}/>"
)
xml_disk += f"<driver name='{old_driver_name}' {type_attr}{additionals}/>"
if disk_el.get("type") == "file":
xml_disk += f"<source file='{source}'/>"

View file

@ -304,3 +304,22 @@ class TestChangeDiskBus(unittest.TestCase):
self.assertEqual(disk.find("source").get("name"), "pool/vm-disk")
self.assertEqual(disk.find("source/host").get("name"), "mon1")
self.assertEqual(disk.find("auth/secret").get("uuid"), "aaaa")
def test_edit_disk_without_format_omits_the_driver_type(self):
from lxml import etree
inst = wvmInstance.__new__(wvmInstance)
inst.instance = MagicMock()
inst._XMLDesc = MagicMock(
return_value=(
"<domain><devices><disk type='file' device='disk'>"
"<driver name='qemu'/><source file='/images/vm.img'/>"
"<target dev='vda' bus='virtio'/></disk></devices></domain>"
)
)
inst.edit_disk(
"vda", "/images/vm.img", False, False, "virtio", "", "",
"default", "default", "default", "default",
)
disk = etree.fromstring(inst.instance.updateDeviceFlags.call_args[0][0])
self.assertIsNone(disk.find("driver").get("type"))