About: how pages are checked

tests/sims/rollout.test.ts

The automated tests for the /simulators/k8s-rollout 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. A 3-replica rolling update with the defaults scales the two ReplicaSets in the order the Kubernetes Deployment docs show.
  2. On 300 random settings, pods never exceed replicas + maxSurge, available pods never drop below replicas - maxUnavailable, and a broken image hits the progress deadline.
  3. MaxSurge rounds up and maxUnavailable rounds down, as the docs specify.
// /simulators/k8s-rollout: a Deployment's RollingUpdate and Recreate strategies.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { simulate, fenceposts, type Cfg } from '../../src/lib/sims/rollout.ts';
import { checker, lcg } from './check.ts';

const base: Cfg = { replicas: 3, strategy: 'RollingUpdate', maxSurge: '25%', maxUnavailable: '25%', startSec: 2, readySec: 1, minReadySeconds: 0, graceSec: 3, newHealthy: true, progressDeadlineSeconds: 600, tick: 0.5, maxSec: 900 };

test('a 3-replica rolling update with the defaults scales the two ReplicaSets in the order the Kubernetes Deployment docs show', () => {
	const ev = simulate(base).events.filter((e) => e.msg.startsWith('Scaled')).map((e) => e.msg.replace('web-5f8c6a', 'NEW').replace('web-7d4b9c', 'OLD'));
	assert.deepEqual(ev, ['Scaled up replica set NEW to 1', 'Scaled down replica set OLD to 2', 'Scaled up replica set NEW to 2', 'Scaled down replica set OLD to 1', 'Scaled up replica set NEW to 3', 'Scaled down replica set OLD to 0']);
});

test('on 300 random settings, pods never exceed replicas + maxSurge, available pods never drop below replicas - maxUnavailable, and a broken image hits the progress deadline', () => {
	const { ok, done } = checker();
	const r = lcg(5);
	const pick = <T,>(a: T[]) => a[Math.floor(r() * a.length)];
	for (let i = 0; i < 300; i++) {
		const c: Cfg = { ...base, replicas: pick([1, 2, 3, 4, 5, 6, 10]), maxSurge: pick(['0', '1', '2', '25%', '50%', '100%']), maxUnavailable: pick(['0', '1', '2', '25%', '50%']), startSec: pick([1, 3, 8]), minReadySeconds: pick([0, 5]), graceSec: pick([1, 5, 20]), newHealthy: r() < 0.85 };
		const x = simulate(c);
		if (x.invalid) continue;
		const { surge, unavailable } = fenceposts(c.maxSurge, c.maxUnavailable, c.replicas);
		const tag = JSON.stringify({ replicas: c.replicas, maxSurge: c.maxSurge, maxUnavailable: c.maxUnavailable, healthy: c.newHealthy });
		ok(x.maxPods <= c.replicas + surge, `${tag}: ${x.maxPods} pods at once`);
		if (c.newHealthy) {
			ok(x.minAvail >= c.replicas - unavailable, `${tag}: only ${x.minAvail} available`);
			ok(x.done !== null, `${tag}: never finished`);
		} else ok(x.deadline !== null, `${tag}: a failing rollout never hit the progress deadline`);
	}
	done();
});

test('maxSurge rounds up and maxUnavailable rounds down, as the docs specify', () => {
	assert.deepEqual(fenceposts('25%', '25%', 3), { surge: 1, unavailable: 0 });
	assert.deepEqual(fenceposts('25%', '25%', 10), { surge: 3, unavailable: 2 });
	const rec = simulate({ ...base, replicas: 4, strategy: 'Recreate' });
	assert.equal(rec.minAvail, 0, 'Recreate takes every pod down first');
});

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

Published