forked from bicomsystems/go-libzfs
-
Notifications
You must be signed in to change notification settings - Fork 4
/
zfs_test.go
192 lines (168 loc) · 4.82 KB
/
zfs_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package zfs_test
import (
"fmt"
"testing"
"github.com/bicomsystems/go-libzfs"
)
/* ------------------------------------------------------------------------- */
// HELPERS:
var TSTDatasetPath = TSTPoolName + "/DATASET"
var TSTVolumePath = TSTDatasetPath + "/VOLUME"
var TSTDatasetPathSnap = TSTDatasetPath + "@test"
func printDatasets(ds []zfs.Dataset) error {
for _, d := range ds {
path, err := d.Path()
if err != nil {
return err
}
p, err := d.GetProperty(zfs.DatasetPropType)
if err != nil {
return err
}
fmt.Printf(" %30s | %10s\n", path, p.Value)
if len(d.Children) > 0 {
printDatasets(d.Children)
}
}
return nil
}
/* ------------------------------------------------------------------------- */
// TESTS:
func zfsTestDatasetCreate(t *testing.T) {
// reinit names used in case TESTPOOL was in conflict
TSTDatasetPath = TSTPoolName + "/DATASET"
TSTVolumePath = TSTDatasetPath + "/VOLUME"
TSTDatasetPathSnap = TSTDatasetPath + "@test"
println("TEST DatasetCreate(", TSTDatasetPath, ") (filesystem) ... ")
props := make(map[zfs.Prop]zfs.Property)
d, err := zfs.DatasetCreate(TSTDatasetPath, zfs.DatasetTypeFilesystem, props)
if err != nil {
t.Error(err)
return
}
d.Close()
print("PASS\n\n")
strSize := "536870912" // 512M
println("TEST DatasetCreate(", TSTVolumePath, ") (volume) ... ")
props[zfs.DatasetPropVolsize] = zfs.Property{Value: strSize}
// In addition I explicitly choose some more properties to be set.
props[zfs.DatasetPropVolblocksize] = zfs.Property{Value: "4096"}
props[zfs.DatasetPropReservation] = zfs.Property{Value: strSize}
d, err = zfs.DatasetCreate(TSTVolumePath, zfs.DatasetTypeVolume, props)
if err != nil {
t.Error(err)
return
}
d.Close()
print("PASS\n\n")
}
func zfsTestDatasetOpen(t *testing.T) {
println("TEST DatasetOpen(", TSTDatasetPath, ") ... ")
d, err := zfs.DatasetOpen(TSTDatasetPath)
if err != nil {
t.Error(err)
return
}
d.Close()
print("PASS\n\n")
}
func zfsTestDatasetOpenAll(t *testing.T) {
println("TEST DatasetOpenAll()/DatasetCloseAll() ... ")
ds, err := zfs.DatasetOpenAll()
if err != nil {
t.Error(err)
return
}
if err = printDatasets(ds); err != nil {
zfs.DatasetCloseAll(ds)
t.Error(err)
return
}
zfs.DatasetCloseAll(ds)
print("PASS\n\n")
}
func zfsTestDatasetSnapshot(t *testing.T) {
println("TEST DatasetSnapshot(", TSTDatasetPath, ", true, ...) ... ")
props := make(map[zfs.Prop]zfs.Property)
d, err := zfs.DatasetSnapshot(TSTDatasetPathSnap, true, props)
if err != nil {
t.Error(err)
return
}
defer d.Close()
print("PASS\n\n")
}
func zfsTestDatasetDestroy(t *testing.T) {
println("TEST DATASET Destroy( ", TSTDatasetPath, " ) ... ")
d, err := zfs.DatasetOpen(TSTDatasetPath)
if err != nil {
t.Error(err)
return
}
defer d.Close()
if err = d.DestroyRecursive(); err != nil {
t.Error(err)
return
}
print("PASS\n\n")
}
/* ------------------------------------------------------------------------- */
// EXAMPLES:
// Example of creating ZFS volume
func ExampleDatasetCreate() {
// Create map to represent ZFS dataset properties. This is equivalent to
// list of properties you can get from ZFS CLI tool, and some more
// internally used by libzfs.
props := make(map[zfs.Prop]zfs.Property)
// I choose to create (block) volume 1GiB in size. Size is just ZFS dataset
// property and this is done as map of strings. So, You have to either
// specify size as base 10 number in string, or use strconv package or
// similar to convert in to string (base 10) from numeric type.
strSize := "1073741824"
props[zfs.DatasetPropVolsize] = zfs.Property{Value: strSize}
// In addition I explicitly choose some more properties to be set.
props[zfs.DatasetPropVolblocksize] = zfs.Property{Value: "4096"}
props[zfs.DatasetPropReservation] = zfs.Property{Value: strSize}
// Lets create desired volume
d, err := zfs.DatasetCreate("TESTPOOL/VOLUME1", zfs.DatasetTypeVolume, props)
if err != nil {
println(err.Error())
return
}
// Dataset have to be closed for memory cleanup
defer d.Close()
println("Created zfs volume TESTPOOL/VOLUME1")
}
func ExampleDatasetOpen() {
// Open dataset and read its available space
d, err := zfs.DatasetOpen("TESTPOOL/DATASET1")
if err != nil {
panic(err.Error())
}
defer d.Close()
var p zfs.Property
if p, err = d.GetProperty(zfs.DatasetPropAvailable); err != nil {
panic(err.Error())
}
println(zfs.DatasetPropertyToName(zfs.DatasetPropAvailable), " = ",
p.Value)
}
func ExampleDatasetOpenAll() {
datasets, err := zfs.DatasetOpenAll()
if err != nil {
panic(err.Error())
}
defer zfs.DatasetCloseAll(datasets)
// Print out path and type of root datasets
for _, d := range datasets {
path, err := d.Path()
if err != nil {
panic(err.Error())
}
p, err := d.GetProperty(zfs.DatasetPropType)
if err != nil {
panic(err.Error())
}
fmt.Printf("%30s | %10s\n", path, p.Value)
}
}