mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-10 12:16:49 +00:00
fix(networks): a network's addresses are read as libvirt writes them, and its changes keep each other
Reviewed with Codex against libvirt's network format and driver.
- Addresses: wvmNetwork found a network's <ip> by its text, so one
written as <ip .../> or <ip><dhcp>... without whitespace had none,
and a second address of a family overwrote the first. ip_networks
reads every <ip> in order, its index being libvirt's parentIndex:
family (ipv4 when written), address, subnet, DHCP (a <dhcp>, also
with fixed addresses only), first range and fixed addresses. The
parentIndex was guessed from the number of families, wrong when IPv6
comes first; changes now name the address by its index.
- One read: the page read the network's XML once per getter, ten or
more round trips; it reads it once (a cached_property of the
request's wvmNetwork). A change starts from the saved definition
read anew (VIR_NETWORK_XML_INACTIVE), not the running one: two QoS
changes on a running network no longer undo the first. The XML
editor shows and changes the saved definition, and defines this
network only (its name, and its UUID if given), read for that with
no entity expanded and handed to libvirt as written; an XML declaring
its encoding is taken. The actions go back to the network's page by
its own URL.
- Fixed addresses are found by the address they had (a hidden field),
not by the editable box: changing it and pressing Delete deleted
another entry or none. A change deletes the entry and adds it anew
from a copy, keeping what the page does not show (a client id, a
lease), and puts it back if the add fails: libvirt's own change
finds an entry by any of its MAC, address or name, so it could
replace another entry, and never by an IPv6 DUID. The address must
be of the subnet, not the host's, and neither it, the MAC or DUID
nor the name another entry's; an entry gone meanwhile is refused,
not added anew; an unchanged one writes no audit row. They can be
added while the network is stopped; a running network saved with
other addresses than it runs with is not changed live, as
parentIndex would name another <ip> of it.
- An address without a netmask or prefix takes libvirt's (IPv4 its
class's, IPv6 /64), for its checks and the overlap check.
- The DHCP range must be in the subnet, in order and not take in the
host's address.
- The create form takes a known mode only ("bogus" made an isolated
network, a missing one a server error); NAT and routed networks need
an IPv4 subnet, an isolated one may have none, a bridge or macvtap
network needs its bridge or interface and gets no addresses; IPv6
only with its box (whose value was "false", so never); no name or
subnet of another network of the host. "Fixed Addresses" at create
goes: it made a random-MAC entry for every address of the range,
for no real VM.
- Refusals of a fixed address, the DHCP range, the XML and a QoS
removal are messages, not server errors.
- The IPv6 forwarding of a bridge or macvtap network is the outside
network's, not ROUTE.
- get_networks_info reads each network's XML once and gives its
autostart and addresses too. Unused can_pxe, DHCP lease helpers,
get_uuid and the per-family range and host getters go.
This commit is contained in:
parent
befd1747df
commit
0c9a002ef4
19 changed files with 646 additions and 602 deletions
|
|
@ -1,36 +0,0 @@
|
|||
"""refresh_dhcp_leases must raise a real exception (handled as a libvirt error),
|
||||
not a bare string (which Python turns into TypeError, masking the cause)."""
|
||||
|
||||
import unittest
|
||||
from unittest.mock import Mock
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
if not settings.configured:
|
||||
settings.configure(MAC_OUI="52:54:10")
|
||||
|
||||
from vrtManager import util
|
||||
from vrtManager.network import wvmNetwork
|
||||
|
||||
|
||||
def network_with_leases(side_effect):
|
||||
net = wvmNetwork.__new__(wvmNetwork)
|
||||
net.leases = None
|
||||
net.net = Mock()
|
||||
net.net.DHCPLeases.side_effect = side_effect
|
||||
return net
|
||||
|
||||
|
||||
class RefreshDhcpLeasesTestCase(unittest.TestCase):
|
||||
def test_a_lease_error_raises_an_operation_error(self):
|
||||
net = network_with_leases(Exception("no DHCP on this network"))
|
||||
with self.assertRaises(util.OperationError):
|
||||
net.refresh_dhcp_leases()
|
||||
|
||||
def test_the_error_keeps_the_cause_and_clears_leases(self):
|
||||
cause = Exception("boom")
|
||||
net = network_with_leases(cause)
|
||||
with self.assertRaises(util.OperationError) as caught:
|
||||
net.refresh_dhcp_leases()
|
||||
self.assertIs(caught.exception.__cause__, cause)
|
||||
self.assertEqual(net.leases, [])
|
||||
|
|
@ -12,7 +12,8 @@ except ImportError:
|
|||
sys.modules["django"] = django_mock
|
||||
sys.modules["django.conf"] = django_mock.conf
|
||||
|
||||
from vrtManager.network import network_size, wvmNetwork
|
||||
from lxml import etree
|
||||
from vrtManager.network import ip_networks, network_size
|
||||
|
||||
|
||||
class TestNetworkSize(unittest.TestCase):
|
||||
|
|
@ -75,23 +76,8 @@ class TestNetworkSize(unittest.TestCase):
|
|||
network_size("invalid.subnet/99")
|
||||
|
||||
|
||||
class DummyNetwork(wvmNetwork):
|
||||
"""Subclass of wvmNetwork with mock XML for isolated IP method testing."""
|
||||
|
||||
def __init__(self, xml_content):
|
||||
self._xml = xml_content
|
||||
self.parent_count = 2
|
||||
self.updates = []
|
||||
|
||||
def _XMLDesc(self, flags):
|
||||
return self._xml
|
||||
|
||||
def update(self, command, section, xml, parent_index, flags):
|
||||
self.updates.append((command, section, xml, parent_index, flags))
|
||||
|
||||
|
||||
class TestWvmNetworkIPOperations(unittest.TestCase):
|
||||
"""Unit tests for IP parsing, DHCP ranges, and host addresses in wvmNetwork."""
|
||||
class TestIpNetworks(unittest.TestCase):
|
||||
"""A network's addresses (ip_networks): subnet, DHCP range and fixed addresses of each family."""
|
||||
|
||||
DUAL_STACK_XML = """<network>
|
||||
<name>default</name>
|
||||
|
|
@ -109,60 +95,15 @@ class TestWvmNetworkIPOperations(unittest.TestCase):
|
|||
</ip>
|
||||
</network>"""
|
||||
|
||||
NO_IP_XML = """<network>
|
||||
<name>isolated</name>
|
||||
<bridge name="virbr1"/>
|
||||
</network>"""
|
||||
def test_dual_stack(self):
|
||||
v4, v6 = ip_networks(etree.fromstring(self.DUAL_STACK_XML))
|
||||
self.assertEqual((str(v4["subnet"]), v4["range"]), ("192.168.100.0/24", ("192.168.100.2", "192.168.100.254")))
|
||||
self.assertEqual(v4["hosts"], [{"ip": "192.168.100.50", "mac": "52:54:00:12:34:56", "id": None, "name": "vm1"}])
|
||||
self.assertEqual((str(v6["subnet"]), v6["range"]), ("2001:db8:ca2:2::/64", ("2001:db8:ca2:2::100", "2001:db8:ca2:2::1ff")))
|
||||
self.assertEqual(v6["hosts"][0]["id"], "0:1:0:1")
|
||||
|
||||
def test_get_ip_networks_dual_stack(self):
|
||||
net = DummyNetwork(self.DUAL_STACK_XML)
|
||||
ip_networks = net.get_ip_networks()
|
||||
self.assertIn("ipv4", ip_networks)
|
||||
self.assertIn("ipv6", ip_networks)
|
||||
self.assertEqual(str(ip_networks["ipv4"]), "192.168.100.0/24")
|
||||
self.assertEqual(str(ip_networks["ipv6"]), "2001:db8:ca2:2::/64")
|
||||
|
||||
def test_get_ip_networks_no_ip(self):
|
||||
net = DummyNetwork(self.NO_IP_XML)
|
||||
ip_networks = net.get_ip_networks()
|
||||
self.assertEqual(ip_networks, {})
|
||||
|
||||
def test_get_dhcp_range_ipv4(self):
|
||||
net = DummyNetwork(self.DUAL_STACK_XML)
|
||||
dhcp = net.get_dhcp_range("ipv4")
|
||||
self.assertIsNotNone(dhcp)
|
||||
self.assertEqual(len(dhcp), 2)
|
||||
self.assertEqual(str(dhcp[0]), "192.168.100.2")
|
||||
self.assertEqual(str(dhcp[1]), "192.168.100.254")
|
||||
|
||||
def test_get_dhcp_range_ipv6(self):
|
||||
net = DummyNetwork(self.DUAL_STACK_XML)
|
||||
dhcp = net.get_dhcp_range("ipv6")
|
||||
self.assertIsNotNone(dhcp)
|
||||
self.assertEqual(len(dhcp), 2)
|
||||
self.assertEqual(str(dhcp[0]), "2001:db8:ca2:2::100")
|
||||
self.assertEqual(str(dhcp[1]), "2001:db8:ca2:2::1ff")
|
||||
|
||||
def test_get_dhcp_range_empty(self):
|
||||
net = DummyNetwork(self.NO_IP_XML)
|
||||
self.assertIsNone(net.get_dhcp_range("ipv4"))
|
||||
self.assertIsNone(net.get_dhcp_range("ipv6"))
|
||||
|
||||
def test_get_dhcp_range_start_and_end(self):
|
||||
net = DummyNetwork(self.DUAL_STACK_XML)
|
||||
self.assertEqual(str(net.get_dhcp_range_start("ipv4")), "192.168.100.2")
|
||||
self.assertEqual(str(net.get_dhcp_range_end("ipv4")), "192.168.100.254")
|
||||
self.assertEqual(str(net.get_dhcp_range_start("ipv6")), "2001:db8:ca2:2::100")
|
||||
self.assertEqual(str(net.get_dhcp_range_end("ipv6")), "2001:db8:ca2:2::1ff")
|
||||
|
||||
def test_modify_fixed_address_validation(self):
|
||||
net = DummyNetwork(self.DUAL_STACK_XML)
|
||||
# Invalid IP address must raise ValueError
|
||||
with self.assertRaises(ValueError):
|
||||
net.modify_fixed_address("bad_host", "999.999.999.999", "52:54:00:11:22:33", family="ipv4")
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
net.modify_fixed_address("bad_host", "not-an-ip", "0:1:0:2", family="ipv6")
|
||||
def test_no_address(self):
|
||||
self.assertEqual(ip_networks(etree.fromstring("<network><name>isolated</name><bridge name='virbr1'/></network>")), [])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
"""Values from requests do not change the XPath queries they are used in."""
|
||||
|
||||
import unittest
|
||||
from unittest.mock import Mock
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
|
|
@ -57,7 +58,7 @@ class XPathValueTests(unittest.TestCase):
|
|||
def test_qos_direction_must_be_known(self):
|
||||
net = wvmNetwork.__new__(wvmNetwork)
|
||||
net.wvm = FakeConn()
|
||||
net._XMLDesc = lambda flags: "<network><bandwidth><inbound average='1'/><outbound average='2'/></bandwidth></network>"
|
||||
net.net = Mock(**{"XMLDesc.return_value": "<network><bandwidth><inbound average='1'/><outbound average='2'/></bandwidth></network>"})
|
||||
with self.assertRaises(ValueError):
|
||||
net.unset_qos("*")
|
||||
net.unset_qos("inbound")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue