1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00
webvirtcloud/computes/tests/test_balance_js.py
catborise 4941a8a048 feat(computes): Balance proposes migrations that even out the hosts
A Balance button on the Computes page opens computes/balance/: the memory
and vCPUs each host gives its running VMs, before and after, and the
proposed migrations. The proposal (computes/balance.py) takes, step by
step, the one move that most lowers the sum of the squared memory loads,
then of the squared vCPU loads, and stops when none does; each VM moves
once, and the VMs moved to a host fit in the memory its running and
paused VMs leave without counting those moved away, so the moves may
run in any order. Only running VMs count and
move; one that cannot move (no destination shares its disks) still
counts on its host and is listed with the reason. Unticking a VM keeps
it where it is and makes the proposal again.

Migrate runs the moves shown in one host.balance job. It shares the bulk
migration's loop (computes.jobs.Migrations): migrate jobs, the chosen
number at once, one bulk job at a time. Its migrations take only a
running VM (moveto running_only): one stopped since stays where it is.

A job another one started now shows only on its own page: a host's page
no longer lists a bulk migration's or a balance's migrations, and the
Computes page shows the bulk jobs.
2026-11-16 07:37:10 +03:00

45 lines
1.8 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: "moves", checked: true}, {name: "stays", checked: false}];
let handler, submitted = 0;
const form = {
addEventListener: (name, fn) => { handler = fn; },
querySelectorAll: () => boxes,
submit: () => { submitted += 1; },
};
vm.runInNewContext(SCRIPT, vm.createContext({document: {getElementById: () => form}}));
const seen = [];
for (const [id, checked] of STEPS) {
handler({target: {id, checked}});
seen.push(boxes.filter((b) => b.checked).map((b) => b.name));
}
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_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})