Diffinery

homeflare/diffinerypublic

Open-source, agent-first GitHub and GitHub Actions alternative that runs on your own Cloudflare account: git hosting, pull requests, code review and drop-in GitHub Actions YAML on Workers, Durable Objects, R2 and Containers.

diffinery/containers/runner/test/github-runner.test.ts
51 lines · 2.6 KBRaw History
1import { chmod, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
2import { tmpdir } from "node:os";
3import { join } from "node:path";
4import { afterEach, describe, expect, it } from "vitest";
5import { runGitHubRunner } from "../src/github-runner.js";
6
7const dirs: string[] = [];
8afterEach(async () => {
9 for (const d of dirs.splice(0)) await rm(d, { recursive: true, force: true });
10});
11
12/** A stand-in for GitHub's run.sh that records its arguments and environment, then exits with `code`. */
13async function fakeRunner(script: string): Promise<string> {
14 const dir = await mkdtemp(join(tmpdir(), "gh-runner-"));
15 dirs.push(dir);
16 await writeFile(join(dir, "run.sh"), `#!/bin/sh\n${script}\n`);
17 await chmod(join(dir, "run.sh"), 0o755);
18 return dir;
19}
20
21describe.skipIf(process.platform === "win32")("GitHub's runner", () => {
22 it("runs with the just-in-time configuration, as root, without the configuration in the job's environment", async () => {
23 const dir = await fakeRunner('echo "$1 $2" > args; env > env; exit 3');
24 const code = await runGitHubRunner({ jitConfig: "abc==", runnerDir: dir, env: { PATH: process.env.PATH, DIFFINERY_GITHUB_JIT_CONFIG: "abc==", KEEP: "1" } });
25 expect(code).toBe(3);
26 expect((await readFile(join(dir, "args"), "utf8")).trim()).toBe("--jitconfig abc==");
27 const env = await readFile(join(dir, "env"), "utf8");
28 expect(env).toContain("RUNNER_ALLOW_RUNASROOT=1");
29 expect(env).toContain("KEEP=1");
30 expect(env).not.toContain("DIFFINERY_GITHUB_JIT_CONFIG");
31 });
32
33 it("passes SIGINT on at once and holds SIGTERM back while the job may finish", async () => {
34 const dir = await fakeRunner('trap "echo int > got; exit 130" INT; trap "echo term > got; exit 143" TERM; touch ready; while true; do sleep 0.05; done');
35 const running = runGitHubRunner({ jitConfig: "x", runnerDir: dir, env: { PATH: process.env.PATH }, drainOnSigtermMs: 60_000 });
36 // Its traps are set once it says it is ready.
37 while (!(await readFile(join(dir, "ready")).then(() => true, () => false))) await new Promise((r) => setTimeout(r, 20));
38 process.emit("SIGTERM");
39 await new Promise((r) => setTimeout(r, 300));
40 await expect(readFile(join(dir, "got"), "utf8")).rejects.toThrow();
41 process.emit("SIGINT");
42 expect(await running).toBe(130);
43 expect((await readFile(join(dir, "got"), "utf8")).trim()).toBe("int");
44 });
45
46 it("reports a runner that isn't there", async () => {
47 const dir = await mkdtemp(join(tmpdir(), "gh-runner-"));
48 dirs.push(dir);
49 expect(await runGitHubRunner({ jitConfig: "x", runnerDir: dir, env: { PATH: process.env.PATH } })).toBe(2);
50 });
51});