Files
clean-architecture-frontend…/scripts/check-system-test-prerequisites.ts
T
DongHyeonka 4e1a385fd9 fix(ci): 시스템 시험 사전 점검이 user manager의 cgroup 제어기를 본다
문제: provider scope는 systemd user manager에게서 cpu·memory·pids
제어기를 받아야 한도를 건다. 사전 점검은 root cgroup에 쓸 수 있는
제어기가 있는지만 봐서, user manager가 아무 제어기도 받지 못한
컨테이너(2026-11-14, desktop 4회 중 1회)에서도 PASS를 냈다. 그 뒤
provider 시험 18개가 하나씩 `cannot read memory.max`로 떨어졌다.

고른 방법: 이 프로세스의 uid(/proc/self/status)로 user manager의
cgroup(user.slice/user-<uid>.slice/user@<uid>.service)을 찾고, 그
cgroup.controllers에 cpu·memory·pids가 다 있는지 본다. 빠지면 빠진
이름과 있는 목록을 댄다. cgroup v2 확인이 실패한 호스트에서는 이 경로가
뜻이 없어 건너뛴다. 호스트를 읽는 부분은 기존처럼 SystemTestHost를
거친다.
버린 대안: systemd-run --user --scope로 실제 scope를 띄워 memory.max를
읽기는 본 단계와 더 같지만, 사전 점검이 unit을 만들고 정리해야 한다.
제어기 목록은 scope가 받을 수 있는 것과 같은 값이라 읽기로 충분하다.

위험·한계: systemd의 표준 user manager cgroup 경로를 가정한다. 그 경로에
manager가 없으면 사전 점검이 실패한다(provider는 systemd-run --user를
쓰므로 그때도 돌 수 없다).

검증: 노트북 감싸개에서 새 시험이 옛 점검으로 4개 실패(RED), 고친 뒤
system-test-prerequisites 13/13 통과. 노트북에서 실제 점검 PASS.
2026-11-14 06:04:30 +09:00

201 lines
8.1 KiB
TypeScript

