1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

fix: a volume a job writes is not copied, attached or its pool stopped; download progress each second

A download into a pool listed its volume while it was written. The page
let a superuser copy it (the copy got a half-written file, live), attach
it to a VM, or stop the pool under it. Now:

- the copy job claims its source volume shared, and the worker refuses
  a copy whose name the source's format changed before the claim;
- attaching an existing volume holds it exclusive from its first read,
  so neither a download nor a copy is under way, and its format is read
  after the download ended;
- stopping or deleting a pool (page and API) holds the pool exclusive,
  and its backup target's claim if it is one; restores claim the pools
  they write into shared.

Each is refused with the job in the way named, as a volume delete
already was.

The download recorded its progress every 16 MiB, so a slow server's
bar stayed empty for a long time; it records it each second, with the
cancel check. The worker checks the name is still free before it
starts. A snapshot name or tag typed in the VM list's bulk form keeps
the list from reloading, as a chosen VM did.
This commit is contained in:
catborise 2026-11-20 15:12:20 +03:00
parent 7c8f5b3150
commit 7e70595fd2
19 changed files with 588 additions and 450 deletions

View file

@ -14,6 +14,8 @@ 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 operations.claims import admit
from operations.models import Claim
from rest_framework.test import APIClient
NODE = "<node><pool type='dir'><name>use-pool</name><target><path>/use-pool</path></target></pool></node>"
@ -65,6 +67,28 @@ class PoolInUseTests(TestCase):
self.assertIn("running VMs api-user", response.json()["detail"])
self.assertTrue(self.pool.isActive())
def test_a_pool_a_job_works_in_is_not_stopped_or_deleted(self):
# a download or a copy in it: its file would be left half-written, its cleanup could not see it
admit({f"pool:{self.compute.id}:use-pool": Claim.SHARED}, holder="other-node:1:1")
for action in ("stop", "delete"):
with self.subTest(action=action):
self.assertIn("A short operation is in progress on this resource; try again in a moment.", self.post(action))
self.assertTrue(self.pool.isActive())
api = APIClient()
api.force_login(self.admin)
response = api.post(reverse("compute-storage-stop", kwargs={"compute_pk": self.compute.id, "pk": "use-pool"}))
self.assertEqual(response.status_code, 409)
self.assertTrue(self.pool.isActive())
def test_a_backup_target_pool_is_not_stopped_during_a_backup(self):
from vmbackups.models import BackupTarget, BackupTargetPool
target = BackupTarget.objects.create(name="nfs-b")
BackupTargetPool.objects.create(target=target, compute=self.compute, pool="use-pool", pool_uuid="u1")
admit({f"backup_target:{target.pk}": Claim.EXCLUSIVE}, holder="other-node:1:1") # a backup writes to it
self.assertIn("A short operation is in progress on this resource; try again in a moment.", self.post("stop"))
self.assertTrue(self.pool.isActive())
def test_a_pool_a_vm_definition_names_is_not_deleted(self):
self.conn.defineXML(domain("by-name", "type='volume'><source pool='use-pool' volume='v1.img'/>"))
self.conn.defineXML(domain("by-path", f"type='file'><source file='{self.path}'/>"))

View file

