-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_util.go
97 lines (84 loc) · 2.22 KB
/
test_util.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
89
90
91
92
93
94
95
96
97
package zfs
import (
"context"
"fmt"
"math"
"os"
"os/exec"
"strings"
"time"
)
// sudo zfs allow <user> canmount,clone,compression,create,destroy,encryption,keyformat,keylocation,load-key,mount,
// mountpoint,promote,readonly,receive,refquota,refreservation,rename,rollback,send,snapshot,userprop,volblocksize,
// volmode,volsize <dataset>
var zfsPermissions = []string{
"canmount",
"clone",
"compression",
"create",
"destroy",
"encryption",
"keyformat",
"keylocation",
"load-key",
"mount",
"mountpoint",
"promote",
"readonly",
"receive",
"refquota",
"refreservation",
"rename",
"rollback",
"send",
"snapshot",
"userprop",
"volblocksize",
"volmode",
"volsize",
}
// TestZPool uses some temp files to create a zpool with the given name to run tests with
func TestZPool(zpool string, fn func()) {
noErr := func(err error, context, out string) {
if err != nil {
fmt.Println("context: " + context)
fmt.Println("output: " + out)
panic(err)
}
}
args := []string{
"zpool", "create", zpool,
}
for i := 0; i < 3; i++ {
f, err := os.CreateTemp(os.TempDir(), "test-zpool-")
noErr(err, fmt.Sprintf("create zpool file %d", i), "")
err = f.Truncate(pow2(29))
noErr(err, fmt.Sprintf("truncate zpool file %d", i), "")
noErr(f.Close(), fmt.Sprintf("close zpool file %d", i), "")
args = append(args, f.Name())
defer os.Remove(f.Name()) // nolint:revive // its ok to defer to end of func
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "sudo", args...)
out, err := cmd.CombinedOutput()
noErr(err, "sudo "+strings.Join(args, " "), string(out))
cmd = exec.CommandContext(ctx, "sudo",
"zfs", "allow", "everyone",
strings.Join(zfsPermissions, ","),
zpool,
)
out, err = cmd.CombinedOutput()
noErr(err, "sudo zfs allow everyone "+strings.Join(zfsPermissions, ",")+" "+zpool, string(out))
defer func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "sudo", "zpool", "destroy", zpool)
out, err := cmd.CombinedOutput()
noErr(err, "sudo zpool destroy "+zpool, string(out))
}()
fn()
}
func pow2(x int) int64 {
return int64(math.Pow(2, float64(x)))
}