import { readFile } from "node:fs/promises"; import { describe, expect, it } from "vitest"; import { findSystemTestPrerequisiteFailures, formatSystemTestPrerequisiteReport, NAMESPACE_PROBE_ARGUMENTS, type SystemTestHost, } from "../../scripts/check-system-test-prerequisites.ts"; import { PROVIDER_SANDBOX_NAMESPACE_ARGUMENTS } from "../../scripts/lib/provider-sandbox-namespaces.ts"; type SpawnCall = Readonly<{ command: string; args: readonly string[] }>; type SpawnResult = ReturnType; const RTM_NEWADDR = "bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted"; /** A host that has every prerequisite, except what `bwrap` and `files` say. */ function fakeHost( bwrap: SpawnResult, files: Readonly> = {}, ): { host: SystemTestHost; calls: SpawnCall[] } { const calls: SpawnCall[] = []; const host: SystemTestHost = { access: async () => undefined, readFile: async (target) => { if (target === "/sys/fs/cgroup/cgroup.controllers") return "cpuset cpu io memory pids\n"; const content = files[target]; if (content === undefined) throw new Error(`ENOENT: ${target}`); return content; }, spawn: (command, args) => { calls.push({ command, args }); if (command === "/usr/bin/systemctl") return { status: 0, stderr: "" }; if (command === "/usr/bin/bwrap") return bwrap; throw new Error(`unexpected spawn: ${command}`); }, }; return { host, calls }; } // 1105/12-01. The check looked only for the bwrap executable, so a host that // refuses unprivileged user namespaces printed PASS and then failed 18 // provider tests with bwrap's raw netlink error. describe("system test prerequisites (1105/12-01)", () => { it("passes when bubblewrap creates the provider sandbox's namespaces", async () => { const { host, calls } = fakeHost({ status: 0, stderr: "" }); expect(await findSystemTestPrerequisiteFailures(host)).toEqual([]); const probes = calls.filter(({ command }) => command === "/usr/bin/bwrap"); expect(probes).toHaveLength(1); expect(probes[0]?.args).toEqual(NAMESPACE_PROBE_ARGUMENTS); expect(probes[0]?.args.at(-1)).toBe("/bin/true"); }); it("probes with exactly the namespace options the provider sandbox uses", async () => { expect(PROVIDER_SANDBOX_NAMESPACE_ARGUMENTS).toEqual([ "--unshare-pid", "--unshare-ipc", "--unshare-uts", "--unshare-net", ]); expect(NAMESPACE_PROBE_ARGUMENTS.slice(0, PROVIDER_SANDBOX_NAMESPACE_ARGUMENTS.length)).toEqual( PROVIDER_SANDBOX_NAMESPACE_ARGUMENTS, ); // The provider takes the list from the same module instead of its own copy. const provider = await readFile("scripts/run-and-validate-provider.ts", "utf8"); expect(provider).toContain("...PROVIDER_SANDBOX_NAMESPACE_ARGUMENTS,"); expect(provider).not.toMatch(/"--unshare-/u); }); it("fails with the namespace restriction, bwrap's stderr and what to do when bwrap cannot set up loopback", async () => { const { host } = fakeHost( { status: 1, stderr: `${RTM_NEWADDR}\n` }, { "/proc/sys/kernel/apparmor_restrict_unprivileged_userns": "1\n" }, ); const failures = await findSystemTestPrerequisiteFailures(host); expect(failures).toHaveLength(1); const failure = failures[0] ?? ""; expect(failure).toMatch(/^this host does not allow unprivileged user and network namespaces/u); expect(failure).toContain("--unshare-net"); expect(failure).toContain(`bwrap stderr: ${RTM_NEWADDR}`); expect(failure).toContain("kernel.apparmor_restrict_unprivileged_userns = 1"); expect(failure).toMatch(/AppArmor profile/u); expect(failure).toMatch(/sysctl kernel\.apparmor_restrict_unprivileged_userns=0/u); expect(failure).toMatch(/another runner/u); expect(formatSystemTestPrerequisiteReport(failures)).toContain( " - this host does not allow unprivileged user and network namespaces", ); }); it("fails the same way when the probe cannot run to the end", async () => { const { host } = fakeHost({ status: null, stderr: "", error: new Error("spawnSync /usr/bin/bwrap ETIMEDOUT") }); const failures = await findSystemTestPrerequisiteFailures(host); expect(failures).toHaveLength(1); expect(failures[0]).toMatch(/^this host does not allow unprivileged user and network namespaces/u); expect(failures[0]).toContain("did not finish: spawnSync /usr/bin/bwrap ETIMEDOUT"); }); it("keeps the executable, cgroup v2 and systemd bus checks, and probes bwrap only when it is there", async () => { const calls: SpawnCall[] = []; const host: SystemTestHost = { access: async (target) => { if (target === "/usr/bin/bwrap") throw new Error(`EACCES: ${target}`); }, readFile: async () => "\n", spawn: (command, args) => { calls.push({ command, args }); return { status: 1, stderr: "Failed to connect to bus" }; }, }; expect(await findSystemTestPrerequisiteFailures(host)).toEqual([ "bubblewrap is required at /usr/bin/bwrap", "cgroup v2 controllers are unavailable", "a reachable systemd manager bus is required", ]); expect(calls.map(({ command }) => command)).toEqual(["/usr/bin/systemctl"]); }); });