About: how pages are checked

tests/sims/evict.test.ts

The automated tests for the /simulators/pod-eviction 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.

  1. Oom_score_adj matches the kubelet source on random pods and node sizes.
  2. QoS classes follow the Kubernetes definitions.
  3. Eviction order: usage over request first, then lower priority, then usage above request.
  4. The kubelet evicts until memory.available is back above the threshold, and no further.
  5. Kubectl drain never takes an app below its PodDisruptionBudget.
  6. Every example on the page parses without errors.
// /simulators/pod-eviction: QoS classes, oom_score_adj, the kubelet's eviction order and kubectl drain.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { qos, oomScoreAdj, rank, simulate, drain, parse, type Pod, type Pdb } from '../../src/lib/sims/evict.ts';
import { EVICT_PRESETS } from '../../src/lib/sims/evictPresets.ts';
import { checker, lcg } from './check.ts';

const Mi = 1024 ** 2, Gi = 1024 ** 3;

/** GetContainerOOMScoreAdjust from pkg/kubelet/qos/policy.go, with Go's int64 division. */
function kubeletAdj(p: Pod, capacity: number): number {
	const guaranteed = p.lim !== null && p.req === p.lim;
	if (guaranteed) return -997;
	if (p.req === null && p.lim === null) return 1000;
	const adj = 1000n - (1000n * BigInt(p.req ?? 0)) / BigInt(capacity);
	if (adj < 1000n - 997n) return 1000 - 997;
	if (adj === 1000n) return 999;
	return Number(adj);
}

function randomPods(r: () => number, n: number): Pod[] {
	return Array.from({ length: n }, (_, i) => {
		const kind = r();
		const req = kind < 0.25 ? null : Math.floor(r() * 4096) * Mi;
		const lim = kind < 0.25 ? null : kind < 0.5 ? req : r() < 0.5 ? null : (req ?? 0) + Math.floor(r() * 2048) * Mi;
		return { name: `p${i}`, app: ['web', 'db', 'api'][Math.floor(r() * 3)], prio: [0, 0, 1000, 100000][Math.floor(r() * 4)], req, lim, use: Math.floor(r() * 5000) * Mi };
	});
}

test('oom_score_adj matches the kubelet source on random pods and node sizes', () => {
	const { ok, done } = checker();
	const r = lcg(21);
	for (let it = 0; it < 3000; it++) {
		const cap = (1 + Math.floor(r() * 64)) * Gi + Math.floor(r() * 1000) * Mi;
		for (const p of randomPods(r, 5)) ok(oomScoreAdj(p, cap) === kubeletAdj(p, cap), `${JSON.stringify(p)} on ${cap}: ${oomScoreAdj(p, cap)} vs ${kubeletAdj(p, cap)}`);
	}
	// edge cases: request equal to node memory, a tiny request, no request but a limit (copied into the request by the parser)
	const node = 16 * Gi;
	assert.equal(oomScoreAdj({ name: 'a', app: 'a', prio: 0, req: node, lim: null, use: 0 }, node), 3);
	assert.equal(oomScoreAdj({ name: 'b', app: 'b', prio: 0, req: 1, lim: null, use: 0 }, node), 999);
	assert.equal(oomScoreAdj({ name: 'c', app: 'c', prio: 0, req: 4 * Gi, lim: 8 * Gi, use: 0 }, node), 750);
	assert.equal(parse('x lim=1Gi use=1Mi').pods[0].req, Gi);
	done();
});

test('QoS classes follow the Kubernetes definitions', () => {
	assert.equal(qos({ name: 'a', app: 'a', prio: 0, req: Gi, lim: Gi, use: 0 }), 'Guaranteed');
	assert.equal(qos({ name: 'a', app: 'a', prio: 0, req: Gi, lim: 2 * Gi, use: 0 }), 'Burstable');
	assert.equal(qos({ name: 'a', app: 'a', prio: 0, req: Gi, lim: null, use: 0 }), 'Burstable');
	assert.equal(qos({ name: 'a', app: 'a', prio: 0, req: null, lim: null, use: 0 }), 'BestEffort');
});

test('eviction order: usage over request first, then lower priority, then usage above request', () => {
	const { ok, done } = checker();
	const r = lcg(4);
	for (let it = 0; it < 2000; it++) {
		const pods = randomPods(r, 2 + Math.floor(r() * 8));
		const order = rank(pods);
		for (let i = 0; i + 1 < order.length; i++) {
			const a = order[i], b = order[i + 1];
			const oa = a.use > (a.req ?? 0), ob = b.use > (b.req ?? 0);
			const key = (o: boolean, p: Pod) => [o ? 0 : 1, p.prio, -(p.use - (p.req ?? 0))];
			const ka = key(oa, a), kb = key(ob, b);
			const cmp = ka[0] - kb[0] || ka[1] - kb[1] || ka[2] - kb[2];
			ok(cmp <= 0, `${a.name} ranked before ${b.name}: ${ka} vs ${kb}`);
		}
	}
	done();
});

test('the kubelet evicts until memory.available is back above the threshold, and no further', () => {
	const { ok, done } = checker();
	const r = lcg(8);
	for (let it = 0; it < 2000; it++) {
		const pods = randomPods(r, 3 + Math.floor(r() * 8));
		const cap = 16 * Gi, sys = Gi, th = 100 * Mi;
		const o = simulate(pods, cap, sys, th);
		for (const p of pods) if (p.lim !== null && p.use > p.lim) ok(o.status[p.name] === 'OOMKilled', `${p.name} over its limit but ${o.status[p.name]}`);
		const running = pods.filter((p) => o.status[p.name] === 'Running');
		const avail = cap - sys - running.reduce((s, p) => s + o.effUse[p.name], 0);
		ok(avail === o.availableEnd, `available ${avail} vs ${o.availableEnd}`);
		ok(avail >= th || running.length === 0, `stopped at ${avail} with ${running.length} pods running`);
		const evictions = o.events.filter((e) => e.kind === 'evict');
		if (evictions.length) ok(evictions[evictions.length - 2] === undefined || evictions[evictions.length - 2].available < th, 'evicted one pod more than needed');
	}
	done();
});

test('kubectl drain never takes an app below its PodDisruptionBudget', () => {
	const { ok, done } = checker();
	const r = lcg(13);
	for (let it = 0; it < 2000; it++) {
		const pods = randomPods(r, 2 + Math.floor(r() * 8));
		const pdbs: Pdb[] = ['web', 'db'].map((app) => ({ app, min: Math.floor(r() * 4), elsewhere: Math.floor(r() * 3) }));
		const res = drain(pods, pdbs);
		for (const b of pdbs) {
			const total = b.elsewhere + pods.filter((p) => p.app === b.app).length;
			const evicted = res.filter((x) => x.ok && pods.find((p) => p.name === x.pod)!.app === b.app).length;
			ok(total - evicted >= Math.min(b.min, total), `${b.app}: ${total} healthy, evicted ${evicted}, min ${b.min}`);
			ok(evicted === Math.max(0, Math.min(pods.filter((p) => p.app === b.app).length, total - b.min)), `${b.app}: evicted ${evicted}, should be as many as the budget allows`);
		}
		for (const x of res) if (!pdbs.some((b) => b.app === pods.find((p) => p.name === x.pod)!.app)) ok(x.ok, `${x.pod} has no PDB but was refused`);
	}
	done();
});

test('every example on the page parses without errors', () => {
	for (const p of EVICT_PRESETS) assert.deepEqual(parse(p.text).bad, [], p.id);
});

Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.

Published