@norskvideo/ctl-test-harness 0.1.100 → 0.1.102

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 CHANGED
@@ -17,6 +17,7 @@ import { netReachMode, STUDIO_INTERNAL_PORT, studioBaseFrom } from "../container
17
17
  import { ctlSupportsPublishDebugPorts } from "../launch.js";
18
18
  import { pollUntil } from "../poll.js";
19
19
  import { launchWithPortConflictRetry, reapInstance } from "../reap.js";
20
+ import { instanceReadHeaders } from "../studio-identity.js";
20
21
  import { DemoGateFailure } from "./gates.js";
21
22
  import { DemoSession, defaultDemoDeps, demoSlug, expandHome, resolveIngestPort, } from "./run.js";
22
23
  const STUDIO_HOST_PORT_PARAM = "STUDIO_HOST_PORT";
@@ -318,7 +319,9 @@ function urlResolver(o) {
318
319
  async function gateHolds(gate, ctx) {
319
320
  if ("custom" in gate)
320
321
  return gate.custom(ctx);
321
- const r = await ctx.fetch(ctx.url(gate.http));
322
+ // A gate on Studio reads it directly, so as the instance's own caller.
323
+ const headers = "studio" in gate.http ? await instanceReadHeaders(ctx.instanceId) : {};
324
+ const r = await ctx.fetch(ctx.url(gate.http), { headers });
322
325
  if (r.status !== (gate.status ?? 200))
323
326
  return false;
324
327
  if (gate.bodyIncludes === undefined)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@norskvideo/ctl-test-harness",
3
- "version": "0.1.100",
3
+ "version": "0.1.102",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -43,6 +43,10 @@
43
43
  "types": "./studio-state.d.ts",
44
44
  "default": "./studio-state.js"
45
45
  },
46
+ "./studio-identity": {
47
+ "types": "./studio-identity.d.ts",
48
+ "default": "./studio-identity.js"
49
+ },
46
50
  "./harness-config": {
47
51
  "types": "./harness-config.d.ts",
48
52
  "default": "./harness-config.js"
package/smoke.d.ts CHANGED
@@ -143,7 +143,7 @@ export interface SmokeReaders {
143
143
  components(studio: StudioTarget): Promise<ComponentsResponse | null>;
144
144
  componentState(studio: StudioTarget, componentId: string): Promise<unknown | null>;
145
145
  streamMappings(studio: StudioTarget, componentId: string): Promise<StreamMappingEntry[] | null>;
146
- fetch(url: string): Promise<Response>;
146
+ fetch(url: string, init?: RequestInit): Promise<Response>;
147
147
  }
148
148
  export interface SmokeDeps {
149
149
  licenseFile(): string;
package/smoke.js CHANGED
@@ -29,6 +29,7 @@ import { pollUntil } from "./poll.js";
29
29
  import { launchWithPortConflictRetry, reapInstance } from "./reap.js";
30
30
  import { nukePathAsRoot, reportForeignOwners } from "./root-nuke.js";
31
31
  import { readProxySecret, startSrtSources } from "./source-pump.js";
32
+ import { studioReadHeaders } from "./studio-identity.js";
32
33
  import { applyTestHost, fetchComponentState, fetchComponents, fetchStreamMappings, isSrtListenerState, } from "./studio-state.js";
33
34
  import { makeStoreDir, TEST_TMP_BASE } from "./temp-dir.js";
34
35
  // Below 32768: the band is a reservation, and Linux hands 32768-60999 out to
@@ -75,7 +76,7 @@ export const defaultSmokeDeps = {
75
76
  components: fetchComponents,
76
77
  componentState: fetchComponentState,
77
78
  streamMappings: fetchStreamMappings,
78
- fetch: (url) => fetch(url, { signal: AbortSignal.timeout(5000) }),
79
+ fetch: (url, init) => fetch(url, { ...init, signal: AbortSignal.timeout(5000) }),
79
80
  },
80
81
  cleanup: (opts) => cleanupDaemon({ ...opts, stopProxy: async () => { }, proxy: false }),
81
82
  nukeStoreAsRoot: (storeDir) => nukePathAsRoot(storeDir, TEST_TMP_BASE),
@@ -248,7 +249,9 @@ export async function runProductSmoke(slug, spec, deps = defaultSmokeDeps) {
248
249
  storeDir,
249
250
  studio,
250
251
  cli,
251
- fetchStudio: (path) => deps.readers.fetch(`${studioBaseFrom(studio, process.env.NORSK_TEST_HOST ?? "localhost", mode)}${path}`),
252
+ fetchStudio: async (path) => deps.readers.fetch(`${studioBaseFrom(studio, process.env.NORSK_TEST_HOST ?? "localhost", mode)}${path}`, {
253
+ headers: await studioReadHeaders(studio),
254
+ }),
252
255
  surfaceUrl: (path) => {
253
256
  const e = resolveEndpoint({
254
257
  instanceId,
@@ -0,0 +1,12 @@
1
+ import type { StudioReach } from "./container-net.js";
2
+ /** The assertion for an instance whose Studio container has `env`. */
3
+ export declare function studioIdentityFromEnv(env: readonly string[]): Record<string, string>;
4
+ type EnvReader = (container: string) => string[] | undefined;
5
+ /** Test seam: how a Studio container's environment is read. */
6
+ export declare function setStudioEnvReader(reader: EnvReader | undefined): void;
7
+ /** The headers a direct read of an instance's Studio carries. */
8
+ export declare function instanceReadHeaders(instanceId: string): Promise<Record<string, string>>;
9
+ /** The same for a reach: the instance's assertion for a StudioTarget,
10
+ * nothing for a bare published port. */
11
+ export declare function studioReadHeaders(studio: StudioReach): Promise<Record<string, string>>;
12
+ export {};
@@ -0,0 +1,55 @@
1
+ // Who the suite's direct reads of an instance's Studio are from (norsk-ctl task
2
+ // 123 D10). A product whose template offers Studio's access gate the modes ctl
3
+ // sends has Studio refuse a /live/api request the ingress did not stamp; the
4
+ // suite reaches Studio directly, so it asserts the instance's audience, as
5
+ // norsk-ctl's own readiness probe does, from the credentials ctl put in that
6
+ // Studio container's environment. An instance given none (its gate dormant, as
7
+ // every product that does not offer the modes) is read exactly as before.
8
+ import { execFileSync } from "node:child_process";
9
+ import { AUDIENCE_ENV, AUDIENCE_SECRET_ENV, ingressAssertionHeaders } from "@norskvideo/ctl-sdk";
10
+ const SUITE_SUB = "urn:norsk:workload:ctl-test-harness";
11
+ /** The assertion for an instance whose Studio container has `env`. */
12
+ export function studioIdentityFromEnv(env) {
13
+ const value = (name) => env.find((e) => e.startsWith(`${name}=`))?.slice(name.length + 1);
14
+ const audience = value(AUDIENCE_ENV);
15
+ const secret = value(AUDIENCE_SECRET_ENV);
16
+ if (!audience || !secret)
17
+ return {};
18
+ return ingressAssertionHeaders({ audience, secret, sub: SUITE_SUB, roles: ["viewer"] });
19
+ }
20
+ const dockerEnv = (container) => {
21
+ try {
22
+ return JSON.parse(execFileSync("docker", ["inspect", "--format", "{{json .Config.Env}}", container], {
23
+ encoding: "utf8",
24
+ stdio: ["ignore", "pipe", "ignore"],
25
+ }));
26
+ }
27
+ catch {
28
+ return undefined;
29
+ }
30
+ };
31
+ let readEnv = dockerEnv;
32
+ const known = new Map();
33
+ /** Test seam: how a Studio container's environment is read. */
34
+ export function setStudioEnvReader(reader) {
35
+ readEnv = reader ?? dockerEnv;
36
+ known.clear();
37
+ }
38
+ /** The headers a direct read of an instance's Studio carries. */
39
+ export async function instanceReadHeaders(instanceId) {
40
+ const cached = known.get(instanceId);
41
+ if (cached !== undefined)
42
+ return cached;
43
+ const env = readEnv(`${instanceId}-studio-1`);
44
+ // Not created yet: this read goes without, and the next asks again.
45
+ if (env === undefined)
46
+ return {};
47
+ const headers = studioIdentityFromEnv(env);
48
+ known.set(instanceId, headers);
49
+ return headers;
50
+ }
51
+ /** The same for a reach: the instance's assertion for a StudioTarget,
52
+ * nothing for a bare published port. */
53
+ export async function studioReadHeaders(studio) {
54
+ return typeof studio === "number" ? {} : instanceReadHeaders(studio.instanceId);
55
+ }
package/studio-state.js CHANGED
@@ -19,6 +19,7 @@
19
19
  // — NOT `{ id, type }` (which is what the source-yaml schema uses internally).
20
20
  // Verified empirically against a live Studio instance.
21
21
  import { reachInstanceMedia, studioBaseFrom } from "./container-net.js";
22
+ import { studioReadHeaders } from "./studio-identity.js";
22
23
  // The host the studio port is reachable on. Normally the product template
23
24
  // publishes it on the loopback of the machine running the tests, so localhost is
24
25
  // right. When the test process itself runs inside a container that launches the
@@ -84,6 +85,7 @@ export function mediaDirectUrl(rawUrl) {
84
85
  export async function fetchComponents(studio) {
85
86
  const r = await fetch(`${studioBaseFrom(studio, STUDIO_HOST)}/live/api/components`, {
86
87
  signal: AbortSignal.timeout(5000),
88
+ headers: await studioReadHeaders(studio),
87
89
  });
88
90
  if (r.status === 503)
89
91
  return null;
@@ -94,6 +96,7 @@ export async function fetchComponents(studio) {
94
96
  export async function fetchComponentState(studio, componentId) {
95
97
  const r = await fetch(`${studioBaseFrom(studio, STUDIO_HOST)}/live/api/${encodeURIComponent(componentId)}/state`, {
96
98
  signal: AbortSignal.timeout(5000),
99
+ headers: await studioReadHeaders(studio),
97
100
  });
98
101
  if (r.status === 503 || r.status === 404)
99
102
  return null;
@@ -108,7 +111,7 @@ export function isSrtListenerState(value) {
108
111
  return Array.isArray(v.connectedStreams) && typeof v.connectedAt === "object" && v.connectedAt !== null;
109
112
  }
110
113
  async function fetchComponentStreamRoute(studio, componentId, route) {
111
- const r = await fetch(`${studioBaseFrom(studio, STUDIO_HOST)}/live/api/${encodeURIComponent(componentId)}/streams/${route}`, { signal: AbortSignal.timeout(5000) });
114
+ const r = await fetch(`${studioBaseFrom(studio, STUDIO_HOST)}/live/api/${encodeURIComponent(componentId)}/streams/${route}`, { signal: AbortSignal.timeout(5000), headers: await studioReadHeaders(studio) });
112
115
  if (r.status === 503 || r.status === 404)
113
116
  return null;
114
117
  if (!r.ok)