1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

fix(instances): the VM list searches its VMs, collapses its hosts together and says when it is empty

- The search read a row's whole text, so a word on a button or in a
  state label matched every row. VM rows now carry data-search (the
  name, title and owners, and the host in the flat list), and
  table_filter.js searches that text when a row has it; the state has
  its own filter.
- Nothing matching left an empty table. table_filter.js shows the
  page's #no-match while no row does; the VM list's says so and offers
  "Clear the filters". A user with no VMs at all is told so instead of
  an empty table.
- The grouped list's "#" toggled each host on its own: a host
  collapsed by hand opened while the others closed. It now collapses
  every host, or opens them all once all are collapsed.
- One alert names every host the panel cannot reach, with why, in
  place of one dismissible alert per host; users who see no hosts keep
  their own message.
- A host's VM list showed a VM's maximum memory, the Instances page
  its current memory: both show the current memory now.
- The owners' users are prefetched with the VMs; the flat list's
  Bootstrap 4 font-italic is fst-italic.
This commit is contained in:
catborise 2026-10-27 22:20:50 +03:00
parent 321749f8fa
commit 2d80f21a4a
9 changed files with 114 additions and 37 deletions

View file

@ -4,23 +4,24 @@ const {filterTable: filterInstances} = require(process.argv[2]);
// A minimal DOM: table bodies of rows; a VM row holds its state mark, a
// group's header row is marked as one.
function vm(text, state) {
function vm(text, state, search) {
const mark = {dataset: {state}};
return {textContent: text, hidden: false, hasAttribute: () => false,
return {textContent: text, dataset: search === undefined ? {} : {search}, hidden: false, hasAttribute: () => false,
querySelector: (s) => (s === "[data-state]" ? mark : null)};
}
function hostRow(text) {
return {textContent: text, hidden: false, hasAttribute: (a) => a === "data-group-header", querySelector: () => null};
return {textContent: text, dataset: {}, hidden: false, hasAttribute: (a) => a === "data-group-header", querySelector: () => null};
}
function plainRow(text) {
return {textContent: text, hidden: false, hasAttribute: () => false, querySelector: () => null};
return {textContent: text, dataset: {}, hidden: false, hasAttribute: () => false, querySelector: () => null};
}
function group(rows, host) {
return {dataset: host ? {host} : {}, querySelectorAll: (s) => (s === "tr" ? rows : [])};
}
function page(groups, text, state) {
function page(groups, text, state, noMatch) {
// a list without a state list (state undefined) has no such element
const inputs = {filter: {value: text}, "state-filter": state === undefined ? undefined : {value: state}};
const inputs = {filter: {value: text}, "state-filter": state === undefined ? undefined : {value: state},
"no-match": noMatch};
return {
getElementById: (id) => inputs[id],
querySelectorAll: (s) => (s === ".searchable" ? groups : []),
@ -64,3 +65,17 @@ filterInstances(page([group(plain)], "(vda"));
assert.deepStrictEqual(visible(plain), ["bob (vda) Blocked"], "a bracket is text there too");
filterInstances(page([group(plain)], ""));
assert.deepStrictEqual(visible(plain), ["alice Active", "bob (vda) Blocked", "carol Active"]);
// a row with data-search is searched there only: its menus' words are not the VM's
const menus = [vm("web-1 Active Force off Power cycle", "Active", "web-1 alice"), vm("db-1 Off Start", "Off", "db-1 bob")];
filterInstances(page([group(menus)], "off", ""));
assert.deepStrictEqual(visible(menus), [], "a menu's 'Force off' is not a match");
filterInstances(page([group(menus)], "BOB", ""));
assert.deepStrictEqual(visible(menus), ["db-1 Off Start"], "an owner is searched");
// the page says so when nothing matches, and not otherwise
const noMatch = {hidden: true};
filterInstances(page([group(menus)], "nothing-like-this", "", noMatch));
assert.strictEqual(noMatch.hidden, false, "no row: the message shows");
filterInstances(page([group(menus)], "web", "", noMatch));
assert.strictEqual(noMatch.hidden, true, "a row: the message goes");

View file

@ -65,12 +65,12 @@ class ListInfoCallTests(TestCase):
self.assertEqual(response.status_code, 200)
return response
def assert_one_info_per_vm(self, response, memory="2048 MiB"):
def assert_one_info_per_vm(self, response):
self.assertEqual(len(self.domains), 2, "one proxy per VM row")
for domain in self.domains:
self.assertEqual(domain.info_calls, 1)
self.assertEqual(domain.xml_calls, 1, "only the title reads the XML")
self.assertContains(response, memory, count=2)
self.assertContains(response, "2048 MiB", count=2)
self.assertContains(response, "Active")
def test_nongrouped_index(self):
@ -83,4 +83,5 @@ class ListInfoCallTests(TestCase):
def test_compute_instances(self):
# this page shows the maximum memory, as before
self.assert_one_info_per_vm(self.get(reverse("instances", args=[self.compute.id])), memory="4096 MiB")
# the VM's current memory, as on the Instances page, not its maximum
self.assert_one_info_per_vm(self.get(reverse("instances", args=[self.compute.id])))

View file

@ -133,6 +133,20 @@ class StateTests(TestCase):
self.assertEqual([r.xpath("td[@data-sort]/@data-sort") for r in rows[1:2]], [["2", "2097152"]])
def test_rows_are_searched_for_their_vm_not_their_buttons(self):
# table_filter.js searches data-search: a menu's "Force off" would match "off" in every row
for style in ("grouped", "nongrouped"):
AppSettings.objects.filter(key="VIEW_INSTANCES_LIST_STYLE").update(value=style)
page = html.fromstring(self.get(self.admin, reverse("instances:index")))
searched = page.xpath("//tbody//tr/@data-search")
with self.subTest(style=style):
self.assertEqual(len(searched), len(self.vms))
for vm, text in zip(self.vms.values(), searched):
self.assertTrue(text.startswith(vm.name), text)
self.assertIn("st-owner", text)
self.assertNotIn("Force", text)
self.assertTrue(page.xpath("//div[@id='no-match'][@hidden]//button[@id='clear-filters']"))
def test_the_lists_show_the_vms_by_name(self):
# the first VM made comes last by name: the database's own order is not the list's
Instance.objects.filter(pk=self.vms[0].pk).update(name="st-vm-9")
@ -211,3 +225,11 @@ class TemplateCloneTests(TestCase):
self.assertEqual(allowed_actions(cloner, vm, 5), {"clone"})
self.assertEqual(allowed_actions(admin, vm, 5), {"clone"})
self.assertEqual(allowed_actions(owner, vm, 5), set()) # no clone permission
class EmptyListTests(TestCase):
def test_a_user_without_vms_is_told_so(self):
self.client.force_login(get_user_model().objects.create_user("st-nobody", password="pw"))
response = self.client.get(reverse("instances:index"))
self.assertContains(response, "There are no VMs here yet.")
self.assertNotContains(response, "searchable") # no VM table