-
Notifications
You must be signed in to change notification settings - Fork 66
/
zfs_test.go
351 lines (257 loc) · 7.24 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package zfs_test
import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
zfs "github.com/mistifyio/go-zfs/v3"
)
func TestDatasets(t *testing.T) {
defer setupZPool(t).cleanUp()
_, err := zfs.Datasets("")
ok(t, err)
ds, err := zfs.GetDataset("test")
ok(t, err)
equals(t, zfs.DatasetFilesystem, ds.Type)
equals(t, "", ds.Origin)
if runtime.GOOS != "solaris" {
assert(t, ds.Logicalused != 0, "Logicalused is not greater than 0")
}
}
func TestDatasetGetProperty(t *testing.T) {
defer setupZPool(t).cleanUp()
ds, err := zfs.GetDataset("test")
ok(t, err)
prop, err := ds.GetProperty("foobarbaz")
nok(t, err)
equals(t, "", prop)
prop, err = ds.GetProperty("compression")
ok(t, err)
equals(t, "off", prop)
// creation should be a time stamp with spaces in it
prop, err = ds.GetProperty("creation")
ok(t, err)
if len(strings.Fields(prop)) != 5 {
t.Errorf("expected a string with spaces in it, got: %v", prop)
}
}
func TestSnapshots(t *testing.T) {
defer setupZPool(t).cleanUp()
snapshots, err := zfs.Snapshots("")
ok(t, err)
for _, snapshot := range snapshots {
equals(t, zfs.DatasetSnapshot, snapshot.Type)
}
}
func TestFilesystems(t *testing.T) {
defer setupZPool(t).cleanUp()
f, err := zfs.CreateFilesystem("test/filesystem-test", nil)
ok(t, err)
filesystems, err := zfs.Filesystems("")
ok(t, err)
for _, filesystem := range filesystems {
equals(t, zfs.DatasetFilesystem, filesystem.Type)
}
ok(t, f.Destroy(zfs.DestroyDefault))
}
func TestCreateFilesystemWithProperties(t *testing.T) {
defer setupZPool(t).cleanUp()
props := map[string]string{
"compression": "lz4",
}
f, err := zfs.CreateFilesystem("test/filesystem-test", props)
ok(t, err)
equals(t, "lz4", f.Compression)
filesystems, err := zfs.Filesystems("")
ok(t, err)
for _, filesystem := range filesystems {
equals(t, zfs.DatasetFilesystem, filesystem.Type)
}
ok(t, f.Destroy(zfs.DestroyDefault))
}
func TestVolumes(t *testing.T) {
defer setupZPool(t).cleanUp()
v, err := zfs.CreateVolume("test/volume-test", uint64(pow2(23)), nil)
ok(t, err)
// volumes are sometimes "busy" if you try to manipulate them right away
sleep(1)
equals(t, zfs.DatasetVolume, v.Type)
volumes, err := zfs.Volumes("")
ok(t, err)
for _, volume := range volumes {
equals(t, zfs.DatasetVolume, volume.Type)
}
ok(t, v.Destroy(zfs.DestroyDefault))
}
func TestSnapshot(t *testing.T) {
defer setupZPool(t).cleanUp()
f, err := zfs.CreateFilesystem("test/snapshot-test", nil)
ok(t, err)
filesystems, err := zfs.Filesystems("")
ok(t, err)
for _, filesystem := range filesystems {
equals(t, zfs.DatasetFilesystem, filesystem.Type)
}
s, err := f.Snapshot("test", false)
ok(t, err)
equals(t, zfs.DatasetSnapshot, s.Type)
equals(t, "test/snapshot-test@test", s.Name)
ok(t, s.Destroy(zfs.DestroyDefault))
ok(t, f.Destroy(zfs.DestroyDefault))
}
func TestClone(t *testing.T) {
defer setupZPool(t).cleanUp()
f, err := zfs.CreateFilesystem("test/snapshot-test", nil)
ok(t, err)
filesystems, err := zfs.Filesystems("")
ok(t, err)
for _, filesystem := range filesystems {
equals(t, zfs.DatasetFilesystem, filesystem.Type)
}
s, err := f.Snapshot("test", false)
ok(t, err)
equals(t, zfs.DatasetSnapshot, s.Type)
equals(t, "test/snapshot-test@test", s.Name)
c, err := s.Clone("test/clone-test", nil)
ok(t, err)
equals(t, zfs.DatasetFilesystem, c.Type)
ok(t, c.Destroy(zfs.DestroyDefault))
ok(t, s.Destroy(zfs.DestroyDefault))
ok(t, f.Destroy(zfs.DestroyDefault))
}
func TestSendSnapshot(t *testing.T) {
defer setupZPool(t).cleanUp()
f, err := zfs.CreateFilesystem("test/snapshot-test", nil)
ok(t, err)
filesystems, err := zfs.Filesystems("")
ok(t, err)
for _, filesystem := range filesystems {
equals(t, zfs.DatasetFilesystem, filesystem.Type)
}
s, err := f.Snapshot("test", false)
ok(t, err)
file, _ := ioutil.TempFile("/tmp/", "zfs-")
defer file.Close()
err = file.Truncate(pow2(30))
ok(t, err)
defer os.Remove(file.Name())
err = s.SendSnapshot(file)
ok(t, err)
ok(t, s.Destroy(zfs.DestroyDefault))
ok(t, f.Destroy(zfs.DestroyDefault))
}
func TestChildren(t *testing.T) {
defer setupZPool(t).cleanUp()
f, err := zfs.CreateFilesystem("test/snapshot-test", nil)
ok(t, err)
s, err := f.Snapshot("test", false)
ok(t, err)
equals(t, zfs.DatasetSnapshot, s.Type)
equals(t, "test/snapshot-test@test", s.Name)
children, err := f.Children(0)
ok(t, err)
equals(t, 1, len(children))
equals(t, "test/snapshot-test@test", children[0].Name)
ok(t, s.Destroy(zfs.DestroyDefault))
ok(t, f.Destroy(zfs.DestroyDefault))
}
func TestListZpool(t *testing.T) {
defer setupZPool(t).cleanUp()
pools, err := zfs.ListZpools()
ok(t, err)
for _, pool := range pools {
if pool.Name == "test" {
equals(t, "test", pool.Name)
return
}
}
t.Fatal("Failed to find test pool")
}
func TestRollback(t *testing.T) {
defer setupZPool(t).cleanUp()
f, err := zfs.CreateFilesystem("test/snapshot-test", nil)
ok(t, err)
filesystems, err := zfs.Filesystems("")
ok(t, err)
for _, filesystem := range filesystems {
equals(t, zfs.DatasetFilesystem, filesystem.Type)
}
s1, err := f.Snapshot("test", false)
ok(t, err)
_, err = f.Snapshot("test2", false)
ok(t, err)
s3, err := f.Snapshot("test3", false)
ok(t, err)
err = s3.Rollback(false)
ok(t, err)
err = s1.Rollback(false)
assert(t, err != nil, "should error when rolling back beyond most recent snapshot without destroyMoreRecent = true")
err = s1.Rollback(true)
ok(t, err)
ok(t, s1.Destroy(zfs.DestroyDefault))
ok(t, f.Destroy(zfs.DestroyDefault))
}
func TestDiff(t *testing.T) {
defer setupZPool(t).cleanUp()
fs, err := zfs.CreateFilesystem("test/origin", nil)
ok(t, err)
linkedFile, err := os.Create(filepath.Join(fs.Mountpoint, "linked"))
ok(t, err)
movedFile, err := os.Create(filepath.Join(fs.Mountpoint, "file"))
ok(t, err)
snapshot, err := fs.Snapshot("snapshot", false)
ok(t, err)
unicodeFile, err := os.Create(filepath.Join(fs.Mountpoint, "i ❤ unicode"))
ok(t, err)
err = os.Rename(movedFile.Name(), movedFile.Name()+"-new")
ok(t, err)
err = os.Link(linkedFile.Name(), linkedFile.Name()+"_hard")
ok(t, err)
inodeChanges, err := fs.Diff(snapshot.Name)
ok(t, err)
equals(t, 4, len(inodeChanges))
unicodePath := "/test/origin/i\x040\x1c2\x135\x144\x040unicode"
wants := map[string]*zfs.InodeChange{
"/test/origin/linked": {
Type: zfs.File,
Change: zfs.Modified,
ReferenceCountChange: 1,
},
"/test/origin/file": {
Type: zfs.File,
Change: zfs.Renamed,
NewPath: "/test/origin/file-new",
},
"/test/origin/i ❤ unicode": {
Path: "❤❤ unicode ❤❤",
Type: zfs.File,
Change: zfs.Created,
},
unicodePath: {
Path: "❤❤ unicode ❤❤",
Type: zfs.File,
Change: zfs.Created,
},
"/test/origin/": {
Type: zfs.Directory,
Change: zfs.Modified,
},
}
for _, change := range inodeChanges {
want := wants[change.Path]
want.Path = change.Path
delete(wants, change.Path)
equals(t, want, change)
}
equals(t, 1, len(wants))
for _, want := range wants {
equals(t, "❤❤ unicode ❤❤", want.Path)
}
ok(t, movedFile.Close())
ok(t, unicodeFile.Close())
ok(t, linkedFile.Close())
ok(t, snapshot.Destroy(zfs.DestroyForceUmount))
ok(t, fs.Destroy(zfs.DestroyForceUmount))
}