1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00
webvirtcloud/instances/tests/test_create_rollback.py
catborise 590b8d956d fix(instances): a create whose definition answer was lost keeps the VM's disk
A definition can go through on the host while its answer is lost, as when
the connection drops right then. The create job took every failed
definition for none and deleted the new disk and seed, so the VM could be
left defined without them, and recovery then reported it created.

The job now asks the host for the VM's UUID first. Only when the host says
there is no such VM do the new volumes go; otherwise they stay and recovery
decides, recording the VM it finds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
2026-11-11 10:30:54 +03:00

61 lines
2.9 KiB
Python

from types import SimpleNamespace
from unittest.mock import patch
from appsettings.settings import get_settings
from computes.models import Compute
from django.contrib.auth import get_user_model
from django.test import TestCase
from libvirt import VIR_ERR_NO_DOMAIN, libvirtError
from instances.jobs import CreateInstance
from operations import jobs
from operations.jobs import Context
from operations.models import Operation, acquire
PARAMS = {
"name": "rb-vm", "firmware": "BIOS", "vcpu": 1, "vcpu_mode": "host-model", "memory": 128,
"hdd_size": 1, "storage": "default", "mac": "52:54:00:aa:04:01", "networks": "default",
"nwfilter": "", "net_model": "virtio", "cache_mode": "none", "video": "vga",
"listener_addr": "0.0.0.0", "add_cdrom": "None", "add_input": "None", "virtio": True, "qemu_ga": False,
"arch": "x86_64", "machine": "q35",
}
class CreateRollbackTestCase(TestCase):
"""Volumes are removed only while the VM is not defined yet."""
def setUp(self):
get_settings()
self.admin = get_user_model().objects.create_superuser("rb_admin", "r@example.com", "pw")
self.compute = Compute.objects.create(name="rb", hostname="192.0.2.41", login="", password="", type=4)
def run_job(self, **conn_setup):
with patch("instances.jobs.wvmCreate") as wvm:
conn = wvm.return_value
conn.get_cache_modes.return_value = {"none": "Disable cache"}
conn.get_firmwares.return_value = ["BIOS", "UEFI", "UEFI Secure Boot"]
conn.get_dom_capabilities.return_value = {"cpu_modes": ["host-model"]}
conn.get_instances.return_value = []
conn.create_volume.return_value = "/pool/rb-vm.qcow2"
missing = libvirtError("no domain")
missing.get_error_code = lambda: VIR_ERR_NO_DOMAIN
conn.wvm.lookupByUUIDString.side_effect = missing # until the job defines the VM
op = jobs.submit("instance.create", self.admin, {**PARAMS, "compute": self.compute.pk}, "key-rollback1")
acquire("wvc-test:1:a", "wvc-test")
Operation.objects.filter(pk=op.pk).update(state=Operation.RUNNING, worker="wvc-test:1:a")
op.refresh_from_db()
for attr, value in conn_setup.items():
getattr(conn, attr).side_effect = value
with self.assertRaises(Exception):
CreateInstance().run(op, Context(op, SimpleNamespace(identity="wvc-test:1:a")))
return conn
def test_failed_definition_removes_the_new_volume(self):
conn = self.run_job(create_instance=libvirtError("rejected"))
conn.delete_volume.assert_called_once_with("/pool/rb-vm.qcow2")
def test_failure_after_the_definition_keeps_the_volume(self):
with patch("instances.jobs.Instance.objects.get_or_create", side_effect=RuntimeError("db down")):
conn = self.run_job()
conn.create_instance.assert_called_once()
conn.delete_volume.assert_not_called()