@norskvideo/ctl-test-harness 0.1.122 → 0.1.123

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/run.js CHANGED
@@ -30,7 +30,7 @@ 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 { preserveInstanceContainerLogsTo } from "../container-logs.js";
34
34
  import { currentInstanceNetworks, DOCKER_NETWORK_NAME, ensureRunnerOnNetwork, instanceNetworkName, isAlreadyOnNetworkError, netReachMode, } from "../container-net.js";
35
35
  import { cleanupDaemon, ctlSupportsEphemeralPorts, PROXY_PORT_FILE, requireLicenseFile, runCli, startDaemon, } from "../daemon.js";
36
36
  import { freeHostPort } from "../free-port.js";
@@ -883,9 +883,9 @@ export class DemoSession {
883
883
  if (!ciLogDir)
884
884
  return;
885
885
  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)}`);
886
+ const { written, dest } = preserveInstanceContainerLogsTo(id, join(ciLogDir, "containers", id));
887
+ if (written > 0)
888
+ this.deps.log(`preserved ${written} container log(s) for ${id} -> ${dest}`);
889
889
  }
890
890
  }
891
891
  /** 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.123",
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(() => { });