Files
bun.sh/test/js/bun/spawn/container-cgroups-only.test.ts
Claude 9297c13b4c Fix container spawn: Use clone3 for all container features, fix error propagation
- Use clone3 for ANY container features (namespaces or cgroups), vfork only when no container
- Fix cgroup setup error propagation - properly return errno instead of 0
- Fix cgroup path consistency between C++ and Zig code
- Make cgroup failures fatal as requested
- Fix synchronization between parent and child for proper cgroup setup
- Add proper __aligned_u64 definition for clone_args structure

The implementation now correctly:
- Creates cgroups under /sys/fs/cgroup/bun-*
- Adds process to cgroup before it starts executing
- Applies CPU and memory resource limits via cgroup v2
- Cleans up cgroups when process exits

Tests pass with root privileges, fail with EACCES without root as expected.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-27 11:36:44 +02:00

63 lines
1.6 KiB
TypeScript

import { test, expect, describe } from "bun:test";
import { bunEnv } from "harness";
describe("container cgroups v2 only (no namespaces)", () => {
// Skip all tests if not Linux
if (process.platform !== "linux") {
test.skip("container tests are Linux-only", () => {});
return;
}
test("Resource limits without namespaces", async () => {
// Test cgroups without any namespace isolation
await using proc = Bun.spawn({
cmd: ["/bin/echo", "cgroups only"],
env: bunEnv,
container: {
// No namespace isolation
limit: {
cpu: 50, // 50% CPU
ram: 100 * 1024 * 1024, // 100MB RAM
},
},
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([
proc.stdout.text(),
proc.stderr.text(),
proc.exited,
]);
expect(exitCode).toBe(0);
expect(stdout.trim()).toBe("cgroups only");
});
test("Check process cgroup placement", async () => {
await using proc = Bun.spawn({
cmd: ["/bin/sh", "-c", "cat /proc/self/cgroup"],
env: bunEnv,
container: {
limit: {
cpu: 25,
},
},
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([
proc.stdout.text(),
proc.stderr.text(),
proc.exited,
]);
console.log("Process cgroup:", stdout);
expect(exitCode).toBe(0);
// If cgroups worked, we should see a bun-* cgroup
// If not, process will be in default cgroup (that's OK too)
expect(stdout.length).toBeGreaterThan(0);
});
});