Skip to content

Commit e4e048d

Browse files
mchwangclaude
andcommitted
Track the never-started unlabelled agent container by name; comment fixes (#189)
Review round 38: Docker 27.4 removes a network a created, never started container still names, so that test's container is tracked for afterAll; the docs say which holder reads stay quiet, that the sweep reads stderr for them, and that a wrapper can stay on PATH for the rest of the file; the loaded-daemon fake says what it answers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 115a2b2 commit e4e048d

3 files changed

Lines changed: 16 additions & 9 deletions

File tree

‎test/agent-container.test.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ const containers = new Set<string>();
5050
/**
5151
* Put, beside its test-run ID, on every object the `withoutRunnerLabel` wrapper makes and on the holder test's swept
5252
* objects (#189): afterAll sweeps it. Other label-less objects are tracked by name (`containers`, `networks`,
53-
* `volumes`), or found as holders of this run's networks and volumes.
53+
* `volumes`), or found as holders of this run's volumes or of networks that afterAll still finds. (Docker 27.4 removes a
54+
* network that a created, never started container still names, so such a container is tracked by name.)
5455
*/
5556
const TEST_FILE_RUN = randomUUID();
5657
/** Networks a test makes without this run's runner label, removed by name in afterAll as `volumes` are. */
@@ -704,9 +705,10 @@ describe('real Docker agent isolation', () => {
704705
// holds one of this run's volumes or networks, and such a volume or network never, so the wrapper labels it with a
705706
// test-run ID and this file's run ID instead (#189). Whatever has that ID is removed when the test finishes (Vitest
706707
// runs `onTestFinished` after a pass, a failure or a timeout; the `it.fails` pair below tests the failure), and again
707-
// in afterAll, for a create that lands after a timed-out test's removal (its body runs on). A timed-out test leaves the
708-
// wrapper on PATH while its body runs on, so a later test's matching create is unlabelled too and that test fails;
709-
// what it makes carries this file's run ID, so afterAll removes it.
708+
// in afterAll, for a create that lands after a timed-out test's removal (its body runs on). A timed-out test can leave
709+
// the wrapper on PATH for the rest of the file (a later shim saves the PATH that holds it, and puts it back), so a
710+
// later test's matching create is unlabelled too and that test fails; what it makes carries this file's run ID, so
711+
// afterAll removes it.
710712
const withoutRunnerLabel = async <T>(prefixes: string[][], run: () => Promise<T> | T): Promise<T> => {
711713
if (!prefixes.every(prefix => prefix.includes('create'))) throw new Error('Only create commands can be unlabelled.');
712714
const testRun = randomUUID();
@@ -970,6 +972,9 @@ describe('real Docker agent isolation', () => {
970972

971973
it('never removes an agent container found by name after a killed create when a label is missing', async () => {
972974
const data = fixture(), live = await profile(data, 'planning', 'must-not-run');
975+
// Tracked for afterAll should the test time out: never started and without the runner label, it is no holder a
976+
// sweep finds once its network is gone (#189).
977+
containers.add(live.name);
973978
// The create lands without the runner label, and its client is killed before it reports the ID. Only this
974979
// container's: should the test time out with the shim still on PATH, other tests' creates pass through.
975980
const unlabelled = [

‎test/docker-sweep.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import type * as Sweep from './fixtures/docker-sweep.ts';
88

99
// The test sweep's and `removeByName`'s handling of answers they cannot use (#189), each test with its own fake
1010
// `docker`, described beside it. The sweep reads only stdout, the exit status, whether a signal killed the call and
11-
// whether it could start, and stderr only to report a failure; `removeByName` also reads an inspect's stderr to tell a
12-
// missing object from a failure. Against the daemon, agent-container's holder test covers the sweep and removeByName's
11+
// whether it could start, and stderr to report a failure, and, for its read of holders, to tell one gone since the
12+
// listing ("No such container") from a failure; `removeByName` also reads an inspect's stderr to tell a missing object
13+
// from a failure. Against the daemon, agent-container's holder test covers the sweep and removeByName's
1314
// removal, its silence on a missing name, and its refusal of an object found by an ID prefix.
1415
const roots: string[] = [];
1516
afterEach(() => {
@@ -119,6 +120,7 @@ it('talks to the daemon set when it loaded, though a test changed DOCKER_HOST or
119120
it('takes the daemon set at load, not the default, when one was set', async () => {
120121
const root = mkdtempSync(join(tmpdir(), 'codeboost-sweep-')); roots.push(root);
121122
const seen = join(root, 'seen');
123+
// Records the address each call is given, and answers as Docker 27.4 does for a missing container and an empty `ls`.
122124
writeFileSync(join(root, 'docker'), ['#!/bin/sh', `printf '%s|%s\\n' "\${DOCKER_HOST-unset}" "\${DOCKER_CONTEXT-unset}" >> '${seen}'`,
123125
'if [ "$2" = inspect ]; then echo; echo "Error response from daemon: No such container: x" >&2; exit 1; fi', 'exit 0'].join('\n'),
124126
{ mode: 0o755 });

‎test/fixtures/docker-sweep.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ export function removeRunnerObjects(runnerOwner: string, when = 'afterAll'): voi
7373
* use. Before it removes a network or a volume it also removes every container attached to it or mounting it, labelled
7474
* or not: a test can make a network or volume with one owner and the container that holds it with another, or without
7575
* one. Containers and volumes go by force; a network another container joins meanwhile stays. It reports what it finds
76-
* and any Docker call that fails, prefixed with `when`, and never throws, so cleanup can call it without hiding an
77-
* earlier error. Each call has a 30 s limit, so all eleven can take up to 5.5 minutes. It runs `docker`, the real one by
78-
* default (`REAL_DOCKER`).
76+
* and any Docker call that fails (but not a read of holders that fails only because one has gone since the listing),
77+
* prefixed with `when`, and never throws, so cleanup can call it without hiding an earlier error. Each call has a 30 s
78+
* limit, so all eleven can take up to 5.5 minutes. It runs `docker`, the real one by default (`REAL_DOCKER`).
7979
*/
8080
export function removeLabelledObjects(label: string, value: string, when: string, docker = REAL_DOCKER): void {
8181
const run = (stage: string, args: string[]) => {

0 commit comments

Comments
 (0)