Files
clean-architecture-frontend…/tests/unit/system-test-prerequisites.test.ts
T
DongHyeonka eb7d89c742 fix(system-test): 사전 점검이 provider와 같은 user manager·scope 호출을 한다
문제: `test:system` 사전 점검은 system manager bus(`systemctl
show-environment`)와 실행 파일 셋만 보고 PASS를 냈다. provider 본 단계는
`/usr/bin/prlimit`·`/usr/bin/systemd-run`을 쓰고, user manager 버전(254
이상)을 묻고, `systemd-run --user --scope`로 cgroup 한도를 강제한다. 그래서
user bus가 없거나 버전이 낮거나 controller가 위임되지 않은 호스트는 PASS
뒤 provider 시험마다 `provider cgroup unavailable: ...`로 떨어졌다.

고친 것: 점검이 prlimit·systemd-run 실행 가능 여부를 보고, provider와
같은 인자(`USER_MANAGER_VERSION_ARGUMENTS`)로 user manager 버전을 묻고,
provider scope와 같은 속성·같은 cgroup 강제 확인으로 `/bin/true`를 한 번
띄운다. scope 인자와 최소 버전은 `scripts/lib/provider-cgroup.ts` 한
곳에 두고 provider와 점검이 같이 쓴다. system bus 점검은 provider가 쓰지
않아 user manager 점검으로 바꿨다. 위임 controller 값은 scope 안의 강제
확인(memory.max·pids.max·cpu.max)이 실제로 읽어 보므로 UNVERIFIED로 남길
몫이 없다.

위험·한계: 점검이 user scope 하나를 띄웠다 지운다(`--collect`, 5초
상한). 시스템 컨테이너 안(root)에서도 provider와 같은 호출이라 같은
결과를 낸다고 보고, FE-GATE-013으로 확인한다.

검증: desktop `scripts/verify.sh exec`로
`node scripts/run-vitest.ts run tests/unit/system-test-prerequisites.test.ts`
등 7파일 134건 통과. 점검 코드를 기준 커밋으로 되돌리면 새 시험 6건이
실패한다(RED).

Refs: 1112/08-03
2026-11-13 21:49:58 +09:00

261 lines
12 KiB
TypeScript

