Skip to content

Commit 07938e2

Browse files
mchwangclaude
andcommitted
Sweep quiet on a network gone since its listing; full loaded-daemon test; hook budget (#189)
Review round 54: the sweep treats Docker 27.4's 'network <ID> not found' on its network removal as gone, as removeByName does, with a test that still reports any other failure; the loaded-daemon test now lists a container, a network and a volume, each with a holder, and checks every command line; the after-sweep check's budget covers its three listings; the Agent isolation comment names the shared sweep. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2af834e commit 07938e2

4 files changed

Lines changed: 52 additions & 16 deletions

File tree

‎.github/workflows/agent-isolation.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
real-docker:
1111
runs-on: ubuntu-latest
1212
# Covers the 10 min image build and ordinary run times of the Docker-backed files. It does not cover every test
13-
# and hook at its own Vitest limit: the five afterAll hooks that sweep allow 46 min between them (#189), which only
13+
# and hook at its own Vitest limit: the five afterAll hooks that call the shared sweep allow 46 min between them (#189), which only
1414
# a stalled daemon reaches.
1515
timeout-minutes: 45
1616
steps:

‎test/agent-container.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2382,7 +2382,8 @@ describe('real Docker agent isolation', () => {
23822382
// that sweep has run either; this check, registered first, runs after the sweep (Vitest runs onTestFinished hooks
23832383
// in reverse order).
23842384
let passed = false;
2385-
onTestFinished(() => { if (passed) expect(byAllocation(owner.allocationId)).toEqual([]); }, 2 * 60_000);
2385+
// Its budget covers byAllocation's three listings at their 60 s limits, plus a minute.
2386+
onTestFinished(() => { if (passed) expect(byAllocation(owner.allocationId)).toEqual([]); }, 4 * 60_000);
23862387
sweepWhenFinished('io.codeboost.allocation', owner.allocationId);
23872388
// The seeder is refused, and every volume removal fails, so the allocation's cleanup does not settle.
23882389
const failing = [

‎test/docker-sweep.test.ts‎

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,16 @@ it('talks to the daemon set when it loaded, though a test changed DOCKER_HOST or
130130

131131
it('takes the daemon set at load, not the default, when one was set, for every call', async () => {
132132
const root = mkdtempSync(join(tmpdir(), 'codeboost-sweep-')); roots.push(root);
133-
const seen = join(root, 'seen'), net = 'n'.repeat(64), holder = 'a'.repeat(64), named = 'b'.repeat(64);
134-
// Records the address and command of each call. Answers as Docker 27.4 does, in the parts the code reads, for one
135-
// swept network with one holder and for a container named x; every other call gets no output and exit 0, as for
136-
// `ls` with no match, and the removals' stdout is not read. The network line holds only the fields the sweep reads.
133+
const seen = join(root, 'seen'), net = 'n'.repeat(64), holder = 'a'.repeat(64), named = 'b'.repeat(64), labelled = 'c'.repeat(64);
134+
// Records the address and the whole command of each call. Answers as Docker 27.4 does, in the parts the code reads,
135+
// for one labelled container, one swept network and one swept volume, each held by one container, and for a container
136+
// named x; every other call gets no output and exit 0, and the removals' stdout is not read. The network line holds
137+
// only the fields the sweep reads, and the holder's inspect an empty endpoint for its network.
137138
writeFileSync(join(root, 'docker'), ['#!/bin/sh', 'for arg; do last="$arg"; done',
138-
`printf '%s|%s|%s %s\\n' "\${DOCKER_HOST-unset}" "\${DOCKER_CONTEXT-unset}" "$1" "$2" >> '${seen}'`,
139+
`printf '%s|%s|%s\\n' "\${DOCKER_HOST-unset}" "\${DOCKER_CONTEXT-unset}" "$*" >> '${seen}'`,
140+
`if [ "$1 $2" = "container ls" ]; then printf '%s\\n' ${labelled}; exit 0; fi`,
139141
`if [ "$1 $2" = "network ls" ]; then printf '%s\\n' '{"ID":"${net}","Name":"swept"}'; exit 0; fi`,
142+
`if [ "$1 $2" = "volume ls" ]; then printf '%s\\n' swept-volume; exit 0; fi`,
140143
`if [ "$1" = ps ]; then printf '%s\\n' ${holder}; exit 0; fi`,
141144
`if [ "$1 $2" = "container inspect" ] && [ "$last" = x ]; then printf '%s\\n' '"${named}" "/x"'; exit 0; fi`,
142145
`if [ "$1 $2" = "container inspect" ]; then printf '%s\\n' '"${holder}" {"swept":{}}'; exit 0; fi`,
@@ -149,18 +152,47 @@ it('takes the daemon set at load, not the default, when one was set, for every c
149152
const fresh = await import('./fixtures/docker-sweep.ts') as typeof Sweep;
150153
vi.stubEnv('DOCKER_HOST', 'unix:///codeboost-changed.sock');
151154
vi.stubEnv('DOCKER_CONTEXT', 'codeboost-changed');
155+
vi.spyOn(console, 'warn').mockImplementation(() => undefined);
152156
try {
153157
fresh.removeByName('container', 'x', 'test', join(root, 'docker'));
154158
fresh.removeLabelledObjects(TEST_RUN_LABEL, 'x', 'test', join(root, 'docker'));
155159
} finally { vi.unstubAllEnvs(); }
156160
const lines = readFileSync(seen, 'utf8').split('\n').filter(Boolean).map(line => line.split('|'));
157-
// Every kind of call the two make: inspect and removal by name, the listings, the holder lookup and read, the
158-
// holders' removal and the network's.
159-
expect(new Set(lines.map(line => line[2]))).toEqual(new Set(['container inspect', 'rm --force', 'container ls', 'network ls',
160-
'volume ls', 'ps --all', 'network rm']));
161+
const commands = lines.map(line => line[2]);
162+
// Every call the two make, each checked by its own command line.
163+
for (const command of [
164+
`container inspect --format {{json .Id}} {{json .Name}} x`, `rm --force ${named}`,
165+
`container ls --all --quiet --no-trunc --filter label=${TEST_RUN_LABEL}=x`, `container rm --force ${labelled}`,
166+
`network ls --no-trunc --format {{json .}} --filter label=${TEST_RUN_LABEL}=x`,
167+
'ps --all --quiet --no-trunc --filter network=swept',
168+
`container inspect --format {{json .Id}} {{json .NetworkSettings.Networks}} ${holder}`, `rm --force ${holder}`,
169+
`network rm ${net}`,
170+
`volume ls --quiet --filter label=${TEST_RUN_LABEL}=x`, 'ps --all --quiet --no-trunc --filter volume=swept-volume',
171+
'volume rm --force swept-volume',
172+
]) expect(commands).toContain(command);
161173
expect(new Set(lines.map(line => `${line[0]}|${line[1]}`))).toEqual(new Set(['unix:///codeboost-loaded.sock|codeboost-loaded']));
162174
});
163175

176+
it('says nothing of a network gone since the listing, and reports any other failure to remove one', () => {
177+
const root = mkdtempSync(join(tmpdir(), 'codeboost-sweep-')); roots.push(root);
178+
const net = 'd'.repeat(64), answer = join(root, 'answer');
179+
// Answers as Docker 27.4 does for one swept network with no holder, whose removal gives the stderr in `answer`; every
180+
// other call gets no output and exit 0, as for `ls` with no match. The network line holds only the fields the sweep
181+
// reads. The second answer is invented, as a failure 27.4 might give.
182+
writeFileSync(join(root, 'docker'), ['#!/bin/sh',
183+
`if [ "$1 $2" = "network ls" ]; then printf '%s\\n' '{"ID":"${net}","Name":"swept"}'; exit 0; fi`,
184+
`if [ "$1 $2" = "network rm" ]; then cat '${answer}' >&2; exit 1; fi`, 'exit 0'].join('\n'),
185+
{ mode: 0o755 });
186+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
187+
const failures = () => warn.mock.calls.filter(call => String(call[0]).endsWith('failed:'));
188+
writeFileSync(answer, `Error response from daemon: network ${net} not found\n`);
189+
removeLabelledObjects(TEST_RUN_LABEL, 'x', 'test', join(root, 'docker'));
190+
expect(failures()).toEqual([]);
191+
writeFileSync(answer, 'Error response from daemon: permission denied\n');
192+
removeLabelledObjects(TEST_RUN_LABEL, 'x', 'test', join(root, 'docker'));
193+
expect(failures()).toEqual([['test: removing networks: docker network rm failed:', 'Error response from daemon: permission denied']]);
194+
});
195+
164196
it.each([
165197
['says nothing of a holder gone since the listing', 'Error response from daemon: No such container: b', []],
166198
['reports any other failure to read the holders', 'permission denied', [['test: reading holders: docker container inspect failed:', 'permission denied']]],

‎test/fixtures/docker-sweep.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,20 @@ export function removeRunnerObjects(runnerOwner: string, when = 'afterAll'): voi
8888
* use. Before it removes a network or a volume it also removes every container attached to it or mounting it, labelled
8989
* or not: a test can make a network or volume with one owner and the container that holds it with another, or without
9090
* one. Containers and volumes go by force; a network another container joins meanwhile stays. It reports what it finds
91-
* and any Docker call that fails (but not a read of holders that fails only because one has gone since the listing),
92-
* prefixed with `when`, and never throws, so cleanup can call it without hiding an earlier error. Each call has a 30 s
91+
* and any Docker call that fails (but not one that fails only because what it names has gone since the listing: a
92+
* holder it reads, or a network it removes), prefixed with `when`, and never throws, so cleanup can call it without hiding an earlier error. Each call has a 30 s
9393
* limit, so all eleven can take up to 5.5 minutes. It runs `docker`, the real one by default (`REAL_DOCKER`).
9494
*/
9595
export function removeLabelledObjects(label: string, value: string, when: string, docker = REAL_DOCKER): void {
9696
const run = (stage: string, args: string[]) => {
9797
const result = spawnSync(docker, args, { encoding: 'utf8', timeout: 30_000, killSignal: 'SIGKILL', env: dockerEnv() });
9898
// A holder gone since the listing is expected: Docker 27.4 answers "No such container" for it, and prints the rest.
99-
const holdersGone = stage === 'reading holders' && result.status === 1
100-
&& result.stderr.trim().split('\n').every(line => line.startsWith('Error response from daemon: No such container: '));
101-
if (result.status !== 0 && !holdersGone) console.warn(`${when}: ${stage}: docker ${args.slice(0, 2).join(' ')} failed:`,
99+
// So is a network gone since the listing (removed by its own test meanwhile): "network <ID> not found".
100+
const errors = result.stderr?.trim().split('\n') ?? [];
101+
const gone = result.status === 1 && (stage === 'reading holders'
102+
? errors.every(line => line.startsWith('Error response from daemon: No such container: '))
103+
: stage === 'removing networks' && errors.every(line => /^Error response from daemon: network [0-9a-f]{64} not found$/.test(line)));
104+
if (result.status !== 0 && !gone) console.warn(`${when}: ${stage}: docker ${args.slice(0, 2).join(' ')} failed:`,
102105
result.error?.message ?? (result.signal ? `killed by ${result.signal}` : result.stderr.trim()));
103106
// An inspect of several objects exits 1 if one is gone, yet prints the rest (Docker 27.4), so the output of a call
104107
// that exited by itself is read whatever its status. A killed call's output may be cut short, so it is not.

0 commit comments

Comments
 (0)