@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 118 lines 4.3 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
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 artifactregistry from "@distilled.cloud/gcp/artifactregistry_v1";
7 import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import { expectProbe } from "../bindingHost.ts";
11 import ArtifactRegistryBindingsHost, { Images } from "./fixtures/bindings-host.ts";
12 import { IMAGE, pushDockerVersion } from "./registry.ts";
13
14 const testOptions = { providers: GCP.providers() };
15 const { test, beforeAll, afterAll } = Test.make(testOptions);
16 const sharedStack = Core.scratchStack(testOptions, "ArtifactRegistryBindings");
17
18 let baseUrl: string;
19 let hostAccount: string;
20 let repository: {
21 name: string;
22 repositoryId: string;
23 project: string;
24 location: string;
25 };
26
27 /** Every project-level role (and its IAM Condition) `account` holds. */
28 const projectGrantsOf = (account: string) =>
29 Effect.gen(function* () {
30 const { project } = yield* GcpEnvironment.current;
31 const policy = yield* resourcemanager.getIamPolicyProjects({
32 resource: `projects/${project}`,
33 body: { options: { requestedPolicyVersion: 3 } },
34 });
35 return (policy.bindings ?? [])
36 .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${account}`))
37 .map((binding) => ({
38 role: binding.role,
39 condition: binding.condition?.expression,
40 }));
41 });
42
43 describe(
44 "ArtifactRegistry Bindings",
45 {
46 tags: ["provider:gcp", "provider:gcp:artifactregistry", "provider:gcp:run", "live"],
47 },
48 () => {
49 beforeAll(
50 Effect.gen(function* () {
51 yield* sharedStack.destroy();
52 const out = yield* sharedStack.deploy(
53 Effect.gen(function* () {
54 const host = yield* ArtifactRegistryBindingsHost;
55 const images = yield* Images;
56 return {
57 uri: host.uri,
58 serviceAccount: host.serviceAccount,
59 repository: {
60 name: images.name,
61 repositoryId: images.repositoryId,
62 project: images.project,
63 location: images.location,
64 },
65 };
66 }),
67 );
68 baseUrl = out.uri!;
69 hostAccount = out.serviceAccount!;
70 repository = out.repository;
71 }),
72 { timeout: 900_000 },
73 );
74
75 afterAll(sharedStack.destroy(), { timeout: 600_000 });
76
77 describe("ListDockerImages", () => {
78 test.provider(
79 "lists a pushed image as the host's service account, granted on the repository only",
80 (_stack) =>
81 Effect.gen(function* () {
82 const version = yield* pushDockerVersion(repository, "v1");
83 const digest = version.split("/versions/")[1]!;
84
85 const page = yield* expectProbe<{
86 dockerImages?: Array<{ name?: string; uri?: string }>;
87 }>(baseUrl, "listDockerImages").pipe(
88 // The pushed image is listed once the registry indexes it.
89 Effect.repeat({
90 schedule: Schedule.spaced("3 seconds"),
91 until: (result) => (result.dockerImages ?? []).length > 0,
92 times: 10,
93 }),
94 );
95 expect((page.dockerImages ?? []).map((image) => image.uri)).toEqual([
96 `${repository.location}-docker.pkg.dev/${repository.project}/${repository.repositoryId}/${IMAGE}@${digest}`,
97 ]);
98
99 const policy = yield* artifactregistry.getIamPolicyProjectsLocationsRepositories({
100 resource: repository.name,
101 "options.requestedPolicyVersion": 3,
102 });
103 const roles = (policy.bindings ?? [])
104 .filter((binding) =>
105 (binding.members ?? []).includes(`serviceAccount:${hostAccount}`),
106 )
107 .map((binding) => binding.role);
108 expect(roles).toEqual(["roles/artifactregistry.reader"]);
109 expect(yield* projectGrantsOf(hostAccount)).toEqual([]);
110 }),
111 {
112 tags: ["provider:gcp", "provider:gcp:artifactregistry", "live"],
113 timeout: 600_000,
114 },
115 );
116 });
117 },
118 );