Skip to content
68 linesCodeBlameRaw
1// node --test scripts/runner-release.test.mjs
2import assert from "node:assert/strict";
3import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
4import { tmpdir } from "node:os";
5import { join } from "node:path";
6import { test } from "node:test";
7import { createPrivateKey } from "node:crypto";
8
9import { TARGETS, keygen, manifest, sha256, signBytes, verifyBytes, version } from "./runner-release.mjs";
10import { sandboxFiles, section } from "./sandbox-image.mjs";
11
12test("a release is signed with the private key and checked with the raw public key", () => {
13 const keys = keygen();
14 assert.equal(Buffer.from(keys.public, "base64").length, 32);
15 const key = createPrivateKey({ key: Buffer.from(keys.private, "base64"), format: "der", type: "pkcs8" });
16 const bytes = Buffer.from('{"version":"0.2.0","files":{}}');
17 const signature = signBytes(bytes, key);
18 assert.equal(Buffer.from(signature, "base64").length, 64);
19 assert.ok(verifyBytes(bytes, signature, keys.public));
20 assert.ok(!verifyBytes(Buffer.from('{"version":"9.9.9","files":{}}'), signature, keys.public));
21 assert.ok(!verifyBytes(bytes, signature, keygen().public));
22});
23
24test("the manifest names each binary that was built, with its SHA-256", () => {
25 const dir = mkdtempSync(join(tmpdir(), "runner-release-"));
26 try {
27 writeFileSync(join(dir, TARGETS["linux-x64"].file), "linux");
28 writeFileSync(join(dir, TARGETS["windows-x64"].file), "windows");
29 const m = manifest(dir, "0.2.0");
30 assert.equal(m.version, "0.2.0");
31 assert.equal("agent_image" in m, false);
32 assert.deepEqual(Object.keys(m.files).sort(), ["linux-x64", "windows-x64"]);
33 assert.equal(m.files["linux-x64"].sha256, sha256(Buffer.from("linux")));
34 assert.equal(m.files["windows-x64"].name, "g1t-runner-windows-x64.exe");
35 } finally {
36 rmSync(dir, { recursive: true, force: true });
37 }
38});
39
40test("platforms are named as the runner names them", () => {
41 assert.deepEqual(Object.keys(TARGETS).sort(), ["linux-arm64", "linux-x64", "macos-arm64", "macos-x64", "windows-x64"]);
42 assert.match(version(), /^\d+\.\d+\.\d+/);
43});
44
45test("the sandbox's images join the signed manifest, and one that is missing is refused", async () => {
46 const dir = mkdtempSync(join(tmpdir(), "runner-release-sandbox-"));
47 try {
48 writeFileSync(join(dir, TARGETS["linux-x64"].file), "linux");
49 writeFileSync(join(dir, sandboxFiles("x64")["wsl-x64"]), "rootfs");
50 const s = await section(dir, "g1t.sh/flagon-io/g1t-runner@sha256:ab");
51 assert.deepEqual(Object.keys(s.files), ["wsl-x64"]);
52 assert.equal(s.files["wsl-x64"].sha256, sha256(Buffer.from("rootfs")));
53 assert.equal(s.files["wsl-x64"].size, 6);
54 writeFileSync(join(dir, "sandbox.json"), JSON.stringify(s));
55 const m = manifest(dir, "0.3.0");
56 assert.equal(m.sandbox.runner_image, "g1t.sh/flagon-io/g1t-runner@sha256:ab");
57 assert.equal(m.sandbox.files["wsl-x64"].name, "g1t-sandbox-wsl-x64.tar.gz");
58 writeFileSync(join(dir, "sandbox.json"), JSON.stringify({ runner_image: null, files: { "vm-x64-disk": { name: "gone.raw.gz", sha256: "00" } } }));
59 assert.throws(() => manifest(dir, "0.3.0"), /gone\.raw\.gz/);
60 } finally {
61 rmSync(dir, { recursive: true, force: true });
62 }
63});
64
65test("the sandbox's files are named as the launcher asks for them", () => {
66 // crates/runner/src/selfhosted/launcher: wsl.rs's and vm.rs's image_keys().
67 assert.deepEqual(Object.keys(sandboxFiles("arm64")), ["wsl-arm64", "vm-arm64-kernel", "vm-arm64-initrd", "vm-arm64-disk"]);
68});