1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/computes/tests/test_balance_js.py
catborise 1259c11591 fix(computes): the Balance page shows each host's load as bars
The page was a bare table of rounded GiB totals, a footnote shown also
when nothing would move, three tables with their own columns and a
Migrate button far below the list. Now:

- a breadcrumb back to Computes and a one-line description;
- a card per host with its running VMs' memory and vCPUs now and after,
  as bars on one scale, so the imbalance and its fix show at a glance;
- the proposed migrations in one card, "host -> destination" in one
  column, sizes as on the Computes page, and the number at once with a
  "Migrate N VMs" button in its footer;
- the VMs that stay (kept by the user, or unable to move) in one table
  with the reason;
- a success message when the hosts are balanced.

The VM boxes join the proposal's form by their form attribute, so the
migrations' form holds the list.
2026-11-16 08:11:47 +03:00

48 lines
2.1 KiB
Python

"""The Balance page's script (static/js/balance.js): a ticked or unticked VM
makes the proposal again; the header box ticks or unticks every VM listed
first."""
import json
import shutil
import subprocess
import unittest
from pathlib import Path
SCRIPT = Path(__file__).resolve().parents[2] / "static" / "js" / "balance.js"
RUN = """
const vm = require("vm");
const boxes = [{name: "vm", id: "moves", checked: true}, {name: "vm", id: "stays", checked: false}];
let handler, submitted = 0;
const form = {elements: boxes, submit: () => { submitted += 1; }};
const other = {elements: [], submit: () => { throw new Error("another form"); }};
vm.runInNewContext(SCRIPT, vm.createContext({document: {
getElementById: () => form,
addEventListener: (name, fn) => { handler = fn; },
}}));
const seen = [];
for (const [id, checked, owner] of STEPS) {
handler({target: {id, checked, form: owner === "other" ? other : form}});
seen.push(boxes.filter((b) => b.checked).map((b) => b.id));
}
console.log(JSON.stringify({seen, submitted}));
"""
@unittest.skipUnless(shutil.which("node"), "node is not installed")
class BalanceScriptTests(unittest.TestCase):
def run_script(self, steps):
"""{seen: the ticked VMs after each step, submitted: the form's reloads}."""
code = f"const SCRIPT = {json.dumps(SCRIPT.read_text())};" + RUN.replace("STEPS", json.dumps(steps))
out = subprocess.run(["node", "-e", code], capture_output=True, text=True, check=True, timeout=30)
return json.loads(out.stdout)
def test_a_vm_box_makes_the_proposal_again(self):
self.assertEqual(self.run_script([["balance-7", False]]), {"seen": [["moves"]], "submitted": 1})
def test_a_change_outside_the_proposal_does_not_reload_it(self):
self.assertEqual(self.run_script([["balance-parallel", True, "other"]])["submitted"], 0)
def test_select_all_ticks_or_unticks_every_vm_and_makes_the_proposal_again(self):
result = self.run_script([["balance-all", True], ["balance-all", False]])
self.assertEqual(result, {"seen": [["moves", "stays"], []], "submitted": 2})