@norskvideo/ctl-test-harness 0.1.58 → 0.1.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/demo/harness.js +11 -1
- package/package.json +5 -1
- package/reap.d.ts +37 -0
- package/reap.js +129 -0
- package/smoke.js +10 -1
package/demo/harness.js
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
import { netReachMode, STUDIO_INTERNAL_PORT, studioBaseFrom } from "../container-net.js";
|
|
17
17
|
import { ctlSupportsPublishDebugPorts } from "../launch.js";
|
|
18
18
|
import { pollUntil } from "../poll.js";
|
|
19
|
+
import { launchWithPortConflictRetry, reapInstance } from "../reap.js";
|
|
19
20
|
import { DemoGateFailure } from "./gates.js";
|
|
20
21
|
import { DemoSession, defaultDemoDeps, demoSlug, expandHome, resolveIngestPort, } from "./run.js";
|
|
21
22
|
const STUDIO_HOST_PORT_PARAM = "STUDIO_HOST_PORT";
|
|
@@ -165,7 +166,16 @@ export class ProductHarness {
|
|
|
165
166
|
// the reap step whether or not this process lives to teardown.
|
|
166
167
|
if (this.launchedManifest)
|
|
167
168
|
this.deps.writeFile(this.launchedManifest, `${this.launchedIds.join("\n")}\n`);
|
|
168
|
-
|
|
169
|
+
// A run cancelled before teardown leaves this id's compose stack up, still
|
|
170
|
+
// holding its host ports, and nothing that asks a live daemon can see it
|
|
171
|
+
// (the leaker's daemon is gone). Reap by label first, then retry once if the
|
|
172
|
+
// bind still loses the advisory-check race — see reap.ts.
|
|
173
|
+
reapInstance(instanceId);
|
|
174
|
+
await launchWithPortConflictRetry({
|
|
175
|
+
instanceId,
|
|
176
|
+
attempt: () => s.cliOk(launchArgv),
|
|
177
|
+
log: deps.log,
|
|
178
|
+
});
|
|
169
179
|
await s.waitRunning(instanceId);
|
|
170
180
|
const rows = await s.ingestPorts(instanceId);
|
|
171
181
|
const templateParams = { declared, overrides: specParams };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@norskvideo/ctl-test-harness",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.59",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -27,6 +27,10 @@
|
|
|
27
27
|
"types": "./launch.d.ts",
|
|
28
28
|
"default": "./launch.js"
|
|
29
29
|
},
|
|
30
|
+
"./reap": {
|
|
31
|
+
"types": "./reap.d.ts",
|
|
32
|
+
"default": "./reap.js"
|
|
33
|
+
},
|
|
30
34
|
"./source-pump": {
|
|
31
35
|
"types": "./source-pump.d.ts",
|
|
32
36
|
"default": "./source-pump.js"
|
package/reap.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
/** True when `text` is a host-port binding conflict — the signal to reap and
|
|
3
|
+
* retry. Deliberately narrow: an unhealthy dependency is also a launch
|
|
4
|
+
* failure, and retrying that one just burns the clock. */
|
|
5
|
+
export declare function isPortConflict(text: string): boolean;
|
|
6
|
+
/** The conflicting host port, or null when the message carries no extractable
|
|
7
|
+
* one (still a conflict per {@link isPortConflict}, just not attributable to a
|
|
8
|
+
* port, so only the instance-scoped reap can run). */
|
|
9
|
+
export declare function extractConflictPort(text: string): number | null;
|
|
10
|
+
/** `docker ps` argv listing container ids for ONE instance id — the exact scope
|
|
11
|
+
* that spares co-running products. */
|
|
12
|
+
export declare function instancePsArgs(instanceId: string): string[];
|
|
13
|
+
/** `docker ps` argv listing norsk-ctl container ids publishing a given host
|
|
14
|
+
* port. Role-filtered, so a squatter outside norsk-ctl is never touched. */
|
|
15
|
+
export declare function portPsArgs(port: number): string[];
|
|
16
|
+
type Spawn = typeof spawnSync;
|
|
17
|
+
/** Reap every leftover container for one instance id (media, studio and source
|
|
18
|
+
* all carry the `norsk-ctl.instance` label). */
|
|
19
|
+
export declare function reapInstance(instanceId: string, spawn?: Spawn): string[];
|
|
20
|
+
/** Reap whatever norsk-ctl container holds a specific host port — the fallback
|
|
21
|
+
* when the holder belongs to a different instance, so the instance-id reap
|
|
22
|
+
* alone would not free it. */
|
|
23
|
+
export declare function reapPort(port: number, spawn?: Spawn): string[];
|
|
24
|
+
/**
|
|
25
|
+
* Run a launch, and on a host-port conflict reap the holder and retry ONCE.
|
|
26
|
+
*
|
|
27
|
+
* Once, not a loop: one retry clears a leaked holder or a lost race, and a
|
|
28
|
+
* second conflict means something is genuinely sitting on the port — where the
|
|
29
|
+
* launch's own message, naming the port, is more use than another attempt.
|
|
30
|
+
*/
|
|
31
|
+
export declare function launchWithPortConflictRetry<T>(opts: {
|
|
32
|
+
instanceId: string;
|
|
33
|
+
attempt: () => Promise<T>;
|
|
34
|
+
spawn?: Spawn;
|
|
35
|
+
log?: (line: string) => void;
|
|
36
|
+
}): Promise<T>;
|
|
37
|
+
export {};
|
package/reap.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// Orphan-container reaper for launches that lose a host port.
|
|
2
|
+
//
|
|
3
|
+
// ctl's port-availability check at validation is ADVISORY — the probe holds no
|
|
4
|
+
// reservation, and docker binds host ports at container START — so a port free
|
|
5
|
+
// when the launch was validated can be taken by the time compose binds it
|
|
6
|
+
// (`launch-service.ts`, HOST_PORT_CONFLICT). Its own message says to launch
|
|
7
|
+
// again. On a shared self-hosted runner that race is routine rather than
|
|
8
|
+
// exotic, in two flavours:
|
|
9
|
+
//
|
|
10
|
+
// * a CANCELLED run: `cancel-in-progress` SIGKILLs `bun test` before the
|
|
11
|
+
// harness can tear down, so a `compose up -d` stack outlives its (now dead)
|
|
12
|
+
// daemon and keeps holding the port. Nothing that asks a LIVE daemon can
|
|
13
|
+
// see it, because the leaker's daemon is gone — which is why this reaps off
|
|
14
|
+
// Docker by label rather than through the API.
|
|
15
|
+
// * co-running products: six product repos' CI can fire within seconds of
|
|
16
|
+
// each other on one Docker daemon, and a fixed host port (the MoQ preview's
|
|
17
|
+
// `preferred: 4443`) is picked by each of them independently.
|
|
18
|
+
//
|
|
19
|
+
// Scoping is the whole safety argument. Every norsk-ctl container carries
|
|
20
|
+
// `norsk-ctl.role`; per-instance ones also carry `norsk-ctl.instance=<id>`. We
|
|
21
|
+
// reap by the exact instance id, and by port only among containers that carry
|
|
22
|
+
// the role label — so a co-running product is untouched and a squatter outside
|
|
23
|
+
// norsk-ctl is never removed, only reported by the launch that then fails.
|
|
24
|
+
//
|
|
25
|
+
// The regex and argv builders are pure so tests/reap.test.ts covers them with
|
|
26
|
+
// zero Docker; only `reapInstance`/`reapPort` shell out.
|
|
27
|
+
//
|
|
28
|
+
// Lifted from norsk-ctl-product-playout's tests/integration/support/reap.ts,
|
|
29
|
+
// which is where this was worked out. It lived in one product while probe,
|
|
30
|
+
// commentary, reuters and funke had no equivalent and lost tests to the same
|
|
31
|
+
// race, so it belongs here instead.
|
|
32
|
+
import { spawnSync } from "node:child_process";
|
|
33
|
+
// A launch failure that means "the host port I need is already bound". Covers
|
|
34
|
+
// the daemon's own wrapper and the raw docker/kernel strings it quotes, since
|
|
35
|
+
// either can surface depending on where the bind loses the race.
|
|
36
|
+
const CONFLICT_PATTERNS = [
|
|
37
|
+
/was taken between validation and container start/i,
|
|
38
|
+
/port is already allocated/i,
|
|
39
|
+
/address already in use/i,
|
|
40
|
+
];
|
|
41
|
+
// Same failure, with the offending port captured. Ordered most-specific first:
|
|
42
|
+
// the daemon wrapper names the port up front, the raw docker/kernel lines carry
|
|
43
|
+
// it in the bind address.
|
|
44
|
+
const PORT_PATTERNS = [
|
|
45
|
+
/host port (\d{2,5})\b/i,
|
|
46
|
+
/bind for [\d.:[\]]*?:(\d{2,5})\b[^:]*:\s*port is already allocated/i,
|
|
47
|
+
/:(\d{2,5}):\s*bind:\s*address already in use/i,
|
|
48
|
+
/port (\d{2,5}) .*already/i,
|
|
49
|
+
];
|
|
50
|
+
/** True when `text` is a host-port binding conflict — the signal to reap and
|
|
51
|
+
* retry. Deliberately narrow: an unhealthy dependency is also a launch
|
|
52
|
+
* failure, and retrying that one just burns the clock. */
|
|
53
|
+
export function isPortConflict(text) {
|
|
54
|
+
return CONFLICT_PATTERNS.some((re) => re.test(text));
|
|
55
|
+
}
|
|
56
|
+
/** The conflicting host port, or null when the message carries no extractable
|
|
57
|
+
* one (still a conflict per {@link isPortConflict}, just not attributable to a
|
|
58
|
+
* port, so only the instance-scoped reap can run). */
|
|
59
|
+
export function extractConflictPort(text) {
|
|
60
|
+
for (const re of PORT_PATTERNS) {
|
|
61
|
+
const m = text.match(re);
|
|
62
|
+
if (m?.[1])
|
|
63
|
+
return Number(m[1]);
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
/** `docker ps` argv listing container ids for ONE instance id — the exact scope
|
|
68
|
+
* that spares co-running products. */
|
|
69
|
+
export function instancePsArgs(instanceId) {
|
|
70
|
+
return ["ps", "-aq", "--filter", `label=norsk-ctl.instance=${instanceId}`];
|
|
71
|
+
}
|
|
72
|
+
/** `docker ps` argv listing norsk-ctl container ids publishing a given host
|
|
73
|
+
* port. Role-filtered, so a squatter outside norsk-ctl is never touched. */
|
|
74
|
+
export function portPsArgs(port) {
|
|
75
|
+
return ["ps", "-aq", "--filter", "label=norsk-ctl.role", "--filter", `publish=${port}`];
|
|
76
|
+
}
|
|
77
|
+
/** Force-remove the containers `docker ps <psArgs>` lists. Best-effort and
|
|
78
|
+
* synchronous (it must finish before the relaunch); a Docker that is absent or
|
|
79
|
+
* errors leaves the launch to fail with its own clearer message. Returns the
|
|
80
|
+
* ids it removed, for logging. */
|
|
81
|
+
function reap(psArgs, spawn) {
|
|
82
|
+
try {
|
|
83
|
+
const ps = spawn("docker", psArgs, { encoding: "utf8", timeout: 15_000 });
|
|
84
|
+
const ids = (ps.stdout ?? "")
|
|
85
|
+
.split("\n")
|
|
86
|
+
.map((s) => s.trim())
|
|
87
|
+
.filter(Boolean);
|
|
88
|
+
if (ids.length > 0)
|
|
89
|
+
spawn("docker", ["rm", "-f", ...ids], { encoding: "utf8", timeout: 30_000 });
|
|
90
|
+
return ids;
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
return [];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/** Reap every leftover container for one instance id (media, studio and source
|
|
97
|
+
* all carry the `norsk-ctl.instance` label). */
|
|
98
|
+
export function reapInstance(instanceId, spawn = spawnSync) {
|
|
99
|
+
return reap(instancePsArgs(instanceId), spawn);
|
|
100
|
+
}
|
|
101
|
+
/** Reap whatever norsk-ctl container holds a specific host port — the fallback
|
|
102
|
+
* when the holder belongs to a different instance, so the instance-id reap
|
|
103
|
+
* alone would not free it. */
|
|
104
|
+
export function reapPort(port, spawn = spawnSync) {
|
|
105
|
+
return reap(portPsArgs(port), spawn);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Run a launch, and on a host-port conflict reap the holder and retry ONCE.
|
|
109
|
+
*
|
|
110
|
+
* Once, not a loop: one retry clears a leaked holder or a lost race, and a
|
|
111
|
+
* second conflict means something is genuinely sitting on the port — where the
|
|
112
|
+
* launch's own message, naming the port, is more use than another attempt.
|
|
113
|
+
*/
|
|
114
|
+
export async function launchWithPortConflictRetry(opts) {
|
|
115
|
+
const spawn = opts.spawn ?? spawnSync;
|
|
116
|
+
try {
|
|
117
|
+
return await opts.attempt();
|
|
118
|
+
}
|
|
119
|
+
catch (e) {
|
|
120
|
+
const text = e instanceof Error ? e.message : String(e);
|
|
121
|
+
if (!isPortConflict(text))
|
|
122
|
+
throw e;
|
|
123
|
+
const port = extractConflictPort(text);
|
|
124
|
+
const reaped = [...reapInstance(opts.instanceId, spawn), ...(port !== null ? reapPort(port, spawn) : [])];
|
|
125
|
+
opts.log?.(`host port conflict on ${port !== null ? `:${port}` : "an unnamed port"} launching ${opts.instanceId}; ` +
|
|
126
|
+
`reaped ${reaped.length} container(s) and retrying once`);
|
|
127
|
+
return await opts.attempt();
|
|
128
|
+
}
|
|
129
|
+
}
|
package/smoke.js
CHANGED
|
@@ -25,6 +25,7 @@ import { cleanupDaemon, requireLicenseFile, runCli, startDaemon, } from "./daemo
|
|
|
25
25
|
import { hashSlot } from "./harness-config.js";
|
|
26
26
|
import { ctlSupportsNoPublish, ctlSupportsPublishDebugPorts, runnerContainerUser } from "./launch.js";
|
|
27
27
|
import { pollUntil } from "./poll.js";
|
|
28
|
+
import { launchWithPortConflictRetry, reapInstance } from "./reap.js";
|
|
28
29
|
import { nukePathAsRoot, reportForeignOwners } from "./root-nuke.js";
|
|
29
30
|
import { readProxySecret, startSrtSources } from "./source-pump.js";
|
|
30
31
|
import { applyTestHost, fetchComponentState, fetchComponents, fetchStreamMappings, isSrtListenerState, } from "./studio-state.js";
|
|
@@ -339,7 +340,15 @@ export async function runProductSmoke(slug, spec, deps = defaultSmokeDeps) {
|
|
|
339
340
|
launchArgv.push("--publish-debug-ports");
|
|
340
341
|
}
|
|
341
342
|
launched = true;
|
|
342
|
-
|
|
343
|
+
// Same advisory-check race as the demo tier, same remedy: reap this id's
|
|
344
|
+
// orphans from a run that was killed before teardown, then retry once if the
|
|
345
|
+
// bind loses anyway (reap.ts).
|
|
346
|
+
reapInstance(instanceId);
|
|
347
|
+
await launchWithPortConflictRetry({
|
|
348
|
+
instanceId,
|
|
349
|
+
attempt: () => cliOk(launchArgv),
|
|
350
|
+
log: deps.log,
|
|
351
|
+
});
|
|
343
352
|
await pollUntil(async () => {
|
|
344
353
|
const r = await cli(["instance", "list"], { output: "json" });
|
|
345
354
|
if (r.exitCode !== 0)
|