// SPDX-License-Identifier: GPL-2.0-or-later // Copyright (C) 2026 Bryan Joshua Pedini /** * PLAN §12 core item 12 and the Docker-socket matrix: empty / one / large container sets * (FakeDocker), inaccessible / missing / failing / hanging Engine → 500 with a generic body and no * internal details, recovery once the Engine recovers, and running-only (`docker ps`) semantics. */ import { test, expect, assertDbInvariants, expectContainersResponse, expectErrorResponse, expectNoInternalDetails, makeContainerSummary, pollUntil, readOperations, type ContainerSummary, } from '../../harness'; test('no running containers → 200 with exactly []', async ({ cli, startServer, http }) => { const tok = await cli.tokenNew(); const server = await startServer(); server.fakeDocker!.empty(); const pw = await http.get(server.url(), { token: tok }); expect(expectContainersResponse(pw)).toEqual([]); expect(pw.body).toBe('[]'); expect(pw.headers['content-length']).toBe('2'); const c = await http.curl({ url: server.url(), token: tok }); expect(c.body).toBe('[]'); expect((await server.stop()).exitCode).toBe(0); expect(readOperations(server.dbPath)).toHaveLength(2); }); test('the Engine is asked for running containers only (GET /containers/json without all)', async ({ cli, startServer, http }) => { const tok = await cli.tokenNew(); const server = await startServer(); expectContainersResponse(await http.get(server.url('/api/v1/containers?all=true'), { token: tok })); expect((await server.stop()).exitCode).toBe(0); const lists = server.fakeDocker!.requests.filter((r) => r.path === '/containers/json'); expect(lists).toHaveLength(1); expect(lists[0].method).toBe('GET'); expect(new URLSearchParams(lists[0].query).get('all') ?? '0').toMatch(/^(0|false)$/); expect(server.fakeDocker!.stats.other, 'no other Engine endpoints are called').toBe(0); }); test('one container: Engine summary fields are passed through', async ({ cli, startServer, http }) => { const tok = await cli.tokenNew(); const server = await startServer(); const c: ContainerSummary = makeContainerSummary(0, undefined, { Names: ['/web-1'], Image: 'nginx:1.29', Command: 'nginx -g "daemon off;"', Labels: { 'com.example.role': 'web', 'unicode.label': 'naïve ☃' }, Ports: [{ IP: '127.0.0.1', PrivatePort: 80, PublicPort: 8080, Type: 'tcp' }], Status: 'Up 5 minutes', }); server.fakeDocker!.setContainers([c]); const [got] = expectContainersResponse(await http.get(server.url(), { token: tok })); expect(got).toMatchObject({ Id: c.Id, Names: c.Names, Image: c.Image, ImageID: c.ImageID, Command: c.Command, Created: c.Created, Labels: c.Labels, State: 'running', Status: c.Status }); expect(got.Ports).toEqual([{ IP: '127.0.0.1', PrivatePort: 80, PublicPort: 8080, Type: 'tcp' }]); expect((await server.stop()).exitCode).toBe(0); }); test('large container set: 2000 summaries, complete and in order (Playwright and curl)', async ({ cli, startServer, http }) => { const tok = await cli.tokenNew(); const server = await startServer(); server.fakeDocker!.many(2000); const ids = server.fakeDocker!.containers.map((c) => c.Id); const pw = expectContainersResponse(await http.get(server.url(), { token: tok })); expect(pw.map((c) => c.Id)).toEqual(ids); expect(pw.every((c) => c.State === 'running')).toBe(true); const cu = await http.curl({ url: server.url(), token: tok }); expect(expectContainersResponse(cu).map((c) => c.Names[0])).toEqual(ids.map((_, i) => `/fake-${i}`)); expect(cu.bodyBuffer.length).toBeGreaterThan(500_000); expect((await server.stop()).exitCode).toBe(0); expect(readOperations(server.dbPath)).toHaveLength(2); }); test('Engine failures → 500 with the generic body only; details stay in the ERROR log', async ({ cli, startServer, http }) => { const tok = await cli.tokenNew(); const server = await startServer({ logLevel: 'INFO' }); const f = server.fakeDocker!; const modes: Array<[string, () => void]> = [ ['500 with internal message', () => f.error500('boom: /var/lib/docker/overlay2 sqlite secret path')], ['404 (API mismatch)', () => f.error(404, 'page not found')], ['503', () => f.error(503, 'engine starting')], ['connection dropped mid-request', () => f.destroy()], ['malformed JSON', () => f.badJson('{"not":"an array"')], ['JSON of the wrong shape', () => f.badJson('{"Id":"x"}')], ['HTML instead of JSON', () => f.badJson('proxy error')], ]; for (const [name, apply] of modes) { await test.step(name, async () => { apply(); const r = await http.get(server.url(), { token: tok }); expectErrorResponse(r, 500); expectNoInternalDetails(r.body); expect(Object.keys(r.headers).filter((h) => /^(api-version|docker|ostype|server)/i.test(h)), 'no Engine headers forwarded').toEqual([]); }); } f.many(3); expect(expectContainersResponse(await http.get(server.url(), { token: tok }))).toHaveLength(3); expect((await server.stop()).exitCode).toBe(0); const errors = server.logs().filter({ level: 'ERROR' }); expect(errors.map((e) => e.msg)).toEqual(modes.map(() => 'docker request failed')); expect(errors[0].attrs.error).toContain('boom'); expect(server.logs().requests().map((r) => r.attrs.status)).toEqual([...modes.map(() => '500'), '200']); expect(readOperations(server.dbPath)).toHaveLength(modes.length + 1); assertDbInvariants(server.dbPath); }); test('socket inaccessible (EACCES) and missing (ENOENT): server starts, requests → 500, no details', async ({ cli, startServer, http }) => { const tok = await cli.tokenNew(); for (const docker of ['inaccessible', 'missing'] as const) { await test.step(docker, async () => { const server = await startServer({ docker }); const r = await http.get(server.url(), { token: tok }); expectErrorResponse(r, 500); expectNoInternalDetails(r.body); const c = await http.curl({ url: server.url(), token: tok }); expectErrorResponse(c, 500); expectErrorResponse(await http.get(server.url()), 401); expect((await server.stop()).exitCode).toBe(0); const err = server.logs().filter({ level: 'ERROR', msg: 'docker request failed' }); expect(err).toHaveLength(2); expect(err[0].attrs.error).toMatch(docker === 'inaccessible' ? /permission denied/ : /no such file or directory/); }); } }); test('temporarily failing Engine: errors while failing, recovery afterwards', async ({ cli, startServer, http }) => { const tok = await cli.tokenNew(); const server = await startServer(); const f = server.fakeDocker!.many(2).flaky(3); const statuses: number[] = []; for (let i = 0; i < 5; i++) statuses.push((await http.get(server.url(), { token: tok })).status); expect(statuses).toEqual([500, 500, 500, 200, 200]); // full outage then recovery f.destroy(); expectErrorResponse(await http.get(server.url(), { token: tok }), 500); f.many(1); expect(expectContainersResponse(await http.get(server.url(), { token: tok }))).toHaveLength(1); expect((await server.stop()).exitCode).toBe(0); expect(readOperations(server.dbPath)).toHaveLength(7); }); test('hung Engine: request bounded by the 10 s Docker timeout, server stays responsive, graceful shutdown waits for it', async ({ cli, startServer, http }) => { test.setTimeout(90_000); const tok = await cli.tokenNew(); const server = await startServer({ logLevel: 'DEBUG' }); const f = server.fakeDocker!.hang(); const t0 = performance.now(); const hung = http.get(server.url(), { token: tok, timeoutMs: 30_000 }); await pollUntil('the hung list call to reach the fake', () => (f.listCalls() >= 1 ? true : undefined)); // other requests are served meanwhile const quick = await http.get(server.url('/elsewhere')); expectErrorResponse(quick, 404); expect(quick.durationMs).toBeLessThan(2000); const r = await hung; const elapsed = performance.now() - t0; expectErrorResponse(r, 500); expect(elapsed, 'bounded by the 10 s Engine timeout').toBeGreaterThan(9_500); expect(elapsed).toBeLessThan(15_000); expect(server.logs().find({ level: 'ERROR', msg: 'docker request failed' })!.attrs.error).toMatch(/context deadline exceeded|timeout/i); // second hung request in flight while SIGTERM arrives: shutdown drains it and exits 0 const hung2 = http.get(server.url(), { token: tok, timeoutMs: 30_000 }); await pollUntil('second hung call', () => (f.listCalls() >= 2 ? true : undefined)); const stopping = server.stop('SIGTERM', { timeoutMs: 25_000 }); expectErrorResponse(await hung2, 500); const exit = await stopping; expect(exit.exitCode).toBe(0); expect(server.logs().has({ msg: 'server stopped' })).toBe(true); expect(readOperations(server.dbPath)).toHaveLength(2); });