8554: improved tests
[arvados.git] / services / keepstore / volume_generic_test.go
1 package main
2
3 import (
4         "bytes"
5         "crypto/md5"
6         "fmt"
7         "os"
8         "regexp"
9         "sort"
10         "strings"
11         "time"
12
13         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
14 )
15
16 type TB interface {
17         Error(args ...interface{})
18         Errorf(format string, args ...interface{})
19         Fail()
20         FailNow()
21         Failed() bool
22         Fatal(args ...interface{})
23         Fatalf(format string, args ...interface{})
24         Log(args ...interface{})
25         Logf(format string, args ...interface{})
26 }
27
28 // A TestableVolumeFactory returns a new TestableVolume. The factory
29 // function, and the TestableVolume it returns, can use "t" to write
30 // logs, fail the current test, etc.
31 type TestableVolumeFactory func(t TB) TestableVolume
32
33 // DoGenericVolumeTests runs a set of tests that every TestableVolume
34 // is expected to pass. It calls factory to create a new TestableVolume
35 // for each test case, to avoid leaking state between tests.
36 func DoGenericVolumeTests(t TB, factory TestableVolumeFactory) {
37         testGet(t, factory)
38         testGetNoSuchBlock(t, factory)
39
40         testCompareNonexistent(t, factory)
41         testCompareSameContent(t, factory, TestHash, TestBlock)
42         testCompareSameContent(t, factory, EmptyHash, EmptyBlock)
43         testCompareWithCollision(t, factory, TestHash, TestBlock, []byte("baddata"))
44         testCompareWithCollision(t, factory, TestHash, TestBlock, EmptyBlock)
45         testCompareWithCollision(t, factory, EmptyHash, EmptyBlock, TestBlock)
46         testCompareWithCorruptStoredData(t, factory, TestHash, TestBlock, []byte("baddata"))
47         testCompareWithCorruptStoredData(t, factory, TestHash, TestBlock, EmptyBlock)
48         testCompareWithCorruptStoredData(t, factory, EmptyHash, EmptyBlock, []byte("baddata"))
49
50         testPutBlockWithSameContent(t, factory, TestHash, TestBlock)
51         testPutBlockWithSameContent(t, factory, EmptyHash, EmptyBlock)
52         testPutBlockWithDifferentContent(t, factory, arvadostest.MD5CollisionMD5, arvadostest.MD5CollisionData[0], arvadostest.MD5CollisionData[1])
53         testPutBlockWithDifferentContent(t, factory, arvadostest.MD5CollisionMD5, EmptyBlock, arvadostest.MD5CollisionData[0])
54         testPutBlockWithDifferentContent(t, factory, arvadostest.MD5CollisionMD5, arvadostest.MD5CollisionData[0], EmptyBlock)
55         testPutBlockWithDifferentContent(t, factory, EmptyHash, EmptyBlock, arvadostest.MD5CollisionData[0])
56         testPutMultipleBlocks(t, factory)
57
58         testPutAndTouch(t, factory)
59         testTouchNoSuchBlock(t, factory)
60
61         testMtimeNoSuchBlock(t, factory)
62
63         testIndexTo(t, factory)
64
65         testDeleteNewBlock(t, factory)
66         testDeleteOldBlock(t, factory)
67         testDeleteNoSuchBlock(t, factory)
68
69         testStatus(t, factory)
70
71         testString(t, factory)
72
73         testUpdateReadOnly(t, factory)
74
75         testGetConcurrent(t, factory)
76         testPutConcurrent(t, factory)
77
78         testPutFullBlock(t, factory)
79
80         testTrashUntrash(t, factory)
81         testTrashEmptyTrashUntrash(t, factory)
82         testTrashUntrashWithEmptyTrashGoroutine(t, factory)
83 }
84
85 // Put a test block, get it and verify content
86 // Test should pass for both writable and read-only volumes
87 func testGet(t TB, factory TestableVolumeFactory) {
88         v := factory(t)
89         defer v.Teardown()
90
91         v.PutRaw(TestHash, TestBlock)
92
93         buf, err := v.Get(TestHash)
94         if err != nil {
95                 t.Fatal(err)
96         }
97
98         bufs.Put(buf)
99
100         if bytes.Compare(buf, TestBlock) != 0 {
101                 t.Errorf("expected %s, got %s", string(TestBlock), string(buf))
102         }
103 }
104
105 // Invoke get on a block that does not exist in volume; should result in error
106 // Test should pass for both writable and read-only volumes
107 func testGetNoSuchBlock(t TB, factory TestableVolumeFactory) {
108         v := factory(t)
109         defer v.Teardown()
110
111         if _, err := v.Get(TestHash2); err == nil {
112                 t.Errorf("Expected error while getting non-existing block %v", TestHash2)
113         }
114 }
115
116 // Compare() should return os.ErrNotExist if the block does not exist.
117 // Otherwise, writing new data causes CompareAndTouch() to generate
118 // error logs even though everything is working fine.
119 func testCompareNonexistent(t TB, factory TestableVolumeFactory) {
120         v := factory(t)
121         defer v.Teardown()
122
123         err := v.Compare(TestHash, TestBlock)
124         if err != os.ErrNotExist {
125                 t.Errorf("Got err %T %q, expected os.ErrNotExist", err, err)
126         }
127 }
128
129 // Put a test block and compare the locator with same content
130 // Test should pass for both writable and read-only volumes
131 func testCompareSameContent(t TB, factory TestableVolumeFactory, testHash string, testData []byte) {
132         v := factory(t)
133         defer v.Teardown()
134
135         v.PutRaw(testHash, testData)
136
137         // Compare the block locator with same content
138         err := v.Compare(testHash, testData)
139         if err != nil {
140                 t.Errorf("Got err %q, expected nil", err)
141         }
142 }
143
144 // Test behavior of Compare() when stored data matches expected
145 // checksum but differs from new data we need to store. Requires
146 // testHash = md5(testDataA).
147 //
148 // Test should pass for both writable and read-only volumes
149 func testCompareWithCollision(t TB, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
150         v := factory(t)
151         defer v.Teardown()
152
153         v.PutRaw(testHash, testDataA)
154
155         // Compare the block locator with different content; collision
156         err := v.Compare(TestHash, testDataB)
157         if err == nil {
158                 t.Errorf("Got err nil, expected error due to collision")
159         }
160 }
161
162 // Test behavior of Compare() when stored data has become
163 // corrupted. Requires testHash = md5(testDataA) != md5(testDataB).
164 //
165 // Test should pass for both writable and read-only volumes
166 func testCompareWithCorruptStoredData(t TB, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
167         v := factory(t)
168         defer v.Teardown()
169
170         v.PutRaw(TestHash, testDataB)
171
172         err := v.Compare(testHash, testDataA)
173         if err == nil || err == CollisionError {
174                 t.Errorf("Got err %+v, expected non-collision error", err)
175         }
176 }
177
178 // Put a block and put again with same content
179 // Test is intended for only writable volumes
180 func testPutBlockWithSameContent(t TB, factory TestableVolumeFactory, testHash string, testData []byte) {
181         v := factory(t)
182         defer v.Teardown()
183
184         if v.Writable() == false {
185                 return
186         }
187
188         err := v.Put(testHash, testData)
189         if err != nil {
190                 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
191         }
192
193         err = v.Put(testHash, testData)
194         if err != nil {
195                 t.Errorf("Got err putting block second time %q: %q, expected nil", TestBlock, err)
196         }
197 }
198
199 // Put a block and put again with different content
200 // Test is intended for only writable volumes
201 func testPutBlockWithDifferentContent(t TB, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
202         v := factory(t)
203         defer v.Teardown()
204
205         if v.Writable() == false {
206                 return
207         }
208
209         v.PutRaw(testHash, testDataA)
210
211         putErr := v.Put(testHash, testDataB)
212         buf, getErr := v.Get(testHash)
213         if putErr == nil {
214                 // Put must not return a nil error unless it has
215                 // overwritten the existing data.
216                 if bytes.Compare(buf, testDataB) != 0 {
217                         t.Errorf("Put succeeded but Get returned %+q, expected %+q", buf, testDataB)
218                 }
219         } else {
220                 // It is permissible for Put to fail, but it must
221                 // leave us with either the original data, the new
222                 // data, or nothing at all.
223                 if getErr == nil && bytes.Compare(buf, testDataA) != 0 && bytes.Compare(buf, testDataB) != 0 {
224                         t.Errorf("Put failed but Get returned %+q, which is neither %+q nor %+q", buf, testDataA, testDataB)
225                 }
226         }
227         if getErr == nil {
228                 bufs.Put(buf)
229         }
230 }
231
232 // Put and get multiple blocks
233 // Test is intended for only writable volumes
234 func testPutMultipleBlocks(t TB, factory TestableVolumeFactory) {
235         v := factory(t)
236         defer v.Teardown()
237
238         if v.Writable() == false {
239                 return
240         }
241
242         err := v.Put(TestHash, TestBlock)
243         if err != nil {
244                 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
245         }
246
247         err = v.Put(TestHash2, TestBlock2)
248         if err != nil {
249                 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock2, err)
250         }
251
252         err = v.Put(TestHash3, TestBlock3)
253         if err != nil {
254                 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock3, err)
255         }
256
257         data, err := v.Get(TestHash)
258         if err != nil {
259                 t.Error(err)
260         } else {
261                 if bytes.Compare(data, TestBlock) != 0 {
262                         t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock)
263                 }
264                 bufs.Put(data)
265         }
266
267         data, err = v.Get(TestHash2)
268         if err != nil {
269                 t.Error(err)
270         } else {
271                 if bytes.Compare(data, TestBlock2) != 0 {
272                         t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock2)
273                 }
274                 bufs.Put(data)
275         }
276
277         data, err = v.Get(TestHash3)
278         if err != nil {
279                 t.Error(err)
280         } else {
281                 if bytes.Compare(data, TestBlock3) != 0 {
282                         t.Errorf("Block present, but to %+q, expected %+q", data, TestBlock3)
283                 }
284                 bufs.Put(data)
285         }
286 }
287
288 // testPutAndTouch
289 //   Test that when applying PUT to a block that already exists,
290 //   the block's modification time is updated.
291 // Test is intended for only writable volumes
292 func testPutAndTouch(t TB, factory TestableVolumeFactory) {
293         v := factory(t)
294         defer v.Teardown()
295
296         if v.Writable() == false {
297                 return
298         }
299
300         if err := v.Put(TestHash, TestBlock); err != nil {
301                 t.Error(err)
302         }
303
304         // We'll verify { t0 < threshold < t1 }, where t0 is the
305         // existing block's timestamp on disk before Put() and t1 is
306         // its timestamp after Put().
307         threshold := time.Now().Add(-time.Second)
308
309         // Set the stored block's mtime far enough in the past that we
310         // can see the difference between "timestamp didn't change"
311         // and "timestamp granularity is too low".
312         v.TouchWithDate(TestHash, time.Now().Add(-20*time.Second))
313
314         // Make sure v.Mtime() agrees the above Utime really worked.
315         if t0, err := v.Mtime(TestHash); err != nil || t0.IsZero() || !t0.Before(threshold) {
316                 t.Errorf("Setting mtime failed: %v, %v", t0, err)
317         }
318
319         // Write the same block again.
320         if err := v.Put(TestHash, TestBlock); err != nil {
321                 t.Error(err)
322         }
323
324         // Verify threshold < t1
325         if t1, err := v.Mtime(TestHash); err != nil {
326                 t.Error(err)
327         } else if t1.Before(threshold) {
328                 t.Errorf("t1 %v should be >= threshold %v after v.Put ", t1, threshold)
329         }
330 }
331
332 // Touching a non-existing block should result in error.
333 // Test should pass for both writable and read-only volumes
334 func testTouchNoSuchBlock(t TB, factory TestableVolumeFactory) {
335         v := factory(t)
336         defer v.Teardown()
337
338         if err := v.Touch(TestHash); err == nil {
339                 t.Error("Expected error when attempted to touch a non-existing block")
340         }
341 }
342
343 // Invoking Mtime on a non-existing block should result in error.
344 // Test should pass for both writable and read-only volumes
345 func testMtimeNoSuchBlock(t TB, factory TestableVolumeFactory) {
346         v := factory(t)
347         defer v.Teardown()
348
349         if _, err := v.Mtime("12345678901234567890123456789012"); err == nil {
350                 t.Error("Expected error when updating Mtime on a non-existing block")
351         }
352 }
353
354 // Put a few blocks and invoke IndexTo with:
355 // * no prefix
356 // * with a prefix
357 // * with no such prefix
358 // Test should pass for both writable and read-only volumes
359 func testIndexTo(t TB, factory TestableVolumeFactory) {
360         v := factory(t)
361         defer v.Teardown()
362
363         v.PutRaw(TestHash, TestBlock)
364         v.PutRaw(TestHash2, TestBlock2)
365         v.PutRaw(TestHash3, TestBlock3)
366
367         // Blocks whose names aren't Keep hashes should be omitted from
368         // index
369         v.PutRaw("fffffffffnotreallyahashfffffffff", nil)
370         v.PutRaw("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", nil)
371         v.PutRaw("f0000000000000000000000000000000f", nil)
372         v.PutRaw("f00", nil)
373
374         buf := new(bytes.Buffer)
375         v.IndexTo("", buf)
376         indexRows := strings.Split(string(buf.Bytes()), "\n")
377         sort.Strings(indexRows)
378         sortedIndex := strings.Join(indexRows, "\n")
379         m, err := regexp.MatchString(
380                 `^\n`+TestHash+`\+\d+ \d+\n`+
381                         TestHash3+`\+\d+ \d+\n`+
382                         TestHash2+`\+\d+ \d+$`,
383                 sortedIndex)
384         if err != nil {
385                 t.Error(err)
386         } else if !m {
387                 t.Errorf("Got index %q for empty prefix", sortedIndex)
388         }
389
390         for _, prefix := range []string{"f", "f15", "f15ac"} {
391                 buf = new(bytes.Buffer)
392                 v.IndexTo(prefix, buf)
393
394                 m, err := regexp.MatchString(`^`+TestHash2+`\+\d+ \d+\n$`, string(buf.Bytes()))
395                 if err != nil {
396                         t.Error(err)
397                 } else if !m {
398                         t.Errorf("Got index %q for prefix %s", string(buf.Bytes()), prefix)
399                 }
400         }
401
402         for _, prefix := range []string{"zero", "zip", "zilch"} {
403                 buf = new(bytes.Buffer)
404                 v.IndexTo(prefix, buf)
405                 if err != nil {
406                         t.Errorf("Got error on IndexTo with no such prefix %v", err.Error())
407                 } else if buf.Len() != 0 {
408                         t.Errorf("Expected empty list for IndexTo with no such prefix %s", prefix)
409                 }
410         }
411 }
412
413 // Calling Delete() for a block immediately after writing it (not old enough)
414 // should neither delete the data nor return an error.
415 // Test is intended for only writable volumes
416 func testDeleteNewBlock(t TB, factory TestableVolumeFactory) {
417         v := factory(t)
418         defer v.Teardown()
419         blobSignatureTTL = 300 * time.Second
420
421         if v.Writable() == false {
422                 return
423         }
424
425         v.Put(TestHash, TestBlock)
426
427         if err := v.Trash(TestHash); err != nil {
428                 t.Error(err)
429         }
430         data, err := v.Get(TestHash)
431         if err != nil {
432                 t.Error(err)
433         } else {
434                 if bytes.Compare(data, TestBlock) != 0 {
435                         t.Errorf("Got data %+q, expected %+q", data, TestBlock)
436                 }
437                 bufs.Put(data)
438         }
439 }
440
441 // Calling Delete() for a block with a timestamp older than
442 // blobSignatureTTL seconds in the past should delete the data.
443 // Test is intended for only writable volumes
444 func testDeleteOldBlock(t TB, factory TestableVolumeFactory) {
445         v := factory(t)
446         defer v.Teardown()
447         blobSignatureTTL = 300 * time.Second
448
449         if v.Writable() == false {
450                 return
451         }
452
453         v.Put(TestHash, TestBlock)
454         v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
455
456         if err := v.Trash(TestHash); err != nil {
457                 t.Error(err)
458         }
459         if _, err := v.Get(TestHash); err == nil || !os.IsNotExist(err) {
460                 t.Errorf("os.IsNotExist(%v) should have been true", err)
461         }
462 }
463
464 // Calling Delete() for a block that does not exist should result in error.
465 // Test should pass for both writable and read-only volumes
466 func testDeleteNoSuchBlock(t TB, factory TestableVolumeFactory) {
467         v := factory(t)
468         defer v.Teardown()
469
470         if err := v.Trash(TestHash2); err == nil {
471                 t.Errorf("Expected error when attempting to delete a non-existing block")
472         }
473 }
474
475 // Invoke Status and verify that VolumeStatus is returned
476 // Test should pass for both writable and read-only volumes
477 func testStatus(t TB, factory TestableVolumeFactory) {
478         v := factory(t)
479         defer v.Teardown()
480
481         // Get node status and make a basic sanity check.
482         status := v.Status()
483         if status.DeviceNum == 0 {
484                 t.Errorf("uninitialized device_num in %v", status)
485         }
486
487         if status.BytesFree == 0 {
488                 t.Errorf("uninitialized bytes_free in %v", status)
489         }
490
491         if status.BytesUsed == 0 {
492                 t.Errorf("uninitialized bytes_used in %v", status)
493         }
494 }
495
496 // Invoke String for the volume; expect non-empty result
497 // Test should pass for both writable and read-only volumes
498 func testString(t TB, factory TestableVolumeFactory) {
499         v := factory(t)
500         defer v.Teardown()
501
502         if id := v.String(); len(id) == 0 {
503                 t.Error("Got empty string for v.String()")
504         }
505 }
506
507 // Putting, updating, touching, and deleting blocks from a read-only volume result in error.
508 // Test is intended for only read-only volumes
509 func testUpdateReadOnly(t TB, factory TestableVolumeFactory) {
510         v := factory(t)
511         defer v.Teardown()
512
513         if v.Writable() == true {
514                 return
515         }
516
517         v.PutRaw(TestHash, TestBlock)
518
519         // Get from read-only volume should succeed
520         _, err := v.Get(TestHash)
521         if err != nil {
522                 t.Errorf("got err %v, expected nil", err)
523         }
524
525         // Put a new block to read-only volume should result in error
526         err = v.Put(TestHash2, TestBlock2)
527         if err == nil {
528                 t.Errorf("Expected error when putting block in a read-only volume")
529         }
530         _, err = v.Get(TestHash2)
531         if err == nil {
532                 t.Errorf("Expected error when getting block whose put in read-only volume failed")
533         }
534
535         // Touch a block in read-only volume should result in error
536         err = v.Touch(TestHash)
537         if err == nil {
538                 t.Errorf("Expected error when touching block in a read-only volume")
539         }
540
541         // Delete a block from a read-only volume should result in error
542         err = v.Trash(TestHash)
543         if err == nil {
544                 t.Errorf("Expected error when deleting block from a read-only volume")
545         }
546
547         // Overwriting an existing block in read-only volume should result in error
548         err = v.Put(TestHash, TestBlock)
549         if err == nil {
550                 t.Errorf("Expected error when putting block in a read-only volume")
551         }
552 }
553
554 // Launch concurrent Gets
555 // Test should pass for both writable and read-only volumes
556 func testGetConcurrent(t TB, factory TestableVolumeFactory) {
557         v := factory(t)
558         defer v.Teardown()
559
560         v.PutRaw(TestHash, TestBlock)
561         v.PutRaw(TestHash2, TestBlock2)
562         v.PutRaw(TestHash3, TestBlock3)
563
564         sem := make(chan int)
565         go func(sem chan int) {
566                 buf, err := v.Get(TestHash)
567                 if err != nil {
568                         t.Errorf("err1: %v", err)
569                 }
570                 bufs.Put(buf)
571                 if bytes.Compare(buf, TestBlock) != 0 {
572                         t.Errorf("buf should be %s, is %s", string(TestBlock), string(buf))
573                 }
574                 sem <- 1
575         }(sem)
576
577         go func(sem chan int) {
578                 buf, err := v.Get(TestHash2)
579                 if err != nil {
580                         t.Errorf("err2: %v", err)
581                 }
582                 bufs.Put(buf)
583                 if bytes.Compare(buf, TestBlock2) != 0 {
584                         t.Errorf("buf should be %s, is %s", string(TestBlock2), string(buf))
585                 }
586                 sem <- 1
587         }(sem)
588
589         go func(sem chan int) {
590                 buf, err := v.Get(TestHash3)
591                 if err != nil {
592                         t.Errorf("err3: %v", err)
593                 }
594                 bufs.Put(buf)
595                 if bytes.Compare(buf, TestBlock3) != 0 {
596                         t.Errorf("buf should be %s, is %s", string(TestBlock3), string(buf))
597                 }
598                 sem <- 1
599         }(sem)
600
601         // Wait for all goroutines to finish
602         for done := 0; done < 3; {
603                 done += <-sem
604         }
605 }
606
607 // Launch concurrent Puts
608 // Test is intended for only writable volumes
609 func testPutConcurrent(t TB, factory TestableVolumeFactory) {
610         v := factory(t)
611         defer v.Teardown()
612
613         if v.Writable() == false {
614                 return
615         }
616
617         sem := make(chan int)
618         go func(sem chan int) {
619                 err := v.Put(TestHash, TestBlock)
620                 if err != nil {
621                         t.Errorf("err1: %v", err)
622                 }
623                 sem <- 1
624         }(sem)
625
626         go func(sem chan int) {
627                 err := v.Put(TestHash2, TestBlock2)
628                 if err != nil {
629                         t.Errorf("err2: %v", err)
630                 }
631                 sem <- 1
632         }(sem)
633
634         go func(sem chan int) {
635                 err := v.Put(TestHash3, TestBlock3)
636                 if err != nil {
637                         t.Errorf("err3: %v", err)
638                 }
639                 sem <- 1
640         }(sem)
641
642         // Wait for all goroutines to finish
643         for done := 0; done < 3; {
644                 done += <-sem
645         }
646
647         // Double check that we actually wrote the blocks we expected to write.
648         buf, err := v.Get(TestHash)
649         if err != nil {
650                 t.Errorf("Get #1: %v", err)
651         }
652         bufs.Put(buf)
653         if bytes.Compare(buf, TestBlock) != 0 {
654                 t.Errorf("Get #1: expected %s, got %s", string(TestBlock), string(buf))
655         }
656
657         buf, err = v.Get(TestHash2)
658         if err != nil {
659                 t.Errorf("Get #2: %v", err)
660         }
661         bufs.Put(buf)
662         if bytes.Compare(buf, TestBlock2) != 0 {
663                 t.Errorf("Get #2: expected %s, got %s", string(TestBlock2), string(buf))
664         }
665
666         buf, err = v.Get(TestHash3)
667         if err != nil {
668                 t.Errorf("Get #3: %v", err)
669         }
670         bufs.Put(buf)
671         if bytes.Compare(buf, TestBlock3) != 0 {
672                 t.Errorf("Get #3: expected %s, got %s", string(TestBlock3), string(buf))
673         }
674 }
675
676 // Write and read back a full size block
677 func testPutFullBlock(t TB, factory TestableVolumeFactory) {
678         v := factory(t)
679         defer v.Teardown()
680
681         if !v.Writable() {
682                 return
683         }
684
685         wdata := make([]byte, BlockSize)
686         wdata[0] = 'a'
687         wdata[BlockSize-1] = 'z'
688         hash := fmt.Sprintf("%x", md5.Sum(wdata))
689         err := v.Put(hash, wdata)
690         if err != nil {
691                 t.Fatal(err)
692         }
693         rdata, err := v.Get(hash)
694         if err != nil {
695                 t.Error(err)
696         } else {
697                 defer bufs.Put(rdata)
698         }
699         if bytes.Compare(rdata, wdata) != 0 {
700                 t.Error("rdata != wdata")
701         }
702 }
703
704 // With trashLifetime != 0, perform:
705 // Trash an old block - which either raises ErrNotImplemented or succeeds
706 // Untrash -  which either raises ErrNotImplemented or succeeds
707 // Get - which must succeed
708 func testTrashUntrash(t TB, factory TestableVolumeFactory) {
709         v := factory(t)
710         defer v.Teardown()
711         defer func() {
712                 trashLifetime = 0 * time.Second
713         }()
714
715         trashLifetime = 3600 * time.Second
716
717         // put block and backdate it
718         v.PutRaw(TestHash, TestBlock)
719         v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
720
721         buf, err := v.Get(TestHash)
722         if err != nil {
723                 t.Fatal(err)
724         }
725         if bytes.Compare(buf, TestBlock) != 0 {
726                 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
727         }
728         bufs.Put(buf)
729
730         // Trash
731         err = v.Trash(TestHash)
732         if v.Writable() == false {
733                 if err != MethodDisabledError {
734                         t.Error(err)
735                 }
736         } else if err != nil {
737                 if err != ErrNotImplemented {
738                         t.Error(err)
739                 }
740         } else {
741                 _, err = v.Get(TestHash)
742                 if err == nil || !os.IsNotExist(err) {
743                         t.Errorf("os.IsNotExist(%v) should have been true", err)
744                 }
745
746                 // Untrash
747                 err = v.Untrash(TestHash)
748                 if err != nil {
749                         t.Fatal(err)
750                 }
751         }
752
753         // Get the block - after trash and untrash sequence
754         buf, err = v.Get(TestHash)
755         if err != nil {
756                 t.Fatal(err)
757         }
758         if bytes.Compare(buf, TestBlock) != 0 {
759                 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
760         }
761         bufs.Put(buf)
762 }
763
764 func testTrashEmptyTrashUntrash(t TB, factory TestableVolumeFactory) {
765         v := factory(t)
766         defer v.Teardown()
767         defer func() {
768                 trashLifetime = 0 * time.Second
769         }()
770
771         // With trashLifetime = 1h, test trash/untrash cycle.
772         trashLifetime = 1 * time.Hour
773
774         // put block and backdate it
775         v.PutRaw(TestHash, TestBlock)
776         v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
777
778         buf, err := v.Get(TestHash)
779         if err != nil {
780                 t.Fatal(err)
781         }
782         if bytes.Compare(buf, TestBlock) != 0 {
783                 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
784         }
785         bufs.Put(buf)
786
787         // Trash it
788         err = v.Trash(TestHash)
789         if err == MethodDisabledError || err == ErrNotImplemented {
790                 return
791         }
792         buf, err = v.Get(TestHash)
793         if err == nil || !os.IsNotExist(err) {
794                 t.Errorf("os.IsNotExist(%v) should have been true", err)
795         }
796
797         // Empty trash; the block is still within trashLifetime and hence is not emptied
798         v.EmptyTrash()
799
800         // Untrash will hence rescue it
801         err = v.Untrash(TestHash)
802         if err != nil {
803                 t.Fatal(err)
804         }
805
806         // Get block will find it
807         buf, err = v.Get(TestHash)
808         if err != nil {
809                 t.Fatal(err)
810         }
811         if bytes.Compare(buf, TestBlock) != 0 {
812                 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
813         }
814         bufs.Put(buf)
815
816         // With trashLifetime = 1ns, test trash/untrash cycle.
817         trashLifetime = 1 * time.Nanosecond
818
819         // Trash it
820         err = v.Trash(TestHash)
821         if err != nil {
822                 t.Fatal(err)
823         }
824         buf, err = v.Get(TestHash)
825         if err == nil || !os.IsNotExist(err) {
826                 t.Errorf("os.IsNotExist(%v) should have been true", err)
827         }
828
829         // Untrash
830         err = v.Untrash(TestHash)
831         if err != nil {
832                 t.Fatal(err)
833         }
834
835         // Get block will find it
836         buf, err = v.Get(TestHash)
837         if err != nil {
838                 t.Fatal(err)
839         }
840         if bytes.Compare(buf, TestBlock) != 0 {
841                 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
842         }
843         bufs.Put(buf)
844
845         // Trash it again
846         err = v.Trash(TestHash)
847         if err == MethodDisabledError || err == ErrNotImplemented {
848                 return
849         }
850         buf, err = v.Get(TestHash)
851         if err == nil || !os.IsNotExist(err) {
852                 t.Errorf("os.IsNotExist(%v) should have been true", err)
853         }
854
855         // Empty trash will empty it
856         v.EmptyTrash()
857
858         // Untrash won't find it
859         err = v.Untrash(TestHash)
860         if err == nil || !os.IsNotExist(err) {
861                 t.Errorf("os.IsNotExist(%v) should have been true", err)
862         }
863
864         // Get block won't find it
865         buf, err = v.Get(TestHash)
866         if err == nil || !os.IsNotExist(err) {
867                 t.Errorf("os.IsNotExist(%v) should have been true", err)
868         }
869
870         // Still with trashLifetime = 1ns: put, trash, put one more, trash etc
871         // put block and backdate it
872         v.PutRaw(TestHash, TestBlock)
873         v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
874
875         // Trash
876         err = v.Trash(TestHash)
877         if err == MethodDisabledError || err == ErrNotImplemented {
878                 return
879         }
880         buf, err = v.Get(TestHash)
881         if err == nil || !os.IsNotExist(err) {
882                 t.Errorf("os.IsNotExist(%v) should have been true", err)
883         }
884
885         // put again
886         err = v.Put(TestHash, TestBlock)
887         if err != nil {
888                 t.Fatal(err)
889         }
890         v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
891
892         // Empty trash will empty the trashed block but the second one is untouched
893         v.EmptyTrash()
894
895         // Get block should work because of the second block
896         buf, err = v.Get(TestHash)
897         if err != nil {
898                 t.Fatal(err)
899         }
900         if bytes.Compare(buf, TestBlock) != 0 {
901                 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
902         }
903         bufs.Put(buf)
904
905         // set trashLifetime to one hour
906         trashLifetime = 1 * time.Hour
907
908         // trash block
909         err = v.Trash(TestHash)
910         if err != nil {
911                 t.Fatal(err)
912         }
913
914         // Empty trash won't empty this second block which is still within trashLifetime
915         v.EmptyTrash()
916
917         // Untrash; the second block which is still within trashLifetime will be rescued
918         err = v.Untrash(TestHash)
919         if err != nil {
920                 t.Fatal(err)
921         }
922
923         // Get block should work because of the second block
924         buf, err = v.Get(TestHash)
925         if err != nil {
926                 t.Fatal(err)
927         }
928         if bytes.Compare(buf, TestBlock) != 0 {
929                 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
930         }
931         bufs.Put(buf)
932 }
933
934 // With trashLifetime = 1ns, perform:
935 // Run emptyTrash goroutine
936 // Trash an old block - which either raises ErrNotImplemented or succeeds
937 // Untrash - after emptyTrash goroutine ticks, and hence does not actually untrash
938 // Get - which must fail to find the block
939 func testTrashUntrashWithEmptyTrashGoroutine(t TB, factory TestableVolumeFactory) {
940         v := factory(t)
941         defer v.Teardown()
942
943         doneEmptyingTrash := make(chan bool)
944         defer func() {
945                 trashLifetime = 0 * time.Second
946                 doneEmptyingTrash <- true
947         }()
948
949         volumes = append(volumes, v)
950
951         trashLifetime = 1 * time.Nanosecond
952         trashCheckInterval = 1 * time.Nanosecond
953
954         go emptyTrash(doneEmptyingTrash, trashCheckInterval)
955
956         // Trash old block and untrash a little after first trashCheckInterval
957         v.PutRaw(TestHash, TestBlock)
958         v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
959
960         buf, err := v.Get(TestHash)
961         if err != nil {
962                 t.Fatal(err)
963         }
964         if bytes.Compare(buf, TestBlock) != 0 {
965                 t.Fatalf("Got data %+q, expected %+q", buf, TestBlock)
966         }
967         bufs.Put(buf)
968
969         // Trash
970         err = v.Trash(TestHash)
971         if err == MethodDisabledError || err == ErrNotImplemented {
972                 return
973         }
974
975         _, err = v.Get(TestHash)
976         if err == nil || !os.IsNotExist(err) {
977                 t.Fatalf("os.IsNotExist(%v) should have been true", err)
978         }
979
980         time.Sleep(2 * time.Nanosecond)
981
982         // Untrash
983         err = v.Untrash(TestHash)
984         if err != nil {
985                 t.Fatal(err)
986         }
987
988         // Get is expected to fail due to EmptyTrash before Untrash
989         // It is still found on readonly volumes
990         buf, err = v.Get(TestHash)
991         if err != nil {
992                 if !os.IsNotExist(err) {
993                         t.Errorf("os.IsNotExist(%v) should have been true", err)
994                 }
995         } else {
996                 if bytes.Compare(buf, TestBlock) != 0 {
997                         t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
998                 }
999                 bufs.Put(buf)
1000         }
1001 }