Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

docker-daemon.ts

57 lines2.3 KB

1/**
2 * The Docker matrix's availability gate.
3 *
4 * WHAT THIS MATRIX IS THE AUTHORITY ON. Its image is `alpine:3`, so BusyBox 1.37,
5 * where `find -newermt` and `find -printf` are unrecognised (exit 1, EMPTY stdout).
6 * That is the trap `journalMtimeListCommand`'s `stat -c '%Y %n'` self-witness exists
7 * to make impossible, and it is the only shell in this repository's test matrix that
8 * sets it: on Windows the local-process provider execs through git-bash, whose `find`
9 * and `stat` are GNU-flavoured, so a green local-process run says nothing about the
10 * age gate's portability. `reaper-conformance.ts`'s module doc names this matrix as
11 * the authority.
12 *
13 * NO DAEMON IS A NAMED SKIP, never a silent pass. An unreachable daemon is declared
14 * `unsupported` with the reason, which the testkit renders as a named skip in the
15 * reporter, so nobody mistakes a `0ms` green tick for coverage.
16 *
17 * KNOWN LIMIT: from inside the suite, a CI runner with no daemon is indistinguishable
18 * from a laptop with no daemon, so if a runner image ever stops shipping Docker this
19 * matrix degrades to skips and CI still reports green. The check that would catch it
20 * belongs at the CI level (assert the suite ran), not here.
21 */
22import Dockerode from 'dockerode'
23​
24/**
25 * Spread into a conformance config. Either nothing (run the suite) or the
26 * `unsupported` declaration the testkit turns into a named skip.
27 */
28export type DockerDaemonGate =
29 | Record<string, never>
30 | { unsupported: { reason: string } }
31​
32/** The ping error as a message, without assuming it is an `Error`. */
33function describeError(reason: unknown): string {
34 return reason instanceof Error ? reason.message : String(reason)
36​
37/** Ping the daemon and answer with the gate for `suite`. */
38export async function dockerDaemonGate(
39 suite: string,
40): Promise<DockerDaemonGate> {
41 const failure = await new Dockerode().ping().then(
42 () => null,
43 (reason: unknown) => describeError(reason),
44 )
45 if (failure === null) return {}
46 return {
47 unsupported: {
48 reason: `${suite}: no Docker daemon reachable (${failure})`,
49 },
50 }
52​
53/** The boolean form, for `describe.skipIf` in the non-conformance provider suite. */
54export async function dockerDaemonAvailable(suite: string): Promise<boolean> {
55 const gate = await dockerDaemonGate(suite)
56 return !('unsupported' in gate)