1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 11:16:47 +00:00
webvirtcloud/nodedevs/tests/test_nodedevs.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

65 lines
3.6 KiB
Python

"""The host's devices page: PCI devices by IOMMU group and USB devices, each
with what the host has on it (network interfaces, disks, its graphics card)
and the VMs that name it, by PCI address, by USB address or by USB model
only. Node device XML as libvirt 11.10 writes it; the connection is a mock,
libvirt's test driver has no node devices."""
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.fakes import NODES, node
from vrtManager.tests.fakes import PCI_0A, USB_1_3, vm
class NodeDevicesPageTests(TestCase):
def setUp(self):
self.compute = Compute.objects.create(name="nd", hostname="192.0.2.30", login="root", password="", type=1)
self.client.force_login(get_user_model().objects.create_superuser("nd", "nd@example.com", "pw"))
self.url = reverse("nodedevs", args=[self.compute.id])
def page(self, nodes=NODES, domains=()):
with patch("vrtManager.connection.connection_manager.get_connection") as get:
get.return_value.listAllDevices.return_value = [node(*n) for n in nodes]
get.return_value.listAllDomains.return_value = list(domains)
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
return html.fromstring(response.content)
def test_the_page_groups_pci_devices_and_links_their_vms(self):
instance = Instance.objects.create(compute=self.compute, name="db-1", uuid="00000000-0000-4000-8000-0000000000d1")
page = self.page(domains=[vm("db-1", PCI_0A), vm("outside-1", USB_1_3)])
headers = [" ".join(h.text_content().split()) for h in page.xpath("//tr[@data-group-header]")]
self.assertEqual(headers, ["IOMMU group 2", "IOMMU group 3", "IOMMU group 4", "Without an IOMMU group"])
self.assertEqual(page.xpath("//a[text()='db-1']/@href"), [reverse("instances:instance", args=[instance.id])])
self.assertIn("outside-1", page.text_content()) # a VM the panel does not have: named, not linked
self.assertNotIn("No IOMMU groups detected", page.text_content())
def test_a_host_without_iommu_groups_says_so(self):
page = self.page(nodes=[n for n in NODES if "iommuGroup" not in n[1]])
self.assertIn("No IOMMU groups detected", page.text_content())
def test_a_host_without_pci_or_usb_devices_says_so(self):
page = self.page(nodes=[n for n in NODES if "usb" in n[0]])
self.assertIn("The host reports no PCI devices.", page.text_content())
self.assertNotIn("No IOMMU groups detected", page.text_content()) # nothing to give
self.assertIn("The host reports no USB devices.", self.page(nodes=NODES[:2]).text_content())
def test_only_superusers(self):
self.client.force_login(get_user_model().objects.create_user("nd-user", "u@example.com", "pw"))
self.assertEqual(self.client.get(self.url).status_code, 403)
def test_a_host_not_read_shows_no_device(self):
# without the VMs' definitions a device would look free
with patch("vrtManager.connection.connection_manager.get_connection") as get:
get.return_value.listAllDevices.return_value = [node(*n) for n in NODES]
get.return_value.listAllDomains.side_effect = libvirt.libvirtError("unreachable")
response = self.client.get(self.url)
page = html.fromstring(response.content)
self.assertEqual(page.xpath("//table"), [])
self.assertIn("unreachable", page.text_content())