mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-01 16:36:06 +00:00
fix(instances): a new VM cannot take the MAC address of another VM on its host
The create form and API took a MAC address for a new VM's NIC without looking at the host's other VMs, so a VM could be defined with a MAC another one has. Running on one network, neither NIC is reached reliably. A clone already refused this among its own NICs. The create now refuses a MAC a NIC of a VM on the host has, in any form the field takes (case, colons), including a NIC added for a VM's next boot only. The worker checks again before it defines the VM, since another VM may take the address while the job is queued. Turkish and Chinese texts included. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
parent
590b8d956d
commit
9fa898a856
6 changed files with 376 additions and 333 deletions
|
|
@ -187,6 +187,23 @@ class CreateJobTestCase(TransactionTestCase):
|
|||
with self.assertRaises(PermissionDenied):
|
||||
CreateInstance().precheck(op, None)
|
||||
|
||||
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>"
|
||||
other = MagicMock()
|
||||
other.isPersistent.return_value = 1
|
||||
# its NIC is added for its next boot only
|
||||
other.XMLDesc.side_effect = lambda flags: nic % "52:54:00:aa:bb:cc" if flags else "<domain/>"
|
||||
self.conn.wvm.listAllDomains.return_value = [other]
|
||||
response = self.client.post(url, {**BODY, "mac": "52:54:00:AA:BB:CC"}, content_type="application/json")
|
||||
self.assertContains(response, "MAC address 52:54:00:aa:bb:cc is a NIC's of another VM", status_code=400)
|
||||
response = self.client.post(url, {**BODY, "mac": "52:54:00:aa:bb:cd"}, content_type="application/json")
|
||||
self.assertEqual(response.status_code, 202)
|
||||
# another VM took it while the job was queued
|
||||
other.XMLDesc.side_effect = lambda flags: nic % "52:54:00:aa:bb:cd"
|
||||
with self.assertRaisesMessage(libvirt.libvirtError, "MAC address 52:54:00:aa:bb:cd is a NIC's of another VM"):
|
||||
CreateInstance().precheck(Operation.objects.get(), None)
|
||||
|
||||
def test_the_api_takes_a_body_without_a_mac_or_a_network_filter(self):
|
||||
body = {k: v for k, v in BODY.items() if k not in ("mac", "nwfilter")}
|
||||
response = self.client.post(f"/api/v1/computes/{self.compute.pk}/instances/create/x86_64/q35/", body,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue