1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 04:36:48 +00:00

feat(computes): the host overview shows its VMs, capacity and load, also when it cannot be reached

A host the panel could not reach made its overview the 500 page,
without the host's sections, and the Computes list gave such a host an
Overview button that led nowhere. The page now keeps the host's name,
sections, "Edit connection" and "Remove from panel" (the existing
confirmation), and says "Not connected" with the reason and a "Try
again". Details that cannot be read are said in an alert; the VMs,
capacity and load still show, as they are read on their own.

A reachable host's page leads with what it runs and what it gave:
- its VMs by state (running, shut off, paused, other), linked to
  Instances;
- the vCPUs, memory and storage pools given, as on the Computes list,
  which now shares those parts (computes/capacity_*.html) and their
  preparation (views.shown_capacity) with it;
- CPU and memory in use, side by side, with the current reading in
  words, a note on what the charts hold and when they were last
  updated, or since when not; memory in GB like the rest of the page;
- Hardware and Software and connection: logical CPUs and clock from
  virNodeGetInfo, but physical cores, sockets and NUMA nodes counted
  from the CPUs of the capabilities' NUMA cells, as virNodeGetInfo
  makes a topology up for hosts it cannot describe (asymmetric NUMA);
  the hypervisor's own name and version, libvirt, emulator, the guest
  architectures as text instead of menus of dead links. A host without
  sysinfo shows no processor instead of failing.
Empty notes no longer show "None"; labels stack over their values on a
phone instead of running into them.

Pools that cannot be read no longer drop a host's vCPUs and memory from
its capacity: the storage part says so. get_capacity also counts the
VMs by state.
This commit is contained in:
catborise 2026-10-27 14:26:03 +03:00
parent 86c708ed68
commit 1b99d2eac5
15 changed files with 487 additions and 201 deletions

View file

@ -11,6 +11,7 @@ from libvirt import libvirtError
from lxml import html
GIB = 1024 ** 3
VMS = {"running": 3, "paused": 1, "shutoff": 2, "other": 0}
def pool(name, size_gib, free_gib, status=1):
@ -33,6 +34,14 @@ class CapacityTests(TestCase):
"cpus": 16, "vcpus": {"all": 24, "running": 8}, "ratio": 1.5,
"pools": [pool("default", 500, 120), pool("iso", 100, 90), pool("nfs", 900, 800)]})
@patch("computes.models.wvmStorages")
@patch("computes.models.wvmHostDetails")
def test_pools_that_cannot_be_read_leave_the_cpus_and_memory(self, details, storages):
details.return_value.get_capacity.return_value = {"cpus": 16, "vcpus": {"all": 24, "running": 8}}
storages.return_value.get_storages_info.side_effect = libvirtError("cannot refresh pool")
self.assertEqual(self.compute.capacity, {
"cpus": 16, "vcpus": {"all": 24, "running": 8}, "ratio": 1.5, "pools": None})
@patch("computes.models.wvmHostDetails")
def test_a_host_whose_read_fails_has_no_capacity(self, details):
# a VM that goes while the host is read, for one
@ -53,7 +62,7 @@ class ComputesPageTests(TestCase):
c1 = Compute.objects.create(name="host-one", hostname="192.0.2.1", login="u", password="p", type=1)
Compute.objects.create(name="host-two", hostname="192.0.2.2", login="u", password="p", type=1)
found = {c1.id: {"cpus": 16, "memory": 64 * GIB, "vcpus": {"all": 40, "running": 24},
"assigned": {"all": 48 * GIB, "running": 32 * GIB}, "ratio": 2.5,
"assigned": {"all": 48 * GIB, "running": 32 * GIB}, "ratio": 2.5, "vms": VMS,
"pools": [pool("default", 500, 120)]}}
with patch.object(Compute, "capacity", new=property(lambda c: found.get(c.id))), \
patch.object(Compute, "status", new=True):
@ -77,7 +86,7 @@ class ComputesPageTests(TestCase):
Compute.objects.create(name="host-one", hostname="192.0.2.1", login="u", password="p", type=1)
pools = [pool("default", 500, 120), pool("backup", 900, 800), pool("iso", 100, 90), pool("nfs", 2000, 1000)]
given = {"cpus": 16, "memory": 64 * GIB, "vcpus": {"all": 8, "running": 8}, "ratio": 0.5,
"assigned": {"all": 8 * GIB, "running": 8 * GIB}, "pools": pools}
"assigned": {"all": 8 * GIB, "running": 8 * GIB}, "pools": pools, "vms": VMS}
with patch.object(Compute, "capacity", new=given), patch.object(Compute, "status", new=True):
page = html.fromstring(self.client.get(reverse("computes")).content)
(storage,) = page.xpath("//tbody/tr/td[4]")
@ -95,7 +104,7 @@ class SingularTests(TestCase):
self.client.force_login(admin)
c = Compute.objects.create(name="host-one", hostname="192.0.2.1", login="u", password="p", type=1)
given = {"cpus": 1, "memory": 4 * GIB, "vcpus": {"all": 1, "running": 1},
"assigned": {"all": GIB, "running": GIB}, "ratio": 1.0, "pools": []}
"assigned": {"all": GIB, "running": GIB}, "ratio": 1.0, "pools": [], "vms": VMS}
with patch.object(Compute, "capacity", new=given), patch.object(Compute, "status", new=True):
page = html.fromstring(self.client.get(reverse("computes")).content)
line = self.client.get(reverse("compute_capacity", args=[c.id])).content.decode()

View file

@ -24,6 +24,7 @@ from vrtManager.util import ConnectionFailed
# the page script runs after the shared chart helpers
CHARTS_JS = settings.BASE_DIR / "static" / "js" / "charts.js"
TOPOLOGY = {"nodes": 1, "sockets": 1, "cores": 2, "threads": 1, "mhz": 2400, "cpus": 2}
def host(*readings):
@ -138,17 +139,20 @@ class GraphScriptTestCase(TestCase):
compute = Compute.objects.create(name="gs", hostname="192.0.2.1", login="u", password="p", type=1)
with patch("computes.views.wvmHostDetails") as details, patch.object(
Compute, "status", new_callable=PropertyMock, return_value=True
):
), patch.object(Compute, "capacity", new=None):
details.return_value.get_node_info.return_value = ("h", "x86_64", 1024, 2, "cpu", "qemu+tcp://h/system")
details.return_value.get_memory_usage.return_value = {"total": 1024, "usage": 512, "percent": 50}
details.return_value.get_cpu_topology.return_value = TOPOLOGY
page = self.client.get(reverse("overview", args=[compute.id])).content.decode()
script = next(s for s in html.fromstring(page).xpath("//script[not(@src)]/text()") if "pollWhileVisible(" in s)
js = """
const vm = require("vm");
const charts = [], requests = [];
const element = (dataset) => ({dataset, textContent: "", classList: {add: () => {}, remove: () => {}}});
const elements = {graph_state: element({updated: "Updated at %(time)s.", stale: "Not updated since %(time)s."}),
cpu_now: element({}), mem_now: element({})};
let poll;
const page = {
document: {body: {}, getElementById: () => ({})},
document: {body: {}, getElementById: (id) => elements[id] || {}},
// Chart.js adds the lists a config leaves out
getComputedStyle: () => ({getPropertyValue: () => ""}),
Chart: Object.assign(function (canvas, config) {
@ -158,21 +162,23 @@ class GraphScriptTestCase(TestCase):
this.update = () => {};
charts.push(this);
}, {defaults: {}}),
$: {getJSON: (url, params) => { requests.push(params); return {}; }},
$: {getJSON: (url, params) => { requests.push(params); return {fail() { return this; }}; }},
pollWhileVisible: (interval, request, onData) => { poll = {request, onData}; },
};
const context = vm.createContext(page);
vm.runInContext(CHARTS, context);
vm.runInContext(SCRIPT, context);
const answer = (cpu, sample) => ({cpudata: cpu, window: 5.1, cpu_sample: sample, timeline: "t",
memdata: {total: 1048576, usage: 0}});
memdata: {total: 2 * 1073741824, usage: 1073741824}});
poll.request();
poll.onData(answer(3, "token-1"));
const shown = [elements.cpu_now.textContent, elements.mem_now.textContent, elements.graph_state.textContent];
poll.request();
poll.onData({cpudata: null, memdata: {total: null, usage: null}, timeline: "t"});
poll.request();
const label = charts[0].options.plugins.tooltip.callbacks.label({dataset: {label: "Usage"}, formattedValue: "3", dataIndex: 0});
console.log(JSON.stringify({requests, label, cpu: charts[0].data.datasets[0].data,
console.log(JSON.stringify({requests, label, shown, state: elements.graph_state.textContent,
cpu: charts[0].data.datasets[0].data,
mem: charts[1].data.datasets[0].data, memMax: charts[1].options.scales.y.max}));
""".replace("SCRIPT", json.dumps(script)).replace("CHARTS", json.dumps(CHARTS_JS.read_text()))
out = subprocess.run(["node", "-e", js], capture_output=True, text=True, timeout=30)
@ -181,5 +187,8 @@ class GraphScriptTestCase(TestCase):
self.assertEqual(result["requests"], [{}, {"cpu_sample": "token-1"}, {}])
self.assertEqual(result["cpu"], [3, None])
# a host that could not be read leaves a gap and keeps the memory axis
self.assertEqual((result["mem"], result["memMax"]), ([0, None], 1))
self.assertEqual((result["mem"], result["memMax"]), ([1, None], 2))
# the readings in words, and since when they are not updated
self.assertEqual(result["shown"], ["3 %", "1 / 2 GB", "Updated at t."])
self.assertEqual(result["state"], "Not updated since t.")
self.assertTrue(result["label"].endswith(": 3 % (average over 5.1 s)"), result["label"])

