@specific.dev/spectest 0.90.3 → 0.91.1

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.
Files changed (46) hide show
  1. package/dist/android-step.js +2 -2
  2. package/dist/components/expo.d.ts +2 -0
  3. package/dist/components/index.d.ts +1 -1
  4. package/dist/components/k3s-trust.d.ts +57 -0
  5. package/dist/components/k3s-trust.js +125 -0
  6. package/dist/components/k3s.d.ts +17 -5
  7. package/dist/components/k3s.js +126 -53
  8. package/dist/daemon.js +71 -28
  9. package/dist/harness/component-images.d.ts +34 -0
  10. package/dist/harness/component-images.js +69 -0
  11. package/dist/harness/concurrency.d.ts +2 -0
  12. package/dist/harness/concurrency.js +9 -0
  13. package/dist/harness/k3s-image-loader.d.ts +3 -0
  14. package/dist/harness/k3s-image-loader.js +152 -0
  15. package/dist/harness/k3s-prepull.d.ts +8 -0
  16. package/dist/harness/k3s-prepull.js +35 -0
  17. package/dist/harness/sticky-consumer.d.ts +3 -3
  18. package/dist/harness/sticky-consumer.js +19 -4
  19. package/dist/harness/sticky-kubernetes.d.ts +15 -0
  20. package/dist/harness/sticky-kubernetes.js +163 -0
  21. package/dist/harness/sticky-paths.d.ts +5 -0
  22. package/dist/harness/sticky-paths.js +18 -0
  23. package/dist/index.d.ts +32 -2
  24. package/dist/index.js +25 -2
  25. package/package.json +1 -1
  26. package/src/android-step.test.ts +3 -3
  27. package/src/android-step.ts +2 -2
  28. package/src/android.test.ts +3 -3
  29. package/src/components/index.ts +1 -0
  30. package/src/components/k3s-concurrency.test.ts +60 -0
  31. package/src/components/k3s-trust.test.ts +81 -0
  32. package/src/components/k3s-trust.ts +135 -0
  33. package/src/components/k3s.ts +131 -51
  34. package/src/daemon.ts +72 -27
  35. package/src/harness/component-images.test.ts +44 -0
  36. package/src/harness/component-images.ts +79 -0
  37. package/src/harness/concurrency.ts +8 -0
  38. package/src/harness/k3s-image-loader.ts +131 -0
  39. package/src/harness/k3s-prepull.test.ts +57 -0
  40. package/src/harness/k3s-prepull.ts +33 -0
  41. package/src/harness/sticky-consumer.test.ts +28 -0
  42. package/src/harness/sticky-consumer.ts +14 -3
  43. package/src/harness/sticky-kubernetes.test.ts +88 -0
  44. package/src/harness/sticky-kubernetes.ts +165 -0
  45. package/src/harness/sticky-paths.ts +18 -0
  46. package/src/index.ts +48 -3
