Repository navigation
perf: batch chroot integration tests to reduce container overhead #845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /** | ||
| * Batch Runner - runs multiple commands in a single AWF container invocation. | ||
| * | ||
| * Each test that calls runner.runWithSudo() spawns a full Docker container | ||
| * lifecycle (~15-25s overhead). This utility batches commands that share the | ||
| * same allowDomains config into one invocation, cutting container startups | ||
| * from ~73 to ~27 across the chroot test suite. | ||
| * | ||
| * Usage: | ||
| * const results = await runBatch(runner, [ | ||
| * { name: 'python_version', command: 'python3 --version' }, | ||
| * { name: 'node_version', command: 'node --version' }, | ||
| * ], { allowDomains: ['github.com'] }); | ||
| * | ||
| * // Each test asserts against its own result: | ||
| * expect(results.get('python_version').exitCode).toBe(0); | ||
| */ | ||
|
|
||
| import { AwfRunner, AwfOptions, AwfResult } from './awf-runner'; | ||
|
|
||
| export interface BatchCommand { | ||
| name: string; | ||
| command: string; | ||
| } | ||
|
|
||
| export interface BatchCommandResult { | ||
| stdout: string; | ||
| exitCode: number; | ||
| } | ||
|
|
||
| export interface BatchResults { | ||
| /** Get result for a named command. Throws if name not found. */ | ||
| get(name: string): BatchCommandResult; | ||
| /** The raw AWF result for the entire batch invocation. */ | ||
| overall: AwfResult; | ||
| } | ||
|
|
||
| // Delimiter tokens – chosen to be unlikely in real command output | ||
| const START = '===BATCH_START:'; | ||
| const EXIT = '===BATCH_EXIT:'; | ||
| const DELIM_END = '==='; | ||
|
|
||
| /** | ||
| * Build a bash script that runs each command in a subshell, capturing its | ||
| * exit code and delimiting its output. | ||
| */ | ||
| function generateScript(commands: BatchCommand[]): string { | ||
| return commands.map(cmd => { | ||
| // Each command runs in a subshell so failures don't abort the batch. | ||
| // stdout and stderr are merged (2>&1) so we capture everything. | ||
| // A blank echo before the EXIT marker ensures a newline separator. | ||
| return [ | ||
| `echo "${START}${cmd.name}${DELIM_END}"`, | ||
| `(${cmd.command}) 2>&1`, | ||
| `echo ""`, | ||
| `echo "${EXIT}${cmd.name}:$?${DELIM_END}"`, | ||
| ].join('; '); | ||
| }).join('; '); | ||
| } | ||
|
|
||
| /** | ||
| * Parse the combined stdout into per-command results. | ||
| */ | ||
| function parseResults(stdout: string, commands: BatchCommand[]): Map<string, BatchCommandResult> { | ||
| const results = new Map<string, BatchCommandResult>(); | ||
|
|
||
| for (const cmd of commands) { | ||
| const startToken = `${START}${cmd.name}${DELIM_END}`; | ||
| const exitPattern = new RegExp(`${EXIT.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}${cmd.name}:(\\d+)${DELIM_END}`); | ||
|
|
||
| const startIdx = stdout.indexOf(startToken); | ||
| const exitMatch = stdout.match(exitPattern); | ||
|
|
||
| if (startIdx === -1 || !exitMatch) { | ||
| // Command output not found – likely the batch was killed early | ||
| results.set(cmd.name, { stdout: '', exitCode: -1 }); | ||
| continue; | ||
| } | ||
|
|
||
| const contentStart = startIdx + startToken.length; | ||
| const contentEnd = stdout.indexOf(exitMatch[0], contentStart) - 1; // -1 for the blank line | ||
| const cmdStdout = stdout.slice(contentStart, contentEnd).trim(); | ||
|
Comment on lines
+72
to
+83
|
||
| const exitCode = parseInt(exitMatch[1], 10); | ||
|
|
||
| results.set(cmd.name, { stdout: cmdStdout, exitCode }); | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| /** | ||
| * Run multiple commands in a single AWF container invocation. | ||
| * | ||
| * All commands share the same AwfOptions (allowDomains, timeout, etc.). | ||
| * Individual command results are parsed from delimited output. | ||
| */ | ||
| export async function runBatch( | ||
| runner: AwfRunner, | ||
| commands: BatchCommand[], | ||
| options: AwfOptions, | ||
| ): Promise<BatchResults> { | ||
| const script = generateScript(commands); | ||
| const result = await runner.runWithSudo(script, options); | ||
| const parsed = parseResults(result.stdout, commands); | ||
|
|
||
| return { | ||
| get(name: string): BatchCommandResult { | ||
| const r = parsed.get(name); | ||
| if (!r) { | ||
| throw new Error(`Batch command "${name}" not found in results. Available: ${[...parsed.keys()].join(', ')}`); | ||
| } | ||
| return r; | ||
| }, | ||
| overall: result, | ||
| }; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cmd.nameis interpolated directly into a double-quotedechoin the generated bash script. If a caller passes a name containing characters like",$, backticks, or newlines, it can break the script and/or spoof delimiter lines, which will corrupt parsing. Consider validatingnameagainst a strict safe pattern (e.g.,^[A-Za-z0-9_\-]+$) and throwing early (or properly escaping for bash) before generating the script.This issue also appears on line 68 of the same file.