mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-11 12:46:49 +00:00
test(snapshots): check the call that creates a snapshot, not a wrapper of it
_snapshotCreateXML only forwarded to the domain's snapshotCreateXML, and every snapshot test replaced it with a mock: a wrapper that called nothing passed all of them. The wrapper is gone and the tests assert on the domain's call, which the code makes directly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
parent
8eb619114c
commit
b737b1f8f7
3 changed files with 15 additions and 22 deletions
|
|
@ -1839,9 +1839,6 @@ class wvmInstance(wvmConnect):
|
|||
grew = True
|
||||
return files[len(tops):] + memory
|
||||
|
||||
def _snapshotCreateXML(self, xml, flag):
|
||||
self.instance.snapshotCreateXML(xml, flag)
|
||||
|
||||
def create_snapshot(self, name, desc=None):
|
||||
self._check_snapshot_state()
|
||||
if self.instance.snapshotListNames(VIR_DOMAIN_SNAPSHOT_LIST_EXTERNAL):
|
||||
|
|
@ -1864,7 +1861,7 @@ class wvmInstance(wvmConnect):
|
|||
xml += self._XMLDesc(VIR_DOMAIN_XML_SECURE)
|
||||
xml += """<active>0</active>
|
||||
</domainsnapshot>"""
|
||||
self._snapshotCreateXML(xml, 0)
|
||||
self.instance.snapshotCreateXML(xml, 0)
|
||||
|
||||
def create_external_snapshot(self, name, desc="", memory=False, quiesce=False, free_percent=10, free_min_gib=1):
|
||||
"""A disk-only snapshot, or with memory too (running VMs only). Every
|
||||
|
|
@ -1922,7 +1919,7 @@ class wvmInstance(wvmConnect):
|
|||
if quiesce:
|
||||
self.ping_agent()
|
||||
flags |= VIR_DOMAIN_SNAPSHOT_CREATE_QUIESCE
|
||||
self._snapshotCreateXML(xml, flags)
|
||||
self.instance.snapshotCreateXML(xml, flags)
|
||||
self._refresh_pools_after_snapshot()
|
||||
|
||||
def _snapshot_memory(self, name, disks):
|
||||
|
|
|
|||
|
|
@ -78,7 +78,6 @@ def proxy(status=1, internal=(), external=(), xml=DOMAIN, jobs=None, managed_sav
|
|||
p.get_uuid = lambda: "11111111-2222-3333-4444-555555555555"
|
||||
p.get_cur_memory = lambda: 1024
|
||||
p.refresh_instance_pools = MagicMock()
|
||||
p._snapshotCreateXML = MagicMock()
|
||||
p.wvm = MagicMock()
|
||||
dom = MagicMock()
|
||||
snaps = {s.getName(): s for s in (*internal, *external)}
|
||||
|
|
@ -242,7 +241,7 @@ class SnapshotStateTestCase(unittest.TestCase):
|
|||
with self.assertRaises(util.OperationError):
|
||||
act(p)
|
||||
p.instance.revertToSnapshot.assert_not_called()
|
||||
p._snapshotCreateXML.assert_not_called()
|
||||
p.instance.snapshotCreateXML.assert_not_called()
|
||||
|
||||
def test_an_internal_snapshot_is_reverted_to_on_a_shut_off_vm(self):
|
||||
for status in (1, 3):
|
||||
|
|
@ -257,7 +256,7 @@ DISK_ONLY_ATOMIC = libvirt.VIR_DOMAIN_SNAPSHOT_CREATE_DISK_ONLY | libvirt.VIR_DO
|
|||
|
||||
|
||||
def created(p):
|
||||
xml, flags = p._snapshotCreateXML.call_args[0]
|
||||
xml, flags = p.instance.snapshotCreateXML.call_args[0]
|
||||
return xml, flags
|
||||
|
||||
|
||||
|
|
@ -306,7 +305,7 @@ class CreateTestCase(unittest.TestCase):
|
|||
side_effect=libvirtError("guest agent didn't respond to command within '10' seconds")):
|
||||
with self.assertRaises(libvirtError):
|
||||
p.create_external_snapshot("q", quiesce=True)
|
||||
p._snapshotCreateXML.assert_not_called()
|
||||
p.instance.snapshotCreateXML.assert_not_called()
|
||||
|
||||
def test_quiesce_needs_a_running_vm_with_its_agent_and_no_memory(self):
|
||||
# libvirt accepts quiesce only for disk-only snapshots, through the agent
|
||||
|
|
@ -322,7 +321,7 @@ class CreateTestCase(unittest.TestCase):
|
|||
def refused(self, p, pattern, **kwargs):
|
||||
with self.assertRaisesRegex(util.OperationError, pattern):
|
||||
p.create_external_snapshot(kwargs.pop("name", "s"), **kwargs)
|
||||
p._snapshotCreateXML.assert_not_called()
|
||||
p.instance.snapshotCreateXML.assert_not_called()
|
||||
|
||||
def test_invalid_names_are_refused(self):
|
||||
for name in ("", ".hidden", "a/b", "a b", "x" * 65, "<x>"):
|
||||
|
|
@ -406,18 +405,18 @@ class CreateTestCase(unittest.TestCase):
|
|||
with self.assertRaisesRegex(util.OperationError, r"needs 4\.0 GiB \(20% of the VM's disks in it, at least 2 GiB"):
|
||||
p.create_external_snapshot("s", free_percent=20, free_min_gib=2)
|
||||
p.create_external_snapshot("s", free_percent=0, free_min_gib=0) # no check
|
||||
p._snapshotCreateXML.assert_called_once()
|
||||
p.instance.snapshotCreateXML.assert_called_once()
|
||||
|
||||
def test_a_volume_that_cannot_be_read_is_refused_not_skipped(self):
|
||||
p = proxy()
|
||||
p.get_volume_by_path = MagicMock(side_effect=libvirtError("permission denied"))
|
||||
with self.assertRaises(libvirtError):
|
||||
p.create_external_snapshot("s")
|
||||
p._snapshotCreateXML.assert_not_called()
|
||||
p.instance.snapshotCreateXML.assert_not_called()
|
||||
|
||||
def test_a_disk_in_no_pool_is_not_checked(self):
|
||||
p = self.space({"/pool/a.qcow2": ("default", 20 * GiB), "/elsewhere/b.qcow2": None}, {"default": 2 * GiB})
|
||||
p._snapshotCreateXML.assert_called_once()
|
||||
p.instance.snapshotCreateXML.assert_called_once()
|
||||
|
||||
|
||||
REVERT_RUNNING = libvirt.VIR_DOMAIN_SNAPSHOT_REVERT_RUNNING
|
||||
|
|
@ -694,7 +693,7 @@ class PoolRefreshTestCase(unittest.TestCase):
|
|||
p = proxy()
|
||||
p.refresh_instance_pools.side_effect = [None, libvirtError("pool busy")]
|
||||
p.create_external_snapshot("one")
|
||||
p._snapshotCreateXML.assert_called_once()
|
||||
p.instance.snapshotCreateXML.assert_called_once()
|
||||
|
||||
def test_create_is_refused_when_the_refresh_before_it_fails(self):
|
||||
# the free space is not known then
|
||||
|
|
@ -702,7 +701,7 @@ class PoolRefreshTestCase(unittest.TestCase):
|
|||
p.refresh_instance_pools.side_effect = libvirtError("pool busy")
|
||||
with self.assertRaisesRegex(util.OperationError, "free space is unknown: pool busy"):
|
||||
p.create_external_snapshot("one")
|
||||
p._snapshotCreateXML.assert_not_called()
|
||||
p.instance.snapshotCreateXML.assert_not_called()
|
||||
|
||||
def test_delete_and_revert_succeed_and_report_nothing_when_the_refresh_fails(self):
|
||||
for action in ("delete_external_snapshot", "revert_external_snapshot"):
|
||||
|
|
@ -737,7 +736,7 @@ class KindTestCase(unittest.TestCase):
|
|||
p = proxy(external=[fake_snap("e")])
|
||||
with self.assertRaisesRegex(util.OperationError, "external snapshots"):
|
||||
p.create_snapshot("i")
|
||||
p._snapshotCreateXML.assert_not_called()
|
||||
p.instance.snapshotCreateXML.assert_not_called()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
|
|
@ -56,15 +56,14 @@ class TestXmlSafety(unittest.TestCase):
|
|||
inst.get_snapshot_jobs = MagicMock(return_value=[])
|
||||
inst.get_status = MagicMock(return_value=5) # shutoff
|
||||
inst._XMLDesc = MagicMock(return_value="<domain></domain>")
|
||||
inst._snapshotCreateXML = MagicMock(return_value=None)
|
||||
|
||||
bad_name = "snap<inject>"es"
|
||||
bad_desc = "desc<tag>&and"
|
||||
|
||||
inst.create_snapshot(bad_name, bad_desc)
|
||||
|
||||
inst._snapshotCreateXML.assert_called_once()
|
||||
created_xml = inst._snapshotCreateXML.call_args[0][0]
|
||||
inst.instance.snapshotCreateXML.assert_called_once()
|
||||
created_xml = inst.instance.snapshotCreateXML.call_args[0][0]
|
||||
self.assertNotIn("<inject>", created_xml)
|
||||
self.assertNotIn("<tag>", created_xml)
|
||||
self.assertIn("<inject>&quotes", created_xml)
|
||||
|
|
@ -76,7 +75,6 @@ class TestXmlSafety(unittest.TestCase):
|
|||
inst.get_snapshot_jobs = MagicMock(return_value=[])
|
||||
inst._XMLDesc = MagicMock(return_value="<domain><os><loader type='rom'>/usr/share/rom.bin</loader></os></domain>")
|
||||
inst._defineXML = MagicMock()
|
||||
inst._snapshotCreateXML = MagicMock(return_value=None)
|
||||
inst.get_status = MagicMock(return_value=5)
|
||||
|
||||
inst.create_snapshot("snap_rom", "testing rom preservation")
|
||||
|
|
@ -108,12 +106,11 @@ class TestXmlSafety(unittest.TestCase):
|
|||
return_value="<domain><os><loader readonly='yes' type='pflash'>/usr/share/OVMF/OVMF.fd</loader></os></domain>"
|
||||
)
|
||||
inst._defineXML = MagicMock()
|
||||
inst._snapshotCreateXML = MagicMock()
|
||||
|
||||
inst.create_snapshot("snap", "uefi")
|
||||
|
||||
inst._defineXML.assert_not_called()
|
||||
self.assertIn("type='pflash'", inst._snapshotCreateXML.call_args[0][0])
|
||||
self.assertIn("type='pflash'", inst.instance.snapshotCreateXML.call_args[0][0])
|
||||
|
||||
|
||||
class TestCloneRollback(unittest.TestCase):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue