mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-08 11:16:47 +00:00
The edit form posts net-*-<n> fields for NIC n and carries only the new MAC. The backend read index 0 and looked the NIC up by the new MAC, so editing a MAC detached nothing and attached a second NIC, and edits of any NIC but the first were applied with empty values. The form now also posts the current MAC (net-old-mac-<n>). The view reads the submitted NIC's fields, validates the new MAC and reports errors. The backend replaces the NIC with the old MAC, refuses an unknown one, and on a failed attach restores the old NIC and raises instead of reporting success. The view crashed with a TypeError without a Referer header; it now returns to the instance page, and a Referer from another site is ignored.
56 lines
2.2 KiB
Python
56 lines
2.2 KiB
Python
import unittest
|
|
from unittest.mock import MagicMock
|
|
|
|
from django.conf import settings
|
|
|
|
if not settings.configured:
|
|
settings.configure(MAC_OUI="52:54:10")
|
|
|
|
from libvirt import VIR_DOMAIN_AFFECT_CONFIG, libvirtError
|
|
from vrtManager.instance import wvmInstance
|
|
|
|
DOMAIN_XML = """<domain><devices>
|
|
<interface type='network'><mac address='52:54:00:00:00:01'/><source network='default'/><model type='virtio'/></interface>
|
|
<interface type='network'><mac address='52:54:00:00:00:02'/><source network='default'/><model type='virtio'/></interface>
|
|
</devices></domain>"""
|
|
|
|
|
|
def stopped_instance():
|
|
inst = wvmInstance.__new__(wvmInstance)
|
|
inst.instance = MagicMock()
|
|
inst._XMLDesc = MagicMock(return_value=DOMAIN_XML)
|
|
inst.get_status = MagicMock(return_value=5)
|
|
return inst
|
|
|
|
|
|
class TestChangeNetwork(unittest.TestCase):
|
|
def test_replaces_the_nic_with_the_old_mac(self):
|
|
inst = stopped_instance()
|
|
inst.change_network("52:54:00:00:00:02", "52:54:00:00:00:03", "default", "net", "e1000", "")
|
|
|
|
detached = inst.instance.detachDeviceFlags.call_args[0][0]
|
|
self.assertIn("52:54:00:00:00:02", detached)
|
|
attached = inst.instance.attachDeviceFlags.call_args[0][0]
|
|
self.assertIn("52:54:00:00:00:03", attached)
|
|
self.assertIn("e1000", attached)
|
|
|
|
def test_unknown_old_mac_adds_nothing(self):
|
|
inst = stopped_instance()
|
|
with self.assertRaises(libvirtError):
|
|
inst.change_network("52:54:00:00:00:09", "52:54:00:00:00:03", "default", "net", "virtio", "")
|
|
inst.instance.detachDeviceFlags.assert_not_called()
|
|
inst.instance.attachDeviceFlags.assert_not_called()
|
|
|
|
def test_failed_add_restores_the_old_nic(self):
|
|
inst = stopped_instance()
|
|
inst.instance.attachDeviceFlags.side_effect = [libvirtError("bad source"), 0]
|
|
with self.assertRaises(libvirtError):
|
|
inst.change_network("52:54:00:00:00:01", "52:54:00:00:00:03", "missing", "net", "virtio", "")
|
|
|
|
restored, flags = inst.instance.attachDeviceFlags.call_args[0]
|
|
self.assertIn("52:54:00:00:00:01", restored)
|
|
self.assertEqual(flags, VIR_DOMAIN_AFFECT_CONFIG)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|