Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/host-kit/src/internal/host-proc.fixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import fs from 'node:fs';
import { vi } from 'vitest';

/**
* Answers the identity reader's `/proc` reads with nothing, so these files' `ps` doubles are the only
* evidence. The reader prefers one `/proc/<pid>/stat` read over the `ps` row it parses, so on a runner
* whose pid space has reached a fixture pid, the host's real state answered instead of the double and
* the case saw a lifetime nobody wrote.
*/
export function mockLinuxProcessStatAbsent(): void {
const readFileSync = fs.readFileSync;
vi.spyOn(fs, 'readFileSync').mockImplementation((path, options) => {
if (String(path).startsWith('/proc/')) {
throw Object.assign(new Error('proc is unavailable in the ps fixture'), { code: 'ENOENT' });
}
return readFileSync(path, options);
});
}
9 changes: 2 additions & 7 deletions packages/host-kit/src/internal/host-process-liveness.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
readProcessIdentities,
readProcessIdentityFacts,
} from './host-process.ts';
import { mockLinuxProcessStatAbsent } from './host-proc.fixtures.ts';

function psReturns(stdout: string, exitCode = 0): void {
mockRunCmdSync.mockReturnValue({ stdout, stderr: '', exitCode });
Expand All @@ -42,13 +43,7 @@ function psInstalledAt(installed: readonly string[]): void {
}

beforeEach(() => {
const readFileSync = fs.readFileSync;
vi.spyOn(fs, 'readFileSync').mockImplementation((path, options) => {
if (String(path).startsWith('/proc/')) {
throw Object.assign(new Error('proc is unavailable in the ps fixture'), { code: 'ENOENT' });
}
return readFileSync(path, options);
});
mockLinuxProcessStatAbsent();
mockRunCmdSync.mockReset();
mockRunCmd.mockReset();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'node:assert/strict';
import { afterEach, test, vi } from 'vitest';
import { afterEach, beforeEach, test, vi } from 'vitest';
import { mockLinuxProcessStatAbsent } from './host-proc.fixtures.ts';

// Every claim the stopper makes is about what the host answers, so one double answers both syscalls it
// reads: `kill(pid, signal)` for the signals and liveness probes, and `ps` for the identity fields.
Expand Down Expand Up @@ -317,6 +318,12 @@ function targetOf(model: HostCase): ProcessStopTarget {
};
}

beforeEach(() => {
// The identity reader reads `/proc/<pid>/stat` before the `ps` row, so an unmocked host can answer
// for the fixture pid. See host-proc.fixtures.ts.
mockLinuxProcessStatAbsent();
});

afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
Expand Down
4 changes: 4 additions & 0 deletions packages/host-kit/src/internal/owned-process-record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'node:fs';
import path from 'node:path';
import { afterEach, beforeEach, test, vi } from 'vitest';
import { mkdtempForTestSync } from './tmp-dir.fixtures.ts';
import { mockLinuxProcessStatAbsent } from './host-proc.fixtures.ts';

// The reaper stops through the shared escalation, so the double answers the two host syscalls it
// reads: `kill(pid, signal)` for signals and liveness, and `ps` for the recorded identity.
Expand Down Expand Up @@ -56,6 +57,9 @@ import { createOwnedProcessRecordStore } from './owned-process-record.ts';
import { reapOwnedProcessRecordsAtStartup } from './owned-process-reaper.ts';

beforeEach(() => {
// The identity reader reads `/proc/<pid>/stat` before the `ps` row, so an unmocked host can answer
// for the fixture pid. See host-proc.fixtures.ts.
mockLinuxProcessStatAbsent();
processState.alive.clear();
processState.starts.clear();
processState.commands.clear();
Expand Down
Loading