import { constants } from "node:fs";
import { access, readFile } from "node:fs/promises";
import { spawnSync } from "node:child_process";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { PROVIDER_SANDBOX_NAMESPACE_ARGUMENTS } from "./lib/provider-sandbox-namespaces.ts";
/** What the check reads from the host; a test passes a fake. */
export type SystemTestHost = Readonly<{
/** Resolves when `target` is executable, rejects otherwise. */
access(target: string): Promise<void>;
readFile(target: string): Promise<string>;
spawn(
command: string,
args: readonly string[],
timeoutMs: number,
): Readonly<{ status: number | null; stderr: string; error?: Error | undefined }>;
}>;
export const nodeSystemTestHost: SystemTestHost = Object.freeze({
access: async (target: string) => access(target, constants.X_OK),
readFile: async (target: string) => readFile(target, "utf8"),
spawn: (command: string, args: readonly string[], timeoutMs: number) => {
const result = spawnSync(command, [...args], { encoding: "utf8", timeout: timeoutMs });
return { status: result.status, stderr: result.stderr ?? "", error: result.error };
},
});
const BWRAP = "/usr/bin/bwrap";
const SYSTEMCTL = "/usr/bin/systemctl";
/**
* 1105/12-01. The provider sandbox's own namespace options on /bin/true. The
* check used to see only that /usr/bin/bwrap was executable, so a host that
* refuses unprivileged user namespaces passed it and then failed 18 provider
* tests with `bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted`.
*/
export const NAMESPACE_PROBE_ARGUMENTS: readonly string[] = Object.freeze([
...PROVIDER_SANDBOX_NAMESPACE_ARGUMENTS,
"--ro-bind", "/", "/",
"--", "/bin/true",
]);
/** Host settings that decide whether an unprivileged process may create user namespaces. */
const USER_NAMESPACE_SETTINGS = Object.freeze([
["kernel.apparmor_restrict_unprivileged_userns", "/proc/sys/kernel/apparmor_restrict_unprivileged_userns"],
["kernel.unprivileged_userns_clone", "/proc/sys/kernel/unprivileged_userns_clone"],
["user.max_user_namespaces", "/proc/sys/user/max_user_namespaces"],
] as const);
const PROBE_TIMEOUT_MS = 5_000;
const MAX_REPORTED_STDERR_LINES = 4;
export async function findSystemTestPrerequisiteFailures(
host: SystemTestHost = nodeSystemTestHost,
): Promise<string[]> {
const failures: string[] = [];
const executable = new Map<string, boolean>();
for (const [target, label] of [
[BWRAP, "bubblewrap"],
[SYSTEMCTL, "systemctl"],
["/usr/bin/tar", "tar"],
] as const) {
try {
await host.access(target);
executable.set(target, true);
} catch {
executable.set(target, false);
failures.push(`${label} is required at ${target}`);
}
}
let cgroupV2 = false;
try {
const controllers = await host.readFile("/sys/fs/cgroup/cgroup.controllers");
if (controllers.trim().length === 0) {
failures.push("cgroup v2 controllers are unavailable");
} else {
cgroupV2 = true;
}
} catch {
failures.push("cgroup v2 is required at /sys/fs/cgroup/cgroup.controllers");
}
if (executable.get(SYSTEMCTL)) {
const probe = host.spawn(SYSTEMCTL, ["show-environment"], 3_000);
if (probe.error || probe.status !== 0) {
failures.push("a reachable systemd manager bus is required");
}
}
// The user manager's cgroup path below is a cgroup v2 path; without v2 it would name the wrong cause.
const controllerFailure = cgroupV2 ? await findProviderScopeControllerFailure(host) : undefined;
if (controllerFailure !== undefined) failures.push(controllerFailure);
if (executable.get(BWRAP)) {
const failure = await findProviderSandboxNamespaceFailure(host);
if (failure !== undefined) failures.push(failure);
}
return failures;
}
/**
* 1101/13-03. Whether this host lets the provider sandbox create its
* namespaces, and if not, why and what to do. The release provider jobs run
* it before the provider (scripts/run-and-validate-provider.ts).
*/
export async function findProviderSandboxNamespaceFailure(
host: SystemTestHost = nodeSystemTestHost,
): Promise<string | undefined> {
const probe = host.spawn(BWRAP, NAMESPACE_PROBE_ARGUMENTS, PROBE_TIMEOUT_MS);
return probe.error || probe.status !== 0 ? namespaceFailure(host, probe) : undefined;
}
async function namespaceFailure(
host: SystemTestHost,
probe: ReturnType<SystemTestHost["spawn"]>,
): Promise<string> {
const settings: string[] = [];
for (const [name, file] of USER_NAMESPACE_SETTINGS) {
try {
settings.push(`${name} = ${(await host.readFile(file)).trim()}`);
} catch {
// Absent on this kernel; the others still say what the host allows.
}
}
const stderr = probe.stderr
.split("\n")
.map((line) => line.trim())
.filter((line) => line.length > 0)
.slice(0, MAX_REPORTED_STDERR_LINES);
const outcome = probe.error
? `did not finish: ${probe.error.message}`
: `exited ${String(probe.status)}`;
return [
"this host does not allow unprivileged user and network namespaces, which the provider " +
`sandbox needs (bwrap ${PROVIDER_SANDBOX_NAMESPACE_ARGUMENTS.join(" ")}).`,
` probe: ${BWRAP} ${NAMESPACE_PROBE_ARGUMENTS.join(" ")} ${outcome}`,
...(stderr.length > 0 ? stderr.map((line) => ` bwrap stderr: ${line}`) : [" bwrap stderr: (empty)"]),
...(settings.length > 0 ? [` host settings: ${settings.join(", ")}`] : []),
" To fix, do one of these:",
" - give /usr/bin/bwrap an AppArmor profile that allows `userns` (Ubuntu 23.10 and later),",
" - on a host you own, set sysctl kernel.apparmor_restrict_unprivileged_userns=0",
" (or kernel.unprivileged_userns_clone=1 where that setting exists),",
" - run FE-GATE-013 on another runner that allows them.",
" Runner requirements: docs/operations/ci-quality-gates.md.",
].join("\n");
}
/** The controllers every provider scope is limited by (scripts/lib/provider-cgroup.ts). */
const PROVIDER_SCOPE_CONTROLLERS = Object.freeze(["cpu", "memory", "pids"] as const);
/**
* Whether the systemd user manager that provider scopes run under was handed
* the cgroup controllers their limits need. A manager without them passed
* every check above, and then each provider stopped with `provider cgroup
* enforcement failed: cannot read memory.max` (2026-11-14: a `docker exec`
* sat in the system container's root cgroup while systemd started, so systemd
* delegated nothing).
*/
async function findProviderScopeControllerFailure(host: SystemTestHost): Promise<string | undefined> {
const status = await host.readFile("/proc/self/status").catch(() => "");
const uid = /^Uid:\s+(\d+)\s/mu.exec(status)?.[1];
if (uid === undefined) return "this process's uid is unreadable at /proc/self/status";
const manager = `/sys/fs/cgroup/user.slice/user-${uid}.slice/user@${uid}.service`;
let delegated: string[];
try {
delegated = (await host.readFile(`${manager}/cgroup.controllers`)).trim().split(/\s+/u);
} catch {
return `a systemd user manager is required at ${manager}`;
}
const missing = PROVIDER_SCOPE_CONTROLLERS.filter((controller) => !delegated.includes(controller));
return missing.length === 0
? undefined
: `the systemd user manager at ${manager} has no ${missing.join(", ")} cgroup controller ` +
`for provider scopes (it has: ${delegated.join(" ") || "none"})`;
}
export function formatSystemTestPrerequisiteReport(failures: readonly string[]): string {
return [
"System/CI-runner test prerequisites are unavailable:",
...failures.map((failure) => ` - ${failure}`),
"",
"Run test:unit/test:contract/test:component/test:integration for the",
"developer loop. test:system is intentionally reserved for a compatible",
"Linux CI-runner host.",
"",
].join("\n");
}
const isCli = process.argv[1] !== undefined && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url);
if (isCli) {
const failures = await findSystemTestPrerequisiteFailures();
if (failures.length > 0) {
process.stderr.write(formatSystemTestPrerequisiteReport(failures));
process.exit(1);
}
process.stdout.write("System test prerequisites: PASS\n");
}