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

main
ts 161 lines 5.51 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as orgpolicy from "@distilled.cloud/gcp/orgpolicy_v2";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 // Project org policies need orgpolicy.policies.create, which only an org-level
15 // Organization Policy Administrator grant provides; the testing credentials get
16 // Forbidden "Permission 'orgpolicy.policies.create' denied on resource ...".
17 // Set GOOGLE_ORGANIZATION_ID when the credentials administer the org.
18 const runLifecycle = !!process.env.GOOGLE_ORGANIZATION_ID;
19
20 const CONSTRAINT_A = "compute.disableSerialPortAccess";
21 const CONSTRAINT_B = "compute.disableSerialPortLogging";
22
23 const waitUntilGone = (name: string) =>
24 orgpolicy.getProjectsPolicies({ name }).pipe(
25 Effect.as("found" as const),
26 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
27 Effect.repeat({
28 schedule: Schedule.spaced("1 second"),
29 until: (status) => status === "gone",
30 times: 10,
31 }),
32 );
33
34 test.provider(
35 "getProjectsPolicies on a missing policy fails with NotFound",
36 (stack) =>
37 Effect.gen(function* () {
38 const { project } = yield* GcpEnvironment.current;
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(
42 orgpolicy.getProjectsPolicies({
43 name: `projects/${project}/policies/compute.alchemyTestDoesNotExist`,
44 }),
45 );
46 expect(error._tag).toBe("NotFound");
47
48 const page = yield* orgpolicy.listProjectsPolicies({
49 parent: `projects/${project}`,
50 pageSize: 10,
51 });
52 expect((page.policies ?? []).map((policy) => policy.name)).not.toContain(
53 `projects/${project}/policies/compute.alchemyTestDoesNotExist`,
54 );
55
56 yield* stack.destroy();
57 }).pipe(logLevel),
58 { tags: ["provider:gcp", "provider:gcp:orgpolicy", "live"], timeout: 90_000 },
59 );
60
61 test.provider.skipIf(runLifecycle)(
62 "createProjectsPolicies without orgpolicy.policies.create fails with Forbidden",
63 () =>
64 Effect.gen(function* () {
65 const { project } = yield* GcpEnvironment.current;
66 const error = yield* Effect.flip(
67 orgpolicy.createProjectsPolicies({
68 parent: `projects/${project}`,
69 body: {
70 name: `projects/${project}/policies/${CONSTRAINT_A}`,
71 spec: { rules: [{ enforce: false }] },
72 },
73 }),
74 );
75 expect(error._tag).toBe("Forbidden");
76 }).pipe(logLevel),
77 { tags: ["provider:gcp", "provider:gcp:orgpolicy", "live"] },
78 );
79
80 test.provider.skipIf(!runLifecycle)(
81 "create, update, replace, and delete a project org policy",
82 (stack) =>
83 Effect.gen(function* () {
84 const { project } = yield* GcpEnvironment.current;
85 yield* stack.destroy();
86
87 const created = yield* stack.deploy(
88 Effect.gen(function* () {
89 return yield* GCP.OrgPolicy.Policy("SerialPort", {
90 constraint: CONSTRAINT_A,
91 spec: {
92 rules: [{ enforce: false }],
93 },
94 });
95 }),
96 );
97
98 expect(created.constraint).toEqual(CONSTRAINT_A);
99 expect(created.parent).toMatch(/^projects\//);
100 expect(created.name).toContain(`/policies/${CONSTRAINT_A}`);
101 expect(created.project).toEqual(project);
102 expect(created.spec?.rules?.[0]?.enforce).toEqual(false);
103
104 const fetched = yield* orgpolicy.getProjectsPolicies({
105 name: created.name,
106 });
107 expect(fetched.name).toEqual(created.name);
108 expect(fetched.spec?.rules?.[0]?.enforce).toEqual(false);
109
110 const updated = yield* stack.deploy(
111 Effect.gen(function* () {
112 return yield* GCP.OrgPolicy.Policy("SerialPort", {
113 constraint: CONSTRAINT_A,
114 spec: {
115 rules: [{ enforce: true }],
116 },
117 });
118 }),
119 );
120
121 expect(updated.name).toEqual(created.name);
122 expect(updated.constraint).toEqual(CONSTRAINT_A);
123 expect(updated.spec?.rules?.[0]?.enforce).toEqual(true);
124
125 const fetchedUpdate = yield* orgpolicy.getProjectsPolicies({
126 name: updated.name,
127 });
128 expect(fetchedUpdate.spec?.rules?.[0]?.enforce).toEqual(true);
129
130 const replaced = yield* stack.deploy(
131 Effect.gen(function* () {
132 return yield* GCP.OrgPolicy.Policy("SerialPort", {
133 constraint: CONSTRAINT_B,
134 spec: {
135 rules: [{ enforce: true }],
136 },
137 });
138 }),
139 );
140
141 expect(replaced.constraint).toEqual(CONSTRAINT_B);
142 expect(replaced.name).toContain(`/policies/${CONSTRAINT_B}`);
143 expect(replaced.name).not.toEqual(created.name);
144 expect(replaced.spec?.rules?.[0]?.enforce).toEqual(true);
145
146 const fetchedReplace = yield* orgpolicy.getProjectsPolicies({
147 name: replaced.name,
148 });
149 expect(fetchedReplace.name).toEqual(replaced.name);
150 expect(fetchedReplace.spec?.rules?.[0]?.enforce).toEqual(true);
151
152 const oldGone = yield* waitUntilGone(created.name);
153 expect(oldGone).toEqual("gone");
154
155 yield* stack.destroy();
156
157 const gone = yield* waitUntilGone(replaced.name);
158 expect(gone).toEqual("gone");
159 }).pipe(logLevel),
160 { tags: ["provider:gcp", "provider:gcp:orgpolicy", "live"], timeout: 90_000 },
161 );