diff --git a/instances/jobs.py b/instances/jobs.py index 0ac2fb53..34ed1ac8 100644 --- a/instances/jobs.py +++ b/instances/jobs.py @@ -987,6 +987,15 @@ class CreateInstance(Kind): tpm_model=conn.get_tpm_model(data["arch"], data["machine"]) if data["tpm"] else None, ) except Exception as err: + # a definition whose answer was lost may have gone through: then, or + # when the host cannot tell, the volumes stay and recovery decides + try: + conn.wvm.lookupByUUIDString(op.intent["uuid"]) + defined = True + except libvirtError as lookup: + defined = lookup.get_error_code() != VIR_ERR_NO_DOMAIN + if defined: + raise # the VM is not defined: its new volumes go leftovers = [] for path in created: diff --git a/instances/tests/test_cloud_init_create.py b/instances/tests/test_cloud_init_create.py index 82a8a025..f70f0148 100644 --- a/instances/tests/test_cloud_init_create.py +++ b/instances/tests/test_cloud_init_create.py @@ -31,6 +31,9 @@ class CloudInitCreateTestCase(TransactionTestCase): patcher = patch("instances.jobs.wvmCreate") self.conn = patcher.start().return_value self.addCleanup(patcher.stop) + missing = libvirt.libvirtError("no domain") + missing.get_error_code = lambda: libvirt.VIR_ERR_NO_DOMAIN + self.conn.wvm.lookupByUUIDString.side_effect = missing # until the job defines the VM self.conn.get_instances.return_value = [] self.conn.get_cache_modes.return_value = {"none": "Disable cache"} self.conn.get_firmwares.return_value = ["BIOS", "UEFI", "UEFI Secure Boot"] diff --git a/instances/tests/test_create_job.py b/instances/tests/test_create_job.py index ecdedd01..4e6e26d7 100644 --- a/instances/tests/test_create_job.py +++ b/instances/tests/test_create_job.py @@ -36,6 +36,9 @@ class CreateJobTestCase(TransactionTestCase): self.conn.get_dom_capabilities.return_value = {"cpu_modes": ["host-model", "custom"], "cpu_custom_models": ["Skylake-Server-noTSX-IBRS"]} self.conn.create_volume.return_value = "/srv/default/api-vm.qcow2" + missing = libvirt.libvirtError("no domain") + missing.get_error_code = lambda: libvirt.VIR_ERR_NO_DOMAIN + self.conn.wvm.lookupByUUIDString.side_effect = missing # until the job defines the VM def test_the_api_queues_the_create_and_the_worker_defines_and_records_it(self): url = f"/api/v1/computes/{self.compute.pk}/instances/create/x86_64/q35/" @@ -118,6 +121,21 @@ class CreateJobTestCase(TransactionTestCase): "Remove these volumes by hand: /srv/default/api-vm.qcow2") self.assertIn("RuntimeError: no such bus", traceback_of(raised.exception)) + def test_a_definition_whose_answer_was_lost_keeps_the_disk(self): + # it may have gone through: recovery records the VM, its disk with it + self.client.post(f"/api/v1/computes/{self.compute.pk}/instances/create/x86_64/q35/", BODY, + content_type="application/json") + op = Operation.objects.get() + 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() + self.conn.create_instance.side_effect = libvirt.libvirtError("End of file while reading data") + for lookup in (None, libvirt.libvirtError("Cannot recv data")): # defined; the host cannot tell + self.conn.wvm.lookupByUUIDString.side_effect = lookup + with self.assertRaisesMessage(libvirt.libvirtError, "End of file while reading data"): + CreateInstance().run(op, Context(op, SimpleNamespace(identity="wvc-test:1:a"))) + self.conn.delete_volume.assert_not_called() + @patch("instances.jobs.wvmConnect") def test_a_create_that_left_nothing_failed(self, conn): op = Operation(params={**BODY, "template": ""}, diff --git a/instances/tests/test_create_rollback.py b/instances/tests/test_create_rollback.py index 0c5d4b5a..12c689e9 100644 --- a/instances/tests/test_create_rollback.py +++ b/instances/tests/test_create_rollback.py @@ -5,7 +5,7 @@ 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 libvirtError +from libvirt import VIR_ERR_NO_DOMAIN, libvirtError from instances.jobs import CreateInstance from operations import jobs @@ -37,6 +37,9 @@ class CreateRollbackTestCase(TestCase): 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")