This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
btrfs_test.go
88 lines (77 loc) · 1.71 KB
/
btrfs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package btrfs
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"syscall"
"testing"
"./fs"
"./loop"
)
func TestIsSubvolume(t *testing.T) {
b, err := IsSubvolume(".")
if err != nil {
t.Fatal(err)
}
t.Log(b)
}
func TestSubvolume(t *testing.T) {
bf, err := backingFile()
if err != nil {
t.Fatal(err)
}
defer os.Remove(bf)
lh, err := loop.AttachLoopDevice(bf)
if err != nil {
t.Fatal(err)
}
defer lh.Close()
tmpdir, err := ioutil.TempDir("", "btrfs-testing.")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
err = syscall.Mount(lh.Name(), tmpdir, "btrfs", uintptr(syscall.MS_NOATIME), "")
if err != nil {
t.Fatal(err)
}
defer syscall.Unmount(tmpdir, 0)
for i := 0; i < 10; i++ {
volname := fmt.Sprintf("vol-%d", i)
if err := SubvolCreate(tmpdir, volname); err != nil {
t.Errorf("failed to create %q: %s", volname, err)
}
for j := 0; j < 10; j++ {
snapname := fmt.Sprintf("%s-snap-%d", volname, j)
err := SubvolSnapshot(filepath.Join(tmpdir, volname), tmpdir, snapname)
if err != nil {
t.Errorf("failed to create %q: %s", snapname, err)
}
if err := SubvolDelete(tmpdir, snapname); err != nil {
t.Errorf("failed to delete %q: %s", snapname, err)
}
}
if err := SubvolDelete(tmpdir, volname); err != nil {
t.Errorf("failed to delete %q: %s", volname, err)
}
}
}
// helper
func backingFile() (string, error) {
fh, err := ioutil.TempFile("", "btrfs-testing.")
if err != nil {
return "", err
}
// get a 1GB non-allocated file
if err := fh.Truncate(1 * 1024 * 1024 * 1024); err != nil {
return "", err
}
if err := fh.Close(); err != nil {
return "", err
}
if err := fs.Mkfs(fh.Name()); err != nil {
return "", err
}
return fh.Name(), nil
}