1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

fix(networks): a network or interface in use is not stopped, and a new one is whole

An interface was stopped from the Interfaces page while running VMs had
cards on it, directly or through a network using it, and they were cut
off; a network in the same case was already refused. Stopping it is now
refused the same way, naming the VMs.

A new network whose autostart could not be set stayed defined and
running while the page said it failed; it is now stopped and undefined
like one that does not start, and a rollback that fails is said with the
first error. A network named "." or ".." is refused: its page address
was another page.

A VM create checks when it is asked and again when its job starts that
the host has the networks and the filter it names, and that no card is
left without a network: one deleted meanwhile, or an empty name in the
list, left a VM that does not start once its disks were made.
This commit is contained in:
catborise 2026-11-12 13:00:56 +03:00
parent 3308a75557
commit 534782fdda
14 changed files with 314 additions and 183 deletions

View file

@ -836,6 +836,18 @@ def _check_disk_name(conn, data):
raise util.OperationError(_("Image has already exist. Please check volumes or change instance name"))
def _check_networks(conn, data):
"""Each NIC's network and the filter must be the host's: the VM would
not start."""
networks = data["networks"].split(",")
if "" in networks:
raise util.OperationError(_("Each network card needs a network"))
for network in sorted(set(networks) - set(conn.get_networks())):
raise util.OperationError(_("This host has no network %(name)s"), name=network)
if data["nwfilter"] and data["nwfilter"] not in conn.get_nwfilters():
raise util.OperationError(_("This host has no network filter %(name)s"), name=data["nwfilter"])
def _check_macs(conn, data):
"""Two NICs of one MAC on a network reach neither reliably."""
try:
@ -902,6 +914,7 @@ class CreateInstance(Kind):
if util.get_xml_path(pool.XMLDesc(0), "/pool/@type") not in FILE_POOL_TYPES:
raise util.OperationError(_("The cloud-init seed needs a directory storage pool for the disk"))
_check_macs(conn, data)
_check_networks(conn, data)
if data["firmware"] and data["firmware"] not in conn.get_firmwares(data["arch"], data["machine"]):
raise util.OperationError(_("This host offers no firmware %(firmware)s"), firmware=data["firmware"])
if data["tpm"] and not conn.get_tpm_model(data["arch"], data["machine"]):
@ -925,6 +938,7 @@ class CreateInstance(Kind):
raise util.OperationError(_("A virtual machine with this name already exists"))
_check_disk_name(conn, op.params)
_check_macs(conn, op.params)
_check_networks(conn, op.params) # one deleted since the job was queued
def run(self, op, ctx):
data = op.params

View file

@ -35,6 +35,7 @@ class CloudInitCreateTestCase(TransactionTestCase):
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_networks.return_value = ["default"]
self.conn.get_cache_modes.return_value = {"none": "Disable cache"}
self.conn.get_firmwares.return_value = ["BIOS", "UEFI", "UEFI Secure Boot"]
self.conn.get_dom_capabilities.return_value = {"cpu_modes": ["host-model"]}

View file

@ -30,6 +30,8 @@ class CreateJobTestCase(TransactionTestCase):
self.conn = patcher.start().return_value
self.addCleanup(patcher.stop)
self.conn.get_instances.return_value = []
self.conn.get_networks.return_value = ["default"]
self.conn.get_nwfilters.return_value = ["clean-traffic"]
self.conn.get_cache_modes.return_value = {"none": "Disable cache"}
self.conn.get_firmwares.return_value = ["BIOS", "UEFI", "UEFI Secure Boot"]
self.conn.get_disk_bus_types.return_value = ["virtio", "sata"]
@ -187,6 +189,29 @@ class CreateJobTestCase(TransactionTestCase):
with self.assertRaises(PermissionDenied):
CreateInstance().precheck(op, None)
def test_a_network_or_filter_deleted_while_queued_is_refused(self):
# the VM would be defined with a card it cannot start with
response = self.client.post(f"/api/v1/computes/{self.compute.pk}/instances/create/x86_64/q35/",
{**BODY, "nwfilter": "clean-traffic"}, content_type="application/json")
self.assertEqual(response.status_code, 202)
op = Operation.objects.get()
CreateInstance().precheck(op, None)
self.conn.get_nwfilters.return_value = []
with self.assertRaisesMessage(libvirt.libvirtError, "This host has no network filter clean-traffic"):
CreateInstance().precheck(op, None)
self.conn.get_networks.return_value = []
with self.assertRaisesMessage(libvirt.libvirtError, "This host has no network default"):
CreateInstance().precheck(op, None)
def test_a_network_card_without_a_network_is_refused(self):
# else the VM would be defined with <source network=''/> once its disks are made
url = f"/api/v1/computes/{self.compute.pk}/instances/create/x86_64/q35/"
for networks in ("default,", ","):
with self.subTest(networks=networks):
response = self.client.post(url, {**BODY, "networks": networks}, content_type="application/json")
self.assertContains(response, "Each network card needs a network", status_code=400)
self.assertFalse(Operation.objects.exists())
def test_a_mac_of_another_vm_on_the_host_is_refused(self):
url = f"/api/v1/computes/{self.compute.pk}/instances/create/x86_64/q35/"
nic = "<domain><devices><interface><mac address='%s'/></interface></devices></domain>"

View file

@ -36,6 +36,7 @@ class CreateRollbackTestCase(TestCase):
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.get_networks.return_value = ["default"]
conn.create_volume.return_value = "/pool/rb-vm.qcow2"
missing = libvirtError("no domain")
missing.get_error_code = lambda: VIR_ERR_NO_DOMAIN