1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 11:16:47 +00:00
webvirtcloud/networks/tests/test_network_pages.py
catborise bdc8a6755c test: each test is in the tests package of the code it tests
Tests that build vrtManager objects and touch no view, form or model of
an app lived in that app; vrtManager/tests already holds such tests, so
the convention was split. They move to vrtManager/tests: the firmware,
CPU mode and PCIe port definitions of a new VM (test_create), USB and PCI
host devices, the console listener, host CPU reads, interface reads,
deletes and XML, network filters, PCI passthrough rules, node devices,
network changes, addresses and leases, pool XML, secrets, and the live
tests that use vrtManager only (disk resize, ISO, snapshots, XML, live
clone). livetest.py, the live-test helper six apps import, and the
network test driver move with them.

Others go to their app: the quota with a host down to instances, the
401/403 Logs rows (webvirtcloud's middleware) to webvirtcloud, the VM
page's tab script test to instances, novncd's own tests out of the
console views tests, the compute lock and input validation tests out of
the computes views tests, the interface views out of the forms tests.
instances/tests/test_views.py, entirely live, is test_live_views.py.

Test modules imported fixtures from each other; shared ones are now in
vrtManager/tests/fakes.py (migration fakes, node devices, IOMMU groups,
leases, a NAT network, a Ceph secret), interfaces/tests/fakes.py,
console/tests/daemons.py (novncd loaded once), livetest.wait_for and
instances/tests/create_page.BODY. Every live test checks
livetest.enabled(). The suite runs the same 2097 tests.
2026-11-06 13:48:04 +03:00

93 lines
5.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""The network pages: a card per network with its mode, device and
addresses (none of its own for a bridge); a network's page with an address
card per <ip>, its fixed addresses in one table, the VMs on it, bandwidth
limits only where libvirt sets them; the new network dialog opens again
with what was entered and why. Runs on a private libvirt test driver."""
import tempfile
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 django.urls import reverse
from instances.models import Instance
from lxml import html
from vrtManager.tests.driver import without_inactive_flag
from vrtManager.tests.fakes import NAT_NETWORK_NODE
class NetworkPageTests(TestCase):
def setUp(self):
node = tempfile.NamedTemporaryFile("w", suffix=".xml")
self.addCleanup(node.close)
node.write(NAT_NETWORK_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="np", hostname="localhost", login="", password="", type=4)
self.client.force_login(get_user_model().objects.create_superuser("np", "np@example.com", "pw"))
def page(self, response):
self.assertEqual(response.status_code, 200)
return html.fromstring(response.content)
def text(self, element):
return " ".join(" ".join(element.itertext()).split())
def test_the_list_tells_each_network_mode_and_addresses(self):
page = self.page(self.client.get(reverse("networks", args=[self.compute.id])))
cards = {card.xpath("string(.//div[contains(@class,'card-header')]//a)"): self.text(card)
for card in page.xpath("//div[@class='card h-100']")}
self.assertIn("Type NAT Device virbr9 IPv4 192.0.2.0/24 DHCP 192.0.2.10 – 192.0.2.20 "
"IPv6 2001:db8:1::/64 DHCP: fixed addresses only", cards["lab"])
self.assertIn("Type Bridge Device br0 Addresses from the outside network", cards["lan"])
self.assertIn("Type Macvtap Device eth0 Addresses from the outside network", cards["direct"])
def test_a_network_page(self):
vm = Instance.objects.create(compute=self.compute, name="web", uuid=self.conn.lookupByName("web").UUIDString())
page = self.page(self.client.get(reverse("network", args=[self.compute.id, "lab"])))
# an address card per <ip>, the fixed addresses of both in one table
self.assertEqual(len(page.xpath("//button[@data-bs-target='#DhcpRange']")), 1)
rows = [self.text(row) for row in page.xpath("//tbody[@class='searchable']/tr")]
self.assertEqual([row.split(" Edit")[0] for row in rows], ["192.0.2.31 52:54:00:00:00:31 web", "2001:db8:1::31 0:1:0:1 –"])
self.assertEqual(page.xpath("//tbody/tr/td//input[@name='ip_index']/@value"), ["0", "1"])
# the VMs on it, linked
self.assertEqual(page.xpath(f"//a[@href='{reverse('instances:instance', args=[vm.id])}']/text()"), ["web"])
self.assertTrue(page.xpath("//div[@id='QosLimit']"))
def test_a_bridge_or_macvtap_network_has_no_addresses_of_libvirt(self):
for name, qos in (("lan", True), ("direct", False)): # libvirt limits a host bridge, not macvtap
with self.subTest(name=name):
page = self.page(self.client.get(reverse("network", args=[self.compute.id, name])))
self.assertIn("take their addresses from the outside network", self.text(page))
self.assertFalse(page.xpath("//div[@id='FixedAddress']"))
self.assertEqual(bool(page.xpath("//div[@id='QosLimit']")), qos)
def test_a_vm_undefined_meanwhile_is_left_out(self):
error = libvirt.libvirtError("Domain not found")
error.get_error_code = lambda: libvirt.VIR_ERR_NO_DOMAIN
with patch.object(libvirt.virDomain, "isActive", side_effect=error):
page = self.page(self.client.get(reverse("network", args=[self.compute.id, "lab"])))
self.assertIn("No VM is defined on this network.", self.text(page))
def test_an_action_does_not_read_the_page(self):
# every action redirects: the VMs on the network are not scanned for it
with patch("vrtManager.network.wvmNetwork.get_vms") as get_vms:
response = self.client.post(reverse("network", args=[self.compute.id, "lab"]), {"set_autostart": ""})
self.assertEqual(response.status_code, 302)
get_vms.assert_not_called()
def test_a_refused_network_opens_the_dialog_again(self):
page = self.page(self.client.post(reverse("networks", args=[self.compute.id]), {
"create": "", "name": "lab2", "forward": "route", "subnet": "192.0.2.0/25"}))
dialog = page.get_element_by_id("AddNetPool")
self.assertIsNotNone(dialog.get("data-open"))
self.assertEqual(page.xpath("//input[@name='forward'][@checked]/@value"), ["route"])
self.assertEqual(page.get_element_by_id("net_subnet").get("aria-invalid"), "true")
self.assertIn("Network lab has 192.0.2.0/24", page.get_element_by_id("net_subnet_error").text_content())