The best way to control Claude Code, Codex and more from any computer or phone

main
ts 173 lines 6.27 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import * as Kubernetes from "@alchemy.run/kubernetes";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Core from "@alchemy.run/test/Core";
6 import * as Provider from "alchemy/Provider";
7 import * as Cause from "effect/Cause";
8 import * as Effect from "effect/Effect";
9 import * as Exit from "effect/Exit";
10 import * as Layer from "effect/Layer";
11 import * as Redacted from "effect/Redacted";
12 import * as Schedule from "effect/Schedule";
13 import {
14 destroyKindStack,
15 KindTestCluster,
16 kubectlGet,
17 removeRbac,
18 restrictedConnection,
19 updateRole,
20 type KindCluster,
21 } from "./fixtures/kind.ts";
22
23 const testOptions = { providers: Layer.mergeAll(AWS.providers(), Kubernetes.providers()) };
24 const { test, beforeAll, afterAll } = Test.make(testOptions);
25
26 // Ungated probe: `Job` is a composite host (in-cluster batch/v1 Job or
27 // CronJob plus adapter-owned cloud resources on managed clusters) with no
28 // faithful single-API enumeration, so `list()` is intentionally empty. The
29 // probe proves the provider is registered and its record type-checks.
30 test.provider(
31 "list returns an empty array (composite host, not enumerable)",
32 () =>
33 Effect.gen(function* () {
34 const provider = yield* Provider.findProvider(Kubernetes.Job);
35 const all = yield* provider.list();
36 expect(Array.isArray(all)).toBe(true);
37 expect(all).toEqual([]);
38 }),
39 { tags: ["provider:aws", "provider:kubernetes", "provider:kubernetes:job", "live"] },
40 );
41
42 const kindStack = Core.scratchStack(testOptions, "KubernetesJobKind");
43
44 // Creates a real kind cluster (~30s); needs Docker, kind, and kubectl. The
45 // Job runs a pre-built image; these cases assert on the applied objects.
46 describe(
47 "Kubernetes Job on kind",
48 {
49 tags: [
50 "provider:kubernetes",
51 "provider:kubernetes:job",
52 "provider:kubernetes:localcluster",
53 "live",
54 ],
55 },
56 () => {
57 let cluster: KindCluster;
58 const image = "registry.k8s.io/pause:3.10";
59 const ns = ["--namespace", "default"];
60 const readJob = (name: string) => kubectlGet(cluster, ["job", name, ...ns]);
61 // Background propagation removes the Job object shortly after DELETE.
62 const waitGone = (name: string) =>
63 readJob(name).pipe(
64 Effect.repeat({
65 schedule: Schedule.spaced("1 second"),
66 until: (job) => job === undefined,
67 times: 20,
68 }),
69 );
70 const envOf = (job: any): Record<string, string> =>
71 Object.fromEntries(
72 (job.spec.template.spec.containers[0].env as { name: string; value: string }[]).map(
73 (entry) => [entry.name, entry.value],
74 ),
75 );
76
77 beforeAll(
78 Effect.gen(function* () {
79 yield* kindStack.destroy();
80 cluster = yield* kindStack.deploy(KindTestCluster("job", 5065));
81 }),
82 { timeout: 300_000 },
83 );
84
85 afterAll.skipIf(!!process.env.NO_DESTROY)(
86 Effect.suspend(() => destroyKindStack(kindStack, cluster)),
87 { timeout: 180_000 },
88 );
89
90 test.provider(
91 "one-shot job names follow unwrapped Redacted env values",
92 (stack) =>
93 Effect.gen(function* () {
94 yield* stack.destroy();
95 const job = (token: string) =>
96 Kubernetes.Job("Rotate", {
97 cluster: cluster.connection,
98 name: "rotate",
99 image,
100 backoffLimit: 0,
101 env: { TOKEN: Redacted.make(token) },
102 });
103
104 const first = yield* stack.deploy(job("job-token-sentinel-a"));
105 expect(envOf(yield* readJob(first.jobName)).TOKEN).toBe("job-token-sentinel-a");
106 expect(JSON.stringify(first.kubernetesObjects)).not.toContain("sentinel");
107
108 // Same secret value, fresh Redacted wrapper: same content address.
109 const again = yield* stack.deploy(job("job-token-sentinel-a"));
110 expect(again.jobName).toBe(first.jobName);
111
112 // A rotated secret is a new spec: a new Job replaces the old one.
113 const rotated = yield* stack.deploy(job("job-token-sentinel-b"));
114 expect(rotated.jobName).not.toBe(first.jobName);
115 expect(envOf(yield* readJob(rotated.jobName)).TOKEN).toBe("job-token-sentinel-b");
116 expect(yield* waitGone(first.jobName)).toBe(undefined);
117
118 yield* stack.destroy();
119 expect(yield* waitGone(rotated.jobName)).toBe(undefined);
120 expect(yield* kubectlGet(cluster, ["serviceaccount", "rotate", ...ns])).toBe(undefined);
121 }),
122 { timeout: 120_000 },
123 );
124
125 test.provider(
126 "destroy surfaces a 403 and leaves the objects in place",
127 (stack) =>
128 Effect.gen(function* () {
129 yield* stack.destroy();
130 const account = "job-no-delete";
131 const rules = (verbs: string[]) => [
132 { apiGroups: [""], resources: ["serviceaccounts"], verbs },
133 { apiGroups: ["batch"], resources: ["jobs"], verbs },
134 ];
135 const restricted = yield* restrictedConnection(
136 cluster,
137 account,
138 rules(["get", "list", "create", "patch", "update"]),
139 );
140 const deployed = yield* stack.deploy(
141 Kubernetes.Job("Kept", {
142 cluster: restricted,
143 name: "kept",
144 image,
145 backoffLimit: 0,
146 }),
147 );
148
149 const exit = yield* Effect.exit(stack.destroy());
150 expect(Exit.isFailure(exit)).toBe(true);
151 if (Exit.isFailure(exit)) {
152 expect(Cause.pretty(exit.cause)).toContain("403");
153 }
154 expect(yield* readJob(deployed.jobName)).toBeDefined();
155 expect(yield* kubectlGet(cluster, ["serviceaccount", "kept", ...ns])).toBeDefined();
156
157 // Granting delete lets the same destroy converge.
158 yield* updateRole(
159 cluster,
160 account,
161 rules(["get", "list", "create", "patch", "update", "delete"]),
162 );
163 yield* stack
164 .destroy()
165 .pipe(Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 5 }));
166 expect(yield* waitGone(deployed.jobName)).toBe(undefined);
167 expect(yield* kubectlGet(cluster, ["serviceaccount", "kept", ...ns])).toBe(undefined);
168 yield* removeRbac(cluster, account);
169 }),
170 { timeout: 120_000 },
171 );
172 },
173 );