1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 19:26:48 +00:00
webvirtcloud/vrtManager/tests/test_clone_nvram.py
catborise c55f52f025 test: each app keeps its tests in a tests package
131 test modules sat next to the code they test; instances alone had
51 of them beside views.py and utils.py. They move into a
tests/ package per app, and the JavaScript checks into
webvirtcloud/tests/js. Each non-empty tests.py becomes tests/test_views.py;
the empty generated stubs are removed.

Imports of the app's own modules become absolute, paths built from
__file__ go one level up, the live helper is instances.tests.livetest,
and coverage omits */tests/*. The suite discovers the same 960 tests as
before.
2026-10-12 15:17:30 +03:00

68 lines
3 KiB
Python

"""A clone of a UEFI VM gets its own copy of the VM's NVRAM, with the
original's format, owner and mode, wherever the definition names the file."""
import contextlib
import unittest
from unittest.mock import MagicMock
from django.conf import settings
if not settings.configured:
settings.configure(MAC_OUI="52:54:10")
from lxml import etree
from vrtManager import util
from vrtManager.instance import wvmInstance
ORIGINAL = """<volume><name>vm_VARS.qcow2</name><key>k</key><capacity>540672</capacity>
<target><path>/var/lib/libvirt/qemu/nvram/vm_VARS.qcow2</path><format type='qcow2'/>
<permissions><mode>0600</mode><owner>107</owner><group>107</group></permissions></target></volume>"""
def domain(nvram):
return f"""<domain><name>vm</name><uuid>u</uuid><os><type>hvm</type>
<loader readonly='yes' type='pflash'>/usr/share/edk2/ovmf/OVMF_CODE.fd</loader>{nvram}</os><devices/></domain>"""
def proxy(nvram):
p = wvmInstance.__new__(wvmInstance) # no connection: only these calls
p.get_status = lambda: 5
p.get_instances = lambda: []
p._XMLDesc = lambda flags: domain(nvram)
p._defineXML = MagicMock()
p.pool = MagicMock()
p.pool.storageVolLookupByName.return_value.XMLDesc.return_value = ORIGINAL
p.pool.createXMLFrom.return_value.path.return_value = "/var/lib/libvirt/qemu/nvram/copy_VARS.qcow2"
p._nvram_pool = lambda: contextlib.nullcontext(p.pool)
return p
class CloneNvramTestCase(unittest.TestCase):
def test_the_copy_keeps_the_format_owner_and_mode(self):
p = proxy("<nvram format='qcow2'>/var/lib/libvirt/qemu/nvram/vm_VARS.qcow2</nvram>")
p.clone_instance({"name": "copy"})
xml, original, _ = p.pool.createXMLFrom.call_args.args
volume = etree.fromstring(xml)
self.assertEqual(volume.findtext("name"), "copy_VARS.qcow2")
self.assertEqual(volume.find("target/format").get("type"), "qcow2")
self.assertEqual(volume.findtext("target/permissions/owner"), "107")
self.assertIsNone(volume.find("target/path"))
self.assertIs(original, p.pool.storageVolLookupByName.return_value)
(defined,), _ = p._defineXML.call_args
self.assertEqual(etree.fromstring(defined).findtext("os/nvram"), "/var/lib/libvirt/qemu/nvram/copy_VARS.qcow2")
def test_an_nvram_source_element_points_at_the_copy(self):
p = proxy("<nvram type='file'>\n <source file='/var/lib/libvirt/qemu/nvram/vm_VARS.qcow2'/>\n</nvram>")
p.clone_instance({"name": "copy"})
p.pool.storageVolLookupByName.assert_called_once_with("vm_VARS.qcow2")
(defined,), _ = p._defineXML.call_args
self.assertEqual(
etree.fromstring(defined).find("os/nvram/source").get("file"), "/var/lib/libvirt/qemu/nvram/copy_VARS.qcow2"
)
def test_an_nvram_that_is_not_a_file_is_refused_before_any_copy(self):
p = proxy("<nvram type='network'><source protocol='iscsi' name='x'/></nvram>")
with self.assertRaises(util.OperationError):
p.clone_instance({"name": "copy"})
p.pool.createXMLFrom.assert_not_called()