Public: anyone can read this; the crew can change it.
Cloud account digitalocean/prod shows more than the forge made #17857
Merged 30h 18m ago, live in production
torque/forge:torque/17849-cloud-account-digitalocean-prod-shows-mo → main 0 commits fork 3 threads · 3 unresolved
2 files
changed +31
−7
src/cloud/watch.ts +2 −2
| @@ -121,7 +121,7 @@ export async function readLive(provider: string, f: Fetch, token: string, pause: | ||
| 121 | 121 | if (per === undefined) throw new Error(`${provider} is not watched`); |
| 122 | 122 | if (provider === "digitalocean") { |
| 123 | 123 | const api = "https://api.digitalocean.com/v2"; |
| 124 | const [d, v, w] = [await pages(f, token, `${api}/droplets`, "droplets", per, pause), await pages(f, token, `${api}/volumes`, "volumes", per, pause), await pages(f, token, `${api}/firewalls`, "firewalls", per, pause)]; | |
| 124 | const [d, v, w] = await Promise.all([pages(f, token, `${api}/droplets`, "droplets", per, pause), pages(f, token, `${api}/volumes`, "volumes", per, pause), pages(f, token, `${api}/firewalls`, "firewalls", per, pause)]); | |
| 125 | 125 | return [ |
| 126 | 126 | ...d.map((x): Seen => ({ kind: "machine", id: text(x.id), name: text(x.name), createdAt: text(x.created_at), datacenter: text(x.region?.slug), size: text(x.size_slug), gib: 0 })), |
| 127 | 127 | ...v.map((x): Seen => ({ kind: "volume", id: text(x.id), name: text(x.name), createdAt: text(x.created_at), datacenter: text(x.region?.slug), size: "", gib: Number(x.size_gigabytes) || VOLUME_GIB })), |
| @@ -129,7 +129,7 @@ export async function readLive(provider: string, f: Fetch, token: string, pause: | ||
| 129 | 129 | ]; |
| 130 | 130 | } |
| 131 | 131 | const api = "https://api.hetzner.cloud/v1"; |
| 132 | const [s, v, w] = [await pages(f, token, `${api}/servers`, "servers", per, pause), await pages(f, token, `${api}/volumes`, "volumes", per, pause), await pages(f, token, `${api}/firewalls`, "firewalls", per, pause)]; | |
| 132 | const [s, v, w] = await Promise.all([pages(f, token, `${api}/servers`, "servers", per, pause), pages(f, token, `${api}/volumes`, "volumes", per, pause), pages(f, token, `${api}/firewalls`, "firewalls", per, pause)]); | |
| 133 | 133 | return [ |
| 134 | 134 | ...s.map((x): Seen => ({ kind: "machine", id: text(x.id), name: text(x.name), createdAt: text(x.created), datacenter: text(x.datacenter?.location?.name), size: text(x.server_type?.name), gib: 0 })), |
| 135 | 135 | ...v.map((x): Seen => ({ kind: "volume", id: text(x.id), name: text(x.name), createdAt: text(x.created), datacenter: text(x.location?.name), size: "", gib: Number(x.size) || VOLUME_GIB })), |
test/cloud-watch-read.test.ts +29 −5
| @@ -60,9 +60,10 @@ describe("paging", () => { | ||
| 60 | 60 | test("a list that never ends fails as not read whole, never a partial pass", async () => { |
| 61 | 61 | let n = 0; |
| 62 | 62 | const read = readLive("hetzner", async (url) => { |
| 63 | // The three lists are read together: only servers never end, the others are empty. | |
| 64 | if (!new URL(url).pathname.endsWith("servers")) return json({ volumes: [], firewalls: [] }); | |
| 63 | 65 | n++; |
| 64 | const key = new URL(url).pathname.endsWith("servers") ? "servers" : "volumes"; | |
| 65 | return json({ [key]: Array.from({ length: 50 }, (_, i) => ({ id: n * 100 + i, name: "x" })) }); | |
| 66 | return json({ servers: Array.from({ length: 50 }, (_, i) => ({ id: n * 100 + i, name: "x" })) }); | |
| 66 | 67 | }, "tok"); |
| 67 | 68 | await expect(read).rejects.toThrow("more than 500 servers: not read whole"); |
| 68 | 69 | expect(n).toBe(10); |
| @@ -87,7 +88,8 @@ describe("paging", () => { | ||
| 87 | 88 | ); |
| 88 | 89 | expect(seen.map((s) => s.id)).toEqual(["1"]); |
| 89 | 90 | expect(tries).toEqual({ "/v1/servers": 2, "/v1/volumes": 3, "/v1/firewalls": 2 }); |
| 90 | expect(paused).toEqual([5_000, 5_000, 15_000, 5_000]); | |
| 91 | // The lists are read together, so their pauses interleave. | |
| 92 | expect([...paused].sort((a, b) => a - b)).toEqual([5_000, 5_000, 5_000, 15_000]); | |
| 91 | 93 | }); |
| 92 | 94 | |
| 93 | 95 | test("three misses in a row fail the read; a refusal is not tried again", async () => { |
| @@ -97,14 +99,36 @@ describe("paging", () => { | ||
| 97 | 99 | throw new DOMException("The operation timed out.", "TimeoutError"); |
| 98 | 100 | }, "tok", async () => {}); |
| 99 | 101 | await expect(slow).rejects.toMatchObject({ name: "TimeoutError" }); |
| 100 | expect(n).toBe(3); | |
| 102 | // Three tries at each of the three lists, read together. | |
| 103 | expect(n).toBe(9); | |
| 101 | 104 | n = 0; |
| 102 | 105 | const refused = readLive("hetzner", async () => { |
| 103 | 106 | n++; |
| 104 | 107 | return json({ error: "unauthorized" }, 401); |
| 105 | 108 | }, "tok", async () => {}); |
| 106 | 109 | await expect(refused).rejects.toThrow("answered 401 to servers"); |
| 107 | expect(n).toBe(1); | |
| 110 | // One try at each list: a refusal is not tried again. | |
| 111 | expect(n).toBe(3); | |
| 112 | }); | |
| 113 | ||
| 114 | test("the three lists are asked together, not one after another", async () => { | |
| 115 | const started: string[] = []; | |
| 116 | let release: () => void = () => {}; | |
| 117 | const held = new Promise<void>((r) => (release = r)); | |
| 118 | let allStarted: () => void = () => {}; | |
| 119 | const together = new Promise<void>((r) => (allStarted = r)); | |
| 120 | const read = readLive("digitalocean", async (url) => { | |
| 121 | const list = new URL(url).pathname.split("/").pop()!; | |
| 122 | started.push(list); | |
| 123 | if (started.length === 3) allStarted(); | |
| 124 | if (list === "droplets") await held; | |
| 125 | return json({ [list]: [] }); | |
| 126 | }, "tok"); | |
| 127 | // Droplets are held open: volumes and firewalls must start before it resolves. | |
| 128 | await together; | |
| 129 | expect([...started].sort()).toEqual(["droplets", "firewalls", "volumes"]); | |
| 130 | release(); | |
| 131 | expect(await read).toEqual([]); | |
| 108 | 132 | }); |
| 109 | 133 | |
| 110 | 134 | test("a provider's strange text is made plain before it reaches a sentence", () => { |
new above
offline