The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Core from "@alchemy.run/test/Core"; |
| 5 | import * as servicedirectory from "@distilled.cloud/gcp/servicedirectory_v1"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { expectProbe } from "../bindingHost.ts"; |
| 8 | import ServiceDirectoryBindingsHost, { |
| 9 | Api, |
| 10 | ENDPOINT_ADDRESS, |
| 11 | ENDPOINT_PORT, |
| 12 | Https, |
| 13 | Registry, |
| 14 | } from "./fixtures/bindings-host.ts"; |
| 15 | |
| 16 | const testOptions = { providers: GCP.providers() }; |
| 17 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 18 | const sharedStack = Core.scratchStack(testOptions, "ServiceDirectoryBindings"); |
| 19 | |
| 20 | let baseUrl: string; |
| 21 | let member: string; |
| 22 | let serviceName: string; |
| 23 | let endpointName: string; |
| 24 | let namespaceName: string; |
| 25 | |
| 26 | /** Roles the host's service account holds on the bound service. */ |
| 27 | const hostRolesOnService = Effect.gen(function* () { |
| 28 | const policy = yield* servicedirectory.getIamPolicyProjectsLocationsNamespacesServices({ |
| 29 | resource: serviceName, |
| 30 | body: { options: { requestedPolicyVersion: 3 } }, |
| 31 | }); |
| 32 | return (policy.bindings ?? []) |
| 33 | .filter((binding) => (binding.members ?? []).includes(member)) |
| 34 | .map((binding) => binding.role) |
| 35 | .sort(); |
| 36 | }); |
| 37 | |
| 38 | /** Roles the host's service account holds on the parent namespace. */ |
| 39 | const hostRolesOnNamespace = Effect.gen(function* () { |
| 40 | const policy = yield* servicedirectory.getIamPolicyProjectsLocationsNamespaces({ |
| 41 | resource: namespaceName, |
| 42 | body: { options: { requestedPolicyVersion: 3 } }, |
| 43 | }); |
| 44 | return (policy.bindings ?? []) |
| 45 | .filter((binding) => (binding.members ?? []).includes(member)) |
| 46 | .map((binding) => binding.role); |
| 47 | }); |
| 48 | |
| 49 | describe( |
| 50 | "ServiceDirectory Bindings", |
| 51 | { |
| 52 | tags: ["provider:gcp", "provider:gcp:servicedirectory", "provider:gcp:run", "live"], |
| 53 | }, |
| 54 | () => { |
| 55 | beforeAll( |
| 56 | Effect.gen(function* () { |
| 57 | yield* sharedStack.destroy(); |
| 58 | const out = yield* sharedStack.deploy( |
| 59 | Effect.gen(function* () { |
| 60 | const host = yield* ServiceDirectoryBindingsHost; |
| 61 | return { |
| 62 | uri: host.uri, |
| 63 | serviceAccount: host.serviceAccount, |
| 64 | namespace: (yield* Registry).name, |
| 65 | service: (yield* Api).name, |
| 66 | endpoint: (yield* Https).name, |
| 67 | }; |
| 68 | }), |
| 69 | ); |
| 70 | baseUrl = out.uri!; |
| 71 | member = `serviceAccount:${out.serviceAccount!}`; |
| 72 | namespaceName = out.namespace; |
| 73 | serviceName = out.service; |
| 74 | endpointName = out.endpoint; |
| 75 | }), |
| 76 | { timeout: 900_000 }, |
| 77 | ); |
| 78 | |
| 79 | afterAll(sharedStack.destroy(), { timeout: 600_000 }); |
| 80 | |
| 81 | describe("Resolve", () => { |
| 82 | test.provider( |
| 83 | "resolves the service with its endpoints, granted viewer on the service only", |
| 84 | (_stack) => |
| 85 | Effect.gen(function* () { |
| 86 | const out = yield* expectProbe<servicedirectory.ResolveServiceResponse>( |
| 87 | baseUrl, |
| 88 | "resolve", |
| 89 | ); |
| 90 | expect(out.service?.name).toEqual(serviceName); |
| 91 | expect( |
| 92 | (out.service?.endpoints ?? []).map((endpoint) => ({ |
| 93 | name: endpoint.name, |
| 94 | address: endpoint.address, |
| 95 | port: endpoint.port, |
| 96 | })), |
| 97 | ).toEqual([ |
| 98 | { |
| 99 | name: endpointName, |
| 100 | address: ENDPOINT_ADDRESS, |
| 101 | port: ENDPOINT_PORT, |
| 102 | }, |
| 103 | ]); |
| 104 | |
| 105 | expect(yield* hostRolesOnService).toEqual(["roles/servicedirectory.viewer"]); |
| 106 | expect(yield* hostRolesOnNamespace).toEqual([]); |
| 107 | }), |
| 108 | { |
| 109 | tags: ["provider:gcp", "provider:gcp:servicedirectory", "live"], |
| 110 | timeout: 600_000, |
| 111 | }, |
| 112 | ); |
| 113 | }); |
| 114 | |
| 115 | describe("GetEndpoint", () => { |
| 116 | test.provider( |
| 117 | "reads the endpoint, granted viewer on its service only", |
| 118 | (_stack) => |
| 119 | Effect.gen(function* () { |
| 120 | const live = yield* expectProbe<servicedirectory.Endpoint>(baseUrl, "getEndpoint"); |
| 121 | expect(live.name).toEqual(endpointName); |
| 122 | expect(live.address).toEqual(ENDPOINT_ADDRESS); |
| 123 | expect(live.port).toEqual(ENDPOINT_PORT); |
| 124 | |
| 125 | // Out of band: the deployer sees the same endpoint. |
| 126 | const direct = yield* servicedirectory.getProjectsLocationsNamespacesServicesEndpoints({ |
| 127 | name: endpointName, |
| 128 | }); |
| 129 | expect(direct.uid).toEqual(live.uid); |
| 130 | |
| 131 | expect(yield* hostRolesOnService).toEqual(["roles/servicedirectory.viewer"]); |
| 132 | }), |
| 133 | { |
| 134 | tags: ["provider:gcp", "provider:gcp:servicedirectory", "live"], |
| 135 | timeout: 600_000, |
| 136 | }, |
| 137 | ); |
| 138 | }); |
| 139 | }, |
| 140 | ); |