@@ -0,0 +1,163 @@
1
+ /** Kubernetes owns a separate containerd store; Docker/BuildKit GC cannot
2
+ * collect it. Maintenance runs only on the isolated publication clone. */
3
+ import { execFile } from "node:child_process";
4
+ import { promisify } from "node:util";
5
+ import { createHash, randomUUID } from "node:crypto";
6
+ import { mkdir, readFile, readdir, rename, writeFile } from "node:fs/promises";
7
+ import { STICKY_ROOT } from "./sticky-paths.js";
8
+ const STORE_TARGET = "/var/lib/rancher/k3s/agent/containerd";
9
+ const SOCKET = "/run/spectest-cache.sock";
10
+ const ROOT = `${STICKY_ROOT}/kubernetes`;
11
+ const IMAGE_ID = /^sha256:[a-f0-9]{64}$/;
12
+ const SERVICE_NAME = /^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/;
13
+ export function stickyKubernetesDirectory(service, version) {
14
+ // Version changes must not open old metadata with a different containerd.
15
+ const layout = createHash("sha256").update(`overlayfs-v1:${version}`).digest("hex").slice(0, 16);
16
+ return `${ROOT}/${service}/${layout}`;
17
+ }
18
+ const runCommand = async (program, args, timeout = 30_000) => (await promisify(execFile)(program, args, { timeout })).stdout.trim();
19
+ export async function registerStickyKubernetesStore(service, version, run = runCommand) {
20
+ if (!SERVICE_NAME.test(service))
21
+ throw new Error(`invalid Kubernetes cache service name: ${service}`);
22
+ // Respect custom Dockerfile overrides of the component's image.
23
+ const image = (await run("docker", ["inspect", "--format", "{{.Image}}", service])).trim();
24
+ if (!IMAGE_ID.test(image))
25
+ throw new Error(`cannot identify Kubernetes runtime image for ${service}`);
26
+ const directory = stickyKubernetesDirectory(service, version);
27
+ await mkdir(directory, { recursive: true });
28
+ const temporary = `${directory}/store.${randomUUID()}.tmp`;
29
+ await writeFile(temporary, JSON.stringify({ schema: 1, image, updatedAt: Date.now() }));
30
+ await rename(temporary, `${directory}/store.json`);
31
+ }
32
+ /** Standalone containerd never starts CRI/kubelet or any inherited workload.
33
+ * Native API deletion preserves content and committed image snapshots while
34
+ * releasing sandbox, container, lease and writable snapshot references. */
35
+ export const KUBERNETES_CACHE_MAINTENANCE_SH = `set -eu
36
+ exec 2>&1
37
+ cat > /run/cache.toml <<'CONFIG'
38
+ version = 2
39
+ disabled_plugins = ["io.containerd.grpc.v1.cri", "io.containerd.cri.v1.runtime", "io.containerd.cri.v1.images"]
40
+ CONFIG
41
+ containerd --config /run/cache.toml --root ${STORE_TARGET} --state /run/cache-state --address ${SOCKET} > /run/cache.log 2>&1 &
42
+ pid=$!
43
+ stop() { kill "$pid"; wait "$pid"; }
44
+ trap 'stop; exit 0' TERM INT
45
+ trap 'kill "$pid" 2>/dev/null || true' EXIT
46
+ c() { ctr --address ${SOCKET} --namespace k8s.io "$@"; }
47
+ i=0
48
+ until c version >/dev/null 2>&1; do
49
+ kill -0 "$pid" || { cat /run/cache.log; exit 1; }
50
+ i=$((i+1)); [ "$i" -lt 300 ] || { cat /run/cache.log; exit 1; }
51
+ sleep 0.1
52
+ done
53
+ ids=$(c containers ls -q)
54
+ if [ -n "$ids" ]; then c containers rm $ids; fi
55
+ # 1.7 has no sandbox store; 2.x does.
56
+ case "$(containerd --version)" in
57
+ *v1.*) ;;
58
+ *) sandboxes=$(c sandboxes ls)
59
+ ids=$(printf '%s\\n' "$sandboxes" | awk 'NR > 1 {print $1}')
60
+ if [ -n "$ids" ]; then c sandboxes rm --force $ids; fi ;;
61
+ esac
62
+ ids=$(c leases ls -q)
63
+ if [ -n "$ids" ]; then c leases rm $ids; fi
64
+ # Remove children before parents, and tolerate asynchronous GC winning a race.
65
+ previous=''
66
+ while :; do
67
+ snapshots=$(c snapshots --snapshotter overlayfs ls)
68
+ ids=$(printf '%s\\n' "$snapshots" | awk 'NR > 1 && $NF != "Committed" {print $1}')
69
+ [ -n "$ids" ] || break
70
+ [ "$ids" != "$previous" ] || { echo 'cannot clean Kubernetes writable snapshots' >&2; exit 1; }
71
+ previous="$ids"
72
+ for id in $ids; do c snapshots --snapshotter overlayfs rm "$id" >/dev/null 2>&1 || true; done
73
+ done
74
+ touch /run/cache-ready
75
+ while :; do sleep 1 & wait $!; done
76
+ `;
77
+ export async function openKubernetesCacheMaintenance(run, root = ROOT) {
78
+ const stores = [];
79
+ const protectedImages = new Set();
80
+ const closeStore = async (store) => {
81
+ if (!store.active)
82
+ return;
83
+ await run("docker", ["stop", "--time", "30", store.container], 45_000);
84
+ const status = JSON.parse(await run("docker", ["inspect", "--format", "{{json .State}}", store.container]));
85
+ if (status.ExitCode !== 0 || status.OOMKilled)
86
+ throw new Error(`unclean Kubernetes cache shutdown: ${store.container}`);
87
+ await run("docker", ["rm", store.container]);
88
+ store.active = false;
89
+ if (!stores.some(other => other.active && other.descriptor.image === store.descriptor.image))
90
+ protectedImages.delete(store.descriptor.image);
91
+ // These contain runtime state, not image content or unpacked layers.
92
+ await run("rm", ["-rf", ...["io.containerd.runtime.v2.task", "io.containerd.grpc.v1.cri", "io.containerd.cri.v1.runtime", "tmpmounts"]
93
+ .map(child => `${store.directory}/containerd/${child}`)]);
94
+ };
95
+ const close = async () => {
96
+ const failures = [];
97
+ for (const store of stores) {
98
+ try {
99
+ await closeStore(store);
100
+ }
101
+ catch (error) {
102
+ failures.push(error);
103
+ }
104
+ }
105
+ if (failures.length)
106
+ throw new AggregateError(failures, "Kubernetes cache shutdown failed");
107
+ };
108
+ const entries = await readdir(root, { withFileTypes: true }).catch((error) => {
109
+ if (error.code === "ENOENT")
110
+ return [];
111
+ throw error;
112
+ });
113
+ try {
114
+ for (const service of entries) {
115
+ if (!service.isDirectory() || !SERVICE_NAME.test(service.name))
116
+ continue;
117
+ for (const layout of await readdir(`${root}/${service.name}`, { withFileTypes: true })) {
118
+ if (!layout.isDirectory() || !/^[a-f0-9]{16}$/.test(layout.name))
119
+ continue;
120
+ const directory = `${root}/${service.name}/${layout.name}`;
121
+ const descriptor = JSON.parse(await readFile(`${directory}/store.json`, "utf8"));
122
+ if (descriptor.schema !== 1 || !IMAGE_ID.test(descriptor.image) || !Number.isSafeInteger(descriptor.updatedAt)) {
123
+ throw new Error(`invalid Kubernetes cache descriptor: ${directory}`);
124
+ }
125
+ const container = `spectest-k8s-cache-${randomUUID()}`;
126
+ await run("docker", ["run", "-d", "--name", container, "--network", "none", "--privileged", "--stop-signal", "SIGTERM", "--tmpfs", "/run", "--tmpfs", "/var/run",
127
+ "--mount", `type=bind,source=${directory}/containerd,target=${STORE_TARGET}`,
128
+ "--entrypoint", "/bin/sh", descriptor.image, "-c", KUBERNETES_CACHE_MAINTENANCE_SH]);
129
+ stores.push({ directory, descriptor, container, active: true });
130
+ protectedImages.add(descriptor.image);
131
+ try {
132
+ await run("docker", ["exec", container, "sh", "-c", "i=0; until [ -f /run/cache-ready ]; do i=$((i+1)); [ $i -lt 900 ] || exit 1; sleep 0.1; done"], 100_000);
133
+ }
134
+ catch (error) {
135
+ const logs = await run("docker", ["logs", "--tail", "40", container]).catch(() => "");
136
+ throw new Error(`Kubernetes cache preparation failed for ${directory}: ${logs}`, { cause: error });
137
+ }
138
+ }
139
+ }
140
+ stores.sort((a, b) => a.descriptor.updatedAt - b.descriptor.updatedAt || a.directory.localeCompare(b.directory));
141
+ let next = 0;
142
+ const pruneOldest = async () => {
143
+ const store = stores[next++];
144
+ if (!store)
145
+ return false;
146
+ await run("docker", ["exec", store.container, "sh", "-ec",
147
+ `ids=$(ctr -a ${SOCKET} -n k8s.io images ls -q); if [ -n "$ids" ]; then ctr -a ${SOCKET} -n k8s.io images rm --sync $ids; fi`], 120_000);
148
+ await closeStore(store);
149
+ // No writer remains. Discard the now-empty cache and its descriptor so
150
+ // obsolete k3s binaries can also be collected by the outer Docker GC.
151
+ await run("rm", ["-rf", store.directory]);
152
+ return true;
153
+ };
154
+ // Abandoned services/versions should not occupy the disk indefinitely.
155
+ while (stores[next] && stores[next].descriptor.updatedAt < Date.now() - 7 * 24 * 60 * 60 * 1000)
156
+ await pruneOldest();
157
+ return { protectedImages, pruneOldest, close };
158
+ }
159
+ catch (error) {
160
+ await close().catch(() => { });
161
+ throw error;
162
+ }
163
+ }
@@ -0,0 +1,5 @@
1
+ export declare const STICKY_ROOT = "/var/lib/spectest-sticky";
2
+ export declare const STICKY_RUNTIME = "/run/spectest-sticky";
3
+ /** Written only after the supervisor mounts this project's private disk. */
4
+ export declare function stickyDiskPathSync(manifest?: string): string | null;
5
+ export declare function isOnStickyDisk(path: string, root?: string | null): boolean;
@@ -0,0 +1,18 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { resolve } from "node:path";
3
+ export const STICKY_ROOT = "/var/lib/spectest-sticky";
4
+ export const STICKY_RUNTIME = "/run/spectest-sticky";
5
+ /** Written only after the supervisor mounts this project's private disk. */
6
+ export function stickyDiskPathSync(manifest = `${STICKY_RUNTIME}/consumer.json`) {
7
+ try {
8
+ const config = JSON.parse(readFileSync(manifest, "utf8"));
9
+ return config.path === STICKY_ROOT ? STICKY_ROOT : null;
10
+ }
11
+ catch {
12
+ return null;
13
+ }
14
+ }
15
+ export function isOnStickyDisk(path, root = stickyDiskPathSync()) {
16
+ const absolute = resolve(path);
17
+ return root !== null && (absolute === root || absolute.startsWith(`${root}/`));
18
+ }
package/dist/index.d.ts CHANGED
@@ -74,6 +74,8 @@ export interface EnvironmentConfig<S extends ServicesMap = ServicesMap> {
74
74
  export interface ServiceConfig {
75
75
  /** Image definition: registry pull or inline build. */
76
76
  image: ServiceImage;
77
+ /** @internal Images built by the harness and loaded into this component. */
78
+ imageBuilds?: readonly ComponentImageBuild[];
77
79
  /**
78
80
  * Shell command to run (sh -c). Defaults to the image's CMD. NB: this
79
81
  * **replaces the image's entrypoint** with `/bin/sh -c` — an
@@ -741,11 +743,14 @@ export interface ServiceTls {
741
743
  * daemon forwards proxied requests here over `spectest-net`. */
742
744
  port: number;
743
745
  }
744
- export type ServiceImage = {
746
+ /** @internal Normalized wire representation; legacy definitions remain accepted. */
747
+ export type NormalizedServiceImage = {
745
748
  type: "registry";
746
749
  reference: string;
747
750
  } | {
748
751
  type: "dockerfile";
752
+ /** Optional full local image reference. */
753
+ reference?: string;
749
754
  /**
750
755
  * Path of a Dockerfile in your repo, relative to the project root
751
756
  * (where `spectest/` lives) — `"Dockerfile"`, `"apps/api/Dockerfile"`.
@@ -787,6 +792,8 @@ export type ServiceImage = {
787
792
  buildArgs?: Readonly<Record<string, string>>;
788
793
  } | {
789
794
  type: "dockerfile";
795
+ /** Optional full local image reference. */
796
+ reference?: string;
790
797
  /**
791
798
  * Dockerfile contents, built as if the file sat outside the build
792
799
  * context (it is never written into your repo). The context is the
@@ -815,6 +822,29 @@ export type ServiceImage = {
815
822
  */
816
823
  buildArgs?: Readonly<Record<string, string>>;
817
824
  };
825
+ /** Dockerfile build options shared by services and Kubernetes images. */
826
+ export type BuildImage = (Omit<Extract<NormalizedServiceImage, {
827
+ type: "dockerfile";
828
+ path: string;
829
+ }>, "type"> | Omit<Extract<NormalizedServiceImage, {
830
+ type: "dockerfile";
831
+ content: string;
832
+ }>, "type">) & {
833
+ /** Full image reference, e.g. "k8s.internal:5000/runtime:spectest".
834
+ * Optional for a service; required for a Kubernetes build. This names a
835
+ * local image and does not publish it to a registry. */
836
+ reference?: string;
837
+ };
838
+ /** A registry reference or Dockerfile build. Tagged objects from older SDKs
839
+ * remain accepted for compatibility; new definitions do not need `type`. */
840
+ export type ServiceImage = string | BuildImage | NormalizedServiceImage;
841
+ /** @internal Keep the wire format compatible with existing control planes. */
842
+ export declare function normalizeServiceImage(image: ServiceImage): NormalizedServiceImage;
843
+ /** @internal Serializes build-only work with the owning component. */
844
+ export interface ComponentImageBuild {
845
+ reference: string;
846
+ image: BuildImage;
847
+ }
818
848
  /**
819
849
  * Check a service's `image` at config time, so a mistake fails the
820
850
  * environment load with a message that names it, instead of a build
@@ -831,7 +861,7 @@ export type ServiceImage = {
831
861
  * checks go through the project-file resolver, so the same rules as
832
862
  * `ctx.readProjectFile` apply.
833
863
  */
834
- export declare function validateServiceImage(serviceName: string, image: ServiceImage): ServiceImage;
864
+ export declare function validateServiceImage(serviceName: string, input: ServiceImage): NormalizedServiceImage;
835
865
  export interface VolumeMount {
836
866
  /**
837
867
  * Named shared volume. Two services mounting the same `name` share one
package/dist/index.js CHANGED
@@ -220,6 +220,19 @@ function expandServiceGroups(input) {
220
220
  }
221
221
  return out;
222
222
  }
223
+ /** @internal Keep the wire format compatible with existing control planes. */
224
+ export function normalizeServiceImage(image) {
225
+ if (typeof image === "string")
226
+ return { type: "registry", reference: image };
227
+ if (!image || typeof image !== "object" || Array.isArray(image))
228
+ throw new Error("image must be a registry reference or Dockerfile build");
229
+ if ("type" in image) {
230
+ if (image.type !== "registry" && image.type !== "dockerfile")
231
+ throw new Error("unsupported image type");
232
+ return image;
233
+ }
234
+ return { ...image, type: "dockerfile" };
235
+ }
223
236
  /** A Dockerfile `ARG` name: what `docker build --build-arg` accepts as a key. */
224
237
  const BUILD_ARG_NAME_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
225
238
  function validateBuildArgs(serviceName, buildArgs) {
@@ -255,9 +268,17 @@ const BUILD_TARGET_RE = /^[^\s]+$/;
255
268
  * checks go through the project-file resolver, so the same rules as
256
269
  * `ctx.readProjectFile` apply.
257
270
  */
258
- export function validateServiceImage(serviceName, image) {
271
+ export function validateServiceImage(serviceName, input) {
272
+ const image = normalizeServiceImage(input);
273
+ const reference = typeof input === "string" ? input : "reference" in input ? input.reference : undefined;
274
+ if (reference !== undefined && (typeof reference !== "string" || !reference || reference.startsWith("-") || /[\s\x00]/.test(reference)))
275
+ throw new Error(`service "${serviceName}" image reference must be a non-empty image name without whitespace`);
259
276
  if (image.type !== "dockerfile")
260
277
  return image;
278
+ if (reference?.includes("@"))
279
+ throw new Error(`service "${serviceName}" build reference must be an image name/tag, not a digest`);
280
+ if (image.exclude !== undefined && (!Array.isArray(image.exclude) || image.exclude.some(e => typeof e !== "string")))
281
+ throw new Error(`service "${serviceName}" image exclude must be an array of glob patterns`);
261
282
  validateBuildArgs(serviceName, image.buildArgs);
262
283
  const hasPath = typeof image.path === "string";
263
284
  const hasContent = typeof image.content === "string";
@@ -265,7 +286,7 @@ export function validateServiceImage(serviceName, image) {
265
286
  throw new Error(`service "${serviceName}" image sets both \`path\` and \`content\` — point at the Dockerfile in your repo with \`path\`, or inline it with \`content\`, not both`);
266
287
  }
267
288
  if (!hasPath && !hasContent) {
268
- throw new Error(`service "${serviceName}" image { type: "dockerfile" } needs \`path\` (a Dockerfile in your repo, relative to the project root) or \`content\``);
289
+ throw new Error(`service "${serviceName}" image build needs \`path\` (a Dockerfile in your repo, relative to the project root) or \`content\``);
269
290
  }
270
291
  if (hasPath) {
271
292
  const file = resolveExistingProjectPath(image.path, `service "${serviceName}" Dockerfile`);
@@ -403,6 +424,8 @@ export function defineEnvironment(input) {
403
424
  const { fakes, services: rawServices, ...rest } = input;
404
425
  const expanded = expandServiceGroups(rawServices);
405
426
  for (const [key, svc] of Object.entries(expanded)) {
427
+ for (const build of svc.imageBuilds ?? [])
428
+ validateServiceImage(`${key} image ${build.reference}`, build.image);
406
429
  expanded[key] = applyCoverageAdapters(key, {
407
430
  ...svc,
408
431
  image: validateServiceImage(key, svc.image),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@specific.dev/spectest",
3
- "version": "0.90.3",
3
+ "version": "0.91.1",
4
4
  "description": "Spectest SDK for defining test environments in TypeScript.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -41,7 +41,7 @@ const cases: [Omit<AndroidStepDetails, "device">, string][] = [
41
41
  target: { kind: "accessibility", value: "Save" },
42
42
  state: "hidden",
43
43
  },
44
- 'wait for accessibility label "Save" to be hidden',
44
+ 'wait for label "Save" to be hidden',
45
45
  ],
46
46
  [
47
47
  {
@@ -86,7 +86,7 @@ const cases: [Omit<AndroidStepDetails, "device">, string][] = [
86
86
  for (const [action, title] of cases) {
87
87
  test(title, () =>
88
88
  expect(androidStepTitle({ device: "phone", ...action })).toBe(
89
- `phone: ${title}`,
89
+ title,
90
90
  ),
91
91
  );
92
92
  }
@@ -100,7 +100,7 @@ test("custom selector expressions remain opaque structured data", () => {
100
100
  value: expression,
101
101
  });
102
102
  expect(androidStepTitle({ device: "phone", action: "tap", target })).toBe(
103
- "phone: tap custom selector",
103
+ "tap custom selector",
104
104
  );
105
105
  for (const [using, kind] of [
106
106
  ["id", "id"],
@@ -43,7 +43,7 @@ export function androidTargetLabel(target: AndroidTarget): string {
43
43
  case "id":
44
44
  return `id ${quoted}`;
45
45
  case "accessibility":
46
- return `accessibility label ${quoted}`;
46
+ return `label ${quoted}`;
47
47
  case "xpath":
48
48
  return `XPath ${quoted}`;
49
49
  case "custom":
@@ -103,5 +103,5 @@ export function androidStepTitle(step: AndroidStepDetails): string {
103
103
  label = `run command ${JSON.stringify(step.command)}`;
104
104
  break;
105
105
  }
106
- return `${step.device}: ${label}`;
106
+ return label;
107
107
  }
@@ -156,12 +156,12 @@ describe("Android case lifecycle", () => {
156
156
  const steps = events.filter((e) => e.kind === "android");
157
157
  expect(steps.length).toBe(7);
158
158
  expect(steps[0]).toMatchObject({
159
- title: String.raw`phone: fill text "a \"quoted\" label"`,
159
+ title: String.raw`fill text "a \"quoted\" label"`,
160
160
  android: { device: "phone", action: "fill", target: { kind: "text", value: 'a "quoted" label' } },
161
161
  });
162
162
  expect(steps[1]).toMatchObject({ android: { device: "phone", action: "text", target: { kind: "id", value: "com.example.app:id/draft" } } });
163
- expect(steps[2]).toMatchObject({ title: "phone: take screenshot", android: { device: "phone", action: "screenshot" } });
164
- expect(steps[3]).toMatchObject({ title: 'phone: tap text "Save"', android: { device: "phone", action: "tap", target: { kind: "text", value: "Save" } } });
163
+ expect(steps[2]).toMatchObject({ title: "take screenshot", android: { device: "phone", action: "screenshot" } });
164
+ expect(steps[3]).toMatchObject({ title: 'tap text "Save"', android: { device: "phone", action: "tap", target: { kind: "text", value: "Save" } } });
165
165
  expect(steps[4]).toMatchObject({ android: { device: "phone", action: "tap", point: { x: 270, y: 1800 } } });
166
166
  expect(steps[5]).toMatchObject({ status: "failed", android: { device: "phone", action: "tap", target: { kind: "text", value: "Gone" } } });
167
167
  expect(JSON.stringify(steps)).not.toContain("private draft");
@@ -26,6 +26,7 @@ export { SQL, type SqlClient, type SqlOptions } from "../sql.js";
26
26
  export {
27
27
  k3s,
28
28
  type K3sOptions,
29
+ type KubernetesImage,
29
30
  type K3sHelpers,
30
31
  type K3sClient,
31
32
  type TaggedObjectApi,
@@ -0,0 +1,60 @@
1
+ import { expect, test } from "bun:test";
2
+ import { k3s } from "./k3s";
3
+
4
+ function deferred() {
5
+ let resolve!: () => void;
6
+ const promise = new Promise<void>(r => { resolve = r; });
7
+ return { promise, resolve };
8
+ }
9
+
10
+ test("registry, Traefik, and every declared pull start without waiting for one another", async () => {
11
+ const started = new Set<string>();
12
+ const allStarted = deferred(), release = deferred();
13
+ const enter = async (name: string) => {
14
+ started.add(name);
15
+ if (started.size === 5) allStarted.resolve();
16
+ await release.promise;
17
+ };
18
+ const cluster = k3s({ images: ["app:1", "app:2", "app:3"] });
19
+ let finished = false;
20
+ const work = cluster.setup({ name: "cluster", helpers: {
21
+ apply: async (manifest: string) => { await enter(manifest.includes("spectest-registry") ? "registry" : "traefik"); },
22
+ waitForRollout: async () => {},
23
+ }, exec: async (_name: string, args: string[]) => {
24
+ if (args[3] === "pull") await enter(args.at(-1)!);
25
+ return { exitCode: 0, stdout: "", stderr: "" };
26
+ } } as never).then(() => { finished = true; });
27
+ try {
28
+ await Promise.race([allStarted.promise, Bun.sleep(1000).then(() => { throw new Error(`serialized setup: ${[...started]}`); })]);
29
+ expect(finished).toBe(false);
30
+ expect(started.size).toBe(5);
31
+ } finally { release.resolve(); await work; }
32
+ });
33
+
34
+ test("a failed infrastructure rollout waits for other rollouts and image pulls to stop", async () => {
35
+ const started = deferred(), release = deferred();
36
+ let active = 0, finished = false;
37
+ const hold = async () => {
38
+ if (++active === 2) started.resolve();
39
+ await release.promise;
40
+ --active;
41
+ };
42
+ const failure = new Error("Traefik failed");
43
+ const cluster = k3s({ images: ["app:1"] });
44
+ const work = cluster.setup({ name: "cluster", helpers: {
45
+ apply: async () => {},
46
+ waitForRollout: async (deployment: string) => {
47
+ if (deployment === "traefik") throw failure;
48
+ await hold();
49
+ },
50
+ }, exec: async (_name: string, args: string[]) => {
51
+ if (args[3] === "pull") await hold();
52
+ return { exitCode: 0, stdout: "", stderr: "" };
53
+ } } as never).then(() => { finished = true; }, error => { finished = true; return error; });
54
+ try {
55
+ await started.promise;
56
+ expect(finished).toBe(false);
57
+ } finally { release.resolve(); }
58
+ expect(await work).toBe(failure);
59
+ expect(active).toBe(0);
60
+ });
@@ -0,0 +1,81 @@
1
+ import { afterEach, expect, test } from "bun:test";
2
+ import { mkdtempSync, readFileSync, rmSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { execFileSync } from "node:child_process";
6
+ import { request } from "node:https";
7
+ import { installPodTrust, podTrustPatch, stopPodTrust, TRUST_OPT_OUT } from "./k3s-trust";
8
+
9
+ function patched<T>(value: T, patch: ReturnType<typeof podTrustPatch>): T {
10
+ const out = structuredClone(value);
11
+ for (const operation of patch) {
12
+ const keys = operation.path.slice(1).split("/");
13
+ const key = keys.pop()!;
14
+ const parent = keys.reduce((o, k) => o[k], out as any);
15
+ if (key === "-") parent.push(operation.value); else parent[key] = operation.value;
16
+ }
17
+ return out;
18
+ }
19
+
20
+ test("mounts trust into regular and init containers without changing images or security settings", () => {
21
+ const pod = { spec: { containers: [{ name: "app", image: "app@sha256:original", securityContext: { runAsUser: 1000, readOnlyRootFilesystem: true } }], initContainers: [{ name: "init", image: "init:1" }] } };
22
+ const patch = podTrustPatch(pod, "bundle");
23
+ const result: any = patched(pod, patch);
24
+ expect(result.spec.containers[0].image).toBe("app@sha256:original");
25
+ expect(result.spec.containers[0].securityContext).toEqual(pod.spec.containers[0].securityContext);
26
+ expect(result.spec.initContainers[0].env).toContainEqual({ name: "SSL_CERT_FILE", value: "/spectest/ca-bundle.crt" });
27
+ expect(result.spec.volumes[0].configMap.name).toBe("bundle");
28
+ expect(result.spec.containers[0].volumeMounts.every((m: any) => m.readOnly)).toBe(true);
29
+ expect(podTrustPatch(result, "bundle")).toEqual([]);
30
+ expect(pod).not.toHaveProperty("spec.volumes");
31
+ });
32
+
33
+ test("preserves explicit trust settings and mounts, including volume name collisions", () => {
34
+ const pod = { spec: {
35
+ volumes: [{ name: "spectest-ca", emptyDir: {} }],
36
+ containers: [{ env: [{ name: "SSL_CERT_FILE", value: "/custom.pem" }], volumeMounts: [{ name: "custom", mountPath: "/etc/ssl" }] }],
37
+ } };
38
+ const result: any = patched(pod, podTrustPatch(pod, "bundle"));
39
+ expect(result.spec.volumes[1].name).toBe("spectest-ca-1");
40
+ expect(result.spec.containers[0].env.filter((e: any) => e.name === "SSL_CERT_FILE")).toEqual([{ name: "SSL_CERT_FILE", value: "/custom.pem" }]);
41
+ expect(result.spec.containers[0].volumeMounts.some((m: any) => m.mountPath === "/etc/ssl/certs/ca-certificates.crt")).toBe(false);
42
+ });
43
+
44
+ test("opt-out, Windows and ephemeral container updates are handled without changing immutable Pod volumes", () => {
45
+ expect(podTrustPatch({ metadata: { annotations: { [TRUST_OPT_OUT]: "false" } }, spec: { containers: [{}] } }, "bundle")).toEqual([]);
46
+ expect(podTrustPatch({ spec: { os: { name: "windows" }, containers: [{}] } }, "bundle")).toEqual([]);
47
+ expect(podTrustPatch({ spec: { ephemeralContainers: [{}] } }, "bundle", true)).toEqual([]);
48
+ const result: any = patched({ spec: { containers: [{}], ephemeralContainers: [{}], volumes: [{ name: "existing", configMap: { name: "bundle" } }] } }, podTrustPatch({ spec: { ephemeralContainers: [{}], volumes: [{ name: "existing", configMap: { name: "bundle" } }] } }, "bundle", true));
49
+ expect(result.spec.volumes).toHaveLength(1);
50
+ expect(result.spec.containers[0]).toEqual({});
51
+ expect(result.spec.ephemeralContainers[0].volumeMounts[0].name).toBe("existing");
52
+ });
53
+
54
+ afterEach(() => stopPodTrust("fixture"));
55
+ test("admission uses verified TLS, provisions bundles on real requests, and fails closed", async () => {
56
+ const dir = mkdtempSync(join(tmpdir(), "k3s-trust-"));
57
+ try {
58
+ execFileSync("openssl", ["req", "-x509", "-newkey", "rsa:2048", "-nodes", "-keyout", join(dir, "key"), "-out", join(dir, "cert"), "-subj", "/CN=fixture", "-addext", "subjectAltName=IP:127.0.0.1", "-days", "1"], { stdio: "ignore" });
59
+ const cert = readFileSync(join(dir, "cert"), "utf8");
60
+ let config: any, calls = 0, fail = false;
61
+ await installPodTrust({ name: "fixture", gateway: "127.0.0.1", bundle: "bundle", cert, key: readFileSync(join(dir, "key"), "utf8"), ca: cert }, {
62
+ apply: async c => { config = c; },
63
+ ensureBundle: async namespace => { expect(namespace).toBe("app"); calls++; if (fail) throw new Error("API unavailable"); },
64
+ });
65
+ expect(config.webhooks[0].failurePolicy).toBe("Fail");
66
+ const send = (dryRun = false) => new Promise<{ status: number; body: any }>((resolve, reject) => {
67
+ const req = request(config.webhooks[0].clientConfig.url + "?timeout=10s", { method: "POST", ca: cert }, res => {
68
+ let body = ""; res.on("data", c => body += c); res.on("end", () => resolve({ status: res.statusCode!, body: res.statusCode === 200 ? JSON.parse(body) : body }));
69
+ });
70
+ req.on("error", reject);
71
+ req.end(JSON.stringify({ request: { uid: "request-1", resource: { resource: "pods" }, namespace: "app", dryRun, object: { spec: { containers: [{}] } } } }));
72
+ });
73
+ const dry = await send(true);
74
+ expect(dry.body.response.uid).toBe("request-1");
75
+ expect(calls).toBe(0);
76
+ expect((await send()).status).toBe(200);
77
+ expect(calls).toBe(1);
78
+ fail = true;
79
+ expect((await send()).status).toBe(500);
80
+ } finally { rmSync(dir, { recursive: true, force: true }); }
81
+ });