Path: blob/master/src/packages/file-server/btrfs/test/setup.ts
1543 views
import {1filesystem,2type Filesystem,3} from "@cocalc/file-server/btrfs/filesystem";4import { chmod, mkdtemp, mkdir, rm, stat } from "node:fs/promises";5import { tmpdir } from "node:os";6import { join } from "path";7import { until } from "@cocalc/util/async-utils";8import { sudo } from "../util";9export { sudo };10export { delay } from "awaiting";1112export let fs: Filesystem;13let tempDir;1415const TEMP_PREFIX = "cocalc-test-btrfs-";1617async function ensureMoreLoops() {18// to run tests, this is helpful19//for i in $(seq 8 63); do sudo mknod -m660 /dev/loop$i b 7 $i; sudo chown root:disk /dev/loop$i; done20for (let i = 0; i < 64; i++) {21try {22await stat(`/dev/loop${i}`);23continue;24} catch {}25try {26// also try/catch this because ensureMoreLoops happens in parallel many times at once...27await sudo({28command: "mknod",29args: ["-m660", `/dev/loop${i}`, "b", "7", `${i}`],30});31} catch {}32await sudo({ command: "chown", args: ["root:disk", `/dev/loop${i}`] });33}34}3536export async function before() {37try {38const command = `umount ${join(tmpdir(), TEMP_PREFIX)}*/mnt`;39// attempt to unmount any mounts left from previous runs.40// TODO: this could impact runs in parallel41await sudo({ command, bash: true });42} catch {}43await ensureMoreLoops();44tempDir = await mkdtemp(join(tmpdir(), TEMP_PREFIX));45// Set world read/write/execute46await chmod(tempDir, 0o777);47const mount = join(tempDir, "mnt");48await mkdir(mount);49await chmod(mount, 0o777);50fs = await filesystem({51device: join(tempDir, "btrfs.img"),52formatIfNeeded: true,53mount: join(tempDir, "mnt"),54});55}5657export async function after() {58await until(async () => {59try {60await fs.unmount();61return true;62} catch {63return false;64}65});66await rm(tempDir, { force: true, recursive: true });67}686970