mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-09 19:56:48 +00:00
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.
91 lines
5.1 KiB
Python
91 lines
5.1 KiB
Python
"""Every HTML page a superuser can open, rendered on libvirt's test driver."""
|
|
|
|
import re
|
|
from collections import Counter
|
|
from unittest.mock import patch
|
|
|
|
import libvirt
|
|
from computes.models import Compute
|
|
from django.contrib.auth import get_user_model
|
|
from django.test import TestCase
|
|
from networks.tests.driver import without_inactive_flag
|
|
from django.urls import NoReverseMatch, URLPattern, URLResolver, get_resolver, reverse
|
|
from instances.models import Flavor, Instance
|
|
from lxml import html
|
|
|
|
|
|
def named_urls(patterns, prefix="", keys=()):
|
|
for p in patterns:
|
|
if isinstance(p, URLResolver):
|
|
yield from named_urls(p.url_patterns, f"{prefix}{p.namespace}:" if p.namespace else prefix,
|
|
keys + tuple(p.pattern.regex.groupindex))
|
|
elif isinstance(p, URLPattern) and p.name:
|
|
yield prefix + p.name, keys + tuple(p.pattern.regex.groupindex)
|
|
|
|
|
|
class PageTests(TestCase):
|
|
def pages(self):
|
|
"""(URL name, parsed page) for every HTML page; asserts the main ones are among them."""
|
|
conn = libvirt.open("test:///default")
|
|
self.addCleanup(conn.close)
|
|
without_inactive_flag(self)
|
|
compute = Compute.objects.create(name="testdrv", hostname="localhost", login="", password="", type=4)
|
|
vm = Instance.objects.create(compute=compute, name="test", uuid=conn.lookupByName("test").UUIDString())
|
|
Flavor.objects.create(label="second", memory=512, vcpu=1, disk=10) # the flavor modals repeat per flavor
|
|
self.client.force_login(get_user_model().objects.create_superuser("ids", "ids@example.com", "pw"))
|
|
self.client.raise_request_exception = False
|
|
values = {"pk": vm.id, "compute_id": compute.id, "pool": "default-pool", "arch": "i686", "machine": "pc"}
|
|
checked = []
|
|
with patch("vrtManager.connection.connection_manager.get_connection", return_value=conn), \
|
|
patch("vrtManager.connection.wvmConnect.get_machine_types", return_value=["pc"]), \
|
|
patch("vrtManager.connection.wvmConnect.get_nwfilters", return_value=[]), \
|
|
patch("vrtManager.connection.wvmConnect.get_cpu_modes", return_value=["host-model"]), \
|
|
patch.object(libvirt.virConnect, "getMemoryStats", return_value={"total": 8 << 20, "free": 4 << 20, "buffers": 0, "cached": 0}), \
|
|
patch.object(libvirt.virConnect, "getCPUStats", return_value={"kernel": 1, "idle": 1, "user": 1, "iowait": 0}):
|
|
for name, keys in named_urls(get_resolver().url_patterns):
|
|
try:
|
|
url = reverse(name, kwargs={k: values.get(k, 1) for k in keys})
|
|
except NoReverseMatch:
|
|
continue
|
|
response = self.client.get(url)
|
|
if response.status_code != 200 or not response["Content-Type"].startswith("text/html"):
|
|
continue
|
|
checked.append(name)
|
|
yield name, html.fromstring(response.content)
|
|
# its pool key names a network, not the storage pool above
|
|
response = self.client.get(reverse("network", kwargs={"compute_id": compute.id, "pool": "default"}))
|
|
self.assertEqual(response.status_code, 200)
|
|
checked.append("network")
|
|
yield "network", html.fromstring(response.content)
|
|
# a signed-in user is sent on from the login page
|
|
response = self.client_class().get(reverse("accounts:login"))
|
|
self.assertEqual(response.status_code, 200)
|
|
checked.append("accounts:login")
|
|
yield "accounts:login", html.fromstring(response.content)
|
|
self.assertTrue({"index", "overview", "instances:instance", "nwfilter"} <= set(checked), checked)
|
|
self.assertIn("instances:create_instance", checked)
|
|
|
|
def test_no_page_repeats_an_id(self):
|
|
# a repeated ID makes getElementById, labels and Bootstrap targets reach the first one only
|
|
for name, page in self.pages():
|
|
ids = Counter(page.xpath("//@id"))
|
|
with self.subTest(page=name):
|
|
self.assertEqual({i: n for i, n in ids.items() if n > 1}, {})
|
|
|
|
def test_every_table_scrolls_in_its_own_box(self):
|
|
# a wide table scrolls inside .table-responsive instead of widening the page on a phone
|
|
for name, page in self.pages():
|
|
bare = [t.get("class") for t in page.xpath("//table[not(parent::div[contains(@class, 'table-responsive')])]")]
|
|
with self.subTest(page=name):
|
|
self.assertEqual(bare, [])
|
|
|
|
def test_grid_columns_fit_a_phone(self):
|
|
# below a column's breakpoint it is full width, so an offset without one
|
|
# pushes it past the screen; a row needs columns inside, not another row
|
|
row = "contains(concat(' ', normalize-space(@class), ' '), ' row ')"
|
|
for name, page in self.pages():
|
|
offsets = [cls for cls in page.xpath("//@class")
|
|
if re.search(r"\boffset-\d+\b", cls) and not re.search(r"\bcol-\d+\b", cls)]
|
|
with self.subTest(page=name):
|
|
self.assertEqual(offsets, [])
|
|
self.assertEqual(page.xpath(f"//*[{row}]/*[{row}]/@class"), [])
|