Path: blob/master/src/packages/file-server/zfs/test/nfs.test.ts
1450 views
/*1DEVELOPMENT:23pnpm exec jest --watch nfs.test.ts4*/56import { executeCode } from "@cocalc/backend/execute-code";7import {8createTestPools,9deleteTestPools,10restartNfsServer,11init,12describe,13} from "./util";14import {15createFilesystem,16createSnapshot,17get,18shareNFS,19unshareNFS,20} from "@cocalc/file-server/zfs";21import { filesystemMountpoint } from "@cocalc/file-server/zfs/names";22import { readFile, writeFile } from "fs/promises";23import { join } from "path";2425describe("create a project, put in a files, snapshot, another file, then share via NFS, mount and verify it works", () => {26let x: any = null;27const project_id = "00000000-0000-0000-0000-000000000001";28let nsfMnt = "";2930beforeAll(async () => {31x = await createTestPools({ count: 1, size: "1G" });32nsfMnt = join(x.tempDir, project_id);33await init();34});3536afterAll(async () => {37if (x != null) {38await deleteTestPools(x);39}40});4142const mnt = filesystemMountpoint({ project_id, namespace: "default" });43const FILE_CONTENT = "hello";44const FILENAME = "cocalc.txt";45it("creates a project and write a file", async () => {46const project = await createFilesystem({47project_id,48});49expect(project.owner_id).toBe(project_id);50const path = join(mnt, FILENAME);51await writeFile(path, FILE_CONTENT);52});5354let snapshot1, snapshot2;55const FILE_CONTENT2 = "hello2";56const FILENAME2 = "cocalc2.txt";5758it("create a snapshot and write another file, so there is a nontrivial snapshot to view through NFS", async () => {59snapshot1 = await createSnapshot({ project_id });60expect(!!snapshot1).toBe(true);61const path = join(mnt, FILENAME2);62await writeFile(path, FILE_CONTENT2);63snapshot2 = await createSnapshot({ project_id, force: true });64expect(snapshot2).not.toEqual(snapshot1);65});6667let host = "";68const client = "127.0.0.1";6970const mount = async () => {71await executeCode({72command: "sudo",73args: ["mkdir", "-p", nsfMnt],74});75await executeCode({76command: "sudo",77args: ["mount", host, nsfMnt],78});79};8081it("shares the project via NFS, and mounts it", async () => {82host = await shareNFS({ project_id, client });83const project = get({ project_id });84expect(project.nfs).toEqual([client]);85await mount();86});8788it("confirms our files and snapshots are there as expected", async () => {89const { stdout } = await executeCode({90command: "sudo",91args: ["ls", nsfMnt],92});93expect(stdout).toContain(FILENAME);94expect(stdout).toContain(FILENAME2);95expect((await readFile(join(nsfMnt, FILENAME))).toString()).toEqual(96FILE_CONTENT,97);98expect((await readFile(join(nsfMnt, FILENAME2))).toString()).toEqual(99FILE_CONTENT2,100);101});102103it("stop NFS share and confirms it no longers works", async () => {104await executeCode({105command: "sudo",106args: ["umount", nsfMnt],107});108await restartNfsServer();109await unshareNFS({ project_id, client });110try {111await mount();112throw Error("bug -- mount should fail");113} catch (err) {114expect(`${err}`).toMatch(/not permitted|denied/);115}116});117});118119120