Reproduce and fix the launchd SIGTERMed shutdown race; wait for owned processes to exit and report timeouts. Describe local task workspaces without claiming OS isolation.
129 lines
5.0 KiB
JavaScript
129 lines
5.0 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
import { spawnSync } from 'node:child_process';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import {
|
|
LABEL,
|
|
launchAgentPlist,
|
|
minimalEnvironment,
|
|
parseServiceAction,
|
|
processStatus,
|
|
rotateAppend,
|
|
stopLocalService,
|
|
} from './local-poc-service.mjs';
|
|
|
|
test('stop waits through SIGTERMed and an orphaned owned child before reporting completion', async () => {
|
|
let time = 0,
|
|
inspected = 0,
|
|
checked = 0;
|
|
const calls = [];
|
|
const snapshots = [
|
|
{ registered: true, state: 'running', pid: 100, children: [{ name: 'runner', pid: 101 }] },
|
|
{ registered: true, state: 'SIGTERMed', pid: 100, children: [{ name: 'runner', pid: 101 }] },
|
|
{ registered: false, state: 'stopped', pid: null, children: [] },
|
|
{ registered: false, state: 'stopped', pid: null, children: [] },
|
|
];
|
|
const result = await stopLocalService({
|
|
inspect: () => snapshots[Math.min(inspected++, snapshots.length - 1)],
|
|
control: args => {
|
|
calls.push(args[0]);
|
|
},
|
|
remaining: tracked => {
|
|
assert.ok(tracked.some(item => item.pid === 101));
|
|
return ++checked < 3 ? [101] : [];
|
|
},
|
|
now: () => time,
|
|
sleep: async ms => {
|
|
time += ms;
|
|
},
|
|
intervalMs: 10,
|
|
timeoutMs: 100,
|
|
});
|
|
assert.equal(result.stopCompleted, true);
|
|
assert.equal(result.registered, false);
|
|
assert.equal(time, 20);
|
|
assert.deepEqual(calls, ['disable', 'bootout']);
|
|
});
|
|
|
|
test('stop has a bounded deadline and returns a failed diagnostic when owned processes remain', async () => {
|
|
let time = 0;
|
|
const result = await stopLocalService({
|
|
inspect: () => ({ registered: true, state: 'SIGTERMed', pid: 100, children: [] }),
|
|
control: () => undefined,
|
|
remaining: () => [100],
|
|
now: () => time,
|
|
sleep: async ms => {
|
|
time += ms;
|
|
},
|
|
intervalMs: 10,
|
|
timeoutMs: 30,
|
|
});
|
|
assert.equal(result.stopCompleted, false);
|
|
assert.deepEqual(result.remainingOwnedProcesses, [100]);
|
|
assert.equal(time, 30);
|
|
});
|
|
|
|
test('stop does not report success when process termination cannot be checked', async () => {
|
|
const result = await stopLocalService({
|
|
inspect: () => ({ registered: false, state: 'stopped', pid: null, children: [] }),
|
|
control: () => undefined,
|
|
remaining: () => null,
|
|
timeoutMs: 0,
|
|
});
|
|
assert.equal(result.stopCompleted, false);
|
|
assert.equal(result.remainingOwnedProcesses, null);
|
|
assert.match(result.stopDetail, /Could not verify/);
|
|
});
|
|
|
|
test('a restarted supervisor must not report children retained from an older process', () => {
|
|
const report = processStatus(
|
|
{ status: 0, stdout: 'state = running\n pid = 200\n' },
|
|
{ supervisorPid: 100, children: [{ name: 'runner', pid: 101 }] },
|
|
);
|
|
assert.deepEqual(report.children, []);
|
|
assert.equal(report.stateSnapshot, 'stale');
|
|
});
|
|
|
|
test('defaults to read-only status and rejects arbitrary service commands', () => {
|
|
assert.equal(parseServiceAction([]), 'status');
|
|
for (const action of ['install', 'start', 'stop', 'status']) assert.equal(parseServiceAction([action]), action);
|
|
for (const args of [['restart', 'other-service'], ['start', '--force'], ['killall']])
|
|
assert.throws(() => parseServiceAction(args));
|
|
});
|
|
|
|
test('launchd plist contains only fixed commands and is valid without registration', () => {
|
|
const temporary = mkdtempSync(path.join(os.tmpdir(), 'wishforge-launchd-test-'));
|
|
try {
|
|
const plist = launchAgentPlist('/owned/path & space/wishforge', '/owned/home');
|
|
assert.ok(plist.includes(LABEL));
|
|
assert.ok(plist.includes('/owned/path & space/wishforge/scripts/local-poc-service.mjs'));
|
|
assert.ok(plist.includes('<key>KeepAlive</key><true/>'));
|
|
assert.ok(plist.includes('<key>ThrottleInterval</key><integer>15</integer>'));
|
|
assert.ok(!plist.includes('WF_RUNNER_TOKEN'));
|
|
assert.ok(!plist.includes('.env.local'));
|
|
const file = path.join(temporary, 'agent.plist');
|
|
writeFileSync(file, plist);
|
|
if (process.platform === 'darwin') assert.equal(spawnSync('/usr/bin/plutil', ['-lint', file]).status, 0);
|
|
assert.deepEqual(Object.keys(minimalEnvironment()).sort(), ['HOME', 'LANG', 'PATH', 'TMPDIR']);
|
|
} finally {
|
|
rmSync(temporary, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test('private logs rotate with a bounded number of retained files', () => {
|
|
const temporary = mkdtempSync(path.join(os.tmpdir(), 'wishforge-log-test-'));
|
|
try {
|
|
const file = path.join(temporary, 'logs/service.log');
|
|
for (const marker of ['1111', '2222', '3333', '4444', '5555']) rotateAppend(file, marker, 5, 2);
|
|
assert.deepEqual(readdirSync(path.dirname(file)).sort(), ['service.log', 'service.log.1', 'service.log.2']);
|
|
assert.equal(readFileSync(file, 'utf8'), '5555');
|
|
assert.equal(readFileSync(`${file}.2`, 'utf8'), '3333');
|
|
assert.equal(statSync(file).mode & 0o777, 0o600);
|
|
assert.equal(statSync(path.dirname(file)).mode & 0o777, 0o700);
|
|
} finally {
|
|
rmSync(temporary, { recursive: true, force: true });
|
|
}
|
|
});
|