Skip to content

Commit bcf1e9f

Browse files
mchwangclaude
andcommitted
removeByName: tested on unreadable output, quiet on a network gone before its removal (#189)
Review round 44: the fake gives an inspect answer that is not JSON (reported, nothing removed, never thrown) and a network gone between inspect and removal, which Docker 27.4 answers 'network <ID> not found' and removeByName now treats as gone; the keeper comment says afterAll also removes it by name; a comment line is rewrapped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 698f341 commit bcf1e9f

3 files changed

Lines changed: 21 additions & 10 deletions

File tree

‎test/agent-container.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2564,7 +2564,8 @@ describe('real Docker agent isolation', () => {
25642564
containers.add(filesystems.keeper);
25652565
let passed = false;
25662566
try {
2567-
// Inside the try: this keeper carries another runner's label, so only the finally below removes it (#189).
2567+
// Inside the try: this keeper carries another runner's label, so no sweep of this run finds it; the finally below
2568+
// removes it, and afterAll by name (#189).
25682569
docker('run', '--detach', '--name', filesystems.keeper, '--label', `io.codeboost.runner=${'f'.repeat(32)}`,
25692570
'--label', `io.codeboost.attempt=${owner.attemptId}`, '--label', `io.codeboost.allocation=${owner.allocationId}`,
25702571
'--network=none', '--entrypoint', 'sleep', imageId, 'infinity');

‎test/docker-sweep.test.ts‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@ it('reads nothing from a docker call that was killed, whose output may be cut sh
5555

5656
it('removes by name only an object of exactly that name, and reports failures other than no such object', () => {
5757
const root = mkdtempSync(join(tmpdir(), 'codeboost-sweep-')); roots.push(root);
58-
const calls = join(root, 'calls'), id = 'b'.repeat(64), other = 'c'.repeat(64);
58+
const calls = join(root, 'calls'), id = 'b'.repeat(64), other = 'c'.repeat(64), raced = 'd'.repeat(64);
5959
// Answers as Docker 27.4 does for `inspect --format '{{json .Id}} {{json .Name}}'` (a volume: '{{json .Name}}') and
6060
// `rm`, in the parts removeByName reads: stdout of an inspect that succeeded, stderr and the exit status. A missing
6161
// object, an object found under another name (by an ID prefix), a refused removal, an inspect killed by a signal, and a
62-
// failure before the daemon (an unknown context; its meta.json path shortened). The stdout of a failed call (a bare
63-
// newline for a missing object in 27.4, nothing for the others) is not read.
62+
// failure before the daemon (an unknown context; its meta.json path shortened). Also a network gone between its
63+
// inspect and its removal, which 27.4 answers "network <ID> not found", and an inspect answer that is not JSON, which
64+
// 27.4 is not known to give. The stdout of a failed call (a bare newline for a missing object in 27.4, nothing for
65+
// the others) is not read.
6466
writeFileSync(join(root, 'docker'), ['#!/bin/sh', `printf '%s\\n' "$*" >> '${calls}'`,
6567
'for arg; do name="$arg"; done',
6668
'if [ "$2" = inspect ]; then case "$1 $name" in',
@@ -72,21 +74,25 @@ it('removes by name only an object of exactly that name, and reports failures ot
7274
` 'container busy') printf '%s\\n' '"${id}" "/busy"';;`,
7375
` 'network busy') printf '%s\\n' '"${other}" "busy"';;`,
7476
` 'volume busy') printf '%s\\n' '"busy"';;`,
77+
` 'network raced') printf '%s\\n' '"${raced}" "raced"';;`,
78+
" 'container junk') echo 'not json';;",
7579
" 'container killed') kill -9 $$;;",
7680
" 'container lost') echo 'Failed to initialize: unable to resolve docker endpoint: context \"x\": context not found: open /x/meta.json: no such file or directory' >&2; exit 1;;",
7781
'esac; exit 0; fi',
7882
`if [ "$1" = rm ]; then echo 'Error response from daemon: removal of container ${id} is already in progress' >&2; exit 1; fi`,
83+
`if [ "$1 $2 $3" = 'network rm ${raced}' ]; then echo 'Error response from daemon: network ${raced} not found' >&2; exit 1; fi`,
7984
`if [ "$1 $2" = 'network rm' ]; then echo 'Error response from daemon: error while removing network: network busy id ${other} has active endpoints' >&2; exit 1; fi`,
8085
`if [ "$1 $2" = 'volume rm' ]; then echo 'Error response from daemon: remove busy: volume is in use - [${id}]' >&2; exit 1; fi`,
8186
'exit 0'].join('\n'),
8287
{ mode: 0o755 });
8388
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
8489
const objects = [['container', 'gone'], ['network', 'gone'], ['volume', 'gone'], ['container', 'prefix'], ['network', 'prefix'],
85-
['container', 'busy'], ['network', 'busy'], ['volume', 'busy'], ['container', 'killed'], ['container', 'lost']] as const;
86-
for (const [kind, name] of objects) removeByName(kind, name, 'test', join(root, 'docker'));
90+
['container', 'busy'], ['network', 'busy'], ['volume', 'busy'], ['container', 'killed'], ['container', 'lost'],
91+
['network', 'raced'], ['container', 'junk']] as const;
92+
expect(() => { for (const [kind, name] of objects) removeByName(kind, name, 'test', join(root, 'docker')); }).not.toThrow();
8793
// Only the exact names are removed, by full ID (a volume's name is its ID).
8894
expect(readFileSync(calls, 'utf8').split('\n').filter(line => / rm |^rm /.test(line)))
89-
.toEqual([`rm --force ${id}`, `network rm ${other}`, 'volume rm --force busy']);
95+
.toEqual([`rm --force ${id}`, `network rm ${other}`, 'volume rm --force busy', `network rm ${raced}`]);
9096
expect(warn.mock.calls).toEqual([
9197
['test: removing container busy: docker rm failed:',
9298
`Error response from daemon: removal of container ${id} is already in progress`],
@@ -96,6 +102,7 @@ it('removes by name only an object of exactly that name, and reports failures ot
96102
['test: removing container killed: docker container inspect failed:', 'killed by SIGKILL'],
97103
['test: removing container lost: docker container inspect failed:',
98104
'Failed to initialize: unable to resolve docker endpoint: context "x": context not found: open /x/meta.json: no such file or directory'],
105+
['test: removing container junk: docker printed what is not JSON:', 'not json\n'],
99106
]);
100107
});
101108

@@ -148,8 +155,8 @@ it.each([
148155
// Answers as Docker 27.4 does for one swept network with two candidate holders, one gone before the inspect: the
149156
// inspect prints the other and exits 1. Every other call gets no output and exit 0, as for `ls` with no match; the
150157
// removals' stdout (the removed IDs or names in 27.4) is not read. The second case's answer is invented: it stands in
151-
// for an unexpected inspect failure that still printed some holders, which 27.4 is not known to give. The network line holds only the fields the sweep
152-
// reads (27.4 also prints CreatedAt, Driver, IPv6, Internal, Labels and Scope), and the inspect an empty endpoint for
158+
// for an unexpected inspect failure that still printed some holders, which 27.4 is not known to give. The network
159+
// line holds only the fields the sweep reads (27.4 also prints CreatedAt, Driver, IPv6, Internal, Labels and Scope), and the inspect an empty endpoint for
153160
// the network, where 27.4 gives its full settings: the sweep reads only the network names.
154161
writeFileSync(join(root, 'docker'), ['#!/bin/sh', `printf '%s\\n' "$*" >> '${calls}'`,
155162
`if [ "$1 $2" = "network ls" ]; then printf '%s\\n' '{"ID":"${net}","Name":"swept"}'; exit 0; fi`,

‎test/fixtures/docker-sweep.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ export function removeByName(kind: 'container' | 'network' | 'volume', name: str
5555
} catch { console.warn(`${when}: removing ${kind} ${name}: docker printed what is not JSON:`, found.stdout); return; }
5656
if (typeof id !== 'string' || typeof actual !== 'string' || (kind === 'container' ? actual.replace(/^\//, '') : actual) !== name) return;
5757
const removed = spawnSync(docker, kind === 'container' ? ['rm', '--force', id] : [kind, 'rm', ...(kind === 'volume' ? ['--force'] : []), id], options);
58-
if (removed.error || removed.status !== 0) failed(removed, kind === 'container' ? 'rm' : `${kind} rm`);
58+
// A network gone since the inspect is gone: Docker 27.4 answers "network <ID> not found" (rm --force of a missing
59+
// container or volume exits 0).
60+
const raced = kind === 'network' && removed.status === 1 && removed.stderr.includes(`network ${id} not found`);
61+
if ((removed.error || removed.status !== 0) && !raced) failed(removed, kind === 'container' ? 'rm' : `${kind} rm`);
5962
}
6063

6164
/**

0 commit comments

Comments
 (0)