tests/sims/kubesched.test.ts
The automated tests for the /simulators/container-scheduler simulator. They run the model on its own, outside the browser, and compare it with a reference that doesn't depend on the model being right. They run on every change to the simulator's code.
- LeastAllocated scores match the plugin's integer formula from the Kubernetes source.
- Placements never break a filter, on random clusters.
- An unschedulable pod gets the FitError message kube-scheduler prints.
// /simulators/container-scheduler: kube-scheduler filtering and scoring.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { parseNodes, parsePods, schedule, usage } from '../../src/lib/sims/kubesched.ts';
import { checker, lcg } from './check.ts';
test('LeastAllocated scores match the plugin\'s integer formula from the Kubernetes source', () => {
// pkg/scheduler/framework/plugins/noderesources/least_allocated.go:
// per resource (allocatable − requested) × 100 / allocatable, then the weighted mean (weights 1)
const { ok, done } = checker();
const r = lcg(31);
for (let it = 0; it < 2000; it++) {
const cpu = 1 + Math.floor(r() * 16), memGi = 1 + Math.floor(r() * 64);
const pc = [100, 250, 500, 1000, 1500][Math.floor(r() * 5)], pm = [128, 256, 512, 1024, 2048][Math.floor(r() * 5)];
if (pc > cpu * 1000 || pm > memGi * 1024) continue;
const d = schedule(parseNodes(`n1 zone=a cpu=${cpu} mem=${memGi}Gi`).nodes, parsePods(`p1 cpu=${pc}m mem=${pm}Mi`).pods, 'least').decisions[0];
const allocC = cpu * 1000, allocM = memGi * 1024 ** 3, reqM = pm * 1024 ** 2;
const want = Math.floor((Math.floor(((allocC - pc) * 100) / allocC) + Math.floor(((allocM - reqM) * 100) / allocM)) / 2);
ok(d.evals[0].fit === want, `cpu=${cpu} mem=${memGi}Gi pod ${pc}m/${pm}Mi: fit ${d.evals[0].fit} vs ${want}`);
}
done();
});
test('placements never break a filter, on random clusters', () => {
const { ok, done } = checker();
const u = lcg(9);
for (let it = 0; it < 1500; it++) {
const nn = 2 + Math.floor(u() * 5);
let nt = '';
for (let i = 0; i < nn; i++) nt += `n${i} zone=${'abc'[Math.floor(u() * 3)]} cpu=${1 + Math.floor(u() * 8)} mem=${1 + Math.floor(u() * 16)}Gi${u() < 0.2 ? ' taint=gpu=true:NoSchedule' : ''}${u() < 0.3 ? ' label=disk=ssd' : ''}${u() < 0.1 ? ' cordon' : ''}\n`;
let pt = '';
const np = 3 + Math.floor(u() * 10);
for (let i = 0; i < np; i++) pt += `p${i} app=${['web', 'db', 'api'][Math.floor(u() * 3)]} cpu=${[100, 250, 500, 1000, 2000][Math.floor(u() * 5)]}m mem=${[128, 512, 1024, 4096][Math.floor(u() * 4)]}Mi${u() < 0.2 ? ' tolerate=gpu' : ''}${u() < 0.2 ? ' select=disk=ssd' : ''}${u() < 0.3 ? ' anti=host' : ''}${u() < 0.15 ? ' anti=zone' : ''}\n`;
const nodes = parseNodes(nt).nodes, pp = parsePods(pt);
ok(pp.bad.length === 0, `bad pods ${pp.bad}`);
for (const strat of ['least', 'most'] as const) {
const { placement, decisions } = schedule(nodes, pp.pods, strat);
for (const x of usage(nodes, pp.pods, placement)) ok(x.cpuFrac <= 1 + 1e-9 && x.memFrac <= 1 + 1e-9, `${x.node ?? ''} over capacity`);
for (const p of pp.pods) {
const n = placement[p.name];
if (!n) continue;
const node = nodes.find((x) => x.name === n)!;
ok(!node.cordoned, `${p.name} on cordoned ${n}`);
ok(node.taints.every((t) => p.tolerations.some((tl) => tl.key === t.key)), `${p.name} on tainted ${n} without a toleration`);
ok(Object.entries(p.selector).every(([k, v]) => node.labels[k] === v), `${p.name} on ${n} breaks its nodeSelector`);
for (const q of pp.pods) {
if (q === p || q.app !== p.app || !placement[q.name] || !(p.anti || q.anti)) continue;
const other = nodes.find((x) => x.name === placement[q.name])!;
if (p.anti === 'host' || q.anti === 'host') ok(other.name !== node.name, `${p.name} and ${q.name} share ${n} despite anti-affinity`);
if (p.anti === 'zone' && q.anti === 'zone') ok(other.zone !== node.zone, `${p.name} and ${q.name} share zone ${node.zone}`);
}
}
for (const d of decisions) if (!d.node) ok(/^0\/\d+ nodes are available: /.test(d.message ?? ''), `unschedulable message: ${d.message}`);
}
}
done();
});
test('an unschedulable pod gets the FitError message kube-scheduler prints', () => {
const d = schedule(parseNodes('n1 zone=a cpu=2 mem=4Gi\nn2 zone=a cpu=2 mem=4Gi taint=gpu=true:NoSchedule\nn3 zone=b cpu=8 mem=4Gi cordon').nodes, parsePods('big cpu=3 mem=1Gi').pods, 'least');
assert.match(d.decisions[0].message!, /^0\/3 nodes are available: /);
assert.match(d.decisions[0].message!, /Insufficient cpu/);
assert.match(d.decisions[0].message!, /untolerated taint/);
assert.match(d.decisions[0].message!, /unschedulable/);
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.