View file

@ -0,0 +1,94 @@
"""The host overview page: a host the panel reaches, one it cannot reach,
and one whose details cannot be read."""
from unittest.mock import PropertyMock, patch
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
from libvirt import libvirtError
from lxml import html
from computes.models import Compute
GIB = 1024 ** 3
GIVEN = {"cpus": 8, "memory": 32 * GIB, "vcpus": {"all": 12, "running": 6},
"assigned": {"all": 24 * GIB, "running": 12 * GIB}, "ratio": 1.5,
"vms": {"running": 3, "paused": 1, "shutoff": 2, "other": 0},
"pools": [{"name": "default", "status": 1, "size": 500 * GIB, "free": 120 * GIB}]}
def text(page, xpath):
return " ".join(" ".join(page.xpath(xpath)[0].itertext()).split())
class OverviewTestCase(TestCase):
def setUp(self):
self.client.force_login(get_user_model().objects.create_superuser("ov-admin", "ov@example.com", "pw"))
self.compute = Compute.objects.create(name="host-one", hostname="192.0.2.1", login="u", password="p",
type=1, details="rack 4")
self.url = reverse("overview", args=[self.compute.id])
def page(self, connected=True, given=GIVEN):
with patch.object(Compute, "status", new_callable=PropertyMock, return_value=connected), \
patch.object(Compute, "connection_error", new_callable=PropertyMock,
return_value="Connection Failed: 192.0.2.1 did not answer within 5 s"), \
patch.object(Compute, "capacity", new=given and {**given}):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
return html.fromstring(response.content)
@patch("computes.views.wvmHostDetails")
def test_a_host_shows_its_vms_capacity_hardware_and_software(self, details):
conn = details.return_value
conn.get_node_info.return_value = ("wvc-host", "x86_64", 32 * GIB, 8, "Xeon", "qemu+tcp://192.0.2.1/system")
conn.get_cpu_topology.return_value = {"nodes": 2, "sockets": 2, "cores": 4, "mhz": 2400, "cpus": 8}
conn.wvm.getType.return_value = "QEMU"
conn.get_version.return_value = "10.1.0"
conn.get_lib_version.return_value = "11.10.0"
conn.get_emulator.return_value = "/usr/libexec/qemu-kvm"
conn.get_hypervisors_domain_types.return_value = {"i686": ["kvm", "qemu"], "x86_64": ["kvm", "qemu"]}
page = self.page()
self.assertIn("Connected", text(page, "//p[span]"))
self.assertIn("rack 4", text(page, "//p[span]"))
cards = [" ".join(" ".join(card.itertext()).split()) for card in page.xpath("//div[@class='card-body' or contains(@class,'card-body ')]")]
self.assertIn("VMs 6 3 running, 2 shut off, 1 paused Instances →", cards)
self.assertTrue(any(card.startswith("vCPUs 1.5:1 12 vCPUs on 8 CPUs 6 running") for card in cards), cards)
self.assertTrue(any("24.0 GB given of 32.0 GB" in card for card in cards), cards)
self.assertTrue(any("120.0 GB free of 500.0 GB" in card for card in cards), cards)
hardware = text(page, "//h4[.='Hardware']/following-sibling::dl")
self.assertIn("Logical CPUs 8 Physical cores 4 Sockets 2", hardware)
self.assertIn("NUMA nodes 2", hardware)
software = text(page, "//h4[.='Software and connection']/following-sibling::dl")
self.assertIn("Hypervisor QEMU 10.1.0", software)
self.assertIn("x86_64 (kvm, qemu)", software)
# the charts poll only for a host that was read
self.assertTrue(page.xpath("//canvas[@id='cpuChart']"))
@patch("computes.views.wvmHostDetails")
def test_a_host_the_panel_cannot_reach_keeps_its_page_with_the_reason(self, details):
page = self.page(connected=False, given=None)
details.assert_not_called()
self.assertIn("Not connected", text(page, "//p[span]"))
self.assertIn("did not answer within 5 s", text(page, "//div[@role='alert']"))
# the way back: its sections, its connection and its removal
self.assertTrue(page.xpath("//nav[@aria-label='Host sections']"))
self.assertTrue(page.xpath(f"//a[@href='{reverse('compute_update', args=[self.compute.id])}']"))
self.assertTrue(page.xpath(f"//a[@href='{reverse('compute_delete', args=[self.compute.id])}']"))
self.assertFalse(page.xpath("//canvas"))
@patch("computes.views.wvmHostDetails")
def test_details_that_cannot_be_read_leave_the_capacity_and_the_load(self, details):
details.return_value.get_node_info.return_value = ("h", "x86_64", GIB, 2, None, "qemu+tcp://192.0.2.1/system")
details.return_value.get_lib_version.side_effect = libvirtError("cannot read the version")
page = self.page()
self.assertIn("cannot read the version", text(page, "//div[@role='alert']"))
self.assertTrue(page.xpath("//h5[.='VMs']"))
# the load is read on its own, by the charts
self.assertTrue(page.xpath("//canvas[@id='cpuChart']"))
self.assertFalse(page.xpath("//h4[.='Hardware']"))
def test_the_list_links_the_overview_of_a_host_it_cannot_reach(self):
with patch.object(Compute, "status", new_callable=PropertyMock, return_value=False):
page = html.fromstring(self.client.get(reverse("computes")).content)
self.assertTrue(page.xpath(f"//a[@title='Overview'][@href='{self.url}']"))

View file

@ -17,17 +17,6 @@ class OverviewStatusTests(TestCase):
# an ssh_config alias: not a name a TCP probe can reach
self.compute = Compute.objects.create(name="ov-alias", hostname="wvc-alias", login="root", password="", type=2)
@patch("vrtManager.connection.connection_manager.host_is_up", return_value=False)
@patch("computes.views.wvmHostDetails")
def test_overview_state_comes_from_the_connection(self, host_details, host_is_up):
conn = host_details.return_value
conn.get_node_info.return_value = ("host", "x86_64", 1024, 2, "cpu", "qemu+ssh://root@wvc-alias/system")
conn.get_memory_usage.return_value = {"total": 1024, "usage": 512, "percent": 50}
with patch.object(Compute, "status", new_callable=PropertyMock, return_value=True):
response = self.client.get(reverse("overview", args=[self.compute.id]))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["status"], "true")
def test_compute_list_shows_why_a_host_is_not_connected(self):
with patch.object(Compute, "status", new_callable=PropertyMock, return_value=False), patch.object(
Compute, "connection_error", new_callable=PropertyMock, return_value=ERROR