@norskvideo/ctl-test-harness 0.1.133 → 0.1.134

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.
@@ -5,8 +5,18 @@ export type NetReachMode = "publish" | "direct";
5
5
  /** How the suite reaches instance containers. `direct` = norsk-net service DNS
6
6
  * (no host publish, no collision) — only viable on a Linux DooD runner whose
7
7
  * test container is on norsk-net. `publish` = today's host-published port,
8
- * the Docker-Desktop-safe default. Read from NORSK_TEST_NET. */
9
- export declare function netReachMode(env?: Record<string, string | undefined>): NetReachMode;
8
+ * the Docker-Desktop-safe default. Read from NORSK_TEST_NET.
9
+ *
10
+ * `direct` is a request, honoured only where it can work: service DNS answers
11
+ * inside a docker network, and only a container can join one. A runner that
12
+ * is not a container (a host runner, or one that is a VM with a docker daemon
13
+ * of its own) would launch instances with no published port and then fail to
14
+ * resolve every name it dials, so there the mode is `publish`, and the log
15
+ * says so once. */
16
+ export declare function netReachMode(env?: Record<string, string | undefined>, runnerIsContainer?: () => boolean): NetReachMode;
17
+ /** Swap how netReachMode decides the runner is a container (tests); returns a
18
+ * restore function. */
19
+ export declare function useRunnerContainerDetection(next: () => boolean): () => void;
10
20
  export interface Endpoint {
11
21
  host: string;
12
22
  port: number;
package/container-net.js CHANGED
@@ -30,7 +30,8 @@
30
30
  // routable from the host at all, so the direct path can't work there — those
31
31
  // runs MUST keep publishing. Hence the mode switch: `direct` on the Linux DooD
32
32
  // CI box, `publish` (the default) everywhere else. Nothing here changes
33
- // behaviour until a caller opts a suite into `direct`.
33
+ // behaviour until a caller opts a suite into `direct`, and a runner that is not
34
+ // a container stays on `publish` whatever it is asked (netReachMode).
34
35
  import { spawnSync } from "node:child_process";
35
36
  import { existsSync, readFileSync } from "node:fs";
36
37
  import { hostname } from "node:os";
@@ -44,9 +45,42 @@ export const MEDIA_HTTP_INTERNAL_PORT = 8080;
44
45
  /** How the suite reaches instance containers. `direct` = norsk-net service DNS
45
46
  * (no host publish, no collision) — only viable on a Linux DooD runner whose
46
47
  * test container is on norsk-net. `publish` = today's host-published port,
47
- * the Docker-Desktop-safe default. Read from NORSK_TEST_NET. */
48
- export function netReachMode(env = process.env) {
49
- return env.NORSK_TEST_NET === "direct" ? "direct" : "publish";
48
+ * the Docker-Desktop-safe default. Read from NORSK_TEST_NET.
49
+ *
50
+ * `direct` is a request, honoured only where it can work: service DNS answers
51
+ * inside a docker network, and only a container can join one. A runner that
52
+ * is not a container (a host runner, or one that is a VM with a docker daemon
53
+ * of its own) would launch instances with no published port and then fail to
54
+ * resolve every name it dials, so there the mode is `publish`, and the log
55
+ * says so once. */
56
+ export function netReachMode(env = process.env, runnerIsContainer = runnerContainerDetection) {
57
+ if (env.NORSK_TEST_NET !== "direct")
58
+ return "publish";
59
+ if (runnerIsContainer())
60
+ return "direct";
61
+ if (!saidNotAContainer) {
62
+ saidNotAContainer = true;
63
+ console.error("NORSK_TEST_NET=direct, but this runner is not a container and cannot join a docker network: reaching instances by published port instead");
64
+ }
65
+ return "publish";
66
+ }
67
+ let saidNotAContainer = false;
68
+ let runnerIsContainerMemo;
69
+ /** Whether this process is in a container the docker daemon knows. Asked once:
70
+ * the answer cannot change under a running process, and finding it can cost a
71
+ * `docker inspect`. */
72
+ let runnerContainerDetection = () => {
73
+ runnerIsContainerMemo ??= ownContainerId() !== null;
74
+ return runnerIsContainerMemo;
75
+ };
76
+ /** Swap how netReachMode decides the runner is a container (tests); returns a
77
+ * restore function. */
78
+ export function useRunnerContainerDetection(next) {
79
+ const prev = runnerContainerDetection;
80
+ runnerContainerDetection = next;
81
+ return () => {
82
+ runnerContainerDetection = prev;
83
+ };
50
84
  }
51
85
  export const authority = (e) => `${e.host}:${e.port}`;
52
86
  /** Compose's container DNS for a template-launched instance: `<id>-<service>-1`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@norskvideo/ctl-test-harness",
3
- "version": "0.1.133",
3
+ "version": "0.1.134",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",