1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 11:16:47 +00:00
webvirtcloud/networks/tests/test_dhcp_range.py
catborise 0c9a002ef4 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.
2026-10-29 10:09:14 +03:00

74 lines
3.8 KiB
Python

"""A network's DHCP range is changed while the network is stopped, and the
page says so only when it was: a running network, a network without a
range, an address it does not have or a range outside its subnet is
refused with the reason. Runs on a private libvirt test driver:
"test:///default" is shared by the whole process."""
import tempfile
from unittest.mock import patch
import libvirt
from computes.models import Compute
from django.contrib.auth import get_user_model
from django.contrib.messages import get_messages
from django.test import TestCase
from networks.tests.driver import without_inactive_flag
from django.urls import reverse
from lxml import etree
NODE = """<node>
<network><name>lab</name><bridge name='virbr9'/>
<ip address='192.0.2.1' netmask='255.255.255.0'><dhcp><range start='192.0.2.10' end='192.0.2.20'/></dhcp></ip>
</network>
<network><name>plain</name><bridge name='virbr8'/><ip address='198.51.100.1' netmask='255.255.255.0'/></network>
</node>"""
class DhcpRangeTests(TestCase):
def setUp(self):
node = tempfile.NamedTemporaryFile("w", suffix=".xml")
self.addCleanup(node.close)
node.write(NODE)
node.flush()
self.conn = libvirt.open(f"test://{node.name}")
self.addCleanup(self.conn.close)
patcher = patch("vrtManager.connection.connection_manager.get_connection", return_value=self.conn)
patcher.start()
self.addCleanup(patcher.stop)
without_inactive_flag(self)
self.compute = Compute.objects.create(name="dhcp", hostname="localhost", login="", password="", type=4)
self.client.force_login(get_user_model().objects.create_superuser("dhcp", "dhcp@example.com", "pw"))
def change(self, network, index="0", start="192.0.2.30", end="192.0.2.40"):
response = self.client.post(reverse("network", args=[self.compute.id, network]), {
"modify_dhcp_range": "", "range_start": start, "range_end": end, "ip_index": index})
return [str(m) for m in get_messages(response.wsgi_request)]
def dhcp_range(self, network):
xml = etree.fromstring(self.conn.networkLookupByName(network).XMLDesc(0))
return xml.xpath("ip/dhcp/range/@start|ip/dhcp/range/@end")
def test_a_stopped_network_gets_the_new_range(self):
self.conn.networkLookupByName("lab").destroy()
self.assertEqual(self.change("lab"), ["The DHCP range is changed"])
self.assertEqual(self.dhcp_range("lab"), ["192.0.2.30", "192.0.2.40"])
def test_a_running_network_is_refused(self):
messages = self.change("lab")
self.assertEqual(messages, ["Stop network lab to change its DHCP range"])
self.assertEqual(self.dhcp_range("lab"), ["192.0.2.10", "192.0.2.20"])
def test_a_network_without_a_range_or_an_unknown_family_is_refused(self):
self.conn.networkLookupByName("lab").destroy()
self.conn.networkLookupByName("plain").destroy()
self.assertEqual(self.change("plain")[-1], "This address of network plain has no DHCP range")
self.assertEqual(self.change("lab", index="1")[-1], "Network lab has no such address")
self.assertEqual(self.change("lab", index="x")[-1], "Network lab has no such address")
self.assertEqual(self.dhcp_range("lab"), ["192.0.2.10", "192.0.2.20"])
def test_a_range_outside_the_subnet_or_backwards_is_refused(self):
self.conn.networkLookupByName("lab").destroy()
self.assertEqual(self.change("lab", end="198.51.100.9")[-1], "The range end 198.51.100.9 is not in 192.0.2.0/24")
self.assertEqual(self.change("lab", start="192.0.2.1")[-1], "192.0.2.1 is the host's own address on the network")
self.assertEqual(self.change("lab", start="192.0.2.50")[-1], "The range starts at 192.0.2.50, after its end 192.0.2.40")
self.assertEqual(self.dhcp_range("lab"), ["192.0.2.10", "192.0.2.20"])