1
- import { before , after , fs } from "./setup" ;
2
- import { writeFile } from "fs/promises" ;
1
+ import { before , after , fs , sudo } from "./setup" ;
2
+ import { readFile , writeFile , unlink } from "fs/promises" ;
3
3
import { join } from "path" ;
4
- import { delay } from "awaiting" ;
5
4
import { wait } from "@cocalc/backend/conat/test/util" ;
6
5
import { randomBytes } from "crypto" ;
7
6
@@ -24,7 +23,7 @@ describe("setting and getting quota of a subvolume", () => {
24
23
await writeFile ( join ( vol . path , "buf" ) , buf ) ;
25
24
await wait ( {
26
25
until : async ( ) => {
27
- await delay ( 1000 ) ;
26
+ await sudo ( { command : "sync" } ) ;
28
27
const { used } = await vol . usage ( ) ;
29
28
return used > 0 ;
30
29
} ,
@@ -42,4 +41,54 @@ describe("setting and getting quota of a subvolume", () => {
42
41
} ) ;
43
42
} ) ;
44
43
44
+ describe ( "test snapshots" , ( ) => {
45
+ let vol ;
46
+ it ( "creates a volume and write a file to it" , async ( ) => {
47
+ vol = await fs . subvolume ( "snapper" ) ;
48
+ expect ( await vol . hasUnsavedChanges ( ) ) . toBe ( false ) ;
49
+ await writeFile ( join ( vol . path , "a.txt" ) , "hello" ) ;
50
+ expect ( await vol . hasUnsavedChanges ( ) ) . toBe ( true ) ;
51
+ } ) ;
52
+
53
+ it ( "snapshot the volume" , async ( ) => {
54
+ expect ( await vol . snapshots ( ) ) . toEqual ( [ ] ) ;
55
+ await vol . createSnapshot ( "snap1" ) ;
56
+ expect ( await vol . snapshots ( ) ) . toEqual ( [ "snap1" ] ) ;
57
+ expect ( await vol . hasUnsavedChanges ( ) ) . toBe ( false ) ;
58
+ } ) ;
59
+
60
+ it ( "create a file see that we know there are unsaved changes" , async ( ) => {
61
+ await writeFile ( join ( vol . path , "b.txt" ) , "world" ) ;
62
+ await sudo ( { command : "sync" } ) ;
63
+ expect ( await vol . hasUnsavedChanges ( ) ) . toBe ( true ) ;
64
+ } ) ;
65
+
66
+ it ( "delete our file, but then read it in a snapshot" , async ( ) => {
67
+ await unlink ( join ( vol . path , "a.txt" ) ) ;
68
+ const b = await readFile ( join ( vol . snapshotsDir , "snap1" , "a.txt" ) , "utf8" ) ;
69
+ expect ( b ) . toEqual ( "hello" ) ;
70
+ } ) ;
71
+
72
+ it ( "verifies snapshot exists" , async ( ) => {
73
+ expect ( await vol . snapshotExists ( "snap1" ) ) . toBe ( true ) ;
74
+ expect ( await vol . snapshotExists ( "snap2" ) ) . toBe ( false ) ;
75
+ } ) ;
76
+
77
+ it ( "lock our snapshot and confirm it prevents deletion" , async ( ) => {
78
+ await vol . lockSnapshot ( "snap1" ) ;
79
+ expect ( async ( ) => {
80
+ await vol . deleteSnapshot ( "snap1" ) ;
81
+ } ) . rejects . toThrow ( "locked" ) ;
82
+ } ) ;
83
+
84
+ it ( "unlock our snapshot and delete it" , async ( ) => {
85
+ await vol . unlockSnapshot ( "snap1" ) ;
86
+ await vol . deleteSnapshot ( "snap1" ) ;
87
+ expect ( await vol . snapshotExists ( "snap1" ) ) . toBe ( false ) ;
88
+ expect ( await vol . snapshots ( ) ) . toEqual ( [ ] ) ;
89
+ } ) ;
90
+
91
+
92
+ } ) ;
93
+
45
94
afterAll ( after ) ;
0 commit comments