Path: blob/master/src/packages/file-server/zfs/test/archive.test.ts
1450 views
/*1DEVELOPMENT:23pnpm exec jest --watch archive.test.ts4*/56import { executeCode } from "@cocalc/backend/execute-code";7import { createTestPools, deleteTestPools, init, describe } from "./util";8import {9archiveFilesystem,10dearchiveFilesystem,11createFilesystem,12createSnapshot,13getSnapshots,14get,15} from "@cocalc/file-server/zfs";16import { filesystemMountpoint } from "@cocalc/file-server/zfs/names";17import { readFile, writeFile } from "fs/promises";18import { join } from "path";1920describe("create a project, put in some files/snapshot, archive the project, confirm gone, de-archive it, and confirm files are back as expected", () => {21jest.setTimeout(10000);22let x: any = null;2324beforeAll(async () => {25x = await createTestPools({ count: 1, size: "1G" });26await init();27});2829afterAll(async () => {30if (x != null) {31await deleteTestPools(x);32}33});3435const project_id = "00000000-0000-0000-0000-000000000001";36const mnt = filesystemMountpoint({ project_id, namespace: "default" });37const FILE_CONTENT = "hello";38const FILENAME = "cocalc.txt";39it("creates a project and write a file", async () => {40const filesystem = await createFilesystem({41project_id,42});43expect(filesystem.owner_type).toBe("project");44expect(filesystem.owner_id).toBe(project_id);45const path = join(mnt, FILENAME);46await writeFile(path, FILE_CONTENT);47});4849let snapshot1, snapshot2;50const FILE_CONTENT2 = "hello2";51const FILENAME2 = "cocalc2.txt";5253it("create a snapshot and write another file, so there is a nontrivial snapshot to be archived", async () => {54snapshot1 = await createSnapshot({ project_id });55expect(!!snapshot1).toBe(true);56const path = join(mnt, FILENAME2);57await writeFile(path, FILE_CONTENT2);58snapshot2 = await createSnapshot({ project_id, force: true });59expect(snapshot2).not.toEqual(snapshot1);60});6162it("archive the project and checks project is no longer in zfs", async () => {63expect(get({ project_id }).archived).toBe(false);64await archiveFilesystem({ project_id });65const { stdout } = await executeCode({66command: "zfs",67args: ["list", x.pools[0]],68});69expect(stdout).not.toContain(project_id);70expect(get({ project_id }).archived).toBe(true);71});7273it("archiving an already archived project is an error", async () => {74await expect(75async () => await archiveFilesystem({ project_id }),76).rejects.toThrow();77});7879it("dearchive project and verify zfs filesystem is back, along with files and snapshots", async () => {80let called = false;81await dearchiveFilesystem({82project_id,83progress: () => {84called = true;85},86});87expect(called).toBe(true);88expect(get({ project_id }).archived).toBe(false);8990expect((await readFile(join(mnt, FILENAME))).toString()).toEqual(91FILE_CONTENT,92);93expect((await readFile(join(mnt, FILENAME2))).toString()).toEqual(94FILE_CONTENT2,95);96expect(await getSnapshots({ project_id })).toEqual([snapshot1, snapshot2]);97});9899it("dearchiving an already de-archived project is an error", async () => {100await expect(101async () => await dearchiveFilesystem({ project_id }),102).rejects.toThrow();103});104});105106107