@ -10,6 +10,7 @@ from django.urls import reverse
from computes.models import Compute
from operations.jobs import Context
from operations.claims import admit
from operations.models import Claim, Operation, acquire
from libvirt import libvirtError
from storages.jobs import CloneVolume
@ -39,8 +40,9 @@ class VolumeCloneJobTestCase(TransactionTestCase):
self.assertRedirects(response, url, fetch_redirect_response=False)
op = Operation.objects.get()
self.assertEqual((op.kind, op.intent["target"]), ("volume.clone", "copy.qcow2"))
self.assertIn((f"volume:{self.compute.pk}:pool-a/copy.qcow2", "exclusive"),
set(Claim.objects.values_list("resource", "mode")))
self.assertLessEqual({(f"volume:{self.compute.pk}:pool-a/copy.qcow2", "exclusive"),
(f"volume:{self.compute.pk}:pool-a/base.qcow2", "shared")},
set(Claim.objects.values_list("resource", "mode")))
self.stg.clone_volume.assert_not_called()
acquire("wvc-test:1:a", "wvc-test")
@ -74,6 +76,18 @@ class VolumeCloneJobTestCase(TransactionTestCase):
self.assertContains(response, "The running VMs vm use volume base.qcow2")
self.assertFalse(Operation.objects.exists())
@patch("storages.views.wvmStorage")
def test_a_volume_a_job_writes_is_not_copied(self, page):
# a download holds the volume it writes: the copy waits for it, not a half-written file
page.return_value.is_active.return_value = False
page.return_value.get_size.return_value = (1, 1)
url = reverse("storage", args=[self.compute.pk, "pool-a"])
admit({f"volume:{self.compute.pk}:pool-a/base.qcow2": Claim.EXCLUSIVE}, holder="other-node:1:1")
response = self.client.post(url, {"cln_volume": "1", "image": "base.qcow2", "name": "copy",
"submission_key": "key-vol00005"}, follow=True)
self.assertContains(response, "try again in a moment")
self.assertFalse(Operation.objects.exists())
def test_recovery(self):
self.stg.get_volumes.return_value = ["base.qcow2", "copy.qcow2"]
op = Operation(params={"compute": self.compute.pk, "pool": "pool-a"}, intent={"target": "copy.qcow2"})
@ -89,6 +103,10 @@ class VolumeCloneJobTestCase(TransactionTestCase):
params={"compute": self.compute.pk, "pool": "pool-a", "image": "base.qcow2", "name": "copy",
"format": "", "prealloc": False})
CloneVolume().precheck(op, None)
self.stg.clone_name.return_value = "copy.img" # the source's format changed before the claim (a download ended)
with self.assertRaisesMessage(libvirtError, "The volume changed since the copy was asked"):
CloneVolume().precheck(op, None)
self.stg.clone_name.return_value = "copy.qcow2"
self.stg.get_volumes.return_value = ["base.qcow2", "copy.qcow2"] # made outside the panel meanwhile
with self.assertRaisesMessage(libvirtError, "Name of volume already in use"):
CloneVolume().precheck(op, None)

View file

@ -139,6 +139,12 @@ class FetchJobTestCase(TransactionTestCase):
self.run_job(op, [b"ab"])
self.assertEqual(recorded, [True])
def test_the_worker_checks_the_name_is_still_free(self):
op = self.queue()
self.stg.get_volumes.return_value = ["rocky-10.qcow2"] # made outside the panel meanwhile
with self.assertRaisesRegex(util.OperationError, "has a volume named"):
FetchVolume().precheck(op, None)
def test_a_deactivated_superuser_s_job_does_not_run(self):
op = self.queue()
op.user.is_active = False
@ -147,6 +153,22 @@ class FetchJobTestCase(TransactionTestCase):
with self.assertRaises(PermissionDenied):
FetchVolume().precheck(op, None)
def test_the_progress_is_recorded_each_second(self):
# a slow server: the bar moves before 16 MiB came
op = self.queue()
seen = []
def write(name, size, chunks):
for _chunk in chunks:
seen.append(Operation.objects.get(pk=op.pk).intent["done"])
return size
self.stg.write_volume.side_effect = write
clock = iter(range(0, 100, 2))
with patch("storages.jobs.time.monotonic", side_effect=lambda: next(clock)):
self.run_job(op, [b"ab", b"cd", b"ef"])
self.assertEqual(seen, [2, 4, 6])
def test_more_than_the_size_told_is_not_written(self):
op = self.queue()
self.stg.write_volume.side_effect = lambda name, size, chunks: sum(len(c) for c in chunks)