1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-01 16:36:06 +00:00

fix(instances): a disk form loaded before the disk changed changes nothing

The Edit, Detach and Delete forms of a VM's disk name it by its device:
the view acted on whatever disk had that device by then. A form loaded
before a disk move, an external snapshot or another tab's change wrote the
old path back over the new one (the VM then starts from an old or deleted
file), or detached and deleted the volume that replaced it.

The forms already send the path they show. The views now refuse a disk
whose path is not that one, and tell the user to reload the page. Turkish
and Chinese texts included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-11 12:39:40 +03:00
parent a53a3623d2
commit 34048dce83
10 changed files with 433 additions and 374 deletions

View file

@ -213,7 +213,7 @@ class DestroyOrderTestCase(TransactionTestCase):
proxy.paths_used_by_other_domains.return_value = set()
proxy.wait_disk_detached.return_value = True
storage.return_value.del_volume.side_effect = OperationError("Volume data.qcow2 is in use")
response = self.client.post(reverse("instances:delete_vol", args=[self.instance.id]), {"dev": "vdb"})
response = self.client.post(reverse("instances:delete_vol", args=[self.instance.id]), {"dev": "vdb", "path": "/pool/data.qcow2"})
self.assertEqual(response.status_code, 302)
proxy.detach_disk.assert_called_once_with("vdb")
messages = [str(m) for m in get_messages(response.wsgi_request)]

View file

@ -112,7 +112,7 @@ class DiskViewsTenantIsolationTestCase(TestCase):
mock_wvm.return_value.get_managed_save_image.return_value = 0
res = self._post(
"delete_vol",
{"storage": "other-pool", "name": "victim.qcow2", "dev": "vda"},
{"storage": "other-pool", "name": "victim.qcow2", "dev": "vda", "path": VM_DISK["path"]},
)
self.assertEqual(res.status_code, 302)
mock_wvm.return_value.detach_disk.assert_called_once_with("vda")
@ -149,6 +149,7 @@ class DiskViewsTenantIsolationTestCase(TestCase):
{
"edit_volume": "1",
"dev": "vda",
"path": VM_DISK["path"],
"vol_path": VM_DISK["path"],
"vol_bus_old": "virtio",
"vol_bus": "virtio",
@ -276,6 +277,7 @@ class DiskViewsTenantIsolationTestCase(TestCase):
{
"edit_volume": "1",
"dev": "vda",
"path": VM_DISK["path"],
"vol_path": VM_DISK["path"],
"vol_bus_old": "virtio",
"vol_bus": "virtio",
@ -297,6 +299,7 @@ class DiskViewsTenantIsolationTestCase(TestCase):
data = {
"edit_volume": "1",
"dev": "vda",
"path": VM_DISK["path"],
"vol_path": VM_DISK["path"],
"vol_bus_old": "virtio",
"vol_bus": "virtio",
@ -306,6 +309,34 @@ class DiskViewsTenantIsolationTestCase(TestCase):
data.update(extra)
return self._post("edit_volume", data)
def test_a_form_of_a_disk_replaced_since_changes_nothing(self):
# another tab moved vda to another volume after this page was loaded
moved = {**VM_DISK, "path": "/var/lib/libvirt/images/moved.qcow2"}
self.client.force_login(self.superuser)
for name in ("edit_volume", "detach_vol", "delete_vol"):
with self.subTest(name), patch("instances.models.wvmInstance") as mock_wvm, \
patch("instances.views.wvmStorage") as mock_storage:
proxy = mock_wvm.return_value
proxy.get_disk_devices.return_value = [moved]
res = self._post(name, {"edit_volume": "1", "dev": "vda", "path": VM_DISK["path"],
"vol_path": VM_DISK["path"], "vol_bus": "virtio"})
self.assertIn("Disk vda has changed since the page was loaded; reload the page",
[str(m) for m in get_messages(res.wsgi_request)])
proxy.edit_disk.assert_not_called()
proxy.detach_disk.assert_not_called()
mock_storage.return_value.del_volume.assert_not_called()
def test_a_disk_without_a_path_is_detached(self):
# a network disk with only a host (NBD): the form posts an empty path
self.client.force_login(self.superuser)
with patch("instances.models.wvmInstance") as mock_wvm:
mock_wvm.return_value.get_disk_devices.return_value = [{**VM_DISK, "path": None}]
self._post("detach_vol", {"dev": "vda", "path": ""})
mock_wvm.return_value.detach_disk.assert_called_once_with("vda")
mock_wvm.return_value.detach_disk.reset_mock()
self._post("detach_vol", {"dev": "vda"}) # a form without the path
mock_wvm.return_value.detach_disk.assert_not_called()
def test_edit_volume_bus_change_keeps_the_disk_and_applies_options(self):
from vrtManager.instance import wvmInstance

View file

@ -201,7 +201,7 @@ class PendingEditsTestCase(TestCase):
proxy = wvm.return_value
live, pending = disk("vda", "default", ""), {**disk("vda", "default", ""), "path": "/pool/b.qcow2"}
proxy.get_disk_devices.side_effect = lambda **kw: [pending] if kw.get("config") else [live]
self.client.post(reverse("instances:delete_vol", args=[self.instance.id]), {"dev": "vda"})
self.client.post(reverse("instances:delete_vol", args=[self.instance.id]), {"dev": "vda", "path": "/pool/b.qcow2"})
proxy.detach_disk.assert_not_called()
storage.return_value.del_volume.assert_not_called()
@ -211,7 +211,7 @@ class PendingEditsTestCase(TestCase):
proxy = wvm.return_value
proxy.get_managed_save_image.return_value = 1
proxy.get_disk_devices.side_effect = lambda **kw: [disk("vda", "default", "")]
response = self.client.post(reverse("instances:delete_vol", args=[self.instance.id]), {"dev": "vda"})
response = self.client.post(reverse("instances:delete_vol", args=[self.instance.id]), {"dev": "vda", "path": "/pool/vda.qcow2"})
self.assertEqual([str(m) for m in get_messages(response.wsgi_request)],
["The VM has a saved state; start it or remove the saved state first"])
proxy.detach_disk.assert_not_called()

View file

@ -150,9 +150,9 @@ class VMRowTests(TestCase):
self.proxy.paths_used_by_other_domains.return_value = set()
with patch("instances.views.wvmStorage"):
self.proxy.wait_disk_detached.return_value = False
self.post("delete_vol", {"dev": "vdb"})
self.post("delete_vol", {"dev": "vdb", "path": "/pool/data.qcow2"})
self.proxy.wait_disk_detached.return_value = True
self.post("delete_vol", {"dev": "vdb"})
self.post("delete_vol", {"dev": "vdb", "path": "/pool/data.qcow2"})
self.assertEqual(self.rows(), [
("VM vm: detach disk vdb", True),
("VM vm: delete volume default/data.qcow2: the guest has not released the disk", False),