About: how pages are checked

tests/sims/container.test.ts

The automated tests for the /simulators/docker-internals 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. Each namespace changes only what its own command shows, and any set of namespaces combines them independently.
  2. A CPU quota is never exceeded in any period, and a cgroup is throttled in every period exactly when its threads want more than the quota.
// /simulators/docker-internals: Linux namespaces and cgroup CPU limits.
import { test } from 'node:test';
import { runInside, COMMANDS, NAMESPACES, cpuRun, type Ns } from '../../src/lib/sims/container.ts';
import { checker } from './check.ts';

const view = (ns: Ns[]) => Object.fromEntries(COMMANDS.map((c) => [c.id, runInside(c.id, { ns, privileged: false }).lines.join('\n')]));

test('each namespace changes only what its own command shows, and any set of namespaces combines them independently', () => {
	const { ok, done } = checker();
	const host = view([]);
	for (const n of NAMESPACES) {
		const v = view([n.id]);
		for (const c of COMMANDS) {
			const changed = v[c.id] !== host[c.id];
			if (c.id === n.cmd) ok(changed || n.id === 'user', `the ${n.id} namespace should change ${c.id}`);
			else ok(!changed, `the ${n.id} namespace changed ${c.id}`);
		}
	}
	const all = NAMESPACES.map((n) => n.id);
	for (let mask = 0; mask < 1 << all.length; mask++) {
		const ns = all.filter((_, i) => mask & (1 << i)), v = view(ns);
		for (const n of NAMESPACES) ok(v[n.cmd] === view(ns.includes(n.id) ? [n.id] : [])[n.cmd], `${ns.join('+') || 'none'}: ${n.cmd}`);
	}
	done();
});

test('a CPU quota is never exceeded in any period, and a cgroup is throttled in every period exactly when its threads want more than the quota', () => {
	const { ok, done } = checker();
	for (const threads of [1, 2, 4, 8]) for (const q of [25_000, 50_000, 100_000, 200_000, 400_000, Infinity]) {
		const r = cpuRun(threads, q, 100_000, 1_000_000, 30_000);
		for (const p of r.periods) ok(p.runUs <= q + 1e-6 && p.runUs <= threads * 100_000 + 1e-6, `${threads} threads, quota ${q}: ran ${p.runUs} us in a period`);
		if (threads * 100_000 <= q) ok(r.stat.nr_throttled === 0, `${threads} threads fit in quota ${q} but were throttled`);
		else ok(r.stat.nr_throttled === r.stat.nr_periods, `${threads} threads over quota ${q}: throttled ${r.stat.nr_throttled} of ${r.stat.nr_periods} periods`);
	}
	done();
});

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

Published