@zgeoff/rig 0.0.0 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +87 -3
- package/dist/browser/browser-preload.d.ts +1 -0
- package/dist/browser/build-browser-run.d.ts +9 -0
- package/dist/browser/build-lane-environments.d.ts +8 -0
- package/dist/browser/build-recorded-command.d.ts +1 -0
- package/dist/browser/find-browser-run.d.ts +4 -0
- package/dist/browser/find-private-dir-problem.d.ts +2 -0
- package/dist/browser/has-browser-build-script.d.ts +1 -0
- package/dist/browser/has-error-code.d.ts +1 -0
- package/dist/browser/is-zombie-state.d.ts +1 -0
- package/dist/browser/lane-files.d.ts +13 -0
- package/dist/browser/make-browser-lane-hooks.d.ts +22 -0
- package/dist/browser/pick-chrome-dirs.d.ts +7 -0
- package/dist/browser/pick-exit-code-after-tests.d.ts +1 -0
- package/dist/browser/pick-step-after-build.d.ts +7 -0
- package/dist/browser/pick-stop-step.d.ts +17 -0
- package/dist/browser/probe-host-process.d.ts +1 -0
- package/dist/browser/probe-process.d.ts +6 -0
- package/dist/browser/probe-run.d.ts +9 -0
- package/dist/browser/process-group-still-running-error.d.ts +4 -0
- package/dist/browser/process-group-stop-error.d.ts +4 -0
- package/dist/browser/process-groups.d.ts +5 -0
- package/dist/browser/process-runtime.d.ts +15 -0
- package/dist/browser/read-dir-names.d.ts +1 -0
- package/dist/browser/read-process-table.d.ts +6 -0
- package/dist/browser/remove-exited-run-dirs.d.ts +2 -0
- package/dist/browser/remove-path.d.ts +1 -0
- package/dist/browser/run-in-page.d.ts +23 -0
- package/dist/browser/should-keep-run-folders.d.ts +1 -0
- package/dist/browser/start-browser.d.ts +9 -0
- package/dist/browser/start-process-group.d.ts +17 -0
- package/dist/browser/wire-browser-lane-hooks.d.ts +12 -0
- package/dist/build-diet-command.d.ts +1 -0
- package/dist/build-init-scripts.d.ts +3 -0
- package/dist/build-test-selection.d.ts +23 -0
- package/dist/build-unit-command.d.ts +2 -0
- package/dist/cli.d.ts +2 -0
- package/dist/define-rig.d.ts +2 -0
- package/dist/find-timeout-arg.d.ts +1 -0
- package/dist/format-no-match-message.d.ts +1 -0
- package/dist/format-no-tests-message.d.ts +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +255 -0
- package/dist/init-files.d.ts +6 -0
- package/dist/is-command-name.d.ts +3 -0
- package/dist/lane-hooks.d.ts +13 -0
- package/dist/make-repo-lane-hooks.d.ts +12 -0
- package/dist/parse-duration.d.ts +1 -0
- package/dist/parse-lane-hook.d.ts +3 -0
- package/dist/parse-lane-start.d.ts +2 -0
- package/dist/parse-rig-config.d.ts +9 -0
- package/dist/parse-test-args.d.ts +5 -0
- package/dist/pick-e2e-dirs.d.ts +1 -0
- package/dist/pick-lane-selection.d.ts +2 -0
- package/dist/read-cli-args.d.ts +5 -0
- package/dist/read-memory-budget.d.ts +8 -0
- package/dist/read-rig-config.d.ts +6 -0
- package/dist/read-tracked-files.d.ts +1 -0
- package/dist/read-worker-count.d.ts +13 -0
- package/dist/resolve-diet-script.d.ts +1 -0
- package/dist/resolve-related-tests.d.ts +6 -0
- package/dist/run-cli.d.ts +9 -0
- package/dist/run-command.d.ts +6 -0
- package/dist/run-init.d.ts +8 -0
- package/dist/run-lane-hooks.d.ts +2 -0
- package/dist/run-lane.d.ts +18 -0
- package/dist/run-related-tests.d.ts +10 -0
- package/dist/run-unit-tests.d.ts +12 -0
- package/dist/run-unit.d.ts +8 -0
- package/dist/standard-lanes.d.ts +8 -0
- package/dist/system-clock.d.ts +5 -0
- package/dist/types.d.ts +42 -0
- package/package.json +41 -3
- package/src/browser/browser-preload.ts +36 -0
- package/src/browser/build-browser-run.ts +32 -0
- package/src/browser/build-lane-environments.ts +33 -0
- package/src/browser/build-recorded-command.ts +12 -0
- package/src/browser/find-browser-run.ts +15 -0
- package/src/browser/find-private-dir-problem.ts +21 -0
- package/src/browser/has-browser-build-script.ts +7 -0
- package/src/browser/has-error-code.ts +3 -0
- package/src/browser/is-zombie-state.ts +4 -0
- package/src/browser/lane-files.ts +56 -0
- package/src/browser/make-browser-lane-hooks.ts +228 -0
- package/src/browser/pick-chrome-dirs.ts +34 -0
- package/src/browser/pick-exit-code-after-tests.ts +5 -0
- package/src/browser/pick-step-after-build.ts +20 -0
- package/src/browser/pick-stop-step.ts +28 -0
- package/src/browser/probe-host-process.ts +11 -0
- package/src/browser/probe-process.ts +35 -0
- package/src/browser/probe-run.ts +38 -0
- package/src/browser/process-group-still-running-error.ts +9 -0
- package/src/browser/process-group-stop-error.ts +10 -0
- package/src/browser/process-groups.ts +45 -0
- package/src/browser/process-runtime.ts +40 -0
- package/src/browser/read-dir-names.ts +15 -0
- package/src/browser/read-process-table.ts +35 -0
- package/src/browser/remove-exited-run-dirs.ts +21 -0
- package/src/browser/remove-path.ts +5 -0
- package/src/browser/run-in-page.ts +103 -0
- package/src/browser/should-keep-run-folders.ts +7 -0
- package/src/browser/start-browser.ts +40 -0
- package/src/browser/start-process-group.ts +168 -0
- package/src/browser/wire-browser-lane-hooks.ts +88 -0
- package/src/build-diet-command.ts +11 -0
- package/src/build-init-scripts.ts +40 -0
- package/src/build-test-selection.ts +124 -0
- package/src/build-unit-command.ts +22 -0
- package/src/cli.ts +73 -0
- package/src/define-rig.ts +195 -0
- package/src/find-timeout-arg.ts +3 -0
- package/src/format-no-match-message.ts +3 -0
- package/src/format-no-tests-message.ts +3 -0
- package/src/index.ts +14 -0
- package/src/init-files.ts +15 -0
- package/src/is-command-name.ts +9 -0
- package/src/lane-hooks.ts +15 -0
- package/src/make-repo-lane-hooks.ts +70 -0
- package/src/parse-duration.ts +14 -0
- package/src/parse-lane-hook.ts +20 -0
- package/src/parse-lane-start.ts +30 -0
- package/src/parse-rig-config.ts +96 -0
- package/src/parse-test-args.ts +14 -0
- package/src/pick-e2e-dirs.ts +25 -0
- package/src/pick-lane-selection.ts +14 -0
- package/src/read-cli-args.ts +57 -0
- package/src/read-memory-budget.ts +65 -0
- package/src/read-rig-config.ts +32 -0
- package/src/read-tracked-files.ts +20 -0
- package/src/read-worker-count.ts +40 -0
- package/src/resolve-diet-script.ts +7 -0
- package/src/resolve-related-tests.ts +52 -0
- package/src/run-cli.ts +68 -0
- package/src/run-command.ts +36 -0
- package/src/run-init.ts +71 -0
- package/src/run-lane-hooks.ts +49 -0
- package/src/run-lane.ts +138 -0
- package/src/run-related-tests.ts +113 -0
- package/src/run-unit-tests.ts +42 -0
- package/src/run-unit.ts +68 -0
- package/src/standard-lanes.ts +23 -0
- package/src/system-clock.ts +10 -0
- package/src/types.ts +75 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
|
|
3
|
+
export interface BrowserRun {
|
|
4
|
+
readonly runID: string;
|
|
5
|
+
|
|
6
|
+
readonly runsDir: string;
|
|
7
|
+
|
|
8
|
+
readonly runDir: string;
|
|
9
|
+
|
|
10
|
+
readonly groupsDir: string;
|
|
11
|
+
|
|
12
|
+
readonly buildDir: string;
|
|
13
|
+
|
|
14
|
+
readonly profilesDir: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// The run's folders sit in the repo's git-ignored cache, never in TMPDIR, because a host's TMPDIR
|
|
18
|
+
// can sit in memory, and each Chrome profile takes megabytes. A container that mounts the repo
|
|
19
|
+
// sees other process IDs than its host, so each host name keeps its own runs.
|
|
20
|
+
export function buildBrowserRun(repoRoot: string, hostName: string, runID: string): BrowserRun {
|
|
21
|
+
const runsDir = join(repoRoot, 'node_modules', '.cache', 'rig', 'browser', hostName);
|
|
22
|
+
const runDir = join(runsDir, runID);
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
runID,
|
|
26
|
+
runsDir,
|
|
27
|
+
runDir,
|
|
28
|
+
groupsDir: join(runDir, 'groups'),
|
|
29
|
+
buildDir: join(runDir, 'build'),
|
|
30
|
+
profilesDir: join(runDir, 'profiles'),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { BrowserRun } from './build-browser-run.ts';
|
|
2
|
+
import { buildDirVariable, runIDVariable } from './find-browser-run.ts';
|
|
3
|
+
|
|
4
|
+
type Environment = Readonly<Record<string, string | undefined>>;
|
|
5
|
+
|
|
6
|
+
export interface LaneEnvironments {
|
|
7
|
+
readonly buildEnvironment: Environment;
|
|
8
|
+
|
|
9
|
+
readonly testsEnvironment: Environment;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Bun starts Chrome with the environment the test process started with, so Chrome's TMPDIR goes in
|
|
13
|
+
// the tests' environment.
|
|
14
|
+
export function buildLaneEnvironments(
|
|
15
|
+
environment: Environment,
|
|
16
|
+
run: BrowserRun,
|
|
17
|
+
chromeRunDir: string,
|
|
18
|
+
hasBuild: boolean,
|
|
19
|
+
): LaneEnvironments {
|
|
20
|
+
// A build folder that the caller's environment names belongs to no build of this run.
|
|
21
|
+
const { [buildDirVariable]: _, ...baseEnvironment } = environment;
|
|
22
|
+
const buildDirEnvironment = hasBuild ? { [buildDirVariable]: run.buildDir } : {};
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
buildEnvironment: { ...baseEnvironment, ...buildDirEnvironment },
|
|
26
|
+
testsEnvironment: {
|
|
27
|
+
...baseEnvironment,
|
|
28
|
+
...buildDirEnvironment,
|
|
29
|
+
[runIDVariable]: run.runID,
|
|
30
|
+
TMPDIR: chromeRunDir,
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// The shell leads its process group, so its process ID is the group ID. It records the group by
|
|
2
|
+
// moving the lane's pending record, and a rename either wins or loses against a sweep's removal,
|
|
3
|
+
// so a command whose record the sweep removed never starts.
|
|
4
|
+
const recordScript = 'mv -- "$1" "$0/$$" && shift && exec "$@"';
|
|
5
|
+
|
|
6
|
+
export function buildRecordedCommand(
|
|
7
|
+
groupsDir: string,
|
|
8
|
+
pendingRecord: string,
|
|
9
|
+
command: readonly string[],
|
|
10
|
+
): readonly string[] {
|
|
11
|
+
return ['/bin/sh', '-c', recordScript, groupsDir, pendingRecord, ...command];
|
|
12
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { BrowserRun } from './build-browser-run.ts';
|
|
2
|
+
import { buildBrowserRun } from './build-browser-run.ts';
|
|
3
|
+
|
|
4
|
+
export const runIDVariable = 'RIG_BROWSER_RUN_ID';
|
|
5
|
+
export const buildDirVariable = 'RIG_BROWSER_BUILD_DIR';
|
|
6
|
+
|
|
7
|
+
export function findBrowserRun(
|
|
8
|
+
repoRoot: string,
|
|
9
|
+
hostName: string,
|
|
10
|
+
environment: Readonly<Record<string, string | undefined>>,
|
|
11
|
+
): BrowserRun | null {
|
|
12
|
+
const runID = environment[runIDVariable];
|
|
13
|
+
|
|
14
|
+
return runID === undefined || runID === '' ? null : buildBrowserRun(repoRoot, hostName, runID);
|
|
15
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { PathStatus } from './lane-files.ts';
|
|
2
|
+
|
|
3
|
+
export function findPrivateDirProblem(
|
|
4
|
+
path: string,
|
|
5
|
+
status: PathStatus | null,
|
|
6
|
+
userID: number,
|
|
7
|
+
): string | null {
|
|
8
|
+
if (status === null || !status.isDirectory) {
|
|
9
|
+
return `the browser lane's temp folder ${path} is no folder of its own`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (status.ownerID !== userID) {
|
|
13
|
+
return `the browser lane's temp folder ${path} belongs to user ${status.ownerID}, not to user ${userID}`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const openBits = status.mode & 0o077;
|
|
17
|
+
|
|
18
|
+
return openBits === 0
|
|
19
|
+
? null
|
|
20
|
+
: `the browser lane's temp folder ${path} is open to other users, with mode ${(status.mode & 0o777).toString(8)}`;
|
|
21
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { lstat, mkdir } from 'node:fs/promises';
|
|
2
|
+
import { hasErrorCode } from './has-error-code.ts';
|
|
3
|
+
import { readDirNames } from './read-dir-names.ts';
|
|
4
|
+
import { removePath } from './remove-path.ts';
|
|
5
|
+
|
|
6
|
+
export interface PathStatus {
|
|
7
|
+
readonly isDirectory: boolean;
|
|
8
|
+
|
|
9
|
+
readonly ownerID: number;
|
|
10
|
+
|
|
11
|
+
readonly mode: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface LaneFiles {
|
|
15
|
+
readonly createPrivateDir: (path: string) => Promise<void>;
|
|
16
|
+
|
|
17
|
+
readonly createDirs: (path: string) => Promise<void>;
|
|
18
|
+
|
|
19
|
+
readonly readStatus: (path: string) => Promise<PathStatus | null>;
|
|
20
|
+
|
|
21
|
+
readonly readDirNames: (path: string) => Promise<readonly string[]>;
|
|
22
|
+
|
|
23
|
+
readonly removePath: (path: string) => Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const laneFiles: LaneFiles = {
|
|
27
|
+
createPrivateDir: async (path) => {
|
|
28
|
+
await mkdir(path, { mode: 0o700 }).catch((error: unknown) => {
|
|
29
|
+
if (!hasErrorCode(error, 'EEXIST')) {
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
createDirs: async (path) => {
|
|
35
|
+
await mkdir(path, { recursive: true, mode: 0o700 });
|
|
36
|
+
},
|
|
37
|
+
readStatus: async (path) => {
|
|
38
|
+
const stats = await lstat(path).catch((error: unknown) => {
|
|
39
|
+
if (hasErrorCode(error, 'ENOENT')) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
throw error;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return stats === null
|
|
47
|
+
? null
|
|
48
|
+
: {
|
|
49
|
+
isDirectory: stats.isDirectory(),
|
|
50
|
+
ownerID: stats.uid,
|
|
51
|
+
mode: stats.mode,
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
readDirNames,
|
|
55
|
+
removePath,
|
|
56
|
+
};
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import type { LaneHooks, LaneStartOutcome } from '../lane-hooks.ts';
|
|
3
|
+
import type { BrowserRun } from './build-browser-run.ts';
|
|
4
|
+
import type { LaneEnvironments } from './build-lane-environments.ts';
|
|
5
|
+
import { buildLaneEnvironments } from './build-lane-environments.ts';
|
|
6
|
+
import { findPrivateDirProblem } from './find-private-dir-problem.ts';
|
|
7
|
+
import type { LaneFiles } from './lane-files.ts';
|
|
8
|
+
import type { ChromeDirs } from './pick-chrome-dirs.ts';
|
|
9
|
+
import { pickExitCodeAfterTests } from './pick-exit-code-after-tests.ts';
|
|
10
|
+
import { pickStepAfterBuild } from './pick-step-after-build.ts';
|
|
11
|
+
import { pendingRecordPrefix, probeRun } from './probe-run.ts';
|
|
12
|
+
import type { ProcessGroups } from './process-groups.ts';
|
|
13
|
+
import { removeExitedRunDirs } from './remove-exited-run-dirs.ts';
|
|
14
|
+
import { shouldKeepRunFolders } from './should-keep-run-folders.ts';
|
|
15
|
+
|
|
16
|
+
type Environment = Readonly<Record<string, string | undefined>>;
|
|
17
|
+
|
|
18
|
+
export interface BrowserLaneOptions {
|
|
19
|
+
readonly run: BrowserRun;
|
|
20
|
+
|
|
21
|
+
readonly chromeDirs: ChromeDirs;
|
|
22
|
+
|
|
23
|
+
readonly userID: number;
|
|
24
|
+
|
|
25
|
+
readonly preloadPath: string;
|
|
26
|
+
|
|
27
|
+
readonly environment: Environment;
|
|
28
|
+
|
|
29
|
+
readonly signal: AbortSignal;
|
|
30
|
+
|
|
31
|
+
readonly files: LaneFiles;
|
|
32
|
+
|
|
33
|
+
readonly probeProcess: (pid: number) => boolean;
|
|
34
|
+
|
|
35
|
+
readonly groups: Pick<ProcessGroups, 'probeGroup'>;
|
|
36
|
+
|
|
37
|
+
readonly runBuild:
|
|
38
|
+
| ((environment: Environment, signal: AbortSignal, pendingRecord: string) => Promise<number>)
|
|
39
|
+
| null;
|
|
40
|
+
|
|
41
|
+
readonly reportCleanupError: (error: unknown) => void;
|
|
42
|
+
|
|
43
|
+
readonly runTests: (
|
|
44
|
+
testArgs: readonly string[],
|
|
45
|
+
environment: Environment,
|
|
46
|
+
signal: AbortSignal,
|
|
47
|
+
pendingRecord: string,
|
|
48
|
+
) => Promise<number>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// `bun test --parallel=1` isolates each file, so the preload runs again for each one, and the
|
|
52
|
+
// build and the run ID come from here, once per run.
|
|
53
|
+
export function makeBrowserLaneHooks(options: BrowserLaneOptions): LaneHooks {
|
|
54
|
+
const state = { hasRunFolders: false, keepsFolders: false };
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
start: () => startBrowserLane(options, state),
|
|
58
|
+
run: (testArgs, environment) =>
|
|
59
|
+
guardRun(state, () => runBrowserTests(testArgs, environment, options)),
|
|
60
|
+
stop: () => stopBrowserLane(options, state),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface BrowserLaneState {
|
|
65
|
+
hasRunFolders: boolean;
|
|
66
|
+
|
|
67
|
+
keepsFolders: boolean;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function startBrowserLane(
|
|
71
|
+
options: BrowserLaneOptions,
|
|
72
|
+
state: BrowserLaneState,
|
|
73
|
+
): Promise<LaneStartOutcome> {
|
|
74
|
+
const run = options.run;
|
|
75
|
+
const chromeDirs = options.chromeDirs;
|
|
76
|
+
const files = options.files;
|
|
77
|
+
|
|
78
|
+
// Chrome's temp folder can sit in a /tmp that every user of the host shares, so its private
|
|
79
|
+
// folder is checked before the lane writes or removes anything inside it.
|
|
80
|
+
await files.createPrivateDir(chromeDirs.privateDir);
|
|
81
|
+
|
|
82
|
+
const privateDirStatus = await files.readStatus(chromeDirs.privateDir);
|
|
83
|
+
|
|
84
|
+
const privateDirProblem = findPrivateDirProblem(
|
|
85
|
+
chromeDirs.privateDir,
|
|
86
|
+
privateDirStatus,
|
|
87
|
+
options.userID,
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
if (privateDirProblem !== null) {
|
|
91
|
+
throw new Error(privateDirProblem);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const probeOlderRun = (runID: string) =>
|
|
95
|
+
probeRun(runID, join(run.runsDir, runID, 'groups'), options);
|
|
96
|
+
|
|
97
|
+
await Promise.all([
|
|
98
|
+
removeExitedRunDirs(run.runsDir, files, probeOlderRun),
|
|
99
|
+
removeExitedRunDirs(chromeDirs.runsDir, files, probeOlderRun),
|
|
100
|
+
]);
|
|
101
|
+
|
|
102
|
+
const environments = buildLaneEnvironments(
|
|
103
|
+
options.environment,
|
|
104
|
+
run,
|
|
105
|
+
chromeDirs.runDir,
|
|
106
|
+
options.runBuild !== null,
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
state.hasRunFolders = true;
|
|
110
|
+
|
|
111
|
+
return guardRun(state, async () => {
|
|
112
|
+
await createDirs([run.profilesDir, run.groupsDir, chromeDirs.runDir], files);
|
|
113
|
+
|
|
114
|
+
const buildExitCode =
|
|
115
|
+
options.runBuild === null ? null : await runBuild(options.runBuild, environments, options);
|
|
116
|
+
|
|
117
|
+
const step = pickStepAfterBuild(buildExitCode, options.signal.aborted);
|
|
118
|
+
|
|
119
|
+
return step.kind === 'finish'
|
|
120
|
+
? step
|
|
121
|
+
: { kind: 'proceed', environment: environments.testsEnvironment };
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// A process group that the lane could not stop can still write to the run's folders, so the stop
|
|
126
|
+
// step leaves them in place, and the error says so.
|
|
127
|
+
async function guardRun<Result>(
|
|
128
|
+
state: BrowserLaneState,
|
|
129
|
+
step: () => Promise<Result>,
|
|
130
|
+
): Promise<Result> {
|
|
131
|
+
try {
|
|
132
|
+
return await step();
|
|
133
|
+
} catch (error) {
|
|
134
|
+
state.keepsFolders = shouldKeepRunFolders(error);
|
|
135
|
+
|
|
136
|
+
if (state.keepsFolders) {
|
|
137
|
+
throw new Error(
|
|
138
|
+
'the browser lane keeps its run folders, because it could not stop a process group',
|
|
139
|
+
{ cause: error },
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// A failed mkdir rejects Promise.all while the other still runs, and that one would make its
|
|
148
|
+
// folder again after the cleanup, so the lane waits for both.
|
|
149
|
+
async function createDirs(paths: readonly string[], files: LaneFiles): Promise<void> {
|
|
150
|
+
const created = await Promise.allSettled(paths.map((path) => files.createDirs(path)));
|
|
151
|
+
|
|
152
|
+
const failure = created.find(
|
|
153
|
+
(result): result is PromiseRejectedResult => result.status === 'rejected',
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
if (failure !== undefined) {
|
|
157
|
+
throw new Error('the browser lane could not create its folders', { cause: failure.reason });
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function runBuild(
|
|
162
|
+
build: NonNullable<BrowserLaneOptions['runBuild']>,
|
|
163
|
+
environments: LaneEnvironments,
|
|
164
|
+
options: BrowserLaneOptions,
|
|
165
|
+
): Promise<number> {
|
|
166
|
+
const buildRecord = await createPendingRecord(options.run.groupsDir, 'build', options.files);
|
|
167
|
+
|
|
168
|
+
return build(environments.buildEnvironment, options.signal, buildRecord);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// The lane makes each command's pending record before the command starts, so a sweep never sees a
|
|
172
|
+
// run whose lane has exited and whose command can still record its group.
|
|
173
|
+
async function createPendingRecord(
|
|
174
|
+
groupsDir: string,
|
|
175
|
+
commandName: string,
|
|
176
|
+
files: LaneFiles,
|
|
177
|
+
): Promise<string> {
|
|
178
|
+
const pendingRecord = join(groupsDir, `${pendingRecordPrefix}${commandName}`);
|
|
179
|
+
|
|
180
|
+
await files.createDirs(pendingRecord);
|
|
181
|
+
|
|
182
|
+
return pendingRecord;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async function runBrowserTests(
|
|
186
|
+
testArgs: readonly string[],
|
|
187
|
+
environment: Environment,
|
|
188
|
+
options: BrowserLaneOptions,
|
|
189
|
+
): Promise<number> {
|
|
190
|
+
const testsRecord = await createPendingRecord(options.run.groupsDir, 'tests', options.files);
|
|
191
|
+
|
|
192
|
+
const testsExitCode = await options.runTests(
|
|
193
|
+
['--parallel=1', '--preload', options.preloadPath, ...testArgs],
|
|
194
|
+
environment,
|
|
195
|
+
options.signal,
|
|
196
|
+
testsRecord,
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
return pickExitCodeAfterTests(testsExitCode, options.signal.aborted);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async function stopBrowserLane(
|
|
203
|
+
options: BrowserLaneOptions,
|
|
204
|
+
state: BrowserLaneState,
|
|
205
|
+
): Promise<void> {
|
|
206
|
+
if (state.hasRunFolders && !state.keepsFolders) {
|
|
207
|
+
await removeDirs(
|
|
208
|
+
[options.run.runDir, options.chromeDirs.runDir],
|
|
209
|
+
options.files,
|
|
210
|
+
options.reportCleanupError,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// A throw from the cleanup would hide the lane's own error, so the lane reports a removal that fails and goes on.
|
|
216
|
+
async function removeDirs(
|
|
217
|
+
paths: readonly string[],
|
|
218
|
+
files: LaneFiles,
|
|
219
|
+
reportCleanupError: (error: unknown) => void,
|
|
220
|
+
): Promise<void> {
|
|
221
|
+
const removed = await Promise.allSettled(paths.map((path) => files.removePath(path)));
|
|
222
|
+
|
|
223
|
+
for (const result of removed) {
|
|
224
|
+
if (result.status === 'rejected') {
|
|
225
|
+
reportCleanupError(result.reason);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import type { BrowserRun } from './build-browser-run.ts';
|
|
4
|
+
|
|
5
|
+
export interface ChromeDirs {
|
|
6
|
+
readonly privateDir: string;
|
|
7
|
+
|
|
8
|
+
readonly runsDir: string;
|
|
9
|
+
|
|
10
|
+
readonly runDir: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Chrome binds its singleton socket at <TMPDIR>/org.chromium.Chromium.XXXXXX/SingletonSocket and
|
|
14
|
+
// stops at startup when that path passes the 107 bytes a Unix socket address holds.
|
|
15
|
+
const socketPathMaxBytes = 107;
|
|
16
|
+
const socketSuffix = '/org.chromium.Chromium.XXXXXX/SingletonSocket';
|
|
17
|
+
|
|
18
|
+
// Every user of a host shares /tmp, so each user's runs get a parent folder of their own. Each
|
|
19
|
+
// repo and host name gets a folder inside it, so a sweep reads only the runs it can probe.
|
|
20
|
+
export function pickChromeDirs(tmpdir: string, userID: number, run: BrowserRun): ChromeDirs {
|
|
21
|
+
const privateName = `rig-browser-${userID}`;
|
|
22
|
+
const runsKey = createHash('sha256').update(run.runsDir).digest('hex').slice(0, 8);
|
|
23
|
+
const preferred = buildChromeDirs(join(tmpdir, privateName), runsKey, run.runID);
|
|
24
|
+
|
|
25
|
+
return Buffer.byteLength(`${preferred.runDir}${socketSuffix}`) <= socketPathMaxBytes
|
|
26
|
+
? preferred
|
|
27
|
+
: buildChromeDirs(join('/tmp', privateName), runsKey, run.runID);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function buildChromeDirs(privateDir: string, runsKey: string, runID: string): ChromeDirs {
|
|
31
|
+
const runsDir = join(privateDir, runsKey);
|
|
32
|
+
|
|
33
|
+
return { privateDir, runsDir, runDir: join(runsDir, runID) };
|
|
34
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// A leader can exit 0 before an abort reaches it, while the lane still stops the processes it left,
|
|
2
|
+
// so an aborted run fails whatever the tests' exit code.
|
|
3
|
+
export function pickExitCodeAfterTests(testsExitCode: number, isAborted: boolean): number {
|
|
4
|
+
return isAborted ? 1 : testsExitCode;
|
|
5
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type StepAfterBuild =
|
|
2
|
+
| { readonly kind: 'run-tests' }
|
|
3
|
+
| { readonly kind: 'finish'; readonly exitCode: number };
|
|
4
|
+
|
|
5
|
+
// A null exit code means the repo has no build. An aborted run starts no tests and fails, whatever
|
|
6
|
+
// the build's exit code.
|
|
7
|
+
export function pickStepAfterBuild(
|
|
8
|
+
buildExitCode: number | null,
|
|
9
|
+
isAborted: boolean,
|
|
10
|
+
): StepAfterBuild {
|
|
11
|
+
if (isAborted) {
|
|
12
|
+
return { kind: 'finish', exitCode: 1 };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (buildExitCode !== null && buildExitCode !== 0) {
|
|
16
|
+
return { kind: 'finish', exitCode: buildExitCode };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return { kind: 'run-tests' };
|
|
20
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { match } from 'ts-pattern';
|
|
2
|
+
|
|
3
|
+
export type StopPhase = 'before-stop' | 'after-sigterm' | 'after-sigkill';
|
|
4
|
+
|
|
5
|
+
export interface StopObservation {
|
|
6
|
+
readonly phase: StopPhase;
|
|
7
|
+
|
|
8
|
+
readonly isGroupRunning: boolean;
|
|
9
|
+
|
|
10
|
+
readonly isPastDeadline: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type StopStep =
|
|
14
|
+
| { readonly kind: 'done' }
|
|
15
|
+
| { readonly kind: 'signal'; readonly signal: 'SIGTERM' | 'SIGKILL' }
|
|
16
|
+
| { readonly kind: 'wait' }
|
|
17
|
+
| { readonly kind: 'fail' };
|
|
18
|
+
|
|
19
|
+
// A process can ignore SIGTERM, so the group gets SIGKILL once the deadline after SIGTERM passes.
|
|
20
|
+
export function pickStopStep(observation: StopObservation): StopStep {
|
|
21
|
+
return match<StopObservation, StopStep>(observation)
|
|
22
|
+
.with({ isGroupRunning: false }, () => ({ kind: 'done' }))
|
|
23
|
+
.with({ phase: 'before-stop' }, () => ({ kind: 'signal', signal: 'SIGTERM' }))
|
|
24
|
+
.with({ isPastDeadline: false }, () => ({ kind: 'wait' }))
|
|
25
|
+
.with({ phase: 'after-sigterm' }, () => ({ kind: 'signal', signal: 'SIGKILL' }))
|
|
26
|
+
.with({ phase: 'after-sigkill' }, () => ({ kind: 'fail' }))
|
|
27
|
+
.exhaustive();
|
|
28
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { probeProcess } from './probe-process.ts';
|
|
2
|
+
import { readProcessTable } from './read-process-table.ts';
|
|
3
|
+
|
|
4
|
+
export function probeHostProcess(pid: number): boolean {
|
|
5
|
+
return probeProcess(pid, {
|
|
6
|
+
signalProcess: (target, signal) => {
|
|
7
|
+
process.kill(target, signal);
|
|
8
|
+
},
|
|
9
|
+
readProcessTable,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { hasErrorCode } from './has-error-code.ts';
|
|
2
|
+
import { isZombieState } from './is-zombie-state.ts';
|
|
3
|
+
import type { ProcessEntry } from './read-process-table.ts';
|
|
4
|
+
|
|
5
|
+
export interface ProcessProbeDependencies {
|
|
6
|
+
// Throws as `process.kill` does: ESRCH for a missing process, EPERM for another user's.
|
|
7
|
+
readonly signalProcess: (pid: number, signal: number) => void;
|
|
8
|
+
|
|
9
|
+
readonly readProcessTable: () => readonly ProcessEntry[] | null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// A signal of 0 checks the process without signalling it. EPERM means the process runs under
|
|
13
|
+
// another user. A signal of 0 also reaches a zombie, which a host whose first process never reaps
|
|
14
|
+
// its orphans keeps forever, so the probe reads the state too.
|
|
15
|
+
export function probeProcess(pid: number, dependencies: ProcessProbeDependencies): boolean {
|
|
16
|
+
try {
|
|
17
|
+
dependencies.signalProcess(pid, 0);
|
|
18
|
+
} catch (error) {
|
|
19
|
+
if (!hasErrorCode(error, 'EPERM')) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const table = dependencies.readProcessTable();
|
|
25
|
+
|
|
26
|
+
// A failed listing leaves the state unknown, and a sweep removes only what has exited, so the
|
|
27
|
+
// process counts as live.
|
|
28
|
+
if (table === null) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const entry = table.find((candidate) => candidate.pid === pid);
|
|
33
|
+
|
|
34
|
+
return entry !== undefined && !isZombieState(entry.state);
|
|
35
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import type { LaneFiles } from './lane-files.ts';
|
|
3
|
+
import type { ProcessGroups } from './process-groups.ts';
|
|
4
|
+
|
|
5
|
+
export interface RunProbeOptions {
|
|
6
|
+
readonly files: Pick<LaneFiles, 'readDirNames' | 'removePath'>;
|
|
7
|
+
|
|
8
|
+
readonly probeProcess: (pid: number) => boolean;
|
|
9
|
+
|
|
10
|
+
readonly groups: Pick<ProcessGroups, 'probeGroup'>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const pendingRecordPrefix = 'pending-';
|
|
14
|
+
|
|
15
|
+
// A lane that SIGKILL stopped leaves its process groups running. An exited lane makes no more
|
|
16
|
+
// pending records, and a command starts only after it moves one, so once they are gone, the
|
|
17
|
+
// groups folder holds every group the run can ever have.
|
|
18
|
+
export async function probeRun(
|
|
19
|
+
runID: string,
|
|
20
|
+
groupsDir: string,
|
|
21
|
+
options: RunProbeOptions,
|
|
22
|
+
): Promise<boolean> {
|
|
23
|
+
if (options.probeProcess(Number(runID))) {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const names = await options.files.readDirNames(groupsDir);
|
|
28
|
+
|
|
29
|
+
const pendingRecords = names.filter((name) => name.startsWith(pendingRecordPrefix));
|
|
30
|
+
|
|
31
|
+
await Promise.all(pendingRecords.map((name) => options.files.removePath(join(groupsDir, name))));
|
|
32
|
+
|
|
33
|
+
const groupIDs = await options.files.readDirNames(groupsDir);
|
|
34
|
+
|
|
35
|
+
return groupIDs
|
|
36
|
+
.filter((groupID) => /^\d+$/u.test(groupID))
|
|
37
|
+
.some((groupID) => options.groups.probeGroup(Number(groupID)));
|
|
38
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ProcessGroupStopError } from './process-group-stop-error.ts';
|
|
2
|
+
|
|
3
|
+
export class ProcessGroupStillRunningError extends ProcessGroupStopError {
|
|
4
|
+
constructor(groupID: number) {
|
|
5
|
+
super(groupID, `the process group ${groupID} still runs after SIGKILL`);
|
|
6
|
+
|
|
7
|
+
this.name = 'ProcessGroupStillRunningError';
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { hasErrorCode } from './has-error-code.ts';
|
|
2
|
+
import { isZombieState } from './is-zombie-state.ts';
|
|
3
|
+
import { readProcessTable } from './read-process-table.ts';
|
|
4
|
+
|
|
5
|
+
export interface ProcessGroups {
|
|
6
|
+
readonly signalGroup: (groupID: number, signal: NodeJS.Signals) => void;
|
|
7
|
+
|
|
8
|
+
readonly probeGroup: (groupID: number) => boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const processGroups: ProcessGroups = {
|
|
12
|
+
signalGroup: (groupID, signal) => {
|
|
13
|
+
try {
|
|
14
|
+
process.kill(-groupID, signal);
|
|
15
|
+
} catch (error) {
|
|
16
|
+
if (!hasErrorCode(error, 'ESRCH')) {
|
|
17
|
+
throw error;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
// A signal of 0 checks the group without signalling it. EPERM means a member runs under another
|
|
23
|
+
// user.
|
|
24
|
+
probeGroup: (groupID) => {
|
|
25
|
+
try {
|
|
26
|
+
process.kill(-groupID, 0);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
return hasErrorCode(error, 'EPERM');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return probeLiveMember(groupID);
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// A signal of 0 also reaches a zombie, which a host whose first process never reaps its orphans
|
|
36
|
+
// keeps forever, so the group counts as live only while a member that is no zombie runs. A failed
|
|
37
|
+
// listing leaves the group's state unknown, so it counts as live.
|
|
38
|
+
function probeLiveMember(groupID: number): boolean {
|
|
39
|
+
const table = readProcessTable();
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
table === null ||
|
|
43
|
+
table.some((entry) => entry.groupID === groupID && !isZombieState(entry.state))
|
|
44
|
+
);
|
|
45
|
+
}
|