g1t/services/runner/src/self-hosted.test.ts
Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Fast pages, required checks on the branch, self-hosted runners, honest incidents | 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; | |
| 3 | ||
| 4 | import { cancelTask, enqueueTask, handedOverStep, selfHostedRoute, taskEnv, taskRepo } from "./self-hosted.ts"; | |
| 5 | ||
| 6 | /** An actions service that answers each method with what `answers` says. */ | |
| 7 | function actions(answers: Record<string, unknown>, calls: { method: string; body: unknown }[] = []) { | |
| 8 | return { | |
| 9 | async fetch(url: string, init?: RequestInit): Promise<Response> { | |
| 10 | const method = new URL(url).pathname.replace("/rpc/", ""); | |
| 11 | calls.push({ method, body: JSON.parse(String(init?.body ?? "{}")) }); | |
| 12 | if (!(method in answers)) return new Response("no", { status: 500 }); | |
| 13 | return Response.json(answers[method]); | |
| 14 | }, | |
| 15 | }; | |
| 16 | } | |
| 17 | ||
| 18 | const repo = { namespace: "Acme", name: "web" }; | |
| 19 | ||
| 20 | test("work goes to self-hosted runners only when the workspace says so", async () => { | |
| 21 | const calls: { method: string; body: unknown }[] = []; | |
| 22 | assert.deepEqual(await selfHostedRoute(actions({ runner_route: ["self-hosted", "linux"] }, calls), repo), ["self-hosted", "linux"]); | |
| 23 | assert.deepEqual(calls[0], { method: "runner_route", body: { workspace: "acme", repo } }); | |
| 24 | assert.equal(await selfHostedRoute(actions({ runner_route: null }), repo), null); | |
| 25 | assert.equal(await selfHostedRoute(actions({ runner_route: [] }), repo), null); | |
| 26 | // The actions service cannot say: a sandbox, as before. | |
| 27 | assert.equal(await selfHostedRoute(actions({}), repo), null); | |
| 28 | }); | |
| 29 | ||
| 30 | test("a task carries the sandbox's environment, without what only a sandbox has", () => { | |
| 31 | const env = taskEnv({ | |
| 32 | MODE: "checks", | |
| 33 | G1T_TOKEN: "g1t_x", | |
| 34 | HTTPS_PROXY: "http://proxy", | |
| 35 | NODE_EXTRA_CA_CERTS: "/etc/ca.crt", | |
| 36 | GUARDRAILS: "{}", | |
| 37 | }); | |
| 38 | assert.equal(env.MODE, "checks"); | |
| 39 | assert.equal(env.G1T_TOKEN, "g1t_x"); | |
| 40 | assert.equal(env.GUARDRAILS, "{}"); | |
| 41 | assert.equal(env.HTTPS_PROXY, undefined); | |
| 42 | assert.equal(env.NODE_EXTRA_CA_CERTS, undefined); | |
| 43 | assert.equal(env.G1T_ABUSE, "off"); | |
| 44 | assert.equal(env.G1T_SELF_HOSTED, "1"); | |
| 45 | }); | |
| 46 | ||
| 47 | test("handing over and withdrawing a task", async () => { | |
| 48 | const calls: { method: string; body: unknown }[] = []; | |
| 49 | const service = actions({ enqueue_task: { ok: true, value: "rtk_1" }, cancel_task: { ok: true, value: true } }, calls); | |
| 50 | const id = await enqueueTask(service, { | |
| 51 | sandbox: "do_1", | |
| 52 | repo, | |
| 53 | kind: "checks", | |
| 54 | title: "Checks on acme/web#3", | |
| 55 | labels: ["self-hosted"], | |
| 56 | env: { MODE: "checks" }, | |
| 57 | timeoutMinutes: 45, | |
| 58 | }); | |
| 59 | assert.equal(id, "rtk_1"); | |
| 60 | assert.equal((calls[0]!.body as { workspace: string }).workspace, "acme"); | |
| 61 | await cancelTask(service, "do_1", "Stopped."); | |
| 62 | assert.deepEqual(calls[1], { method: "cancel_task", body: { sandbox: "do_1", reason: "Stopped." } }); | |
| 63 | await assert.rejects( | |
| 64 | enqueueTask(actions({ enqueue_task: { ok: false, error: { message: "no key" } } }), { | |
| 65 | sandbox: "do_2", | |
| 66 | repo, | |
| 67 | kind: "agent", | |
| 68 | title: "x", | |
| 69 | labels: ["self-hosted"], | |
| 70 | env: {}, | |
| 71 | timeoutMinutes: 1, | |
| 72 | }), | |
| 73 | /no key/, | |
| 74 | ); | |
| 75 | // Never throws. | |
| 76 | await cancelTask(actions({}), "do_3", null); | |
| 77 | }); | |
| 78 | ||
| 79 | test("the run says where it went and what is not enforced there", () => { | |
| 80 | assert.match(handedOverStep(["self-hosted", "linux"]), /self-hosted, linux.*guardrails are not enforced/); | |
| 81 | }); | |
| 82 | ||
| 83 | test("a task's repository comes from what the sandbox was started with", () => { | |
| 84 | assert.deepEqual(taskRepo({ repo }, undefined, undefined), repo); | |
| 85 | assert.deepEqual(taskRepo(undefined, { repo: "acme/api" }, undefined), { namespace: "acme", name: "api" }); | |
| 86 | assert.deepEqual(taskRepo(undefined, undefined, { repo: "acme/docs" }), { namespace: "acme", name: "docs" }); | |
| 87 | assert.equal(taskRepo(undefined, undefined, undefined), null); | |
| 88 | }); |