1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00
webvirtcloud/storages/tests/test_live_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

62 lines
2.8 KiB
Python

"""Live check of a volume download from the storage page: the bytes are the
volume file's, and a volume a running VM uses is refused. Runs only with
TEST_LIBVIRT_HOST set."""
import os
import unittest
from computes.models import Compute
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 vrtManager.connection import connection_manager
from vrtManager.tests import livetest
P = livetest.PREFIX
@unittest.skipUnless(livetest.enabled(), "Set TEST_LIBVIRT_HOST to run the live libvirt tests")
class LiveDownloadTestCase(TestCase):
def setUp(self):
args = dict(
hostname=os.environ["TEST_LIBVIRT_HOST"],
login=os.environ.get("TEST_LIBVIRT_LOGIN", ""),
password=os.environ.get("TEST_LIBVIRT_PASSWORD", ""),
type=int(os.environ.get("TEST_LIBVIRT_TYPE", 1)),
)
self.conn = connection_manager.get_connection(*args.values())
livetest.ensure_pool(self.conn)
self.addCleanup(livetest.cleanup, self.conn)
self.compute = Compute.objects.create(name="live-download", **args)
self.client.force_login(get_user_model().objects.create_superuser("live-dl", "ld@example.com", "x"))
def download(self, name):
return self.client.get(reverse("download_volume", args=[self.compute.id, livetest.POOL, name]))
def test_the_download_is_the_volume_file(self):
vol = self.conn.storageVolLookupByPath(livetest.create_volume(self.conn, P + "dl", 1, "raw"))
data = os.urandom(1 << 20)
stream = self.conn.newStream(0)
vol.upload(stream, 0, len(data), 0)
stream.send(data)
stream.finish()
response = self.download(P + "dl.raw")
self.assertEqual(response["Content-Length"], str(len(data)))
self.assertEqual(response["Content-Disposition"], f'attachment; filename="{P}dl.raw"')
self.assertEqual(b"".join(response.streaming_content), data)
def test_a_qcow2_volume_comes_as_its_file(self):
vol = self.conn.storageVolLookupByPath(livetest.create_volume(self.conn, P + "dl", 64, "qcow2"))
content = b"".join(self.download(P + "dl.qcow2").streaming_content)
self.assertEqual(content[:4], b"QFI\xfb")
self.assertEqual(len(content), vol.infoFlags(1)[2]) # the file, not the 64 MiB the guest sees
def test_a_running_vm_s_volume_is_refused(self):
path = livetest.create_volume(self.conn, P + "dl", 64, "qcow2")
livetest.define_vm(self.conn, P + "dl-vm", [path]).create()
response = self.download(P + "dl.qcow2")
self.assertRedirects(response, reverse("storage", args=[self.compute.id, livetest.POOL]),
fetch_redirect_response=False)
self.assertIn("shut them down", str(list(get_messages(response.wsgi_request))[0]))