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
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as dns from "@distilled.cloud/gcp/dns_v1"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { MinimumLogLevel } from "effect/References"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import { withNetworkSlot } from "../networkQuota.ts"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | const waitUntilPolicyGone = (project: string, responsePolicy: string) => |
| 15 | dns.getResponsePolicies({ project, responsePolicy }).pipe( |
| 16 | Effect.as("found" as const), |
| 17 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 18 | Effect.repeat({ |
| 19 | schedule: Schedule.spaced("1 second"), |
| 20 | until: (status) => status === "gone", |
| 21 | times: 10, |
| 22 | }), |
| 23 | ); |
| 24 | |
| 25 | const waitUntilRuleGone = (project: string, responsePolicy: string, ruleName: string) => |
| 26 | dns |
| 27 | .getResponsePolicyRules({ |
| 28 | project, |
| 29 | responsePolicy, |
| 30 | responsePolicyRule: ruleName, |
| 31 | }) |
| 32 | .pipe( |
| 33 | Effect.as("found" as const), |
| 34 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 35 | Effect.repeat({ |
| 36 | schedule: Schedule.spaced("1 second"), |
| 37 | until: (status) => status === "gone", |
| 38 | times: 10, |
| 39 | }), |
| 40 | ); |
| 41 | |
| 42 | const lastSegment = (url: string) => url.split("/").pop() ?? url; |
| 43 | |
| 44 | test.provider( |
| 45 | "create, update, replace, and delete a response policy and rule", |
| 46 | (stack) => |
| 47 | Effect.gen(function* () { |
| 48 | yield* stack.destroy(); |
| 49 | |
| 50 | const created = yield* stack.deploy( |
| 51 | Effect.gen(function* () { |
| 52 | const vpc = yield* GCP.Compute.Network("Vpc", { |
| 53 | autoCreateSubnetworks: false, |
| 54 | }); |
| 55 | const policy = yield* GCP.DNS.ResponsePolicy("Overrides", { |
| 56 | description: "alchemy test response policy", |
| 57 | labels: { env: "test" }, |
| 58 | networks: [vpc.networkName], |
| 59 | }); |
| 60 | const rule = yield* GCP.DNS.ResponsePolicyRule("Internal", { |
| 61 | responsePolicy: policy.responsePolicyName, |
| 62 | dnsName: "app.internal.example.com.", |
| 63 | localData: [{ type: "A", ttl: 300, rrdatas: ["10.0.0.10"] }], |
| 64 | }); |
| 65 | return { vpc, policy, rule }; |
| 66 | }), |
| 67 | ); |
| 68 | |
| 69 | expect(created.policy.responsePolicyName).toEqual(expect.any(String)); |
| 70 | expect(created.policy.description).toEqual("alchemy test response policy"); |
| 71 | expect(created.policy.labels).toMatchObject({ env: "test" }); |
| 72 | expect(created.policy.networks.map(lastSegment)).toContain(created.vpc.networkName); |
| 73 | expect(created.rule.responsePolicy).toEqual(created.policy.responsePolicyName); |
| 74 | expect(created.rule.dnsName).toEqual("app.internal.example.com."); |
| 75 | expect(created.rule.localData).toEqual([ |
| 76 | { |
| 77 | name: "app.internal.example.com.", |
| 78 | type: "A", |
| 79 | ttl: 300, |
| 80 | rrdatas: ["10.0.0.10"], |
| 81 | }, |
| 82 | ]); |
| 83 | |
| 84 | const fetched = yield* dns.getResponsePolicies({ |
| 85 | project: created.policy.project, |
| 86 | responsePolicy: created.policy.responsePolicyName, |
| 87 | }); |
| 88 | expect(fetched.responsePolicyName).toEqual(created.policy.responsePolicyName); |
| 89 | expect(fetched.description).toEqual("alchemy test response policy"); |
| 90 | expect(fetched.labels?.env).toEqual("test"); |
| 91 | |
| 92 | const fetchedRule = yield* dns.getResponsePolicyRules({ |
| 93 | project: created.rule.project, |
| 94 | responsePolicy: created.policy.responsePolicyName, |
| 95 | responsePolicyRule: created.rule.ruleName, |
| 96 | }); |
| 97 | expect(fetchedRule.dnsName).toEqual("app.internal.example.com."); |
| 98 | expect(fetchedRule.localData?.localDatas?.[0]?.rrdatas).toEqual(["10.0.0.10"]); |
| 99 | |
| 100 | const updated = yield* stack.deploy( |
| 101 | Effect.gen(function* () { |
| 102 | const vpc = yield* GCP.Compute.Network("Vpc", { |
| 103 | networkName: created.vpc.networkName, |
| 104 | autoCreateSubnetworks: false, |
| 105 | }); |
| 106 | const policy = yield* GCP.DNS.ResponsePolicy("Overrides", { |
| 107 | responsePolicyName: created.policy.responsePolicyName, |
| 108 | description: "updated alchemy test response policy", |
| 109 | labels: { env: "prod", role: "dns" }, |
| 110 | networks: [vpc.networkName], |
| 111 | }); |
| 112 | const rule = yield* GCP.DNS.ResponsePolicyRule("Internal", { |
| 113 | responsePolicy: policy.responsePolicyName, |
| 114 | ruleName: created.rule.ruleName, |
| 115 | dnsName: "app.internal.example.com.", |
| 116 | localData: [{ type: "A", ttl: 60, rrdatas: ["10.0.0.20"] }], |
| 117 | }); |
| 118 | return { vpc, policy, rule }; |
| 119 | }), |
| 120 | ); |
| 121 | |
| 122 | expect(updated.policy.responsePolicyName).toEqual(created.policy.responsePolicyName); |
| 123 | expect(updated.policy.description).toEqual("updated alchemy test response policy"); |
| 124 | expect(updated.policy.labels).toMatchObject({ |
| 125 | env: "prod", |
| 126 | role: "dns", |
| 127 | }); |
| 128 | expect(updated.rule.localData).toEqual([ |
| 129 | { |
| 130 | name: "app.internal.example.com.", |
| 131 | type: "A", |
| 132 | ttl: 60, |
| 133 | rrdatas: ["10.0.0.20"], |
| 134 | }, |
| 135 | ]); |
| 136 | |
| 137 | const fetchedUpdate = yield* dns.getResponsePolicies({ |
| 138 | project: created.policy.project, |
| 139 | responsePolicy: created.policy.responsePolicyName, |
| 140 | }); |
| 141 | expect(fetchedUpdate.description).toEqual("updated alchemy test response policy"); |
| 142 | expect(fetchedUpdate.labels?.env).toEqual("prod"); |
| 143 | expect(fetchedUpdate.labels?.role).toEqual("dns"); |
| 144 | |
| 145 | const fetchedUpdatedRule = yield* dns.getResponsePolicyRules({ |
| 146 | project: created.rule.project, |
| 147 | responsePolicy: created.policy.responsePolicyName, |
| 148 | responsePolicyRule: created.rule.ruleName, |
| 149 | }); |
| 150 | expect(fetchedUpdatedRule.localData?.localDatas?.[0]?.ttl).toEqual(60); |
| 151 | expect(fetchedUpdatedRule.localData?.localDatas?.[0]?.rrdatas).toEqual(["10.0.0.20"]); |
| 152 | |
| 153 | const replacedRuleName = `r${created.rule.ruleName}`.slice(0, 63); |
| 154 | const replaced = yield* stack.deploy( |
| 155 | Effect.gen(function* () { |
| 156 | const vpc = yield* GCP.Compute.Network("Vpc", { |
| 157 | networkName: created.vpc.networkName, |
| 158 | autoCreateSubnetworks: false, |
| 159 | }); |
| 160 | const policy = yield* GCP.DNS.ResponsePolicy("Overrides", { |
| 161 | responsePolicyName: created.policy.responsePolicyName, |
| 162 | description: "updated alchemy test response policy", |
| 163 | labels: { env: "prod", role: "dns" }, |
| 164 | networks: [vpc.networkName], |
| 165 | }); |
| 166 | const rule = yield* GCP.DNS.ResponsePolicyRule("Internal", { |
| 167 | responsePolicy: policy.responsePolicyName, |
| 168 | ruleName: replacedRuleName, |
| 169 | dnsName: "api.internal.example.com.", |
| 170 | localData: [{ type: "A", ttl: 120, rrdatas: ["10.0.0.30"] }], |
| 171 | }); |
| 172 | return { vpc, policy, rule }; |
| 173 | }), |
| 174 | ); |
| 175 | |
| 176 | expect(replaced.rule.ruleName).toEqual(replacedRuleName); |
| 177 | expect(replaced.rule.dnsName).toEqual("api.internal.example.com."); |
| 178 | expect(replaced.rule.localData[0]?.rrdatas).toEqual(["10.0.0.30"]); |
| 179 | |
| 180 | const previousRuleGone = yield* waitUntilRuleGone( |
| 181 | created.rule.project, |
| 182 | created.policy.responsePolicyName, |
| 183 | created.rule.ruleName, |
| 184 | ); |
| 185 | expect(previousRuleGone).toEqual("gone"); |
| 186 | |
| 187 | const fetchedReplacedRule = yield* dns.getResponsePolicyRules({ |
| 188 | project: created.rule.project, |
| 189 | responsePolicy: created.policy.responsePolicyName, |
| 190 | responsePolicyRule: replacedRuleName, |
| 191 | }); |
| 192 | expect(fetchedReplacedRule.dnsName).toEqual("api.internal.example.com."); |
| 193 | |
| 194 | const replacedPolicyName = `r${created.policy.responsePolicyName}`.slice(0, 63); |
| 195 | const replacedPolicy = yield* stack.deploy( |
| 196 | Effect.gen(function* () { |
| 197 | const vpc = yield* GCP.Compute.Network("Vpc", { |
| 198 | networkName: created.vpc.networkName, |
| 199 | autoCreateSubnetworks: false, |
| 200 | }); |
| 201 | const policy = yield* GCP.DNS.ResponsePolicy("Overrides", { |
| 202 | responsePolicyName: replacedPolicyName, |
| 203 | description: "replaced alchemy test response policy", |
| 204 | labels: { env: "prod", role: "dns" }, |
| 205 | networks: [vpc.networkName], |
| 206 | }); |
| 207 | const rule = yield* GCP.DNS.ResponsePolicyRule("Internal", { |
| 208 | responsePolicy: policy.responsePolicyName, |
| 209 | ruleName: replacedRuleName, |
| 210 | dnsName: "api.internal.example.com.", |
| 211 | localData: [{ type: "A", ttl: 120, rrdatas: ["10.0.0.30"] }], |
| 212 | }); |
| 213 | return { vpc, policy, rule }; |
| 214 | }), |
| 215 | ); |
| 216 | |
| 217 | expect(replacedPolicy.policy.responsePolicyName).toEqual(replacedPolicyName); |
| 218 | expect(replacedPolicy.policy.description).toEqual("replaced alchemy test response policy"); |
| 219 | expect(replacedPolicy.rule.responsePolicy).toEqual(replacedPolicyName); |
| 220 | |
| 221 | const previousPolicyGone = yield* waitUntilPolicyGone( |
| 222 | created.policy.project, |
| 223 | created.policy.responsePolicyName, |
| 224 | ); |
| 225 | expect(previousPolicyGone).toEqual("gone"); |
| 226 | |
| 227 | const fetchedReplacement = yield* dns.getResponsePolicies({ |
| 228 | project: created.policy.project, |
| 229 | responsePolicy: replacedPolicyName, |
| 230 | }); |
| 231 | expect(fetchedReplacement.responsePolicyName).toEqual(replacedPolicyName); |
| 232 | expect(fetchedReplacement.description).toEqual("replaced alchemy test response policy"); |
| 233 | |
| 234 | yield* stack.destroy(); |
| 235 | |
| 236 | const ruleGone = yield* waitUntilRuleGone( |
| 237 | created.rule.project, |
| 238 | replacedPolicyName, |
| 239 | replacedRuleName, |
| 240 | ); |
| 241 | expect(ruleGone).toEqual("gone"); |
| 242 | |
| 243 | const gone = yield* waitUntilPolicyGone(created.policy.project, replacedPolicyName); |
| 244 | expect(gone).toEqual("gone"); |
| 245 | }).pipe(logLevel, withNetworkSlot), |
| 246 | { tags: ["provider:gcp", "provider:gcp:dns", "live"], timeout: 120_000 }, |
| 247 | ); |