@norskvideo/ctl-test-harness 0.1.122 → 0.1.124

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.
@@ -37,8 +37,19 @@ export declare function dumpInstanceContainerLogs(instanceId: string, tail?: num
37
37
  * Call BEFORE cleanup. Best-effort throughout and never throws: a diagnostic
38
38
  * that fails the run it was meant to explain is worse than no diagnostic.
39
39
  * Returns the number of logs written, and creates nothing when there are none.
40
+ * A repeat for the same `dest` goes to a numbered sibling (see
41
+ * {@link preserveInstanceContainerLogsTo}), so the first attempt survives.
40
42
  */
41
43
  export declare function preserveInstanceContainerLogs(instanceId: string, dest: string, tail?: number, spawn?: SpawnLike): number;
44
+ /** {@link preserveInstanceContainerLogs}, also returning where the logs went.
45
+ * When `dest` already holds logs (the same fixed instance id launched again,
46
+ * e.g. a quarantined re-run after the failing attempt), they go to the first
47
+ * free `<dest>-2`, `<dest>-3`, … (anything there but an empty directory counts as taken) instead of overwriting the attempt that
48
+ * explains the failure. No containers: `{ written: 0, dest }`, nothing created. */
49
+ export declare function preserveInstanceContainerLogsTo(instanceId: string, dest: string, tail?: number, spawn?: SpawnLike): {
50
+ written: number;
51
+ dest: string;
52
+ };
42
53
  export interface LogHit {
43
54
  container: string;
44
55
  line: string;
package/container-logs.js CHANGED
@@ -103,30 +103,53 @@ export function dumpInstanceContainerLogs(instanceId, tail = DEFAULT_TAIL, spawn
103
103
  * Call BEFORE cleanup. Best-effort throughout and never throws: a diagnostic
104
104
  * that fails the run it was meant to explain is worse than no diagnostic.
105
105
  * Returns the number of logs written, and creates nothing when there are none.
106
+ * A repeat for the same `dest` goes to a numbered sibling (see
107
+ * {@link preserveInstanceContainerLogsTo}), so the first attempt survives.
106
108
  */
107
109
  export function preserveInstanceContainerLogs(instanceId, dest, tail = DEFAULT_TAIL, spawn = defaultSpawn) {
110
+ return preserveInstanceContainerLogsTo(instanceId, dest, tail, spawn).written;
111
+ }
112
+ /** {@link preserveInstanceContainerLogs}, also returning where the logs went.
113
+ * When `dest` already holds logs (the same fixed instance id launched again,
114
+ * e.g. a quarantined re-run after the failing attempt), they go to the first
115
+ * free `<dest>-2`, `<dest>-3`, … (anything there but an empty directory counts as taken) instead of overwriting the attempt that
116
+ * explains the failure. No containers: `{ written: 0, dest }`, nothing created. */
117
+ export function preserveInstanceContainerLogsTo(instanceId, dest, tail = DEFAULT_TAIL, spawn = defaultSpawn) {
108
118
  const entries = collectInstanceContainerLogs(instanceId, tail, spawn);
109
119
  if (entries.length === 0)
110
- return 0;
120
+ return { written: 0, dest };
121
+ let target = dest;
122
+ for (let n = 2; isTaken(target); n++)
123
+ target = `${dest}-${n}`;
111
124
  let written = 0;
112
125
  try {
113
- mkdirSync(dest, { recursive: true });
126
+ mkdirSync(target, { recursive: true });
114
127
  }
115
128
  catch {
116
- return 0;
129
+ return { written: 0, dest: target };
117
130
  }
118
131
  for (const e of entries) {
119
132
  try {
120
133
  // The state goes in the file, not just the name: an `exited` container
121
134
  // with an empty log says something a missing file does not.
122
- writeFileSync(join(dest, `${e.name}.log`), `# ${e.name} (${e.state})\n${e.log}`);
135
+ writeFileSync(join(target, `${e.name}.log`), `# ${e.name} (${e.state})\n${e.log}`);
123
136
  written++;
124
137
  }
125
138
  catch {
126
139
  // Keep going: one unwritable file must not cost the others.
127
140
  }
128
141
  }
129
- return written;
142
+ return { written, dest: target };
143
+ }
144
+ /** Anything at `dir` but an empty directory: a non-empty one, a file in the
145
+ * way, or a directory we can't list (which may hold the first attempt). */
146
+ function isTaken(dir) {
147
+ try {
148
+ return readdirSync(dir).length > 0;
149
+ }
150
+ catch {
151
+ return existsSync(dir);
152
+ }
130
153
  }
131
154
  /** Copy every engine `logs` tree under an instance-runtime root to `dest`,
132
155
  * preserving the relative structure. Why this and not `docker logs`:
package/demo/cli.d.ts CHANGED
@@ -19,6 +19,8 @@ export interface DemoArgs {
19
19
  publicHost?: string;
20
20
  /** `up`/`check`: absent is `assigned`. */
21
21
  ports?: DemoPortPolicy;
22
+ /** `dev-loop up`: extra config for the private daemon, as given (relative to the cwd). */
23
+ daemonConfig?: string;
22
24
  }
23
25
  export declare function parseDemoArgs(argv: string[]): DemoArgs;
24
26
  export interface ResolvedSpecView {
package/demo/cli.js CHANGED
@@ -32,6 +32,7 @@ import { demoDown, demoPorts, demoSlug, runExportCheck } from "./run.js";
32
32
  const USAGE = `usage: demo <up|check|down|spec|ui> [--mode dev|image|standalone] [--daemon private|reuse] [--export-only] [--json]
33
33
  [--ports assigned|band] [--ui zellij|tmux|none] [--public-host <host>] [--spec <path>]
34
34
  demo dev-loop <up|refresh|down|ui> [--ui zellij|tmux|none] [--public-host <host>] [--spec <path>]
35
+ [--daemon-config <file>]
35
36
 
36
37
  up launch, print the URLs, hold (Ctrl-C tears down)
37
38
  check up, then tear down and exit 0/1 — what CI runs, always on a private daemon
@@ -61,6 +62,10 @@ const USAGE = `usage: demo <up|check|down|spec|ui> [--mode dev|image|standalone]
61
62
  --ui zellij|tmux|none the multiplexer (\`none\` prints the pane commands); zellij unless said
62
63
  --public-host <host> the name other machines reach this box by: every URL, the daemon's
63
64
  publicHost, Studio's MoQ preview (default: PUBLIC_HOST)
65
+ --daemon-config <file> dev-loop up: a YAML mapping whose top-level keys are merged into the private
66
+ daemon's config.yaml before it serves (a storage: block, bindAddress), checked
67
+ by that ctl's own parser; keys the driver sets (ports, working directory,
68
+ proxy, public host) are refused
64
69
  --spec <path> the demo spec (default tests/demo.spec.ts)`;
65
70
  const DEFAULT_SPEC = "tests/demo.spec.ts";
66
71
  const DEMO_ACTIONS = ["up", "check", "down", "spec", "ui"];
@@ -131,6 +136,9 @@ export function parseDemoArgs(argv) {
131
136
  case "--public-host":
132
137
  args.publicHost = value();
133
138
  break;
139
+ case "--daemon-config":
140
+ args.daemonConfig = value();
141
+ break;
134
142
  case "--ports": {
135
143
  const p = value();
136
144
  if (p !== "assigned" && p !== "band")
@@ -144,11 +152,14 @@ export function parseDemoArgs(argv) {
144
152
  }
145
153
  if (args.ui !== undefined && args.action !== "ui")
146
154
  throw new Error("--ui belongs to ui (or dev-loop ui)");
155
+ if (args.daemonConfig !== undefined && !(devLoop && args.action === "up")) {
156
+ throw new Error("--daemon-config belongs to dev-loop up");
157
+ }
147
158
  if (args.ports !== undefined && (devLoop || (args.action !== "up" && args.action !== "check"))) {
148
159
  throw new Error("--ports belongs to up and check");
149
160
  }
150
161
  if (devLoop && (args.mode !== undefined || args.daemon !== "private" || args.exportOnly || args.json)) {
151
- throw new Error("dev-loop takes only --ui, --public-host and --spec (no --mode, --daemon, --export-only or --json): it is always the source tier on a private daemon");
162
+ throw new Error("dev-loop takes only --ui, --public-host, --spec and --daemon-config (no --mode, --daemon, --export-only or --json): it is always the source tier on a private daemon");
152
163
  }
153
164
  if (args.mode === "standalone" && !args.exportOnly && args.action !== "spec") {
154
165
  throw new Error(`the full standalone tier is the dev-loop's: \`dev-loop ${args.action === "ui" ? "ui" : "up"}\` (this mode's headless gate is \`check --mode standalone --export-only\`)`);
@@ -263,6 +274,7 @@ export async function demoMain(argv, io) {
263
274
  cwd: io.cwd,
264
275
  dirs,
265
276
  ...publicHost,
277
+ ...(args.daemonConfig !== undefined ? { daemonConfig: resolve(io.cwd, args.daemonConfig) } : {}),
266
278
  ...(io.abort ? { abort: io.abort } : {}),
267
279
  });
268
280
  return 0;
@@ -11,6 +11,9 @@ export interface DevLoopOptions {
11
11
  publicHost?: string;
12
12
  abort?: AbortSignal;
13
13
  timeouts?: DemoTimeouts;
14
+ /** `up`: a YAML file whose top-level keys go into the private daemon's
15
+ * config.yaml before it serves (DemoSession's daemonConfig). */
16
+ daemonConfig?: string;
14
17
  }
15
18
  export interface DevLoopResult {
16
19
  workdir: string;
package/demo/dev-loop.js CHANGED
@@ -157,7 +157,10 @@ export async function runDevLoop(spec, opts, deps = defaultDemoDeps(opts.cwd)) {
157
157
  return { workdir, studioUrl: `http://${s.host}:${studioPort}` };
158
158
  }
159
159
  await refuseIfUp(spec, deps);
160
- const s = new DemoSession(spec, slug, opts.cwd, deps, common);
160
+ const s = new DemoSession(spec, slug, opts.cwd, deps, {
161
+ ...common,
162
+ ...(opts.daemonConfig !== undefined ? { daemonConfig: opts.daemonConfig } : {}),
163
+ });
161
164
  let recorded = false;
162
165
  let result;
163
166
  let journeyError;
package/demo/run.d.ts CHANGED
@@ -274,6 +274,11 @@ export interface SessionOptions {
274
274
  ephemeralPorts?: boolean;
275
275
  /** Private: the daemon's own node MoQ relay. */
276
276
  moqRelay?: MoqRelayOptions;
277
+ /** Private: a YAML file whose top-level keys are merged into the daemon's
278
+ * config.yaml after `init` (see mergeDaemonConfig), so the run's one
279
+ * daemon can carry config the driver has no option for, a storage block
280
+ * say. */
281
+ daemonConfig?: string;
277
282
  }
278
283
  /** The host a demo's daemon-port URLs name.
279
284
  *
@@ -366,6 +371,7 @@ export declare class DemoSession {
366
371
  private readonly proxyAuth;
367
372
  private readonly daemonEnv;
368
373
  private readonly extraContainers;
374
+ private readonly daemonConfig;
369
375
  constructor(spec: DemoSpec, slug: string, cwd: string, deps: DemoDeps, opts: SessionOptions);
370
376
  /** Scheme + authority of the daemon's proxy, for `proxy` URLs. */
371
377
  get proxyBase(): string;
@@ -386,6 +392,12 @@ export declare class DemoSession {
386
392
  /** Private: init a virgin store and start a daemon on the band. Reuse: the
387
393
  * developer's daemon must already answer. */
388
394
  attachDaemon(): Promise<void>;
395
+ /** Merge the extra config into the store's config.yaml, then have the
396
+ * resolved ctl parse it with no daemon (`config show`): a bad block fails
397
+ * here with ctl's own message rather than as a daemon that never answers,
398
+ * and a key ctl silently drops (one it does not know, or a value it does
399
+ * not take) is named rather than lost. */
400
+ private applyDaemonConfig;
389
401
  /** The daemon advertises the relay's fingerprint and WebSocket fallback on
390
402
  * its proxy base, http://localhost:<proxy port>. In direct reach that port
391
403
  * is published on the docker host, not in this container, so this
package/demo/run.js CHANGED
@@ -30,7 +30,8 @@ import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, readlinkSy
30
30
  import { homedir } from "node:os";
31
31
  import { dirname, isAbsolute, join, resolve } from "node:path";
32
32
  import { parseManifestSeed, repoOf } from "@norskvideo/ctl-sdk/manifest-seed";
33
- import { preserveInstanceContainerLogs } from "../container-logs.js";
33
+ import { parseDocument, parse as parseYaml } from "yaml";
34
+ import { preserveInstanceContainerLogsTo } from "../container-logs.js";
34
35
  import { currentInstanceNetworks, DOCKER_NETWORK_NAME, ensureRunnerOnNetwork, instanceNetworkName, isAlreadyOnNetworkError, netReachMode, } from "../container-net.js";
35
36
  import { cleanupDaemon, ctlSupportsEphemeralPorts, PROXY_PORT_FILE, requireLicenseFile, runCli, startDaemon, } from "../daemon.js";
36
37
  import { freeHostPort } from "../free-port.js";
@@ -344,6 +345,51 @@ const CONFIG_FILE = "config.yaml";
344
345
  * The daemon port is no good: it is 0 until assigned, and on the band a
345
346
  * previous run's daemon may still answer there. */
346
347
  const NO_DAEMON_PORT = "1";
348
+ /** config.yaml keys a private daemon's run depends on: `init` writes them from
349
+ * the session's own ports and choices, and the proxy URLs, the store and the
350
+ * sign-in the driver assumes follow from them. Extra daemon config may not
351
+ * set them. */
352
+ const DRIVER_OWNED_CONFIG_KEYS = [
353
+ "networkMode",
354
+ "defaultWorkingDirectory",
355
+ "proxyPort",
356
+ "proxyAuth",
357
+ "httpRedirect",
358
+ "httpRedirectPort",
359
+ "externalPort",
360
+ "publicHost",
361
+ "certSource",
362
+ "certPath",
363
+ "keyPath",
364
+ ];
365
+ /** Merge a YAML mapping's top-level keys into a store's config.yaml text
366
+ * (each key replaces any of the same name). Throws, naming `fragmentPath`,
367
+ * for YAML that does not parse, anything but a non-empty mapping, or a key
368
+ * the driver owns. Judging the values is the resolved ctl's job. */
369
+ function mergeDaemonConfig(configText, fragmentText, fragmentPath) {
370
+ let fragment;
371
+ try {
372
+ fragment = parseYaml(fragmentText);
373
+ }
374
+ catch (e) {
375
+ throw new Error(`${fragmentPath} is not valid YAML: ${e instanceof Error ? e.message : String(e)}`);
376
+ }
377
+ if (fragment === null || typeof fragment !== "object" || Array.isArray(fragment)) {
378
+ throw new Error(`${fragmentPath} must be a YAML mapping of config.yaml keys (storage:, bindAddress: ...)`);
379
+ }
380
+ const keys = Object.keys(fragment);
381
+ if (keys.length === 0)
382
+ throw new Error(`${fragmentPath} sets no config.yaml keys`);
383
+ const owned = keys.filter((k) => DRIVER_OWNED_CONFIG_KEYS.includes(k));
384
+ if (owned.length > 0) {
385
+ throw new Error(`${fragmentPath} sets ${owned.join(", ")}, which the dev-loop sets for its private daemon (its ports, store, proxy and sign-in); remove ${owned.length === 1 ? "it" : "them"}` +
386
+ (owned.includes("publicHost") ? " (--public-host sets the public host)" : ""));
387
+ }
388
+ const doc = parseDocument(configText);
389
+ for (const key of keys)
390
+ doc.set(key, fragment[key]);
391
+ return { text: doc.toString(), keys };
392
+ }
347
393
  /** The host a demo's daemon-port URLs name.
348
394
  *
349
395
  * NORSK_TEST_HOST is a STUDIO-port setting: on a docker-outside-of-docker
@@ -443,6 +489,7 @@ export class DemoSession {
443
489
  proxyAuth;
444
490
  daemonEnv;
445
491
  extraContainers;
492
+ daemonConfig;
446
493
  constructor(spec, slug, cwd, deps, opts) {
447
494
  this.spec = spec;
448
495
  this.slug = slug;
@@ -484,6 +531,10 @@ export class DemoSession {
484
531
  this.proxyAuth = opts.proxy;
485
532
  this.daemonEnv = opts.daemonEnv ?? {};
486
533
  this.extraContainers = opts.extraContainers ?? [];
534
+ if (opts.daemonConfig !== undefined && opts.daemon === "reuse") {
535
+ throw new Error("daemonConfig needs a private daemon: the developer's daemon keeps the config it has");
536
+ }
537
+ this.daemonConfig = opts.daemonConfig;
487
538
  this.timeouts = {
488
539
  devReadyMs: opts.timeouts?.devReadyMs ?? 120_000,
489
540
  healthyMs: opts.timeouts?.healthyMs ?? 180_000,
@@ -561,6 +612,8 @@ export class DemoSession {
561
612
  "--no-start-server",
562
613
  ...(this.publicHost ? ["--public-host", this.publicHost] : []),
563
614
  ]);
615
+ if (this.daemonConfig !== undefined)
616
+ await this.applyDaemonConfig(this.daemonConfig);
564
617
  const relayEnv = this.moqRelayOptions ? await this.prepareMoqRelay(this.moqRelayOptions) : {};
565
618
  const started = this.deps.startDaemon(this.storeDir, {
566
619
  port: this.ports.daemonPort,
@@ -583,6 +636,40 @@ export class DemoSession {
583
636
  }
584
637
  await this.attachMoqRelayPages();
585
638
  }
639
+ /** Merge the extra config into the store's config.yaml, then have the
640
+ * resolved ctl parse it with no daemon (`config show`): a bad block fails
641
+ * here with ctl's own message rather than as a daemon that never answers,
642
+ * and a key ctl silently drops (one it does not know, or a value it does
643
+ * not take) is named rather than lost. */
644
+ async applyDaemonConfig(path) {
645
+ const fragment = this.deps.readFile(path);
646
+ if (fragment === null)
647
+ throw new Error(`cannot read the daemon config ${path}`);
648
+ const configPath = join(this.storeDir, CONFIG_FILE);
649
+ const config = this.deps.readFile(configPath);
650
+ if (config === null)
651
+ throw new Error(`norsk-ctl init left no ${configPath} to merge ${path} into`);
652
+ const merged = mergeDaemonConfig(config, fragment, path);
653
+ this.deps.writeFile(configPath, merged.text);
654
+ const argv = ["config", "show", "-o", "json"];
655
+ const r = await this.deps.cli(this.storeDir, ["--port", NO_DAEMON_PORT, ...argv]);
656
+ this.transcript.push({ argv, stdout: r.stdout, stderr: r.stderr, exitCode: r.exitCode });
657
+ if (r.exitCode !== 0) {
658
+ throw new Error(`norsk-ctl refused the config merged from ${path}:\n${(r.stderr || r.stdout).trim()}`);
659
+ }
660
+ let settings;
661
+ try {
662
+ settings = JSON.parse(r.stdout);
663
+ }
664
+ catch {
665
+ throw new Error(`norsk-ctl config show: not JSON: ${r.stdout.slice(0, 200)}`);
666
+ }
667
+ const dropped = merged.keys.filter((k) => settings[k] === undefined);
668
+ if (dropped.length > 0) {
669
+ throw new Error(`norsk-ctl ignored ${dropped.join(", ")} from ${path}: the resolved ctl does not know ${dropped.length === 1 ? "that key" : "those keys"}, or does not take the value given`);
670
+ }
671
+ this.deps.log(`daemon config: ${merged.keys.join(", ")} from ${path}`);
672
+ }
586
673
  /** The daemon advertises the relay's fingerprint and WebSocket fallback on
587
674
  * its proxy base, http://localhost:<proxy port>. In direct reach that port
588
675
  * is published on the docker host, not in this container, so this
@@ -883,9 +970,9 @@ export class DemoSession {
883
970
  if (!ciLogDir)
884
971
  return;
885
972
  for (const id of instances) {
886
- const n = preserveInstanceContainerLogs(id, join(ciLogDir, "containers", id));
887
- if (n > 0)
888
- this.deps.log(`preserved ${n} container log(s) for ${id} -> ${join(ciLogDir, "containers", id)}`);
973
+ const { written, dest } = preserveInstanceContainerLogsTo(id, join(ciLogDir, "containers", id));
974
+ if (written > 0)
975
+ this.deps.log(`preserved ${written} container log(s) for ${id} -> ${dest}`);
889
976
  }
890
977
  }
891
978
  /** One `teardown: <step> <ms>ms` line per teardown step (ET-5), so the next
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@norskvideo/ctl-test-harness",
3
- "version": "0.1.122",
3
+ "version": "0.1.124",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
package/smoke.js CHANGED
@@ -19,7 +19,7 @@
19
19
  // rule); a product wanting typed commands bridges with toArgv in its own spec.
20
20
  import { existsSync, writeFileSync } from "node:fs";
21
21
  import { join } from "node:path";
22
- import { dumpInstanceContainerLogs, preserveEngineLogs, preserveInstanceContainerLogs } from "./container-logs.js";
22
+ import { dumpInstanceContainerLogs, preserveEngineLogs, preserveInstanceContainerLogsTo } from "./container-logs.js";
23
23
  import { authority, ensureRunnerOnNetwork, netReachMode, resolveEndpoint, STUDIO_INTERNAL_PORT, studioBaseFrom, } from "./container-net.js";
24
24
  import { cleanupDaemon, ctlSupportsEphemeralPorts, PROXY_PORT_FILE, requireLicenseFile, runCli, startDaemon, } from "./daemon.js";
25
25
  import { freeHostPort } from "./free-port.js";
@@ -547,8 +547,8 @@ export async function runProductSmoke(slug, spec, deps = defaultSmokeDeps) {
547
547
  // And the half the bind-mounted tree does not hold: the STUDIO container's
548
548
  // stdout, where "Refusing to run workflow" is printed. Still standing at
549
549
  // this point; everything below removes it.
550
- const c = preserveInstanceContainerLogs(instanceId, join(ciLogDir, slug, "containers", instanceId));
551
- deps.log(`${slug}: preserved ${c} container log(s) -> ${join(ciLogDir, slug, "containers", instanceId)}`);
550
+ const c = preserveInstanceContainerLogsTo(instanceId, join(ciLogDir, slug, "containers", instanceId));
551
+ deps.log(`${slug}: preserved ${c.written} container log(s) -> ${c.dest}`);
552
552
  }
553
553
  if (handles.length)
554
554
  await deps.stopSources(handles).catch(() => { });