import { readFile } from "node:fs/promises";
import { describe, expect, it } from "vitest";
import {
findProviderSandboxNamespaceFailure,
findSystemTestPrerequisiteFailures,
formatSystemTestPrerequisiteReport,
NAMESPACE_PROBE_ARGUMENTS,
type SystemTestHost,
} from "../../scripts/check-system-test-prerequisites.ts";
import {
systemdRunPrerequisiteProbeArguments,
systemdRunProviderArguments,
} from "../../scripts/lib/provider-cgroup.ts";
import { PROVIDER_SANDBOX_NAMESPACE_ARGUMENTS } from "../../scripts/lib/provider-sandbox-namespaces.ts";
type SpawnCall = Readonly<{ command: string; args: readonly string[] }>;
type SpawnResult = ReturnType<SystemTestHost["spawn"]>;
const RTM_NEWADDR = "bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted";
const PROBE_UNIT = "ca-provider-prerequisite-4242-0123456789abcdef01234567.scope";
type HostOverrides = Readonly<{
files?: Readonly<Record<string, string>>;
missing?: readonly string[];
userManager?: SpawnResult;
systemdRun?: SpawnResult;
}>;
/** A host that has every prerequisite, except what `bwrap` and the overrides say. */
function fakeHost(
bwrap: SpawnResult,
options: HostOverrides = {},
): { host: SystemTestHost; calls: SpawnCall[] } {
const calls: SpawnCall[] = [];
const host: SystemTestHost = {
access: async (target) => {
if (options.missing?.includes(target)) throw new Error(`EACCES: ${target}`);
},
readFile: async (target) => {
if (target === "/sys/fs/cgroup/cgroup.controllers") return "cpuset cpu io memory pids\n";
const content = options.files?.[target];
if (content === undefined) throw new Error(`ENOENT: ${target}`);
return content;
},
spawn: (command, args) => {
calls.push({ command, args });
if (command === "/usr/bin/systemctl" && args[0] === "--user") {
return options.userManager ?? { status: 0, stdout: "255\n", stderr: "" };
}
if (command === "/usr/bin/systemctl") return { status: 0, stdout: "", stderr: "" };
if (command === "/usr/bin/systemd-run") return options.systemdRun ?? { status: 0, stdout: "", stderr: "" };
if (command === "/usr/bin/bwrap") return bwrap;
throw new Error(`unexpected spawn: ${command}`);
},
probeUnitName: () => PROBE_UNIT,
};
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` },
{ files: { "/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 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" };
},
};
const failures = await findSystemTestPrerequisiteFailures(host);
expect(failures.slice(0, 2)).toEqual([
"bubblewrap is required at /usr/bin/bwrap",
"cgroup v2 controllers are unavailable",
]);
expect(failures[2]).toMatch(/^a reachable systemd user manager is required/u);
expect(failures).toHaveLength(3);
expect(calls.map(({ command }) => command)).toEqual(["/usr/bin/systemctl"]);
});
});
// 1112/08-03. The check looked at the system manager bus while the provider
// asks the user manager for its version and starts a `systemd-run --user
// --scope`, so a host without a user manager, with an old one, or without
// prlimit, systemd-run or delegated controllers printed PASS and then failed
// each provider test with `provider cgroup unavailable: ...`.
describe("system test prerequisites match the provider's own calls (1112/08-03)", () => {
it("fails by name when the system manager answers but the user manager does not", async () => {
const { host, calls } = fakeHost(
{ status: 0, stderr: "" },
{ userManager: { status: 1, stdout: "", stderr: "Failed to connect to bus: No medium found\n" } },
);
const failures = await findSystemTestPrerequisiteFailures(host);
expect(failures).toHaveLength(1);
expect(failures[0]).toMatch(/^a reachable systemd user manager is required/u);
expect(failures[0]).toContain("/usr/bin/systemctl --user show --property=Version --value exited 1");
expect(failures[0]).toContain("systemctl stderr: Failed to connect to bus: No medium found");
expect(calls.some(({ command }) => command === "/usr/bin/systemd-run")).toBe(false);
});
it("fails when the user manager is older than the provider needs", async () => {
const { host } = fakeHost({ status: 0, stderr: "" }, { userManager: { status: 0, stdout: "253\n", stderr: "" } });
expect(await findSystemTestPrerequisiteFailures(host)).toEqual([
'systemd user manager 254 or newer is required (/usr/bin/systemctl --user show --property=Version --value reported "253")',
]);
});
it("requires the prlimit and systemd-run executables the provider runs", async () => {
const { host } = fakeHost({ status: 0, stderr: "" }, { missing: ["/usr/bin/prlimit", "/usr/bin/systemd-run"] });
expect(await findSystemTestPrerequisiteFailures(host)).toEqual([
"prlimit is required at /usr/bin/prlimit",
"systemd-run is required at /usr/bin/systemd-run",
]);
});
it("starts the provider's own scope once and names a scope that cannot enforce its limits", async () => {
const { host, calls } = fakeHost(
{ status: 0, stderr: "" },
{ systemdRun: { status: 125, stdout: "", stderr: "provider cgroup enforcement failed: cannot read memory.max\n" } },
);
const failures = await findSystemTestPrerequisiteFailures(host);
expect(failures).toHaveLength(1);
expect(failures[0]).toMatch(/^systemd-run could not start the provider's cgroup scope/u);
expect(failures[0]).toContain("exited 125");
expect(failures[0]).toContain("systemd-run stderr: provider cgroup enforcement failed: cannot read memory.max");
expect(failures[0]).toMatch(/memory, pids and cpu controllers delegated/u);
const scopes = calls.filter(({ command }) => command === "/usr/bin/systemd-run");
expect(scopes).toHaveLength(1);
expect(scopes[0]?.args).toEqual(systemdRunPrerequisiteProbeArguments(PROBE_UNIT));
expect(scopes[0]?.args.at(-1)).toBe("/bin/true");
});
it("probes with the provider scope's properties and enforcement gate, not a copy", () => {
const probe = systemdRunPrerequisiteProbeArguments(PROBE_UNIT);
const provider = systemdRunProviderArguments(
"ca-provider-vulnerability-4242-0123456789abcdef01234567.scope",
60_000,
60,
"/usr/bin/node",
"/opt/wrapper.ts",
"/tmp/report.json",
1,
1,
);
const options = (args: readonly string[]) =>
args.filter((arg) => arg.startsWith("--") && !arg.startsWith("--unit=") && !arg.startsWith("--property=RuntimeMaxSec="));
expect(options(probe)).toEqual(options(provider));
const gate = (args: readonly string[]) => args[args.indexOf("-c") + 1];
expect(gate(probe)).toBe(gate(provider));
expect(gate(probe)).toContain("require_cgroup_value memory.max");
expect(() => systemdRunPrerequisiteProbeArguments("ca-provider-vulnerability-1-0123456789abcdef01234567.scope"))
.toThrow(/unit is invalid/u);
});
});
// 1101/13-03. The release provider jobs run the same sandbox on whatever
// runner picks them up. The supervisor checked only that the executables
// exist, so a host that refuses the namespaces failed later with bwrap's raw
// error and without the host settings.
describe("provider sandbox namespace check (1101/13-03)", () => {
it("passes on a host that creates the namespaces and probes only with bwrap", async () => {
const { host, calls } = fakeHost({ status: 0, stderr: "" });
expect(await findProviderSandboxNamespaceFailure(host)).toBeUndefined();
expect(calls).toEqual([{ command: "/usr/bin/bwrap", args: NAMESPACE_PROBE_ARGUMENTS }]);
});
it("names the restriction, bwrap's stderr and the host settings on a host that refuses them", async () => {
const { host } = fakeHost(
{ status: 1, stderr: `${RTM_NEWADDR}\n` },
{ files: { "/proc/sys/kernel/apparmor_restrict_unprivileged_userns": "1\n" } },
);
const failure = (await findProviderSandboxNamespaceFailure(host)) ?? "";
expect(failure).toMatch(/^this host does not allow unprivileged user and network namespaces/u);
expect(failure).toContain(`bwrap stderr: ${RTM_NEWADDR}`);
expect(failure).toContain("kernel.apparmor_restrict_unprivileged_userns = 1");
});
it("runs before the provider supervisor launches anything", async () => {
const provider = await readFile("scripts/run-and-validate-provider.ts", "utf8");
const check = provider.indexOf("await findProviderSandboxNamespaceFailure()");
expect(check).toBeGreaterThan(0);
expect(check).toBeLessThan(provider.indexOf("superviseProviderEvidence({"));
});
});