An open source design system that's fully customizable and agent ready
TypeScript
test(interaction-harness): the WebKit container gets CI's CPU limits (#10336)
Christopher Chedeau993e1fa1bdc0
diff --git a/internal/interaction-harness/README.md b/internal/interaction-harness/README.mdindex 598a36cc3e..0b825c0d0d 100644--- a/internal/interaction-harness/README.md+++ b/internal/interaction-harness/README.md@@ -46,6 +46,11 @@ newly animated layer). The script keeps Mesa's shader cache on the host(`~/.cache/astryx-webkit-shaders/<image>`, or `ASTRYX_WEBKIT_SHADER_CACHE`), soonly the first run on a machine pays for them.+With `CI` set, the script gives the container the four CPUs of CI's WebKit+runners as a quota, and llvmpipe as many threads. `ASTRYX_WEBKIT_CPUS` sets+another count. On Linux the container runs only on the CPUs the script may use,+so `taskset` limits it too.+Evidence (storyboards, curve traces) is written to`test-results/interaction-evidence/` (override with`ASTRYX_INTERACTION_EVIDENCE`) and attached to the test report. It is neverdiff --git a/internal/interaction-harness/scripts/webkit-container.mjs b/internal/interaction-harness/scripts/webkit-container.mjsindex f307ea0bb7..5c983f4392 100644--- a/internal/interaction-harness/scripts/webkit-container.mjs+++ b/internal/interaction-harness/scripts/webkit-container.mjs@@ -24,6 +24,9 @@* and the cache is kept on the host (`~/.cache/astryx-webkit-shaders/<image>`,* or `ASTRYX_WEBKIT_SHADER_CACHE`), so a run starts with every shader an* earlier run compiled instead of paying for them in its first tests.+ *+ * With `CI` set, the container gets CI's four CPUs (`ASTRYX_WEBKIT_CPUS` sets+ * another count). On Linux it runs only on the CPUs this script may use.*/import {spawnSync} from 'node:child_process';import fs from 'node:fs';@@ -51,6 +54,38 @@ if (engine == null) {process.exit(1);}const args = process.argv.slice(2);+// CI's WebKit legs run on four-CPU runners, so with `CI` set the container+// gets four CPUs as a quota; `ASTRYX_WEBKIT_CPUS` sets another count.+const cpusSetting =+ process.env.ASTRYX_WEBKIT_CPUS || ((process.env.CI ?? '') !== '' ? '4' : '');+const cpus = cpusSetting === '' ? null : Number(cpusSetting);+if (cpus != null && (!Number.isInteger(cpus) || cpus < 1)) {+ console.error(+ `webkit-container: ASTRYX_WEBKIT_CPUS must be a positive integer, got "${cpusSetting}"`,+ );+ process.exit(1);+}+// A container starts on every CPU, whatever the caller's affinity. On Linux it+// keeps the CPUs this script may run on, so `taskset` limits it too.+const cpuset = (() => {+ try {+ const allowed = /^Cpus_allowed_list:\s*(\S+)/m.exec(+ fs.readFileSync('/proc/self/status', 'utf8'),+ )?.[1];+ const online = fs+ .readFileSync('/sys/devices/system/cpu/online', 'utf8')+ .trim();+ return allowed != null && allowed !== online ? allowed : null;+ } catch {+ return null;+ }+})();+const cpuLimits = [+ // llvmpipe sizes its thread pool from the CPUs it may run on, not from the+ // quota. So it gets the same count, as in CI's WebKit step.+ ...(cpus == null ? [] : [`--cpus=${cpus}`, '-e', `LP_NUM_THREADS=${cpus}`]),+ ...(cpuset == null ? [] : [`--cpuset-cpus=${cpuset}`]),+];// Where the container sees the host's shader cache (see the header).const containerShaderCache = '/astryx-shader-cache';// Resolved to an absolute path: a bare relative value would be mounted as a@@ -106,6 +141,7 @@ const command = [// at eight workers WebKit could not start a page's process: `newPage` (a// frame decode's scratch page) waited out the test.'--pids-limit=-1',+ ...cpuLimits,'-v',`${root}:${root}`,'-v',@@ -125,8 +161,9 @@ const command = ['test','--config','playwright.interaction.config.ts',- '--project',- 'webkit',+ // One token: a bare `--project` would take the spec paths after it as more+ // project names.+ '--project=webkit',...args,];console.log(`${engine} ${command.join(' ')}`);diff --git a/internal/interaction-harness/scripts/webkit-container.test.mjs b/internal/interaction-harness/scripts/webkit-container.test.mjsnew file mode 100644index 0000000000..8708969ede--- /dev/null+++ b/internal/interaction-harness/scripts/webkit-container.test.mjs@@ -0,0 +1,176 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @input webkit-container.mjs, run with a fake `podman` first on PATH that+ * records the arguments of its `run`.+ * @output The CPU limits and the project flag the script gives the container.+ * @position The script's own test, in the node project.+ */++import {spawnSync} from 'node:child_process';+import fs from 'node:fs';+import os from 'node:os';+import path from 'node:path';+import {fileURLToPath} from 'node:url';+import {afterEach, beforeEach, describe, expect, it} from 'vitest';++const script = path.join(+ path.dirname(fileURLToPath(import.meta.url)),+ 'webkit-container.mjs',+);+const SPEC = 'some.interaction.browser.spec.ts';++/**+ * A preload that gives the script another host: its platform, and the CPU+ * lists it reads. Without lists, /proc and /sys cannot be read, as on macOS.+ * @param {{platform: string, allowed?: string, online?: string}} host+ */+const hostHook = ({platform, allowed, online}) =>+ `data:text/javascript,${encodeURIComponent(`+import fs from 'node:fs';+import {syncBuiltinESMExports} from 'node:module';+Object.defineProperty(process, 'platform', {value: ${JSON.stringify(platform)}});+const lists = ${JSON.stringify(+ allowed == null+ ? null+ : {+ '/proc/self/status': `Cpus_allowed_list:\t${allowed}\n`,+ '/sys/devices/system/cpu/online': `${online}\n`,+ },+ )};+const read = fs.readFileSync;+fs.readFileSync = (file, ...rest) => {+ if (/^\\/(proc|sys)\\//.test(String(file))) {+ if (lists != null && String(file) in lists) return lists[String(file)];+ throw Object.assign(new Error('ENOENT: ' + file), {code: 'ENOENT'});+ }+ return read(file, ...rest);+};+syncBuiltinESMExports();+`)}`;++let dir = '';++beforeEach(() => {+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'webkit-container-'));+});++afterEach(() => {+ fs.rmSync(dir, {recursive: true, force: true});+});++/**+ * Runs the script once; `args` is the fake engine's `run` line, if any.+ * @param {{env?: Record<string, string>, nodeArgs?: string[]}} [options]+ */+function run({env = {}, nodeArgs = []} = {}) {+ const bin = path.join(dir, 'bin');+ const argsFile = path.join(dir, 'run-args.json');+ fs.mkdirSync(bin, {recursive: true});+ fs.writeFileSync(+ path.join(bin, 'podman'),+ `#!/usr/bin/env node+const fs = require('node:fs');+if (process.argv[2] !== '--version') {+ fs.writeFileSync(${JSON.stringify(argsFile)}, JSON.stringify(process.argv.slice(2)));+}+`,+ {mode: 0o755},+ );+ const childEnv = {+ ...process.env,+ PATH: `${bin}${path.delimiter}${process.env.PATH ?? ''}`,+ ASTRYX_WEBKIT_SHADER_CACHE: path.join(dir, 'shaders'),+ };+ delete childEnv.CI;+ delete childEnv.ASTRYX_WEBKIT_CPUS;+ Object.assign(childEnv, env);+ const [command, ...rest] = [process.execPath, ...nodeArgs, script, SPEC];+ const result = spawnSync(command, rest, {env: childEnv, encoding: 'utf8'});+ /** @type {string[] | null} */+ const args = fs.existsSync(argsFile)+ ? JSON.parse(fs.readFileSync(argsFile, 'utf8'))+ : null;+ return {status: result.status, stderr: result.stderr, args};+}++/** @param {string[] | null} args */+const cpuFlags = args =>+ (args ?? []).filter(+ (arg, i, all) =>+ arg.startsWith('--cpus') ||+ arg.startsWith('--cpuset-cpus') ||+ (all[i - 1] === '-e' && arg.startsWith('LP_NUM_THREADS=')),+ );++describe('webkit-container.mjs', () => {+ it("gives the container CI's four CPUs, and llvmpipe four threads, when CI is set", () => {+ const {status, args} = run({env: {CI: '1'}});+ expect(status).toBe(0);+ expect(args).toContain('--cpus=4');+ expect(args).toContain('LP_NUM_THREADS=4');+ });++ it('takes another count from ASTRYX_WEBKIT_CPUS', () => {+ const {status, args} = run({env: {ASTRYX_WEBKIT_CPUS: '2'}});+ expect(status).toBe(0);+ expect(args).toContain('--cpus=2');+ expect(args).toContain('LP_NUM_THREADS=2');+ });++ it('sets no CPU count without CI or ASTRYX_WEBKIT_CPUS', () => {+ const {status, args} = run();+ expect(status).toBe(0);+ expect(+ cpuFlags(args).filter(flag => !flag.startsWith('--cpuset-cpus')),+ ).toEqual([]);+ });++ it('refuses a count that is not a positive integer, and starts no container', () => {+ for (const value of ['0', '1.5', 'four']) {+ const {status, stderr, args} = run({env: {ASTRYX_WEBKIT_CPUS: value}});+ expect(status).toBe(1);+ expect(stderr).toContain(+ `ASTRYX_WEBKIT_CPUS must be a positive integer, got "${value}"`,+ );+ expect(args).toBeNull();+ }+ });++ it('passes the spec paths right after the project', () => {+ const {args} = run();+ const at = args?.indexOf('--project=webkit') ?? -1;+ expect(at).toBeGreaterThan(-1);+ expect(args?.[at + 1]).toBe(SPEC);+ });++ it('keeps the CPUs the script may run on, on Linux', () => {+ const {status, args} = run({+ nodeArgs: [+ '--import',+ hostHook({platform: 'linux', allowed: '0', online: '0-3'}),+ ],+ });+ expect(status).toBe(0);+ expect(args).toContain('--cpuset-cpus=0');+ });++ it('adds no CPU set when the script may run on every CPU', () => {+ const {status, args} = run({+ nodeArgs: [+ '--import',+ hostHook({platform: 'linux', allowed: '0-3', online: '0-3'}),+ ],+ });+ expect(status).toBe(0);+ expect(cpuFlags(args)).toEqual([]);+ });++ it('skips the CPU set where there is no /proc, as on macOS', () => {+ const {status, args} = run({+ nodeArgs: ['--import', hostHook({platform: 'darwin'})],+ });+ expect(status).toBe(0);+ expect(cpuFlags(args)).toEqual([]);+ });+});