-
Notifications
You must be signed in to change notification settings - Fork 27
/
snapshot_test.go
308 lines (273 loc) · 7.64 KB
/
snapshot_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
/*
* knoxite
* Copyright (c) 2016-2018, Christian Muehlhaeuser <muesli@gmail.com>
* Copyright (c) 2020, Nicolas Martin <penguwin@penguwin.eu>
*
* For license see LICENSE
*/
package knoxite
import (
"encoding/hex"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/minio/highwayhash"
"github.com/muesli/combinator"
)
func hashFile(path string) (string, error) {
var key [32]byte
hasher, err := highwayhash.New(key[:])
if err != nil {
return "", err
}
s, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
_, _ = hasher.Write(s)
return hex.EncodeToString(hasher.Sum(nil)), nil
}
func TestSnapshotCreate(t *testing.T) {
testPassword := "this_is_a_password"
type testOptions struct {
Compression uint16
ParityParts uint
ExcludesStore []string
ExcludesRestore []string
}
testData := struct {
Compression []uint16
ParityParts []uint
ExcludesStore [][]string
ExcludesRestore [][]string
}{
Compression: []uint16{CompressionNone, CompressionFlate, CompressionGZip, CompressionLZMA, CompressionZstd},
ParityParts: []uint{0, 1},
ExcludesStore: [][]string{
{},
{"snapshot.go"},
{"snapshot_test.go"},
{"snapshot.go", "snapshot_test.go"}},
ExcludesRestore: [][]string{
{},
{"snapshot.go"},
{"snapshot_test.go"},
{"snapshot.go", "snapshot_test.go"}},
}
var tests []testOptions
err := combinator.Generate(&tests, testData)
if err != nil {
t.Errorf("Failed to generate all testcases: %v", err)
return
}
for _, tt := range tests {
dir, err := ioutil.TempDir("", "knoxite")
if err != nil {
t.Errorf("Failed creating temporary dir for repository: %s", err)
return
}
defer os.RemoveAll(dir)
var snapshotOriginal *Snapshot
{
r, err := NewRepository(dir, testPassword)
if err != nil {
t.Errorf("Failed creating repository: %s", err)
return
}
vol, err := NewVolume("test_name", "test_description")
if err != nil {
t.Errorf("Failed creating volume: %s", err)
return
}
err = r.AddVolume(vol)
if err != nil {
t.Errorf("Failed creating volume: %s", err)
return
}
snapshot, err := NewSnapshot("test_snapshot")
if err != nil {
t.Errorf("Failed creating snapshot: %s", err)
return
}
index, err := OpenChunkIndex(&r)
if err != nil {
t.Errorf("Failed opening chunk-index: %s", err)
return
}
wd, err := os.Getwd()
if err != nil {
t.Errorf("Failed getting working dir: %s", err)
return
}
opts := StoreOptions{
CWD: wd,
Paths: []string{"snapshot_test.go", "snapshot.go"},
Excludes: tt.ExcludesStore,
Compress: tt.Compression,
Encrypt: EncryptionAES,
DataParts: 1,
Pedantic: false,
ParityParts: tt.ParityParts,
}
progress := snapshot.Add(r, &index, opts)
for p := range progress {
if p.Error != nil {
t.Errorf("Failed adding to snapshot: %s", p.Error)
}
}
err = snapshot.Save(&r)
if err != nil {
t.Errorf("Failed saving snapshot: %s", err)
}
err = vol.AddSnapshot(snapshot.ID)
if err != nil {
t.Errorf("Failed adding snapshot to volume: %s", err)
}
err = r.Save()
if err != nil {
t.Errorf("Failed saving volume: %s", err)
return
}
err = index.Save(&r)
if err != nil {
t.Errorf("Failed saving chunk-index: %s", err)
return
}
// Check if we have excluded all the paths in tt.excludes
for _, v := range snapshot.Archives {
for _, e := range tt.ExcludesStore {
if e == v.Path {
t.Errorf("Failed excluding specified files from snapshot store operation: %s", e)
continue
}
}
}
snapshotOriginal = snapshot
}
{
r, err := OpenRepository(dir, testPassword)
if err != nil {
t.Errorf("Failed opening repository: %s", err)
return
}
_, snapshot, err := r.FindSnapshot(snapshotOriginal.ID)
if err != nil {
t.Errorf("Failed finding snapshot: %s", err)
return
}
if !snapshot.Date.Equal(snapshotOriginal.Date) {
t.Errorf("Failed verifying snapshot date: %v != %v", snapshot.Date, snapshotOriginal.Date)
}
if snapshot.Description != snapshotOriginal.Description {
t.Errorf("Failed verifying snapshot description: %s != %s", snapshot.Description, snapshotOriginal.Description)
}
for i, archive := range snapshotOriginal.Archives {
if archive.Path != snapshot.Archives[i].Path {
t.Errorf("Failed verifying snapshot archive: %s != %s", archive.Path, snapshot.Archives[i].Path)
return
}
if archive.Size != snapshot.Archives[i].Size {
t.Errorf("Failed verifying snapshot archive size: %d != %d", archive.Size, snapshot.Archives[i].Size)
return
}
}
targetdir, err := ioutil.TempDir("", "knoxite.target")
if err != nil {
t.Errorf("Failed creating temporary dir for restore: %s", err)
return
}
defer os.RemoveAll(targetdir)
progress, err := DecodeSnapshot(r, snapshot, targetdir, tt.ExcludesRestore, false)
if err != nil {
t.Errorf("Failed restoring snapshot: %s", err)
return
}
for p := range progress {
if p.Error != nil {
t.Errorf("Failed restoring snapshot: %s", p.Error)
}
}
for i, archive := range snapshot.Archives {
file1 := filepath.Join(targetdir, archive.Path)
// Check if the file is a (successfully) excluded one
isExcluded := false
for _, excl := range tt.ExcludesRestore {
excludedFile := filepath.Join(targetdir, excl)
if _, err := os.Stat(excludedFile); os.IsNotExist(err) {
if excludedFile == file1 {
isExcluded = true
break
}
} else if err != nil { // Some other error occured
t.Errorf("Failed to stat file %s: %v", excl, err)
continue
}
}
if isExcluded {
continue
}
hash1, err := hashFile(file1)
if err != nil {
t.Errorf("Failed generating shasum for %s: %s", file1, err)
return
}
hash2, err := hashFile(snapshotOriginal.Archives[i].Path)
if err != nil {
t.Errorf("Failed generating shasum for %s: %s", snapshotOriginal.Archives[i].Path, err)
return
}
if hash1 != hash2 {
t.Errorf("Failed verifying shasum: %s != %s", hash1, hash2)
return
}
}
}
}
}
func TestSnapshotClone(t *testing.T) {
snapshot, _ := NewSnapshot("test_snapshot")
s, err := snapshot.Clone()
if err != nil || s == nil {
t.Errorf("Failed cloning snapshot: %s", err)
}
if snapshot.ID == s.ID {
t.Errorf("ID conflict after cloning, duplicate snapshot ID %s", snapshot.ID)
}
if snapshot.Description != s.Description {
t.Errorf("Description mismatch, got %s expected %s", s.Description, snapshot.Description)
}
for i, archive := range snapshot.Archives {
if archive.Path != s.Archives[i].Path {
t.Errorf("Failed verifying snapshot archive: %s != %s", archive.Path, s.Archives[i].Path)
return
}
if archive.Size != snapshot.Archives[i].Size {
t.Errorf("Failed verifying snapshot archive size: %d != %d", archive.Size, s.Archives[i].Size)
return
}
}
}
func TestSnapshotFind(t *testing.T) {
testPassword := "this_is_a_password"
dir, err := ioutil.TempDir("", "knoxite")
if err != nil {
t.Errorf("Failed creating temporary dir for repository: %s", err)
return
}
defer os.RemoveAll(dir)
r, _ := NewRepository(dir, testPassword)
vol, _ := NewVolume("test", "")
_ = r.AddVolume(vol)
_, _, err = r.FindSnapshot("invalidID")
if err != ErrSnapshotNotFound {
t.Errorf("Expected %v, got %v", ErrSnapshotNotFound, err)
}
snapshot, _ := NewSnapshot("test_snapshot")
_ = snapshot.Save(&r)
_ = vol.AddSnapshot(snapshot.ID)
_, s, err := r.FindSnapshot("latest")
if err != nil || s == nil {
t.Errorf("Failed finding latest snapshot: %s %s", err, snapshot.ID)
}
}