Skip to content

Commit 88ba062

Browse files
mchwangclaude
andcommitted
Sweeps keep the Docker daemon set when they loaded (#189)
Review round 33: a test that times out may leave DOCKER_HOST changed (agent-supervisor points it at a missing socket until its handle settles); every call in docker-sweep.ts now restores DOCKER_HOST and DOCKER_CONTEXT as they were at load, with a unit test that fails without it; the context fake says its path is shortened. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 08d1a65 commit 88ba062

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎test/docker-sweep.test.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ it('removes by name only an object of exactly that name, and reports failures ot
5656
// Answers as Docker 27.4 does for `inspect --format '{{json .Id}} {{json .Name}}'` (a volume: '{{json .Name}}') and
5757
// `rm`, in the parts removeByName reads: stdout of an inspect that succeeded, stderr and the exit status. A missing
5858
// object, an object found under another name (by an ID prefix), a refused removal, an inspect killed by a signal, and a
59-
// failure before the daemon (an unknown context). The stdout of a failed call, a bare newline in 27.4, is not read.
59+
// failure before the daemon (an unknown context; its meta.json path shortened). The stdout of a failed call, a bare
60+
// newline in 27.4, is not read.
6061
writeFileSync(join(root, 'docker'), ['#!/bin/sh', `printf '%s\\n' "$*" >> '${calls}'`,
6162
'for arg; do name="$arg"; done',
6263
'if [ "$2" = inspect ]; then case "$1 $name" in',
@@ -94,3 +95,22 @@ it('removes by name only an object of exactly that name, and reports failures ot
9495
'Failed to initialize: unable to resolve docker endpoint: context "x": context not found: open /x/meta.json: no such file or directory'],
9596
]);
9697
});
98+
99+
it('talks to the daemon set when it loaded, though a test changed DOCKER_HOST or DOCKER_CONTEXT since', () => {
100+
const root = mkdtempSync(join(tmpdir(), 'codeboost-sweep-')); roots.push(root);
101+
const seen = join(root, 'seen');
102+
// Records the address each call is given, and answers as Docker 27.4 does for a missing container and an empty `ls`.
103+
writeFileSync(join(root, 'docker'), ['#!/bin/sh', `printf '%s|%s\\n' "\${DOCKER_HOST-unset}" "\${DOCKER_CONTEXT-unset}" >> '${seen}'`,
104+
'if [ "$2" = inspect ]; then echo; echo "Error response from daemon: No such container: x" >&2; exit 1; fi', 'exit 0'].join('\n'),
105+
{ mode: 0o755 });
106+
const loaded = `${process.env.DOCKER_HOST ?? 'unset'}|${process.env.DOCKER_CONTEXT ?? 'unset'}`;
107+
vi.stubEnv('DOCKER_HOST', 'unix:///codeboost-missing.sock');
108+
vi.stubEnv('DOCKER_CONTEXT', 'codeboost-missing');
109+
try {
110+
removeByName('container', 'x', join(root, 'docker'));
111+
removeLabelledObjects(TEST_RUN_LABEL, 'x', 'test', join(root, 'docker'));
112+
} finally { vi.unstubAllEnvs(); }
113+
const lines = readFileSync(seen, 'utf8').split('\n').filter(Boolean);
114+
expect(lines.length).toBeGreaterThan(1);
115+
expect(new Set(lines)).toEqual(new Set([loaded]));
116+
});

‎test/fixtures/docker-sweep.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ import { RUNNER_LABEL } from '../../agents/labels.ts';
77
* call's SIGKILL does not reach.
88
*/
99
export const REAL_DOCKER = spawnSync('sh', ['-c', 'command -v docker'], { encoding: 'utf8' }).stdout?.trim() || 'docker';
10+
/**
11+
* The daemon `docker` talks to when this module loads. A test that times out may leave DOCKER_HOST or DOCKER_CONTEXT
12+
* changed (agent-supervisor points DOCKER_HOST at a missing socket until its handle settles), so every call here
13+
* restores them.
14+
*/
15+
const LOADED_ENV = { DOCKER_HOST: process.env.DOCKER_HOST, DOCKER_CONTEXT: process.env.DOCKER_CONTEXT };
16+
const dockerEnv = () => {
17+
const env = { ...process.env };
18+
for (const [name, value] of Object.entries(LOADED_ENV)) if (value === undefined) delete env[name]; else env[name] = value;
19+
return env;
20+
};
1021
/** The real `docker` for a shim to call; refuses when none was found, since a shim calling `docker` would call itself. */
1122
export function requireRealDocker(): string {
1223
if (!REAL_DOCKER.startsWith('/')) throw new Error('No docker on PATH when the test files loaded.');
@@ -27,7 +38,7 @@ export const TEST_FILE_LABEL = 'io.codeboost.test-file';
2738
* reports a failed call, other than an inspect that finds no such object, and never throws; each call has a 30 s limit.
2839
*/
2940
export function removeByName(kind: 'container' | 'network' | 'volume', name: string, docker = REAL_DOCKER): void {
30-
const options = { encoding: 'utf8' as const, timeout: 30_000, killSignal: 'SIGKILL' as const };
41+
const options = { encoding: 'utf8' as const, timeout: 30_000, killSignal: 'SIGKILL' as const, env: dockerEnv() };
3142
const failed = (result: SpawnSyncReturns<string>, what: string) => console.warn(`removing ${kind} ${name}: docker ${what} failed:`,
3243
result.error?.message ?? (result.signal ? `killed by ${result.signal}` : result.stderr.trim()));
3344
// As JSON: a network's name may hold spaces. Only a container's name comes with a leading slash.
@@ -67,7 +78,7 @@ export function removeRunnerObjects(runnerOwner: string, when = 'afterAll'): voi
6778
*/
6879
export function removeLabelledObjects(label: string, value: string, when: string, docker = REAL_DOCKER): void {
6980
const run = (args: string[]) => {
70-
const result = spawnSync(docker, args, { encoding: 'utf8', timeout: 30_000, killSignal: 'SIGKILL' });
81+
const result = spawnSync(docker, args, { encoding: 'utf8', timeout: 30_000, killSignal: 'SIGKILL', env: dockerEnv() });
7182
if (result.status !== 0) console.warn(`${when}: docker ${args.slice(0, 2).join(' ')} failed:`,
7283
result.error?.message ?? (result.signal ? `killed by ${result.signal}` : result.stderr.trim()));
7384
// An inspect of several objects exits 1 if one is gone, yet prints the rest (Docker 27.4), so the output of a call

0 commit comments

Comments
 (0)