1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00
webvirtcloud/storages/tests/test_download.py
catborise c9fef349d5 feat(storages): a volume downloads from its pool page
The volume menu of a pool page gets Download: the host reads the volume
with libvirt's download stream and the panel passes it on in chunks, so
a disk image never sits in the panel's memory. The file is the volume as
stored: a qcow2 volume comes as its qcow2 file. A volume a running VM
uses is refused, as for a copy; the refusal is a message on the pool
page. Each download is a row in the audit log.

gunicorn's worker exits when a request runs past its timeout (600 s), to
end requests stuck on a host. A download of a large disk runs longer, so
each chunk sent starts the request's timeout again: a download that stops
sending is still ended. nginx passes the chunks on as they come
(X-Accel-Buffering: no) instead of spooling them to a file.
2026-11-15 00:31:42 +03:00

44 lines
2.3 KiB
Python

"""A volume download streams what the host reads, as a file; a refusal is a
message on the storage page; only a superuser downloads."""
from unittest.mock import 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 vrtManager.util import OperationError
class DownloadTestCase(TestCase):
def setUp(self):
self.compute = Compute.objects.create(name="dl", hostname="192.0.2.30", login="root", password="", type=1)
self.url = reverse("download_volume", args=[self.compute.id, "pool-a", "disk.qcow2"])
patcher = patch("storages.views.wvmStorage")
self.stg = patcher.start().return_value
self.addCleanup(patcher.stop)
self.client.force_login(get_user_model().objects.create_superuser("dl_admin", "a@example.com", "pw"))
def test_the_volume_comes_as_a_file(self):
self.stg.read_volume.return_value = (3, iter([b"ab", b"c"]))
with patch("storages.views.request_progressed") as progressed:
response = self.client.get(self.url)
self.assertEqual(b"".join(response.streaming_content), b"abc")
self.assertEqual(progressed.call_count, 2)
self.assertEqual((response["Content-Length"], response["Content-Disposition"], response["X-Accel-Buffering"]),
("3", 'attachment; filename="disk.qcow2"', "no"))
self.stg.read_volume.assert_called_once_with("disk.qcow2")
def test_a_refusal_is_a_message_on_the_storage_page(self):
self.stg.read_volume.side_effect = OperationError("The running VMs %(vms)s use volume %(name)s; shut them down to copy it",
vms="vm1", name="disk.qcow2")
response = self.client.get(self.url)
self.assertRedirects(response, reverse("storage", args=[self.compute.id, "pool-a"]), fetch_redirect_response=False)
self.assertIn("shut them down", str(list(get_messages(response.wsgi_request))[0]))
def test_only_a_superuser_downloads(self):
self.client.force_login(get_user_model().objects.create_user("dl_user", password="pw"))
self.assertNotEqual(self.client.get(self.url).status_code, 200)
self.stg.read_volume.assert_not_called()