Merge branch 'master' into 8183-projects-dropdown
[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 }
82
83 // Put a test block, get it and verify content
84 // Test should pass for both writable and read-only volumes
85 func testGet(t TB, factory TestableVolumeFactory) {
86         v := factory(t)
87         defer v.Teardown()
88
89         v.PutRaw(TestHash, TestBlock)
90
91         buf, err := v.Get(TestHash)
92         if err != nil {
93                 t.Fatal(err)
94         }
95
96         bufs.Put(buf)
97
98         if bytes.Compare(buf, TestBlock) != 0 {
99                 t.Errorf("expected %s, got %s", string(TestBlock), string(buf))
100         }
101 }
102
103 // Invoke get on a block that does not exist in volume; should result in error
104 // Test should pass for both writable and read-only volumes
105 func testGetNoSuchBlock(t TB, factory TestableVolumeFactory) {
106         v := factory(t)
107         defer v.Teardown()
108
109         if _, err := v.Get(TestHash2); err == nil {
110                 t.Errorf("Expected error while getting non-existing block %v", TestHash2)
111         }
112 }
113
114 // Compare() should return os.ErrNotExist if the block does not exist.
115 // Otherwise, writing new data causes CompareAndTouch() to generate
116 // error logs even though everything is working fine.
117 func testCompareNonexistent(t TB, factory TestableVolumeFactory) {
118         v := factory(t)
119         defer v.Teardown()
120
121         err := v.Compare(TestHash, TestBlock)
122         if err != os.ErrNotExist {
123                 t.Errorf("Got err %T %q, expected os.ErrNotExist", err, err)
124         }
125 }
126
127 // Put a test block and compare the locator with same content
128 // Test should pass for both writable and read-only volumes
129 func testCompareSameContent(t TB, factory TestableVolumeFactory, testHash string, testData []byte) {
130         v := factory(t)
131         defer v.Teardown()
132
133         v.PutRaw(testHash, testData)
134
135         // Compare the block locator with same content
136         err := v.Compare(testHash, testData)
137         if err != nil {
138                 t.Errorf("Got err %q, expected nil", err)
139         }
140 }
141
142 // Test behavior of Compare() when stored data matches expected
143 // checksum but differs from new data we need to store. Requires
144 // testHash = md5(testDataA).
145 //
146 // Test should pass for both writable and read-only volumes
147 func testCompareWithCollision(t TB, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
148         v := factory(t)
149         defer v.Teardown()
150
151         v.PutRaw(testHash, testDataA)
152
153         // Compare the block locator with different content; collision
154         err := v.Compare(TestHash, testDataB)
155         if err == nil {
156                 t.Errorf("Got err nil, expected error due to collision")
157         }
158 }
159
160 // Test behavior of Compare() when stored data has become
161 // corrupted. Requires testHash = md5(testDataA) != md5(testDataB).
162 //
163 // Test should pass for both writable and read-only volumes
164 func testCompareWithCorruptStoredData(t TB, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
165         v := factory(t)
166         defer v.Teardown()
167
168         v.PutRaw(TestHash, testDataB)
169
170         err := v.Compare(testHash, testDataA)
171         if err == nil || err == CollisionError {
172                 t.Errorf("Got err %+v, expected non-collision error", err)
173         }
174 }
175
176 // Put a block and put again with same content
177 // Test is intended for only writable volumes
178 func testPutBlockWithSameContent(t TB, factory TestableVolumeFactory, testHash string, testData []byte) {
179         v := factory(t)
180         defer v.Teardown()
181
182         if v.Writable() == false {
183                 return
184         }
185
186         err := v.Put(testHash, testData)
187         if err != nil {
188                 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
189         }
190
191         err = v.Put(testHash, testData)
192         if err != nil {
193                 t.Errorf("Got err putting block second time %q: %q, expected nil", TestBlock, err)
194         }
195 }
196
197 // Put a block and put again with different content
198 // Test is intended for only writable volumes
199 func testPutBlockWithDifferentContent(t TB, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
200         v := factory(t)
201         defer v.Teardown()
202
203         if v.Writable() == false {
204                 return
205         }
206
207         v.PutRaw(testHash, testDataA)
208
209         putErr := v.Put(testHash, testDataB)
210         buf, getErr := v.Get(testHash)
211         if putErr == nil {
212                 // Put must not return a nil error unless it has
213                 // overwritten the existing data.
214                 if bytes.Compare(buf, testDataB) != 0 {
215                         t.Errorf("Put succeeded but Get returned %+q, expected %+q", buf, testDataB)
216                 }
217         } else {
218                 // It is permissible for Put to fail, but it must
219                 // leave us with either the original data, the new
220                 // data, or nothing at all.
221                 if getErr == nil && bytes.Compare(buf, testDataA) != 0 && bytes.Compare(buf, testDataB) != 0 {
222                         t.Errorf("Put failed but Get returned %+q, which is neither %+q nor %+q", buf, testDataA, testDataB)
223                 }
224         }
225         if getErr == nil {
226                 bufs.Put(buf)
227         }
228 }
229
230 // Put and get multiple blocks
231 // Test is intended for only writable volumes
232 func testPutMultipleBlocks(t TB, factory TestableVolumeFactory) {
233         v := factory(t)
234         defer v.Teardown()
235
236         if v.Writable() == false {
237                 return
238         }
239
240         err := v.Put(TestHash, TestBlock)
241         if err != nil {
242                 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
243         }
244
245         err = v.Put(TestHash2, TestBlock2)
246         if err != nil {
247                 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock2, err)
248         }
249
250         err = v.Put(TestHash3, TestBlock3)
251         if err != nil {
252                 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock3, err)
253         }
254
255         data, err := v.Get(TestHash)
256         if err != nil {
257                 t.Error(err)
258         } else {
259                 if bytes.Compare(data, TestBlock) != 0 {
260                         t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock)
261                 }
262                 bufs.Put(data)
263         }
264
265         data, err = v.Get(TestHash2)
266         if err != nil {
267                 t.Error(err)
268         } else {
269                 if bytes.Compare(data, TestBlock2) != 0 {
270                         t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock2)
271                 }
272                 bufs.Put(data)
273         }
274
275         data, err = v.Get(TestHash3)
276         if err != nil {
277                 t.Error(err)
278         } else {
279                 if bytes.Compare(data, TestBlock3) != 0 {
280                         t.Errorf("Block present, but to %+q, expected %+q", data, TestBlock3)
281                 }
282                 bufs.Put(data)
283         }
284 }
285
286 // testPutAndTouch
287 //   Test that when applying PUT to a block that already exists,
288 //   the block's modification time is updated.
289 // Test is intended for only writable volumes
290 func testPutAndTouch(t TB, factory TestableVolumeFactory) {
291         v := factory(t)
292         defer v.Teardown()
293
294         if v.Writable() == false {
295                 return
296         }
297
298         if err := v.Put(TestHash, TestBlock); err != nil {
299                 t.Error(err)
300         }
301
302         // We'll verify { t0 < threshold < t1 }, where t0 is the
303         // existing block's timestamp on disk before Put() and t1 is
304         // its timestamp after Put().
305         threshold := time.Now().Add(-time.Second)
306
307         // Set the stored block's mtime far enough in the past that we
308         // can see the difference between "timestamp didn't change"
309         // and "timestamp granularity is too low".
310         v.TouchWithDate(TestHash, time.Now().Add(-20*time.Second))
311
312         // Make sure v.Mtime() agrees the above Utime really worked.
313         if t0, err := v.Mtime(TestHash); err != nil || t0.IsZero() || !t0.Before(threshold) {
314                 t.Errorf("Setting mtime failed: %v, %v", t0, err)
315         }
316
317         // Write the same block again.
318         if err := v.Put(TestHash, TestBlock); err != nil {
319                 t.Error(err)
320         }
321
322         // Verify threshold < t1
323         if t1, err := v.Mtime(TestHash); err != nil {
324                 t.Error(err)
325         } else if t1.Before(threshold) {
326                 t.Errorf("t1 %v should be >= threshold %v after v.Put ", t1, threshold)
327         }
328 }
329
330 // Touching a non-existing block should result in error.
331 // Test should pass for both writable and read-only volumes
332 func testTouchNoSuchBlock(t TB, factory TestableVolumeFactory) {
333         v := factory(t)
334         defer v.Teardown()
335
336         if err := v.Touch(TestHash); err == nil {
337                 t.Error("Expected error when attempted to touch a non-existing block")
338         }
339 }
340
341 // Invoking Mtime on a non-existing block should result in error.
342 // Test should pass for both writable and read-only volumes
343 func testMtimeNoSuchBlock(t TB, factory TestableVolumeFactory) {
344         v := factory(t)
345         defer v.Teardown()
346
347         if _, err := v.Mtime("12345678901234567890123456789012"); err == nil {
348                 t.Error("Expected error when updating Mtime on a non-existing block")
349         }
350 }
351
352 // Put a few blocks and invoke IndexTo with:
353 // * no prefix
354 // * with a prefix
355 // * with no such prefix
356 // Test should pass for both writable and read-only volumes
357 func testIndexTo(t TB, factory TestableVolumeFactory) {
358         v := factory(t)
359         defer v.Teardown()
360
361         v.PutRaw(TestHash, TestBlock)
362         v.PutRaw(TestHash2, TestBlock2)
363         v.PutRaw(TestHash3, TestBlock3)
364
365         // Blocks whose names aren't Keep hashes should be omitted from
366         // index
367         v.PutRaw("fffffffffnotreallyahashfffffffff", nil)
368         v.PutRaw("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", nil)
369         v.PutRaw("f0000000000000000000000000000000f", nil)
370         v.PutRaw("f00", nil)
371
372         buf := new(bytes.Buffer)
373         v.IndexTo("", buf)
374         indexRows := strings.Split(string(buf.Bytes()), "\n")
375         sort.Strings(indexRows)
376         sortedIndex := strings.Join(indexRows, "\n")
377         m, err := regexp.MatchString(
378                 `^\n`+TestHash+`\+\d+ \d+\n`+
379                         TestHash3+`\+\d+ \d+\n`+
380                         TestHash2+`\+\d+ \d+$`,
381                 sortedIndex)
382         if err != nil {
383                 t.Error(err)
384         } else if !m {
385                 t.Errorf("Got index %q for empty prefix", sortedIndex)
386         }
387
388         for _, prefix := range []string{"f", "f15", "f15ac"} {
389                 buf = new(bytes.Buffer)
390                 v.IndexTo(prefix, buf)
391
392                 m, err := regexp.MatchString(`^`+TestHash2+`\+\d+ \d+\n$`, string(buf.Bytes()))
393                 if err != nil {
394                         t.Error(err)
395                 } else if !m {
396                         t.Errorf("Got index %q for prefix %s", string(buf.Bytes()), prefix)
397                 }
398         }
399
400         for _, prefix := range []string{"zero", "zip", "zilch"} {
401                 buf = new(bytes.Buffer)
402                 v.IndexTo(prefix, buf)
403                 if err != nil {
404                         t.Errorf("Got error on IndexTo with no such prefix %v", err.Error())
405                 } else if buf.Len() != 0 {
406                         t.Errorf("Expected empty list for IndexTo with no such prefix %s", prefix)
407                 }
408         }
409 }
410
411 // Calling Delete() for a block immediately after writing it (not old enough)
412 // should neither delete the data nor return an error.
413 // Test is intended for only writable volumes
414 func testDeleteNewBlock(t TB, factory TestableVolumeFactory) {
415         v := factory(t)
416         defer v.Teardown()
417         blobSignatureTTL = 300 * time.Second
418
419         if v.Writable() == false {
420                 return
421         }
422
423         v.Put(TestHash, TestBlock)
424
425         if err := v.Trash(TestHash); err != nil {
426                 t.Error(err)
427         }
428         data, err := v.Get(TestHash)
429         if err != nil {
430                 t.Error(err)
431         } else {
432                 if bytes.Compare(data, TestBlock) != 0 {
433                         t.Errorf("Got data %+q, expected %+q", data, TestBlock)
434                 }
435                 bufs.Put(data)
436         }
437 }
438
439 // Calling Delete() for a block with a timestamp older than
440 // blobSignatureTTL seconds in the past should delete the data.
441 // Test is intended for only writable volumes
442 func testDeleteOldBlock(t TB, factory TestableVolumeFactory) {
443         v := factory(t)
444         defer v.Teardown()
445         blobSignatureTTL = 300 * time.Second
446
447         if v.Writable() == false {
448                 return
449         }
450
451         v.Put(TestHash, TestBlock)
452         v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
453
454         if err := v.Trash(TestHash); err != nil {
455                 t.Error(err)
456         }
457         if _, err := v.Get(TestHash); err == nil || !os.IsNotExist(err) {
458                 t.Errorf("os.IsNotExist(%v) should have been true", err)
459         }
460 }
461
462 // Calling Delete() for a block that does not exist should result in error.
463 // Test should pass for both writable and read-only volumes
464 func testDeleteNoSuchBlock(t TB, factory TestableVolumeFactory) {
465         v := factory(t)
466         defer v.Teardown()
467
468         if err := v.Trash(TestHash2); err == nil {
469                 t.Errorf("Expected error when attempting to delete a non-existing block")
470         }
471 }
472
473 // Invoke Status and verify that VolumeStatus is returned
474 // Test should pass for both writable and read-only volumes
475 func testStatus(t TB, factory TestableVolumeFactory) {
476         v := factory(t)
477         defer v.Teardown()
478
479         // Get node status and make a basic sanity check.
480         status := v.Status()
481         if status.DeviceNum == 0 {
482                 t.Errorf("uninitialized device_num in %v", status)
483         }
484
485         if status.BytesFree == 0 {
486                 t.Errorf("uninitialized bytes_free in %v", status)
487         }
488
489         if status.BytesUsed == 0 {
490                 t.Errorf("uninitialized bytes_used in %v", status)
491         }
492 }
493
494 // Invoke String for the volume; expect non-empty result
495 // Test should pass for both writable and read-only volumes
496 func testString(t TB, factory TestableVolumeFactory) {
497         v := factory(t)
498         defer v.Teardown()
499
500         if id := v.String(); len(id) == 0 {
501                 t.Error("Got empty string for v.String()")
502         }
503 }
504
505 // Putting, updating, touching, and deleting blocks from a read-only volume result in error.
506 // Test is intended for only read-only volumes
507 func testUpdateReadOnly(t TB, factory TestableVolumeFactory) {
508         v := factory(t)
509         defer v.Teardown()
510
511         if v.Writable() == true {
512                 return
513         }
514
515         v.PutRaw(TestHash, TestBlock)
516
517         // Get from read-only volume should succeed
518         _, err := v.Get(TestHash)
519         if err != nil {
520                 t.Errorf("got err %v, expected nil", err)
521         }
522
523         // Put a new block to read-only volume should result in error
524         err = v.Put(TestHash2, TestBlock2)
525         if err == nil {
526                 t.Errorf("Expected error when putting block in a read-only volume")
527         }
528         _, err = v.Get(TestHash2)
529         if err == nil {
530                 t.Errorf("Expected error when getting block whose put in read-only volume failed")
531         }
532
533         // Touch a block in read-only volume should result in error
534         err = v.Touch(TestHash)
535         if err == nil {
536                 t.Errorf("Expected error when touching block in a read-only volume")
537         }
538
539         // Delete a block from a read-only volume should result in error
540         err = v.Trash(TestHash)
541         if err == nil {
542                 t.Errorf("Expected error when deleting block from a read-only volume")
543         }
544
545         // Overwriting an existing block in read-only volume should result in error
546         err = v.Put(TestHash, TestBlock)
547         if err == nil {
548                 t.Errorf("Expected error when putting block in a read-only volume")
549         }
550 }
551
552 // Launch concurrent Gets
553 // Test should pass for both writable and read-only volumes
554 func testGetConcurrent(t TB, factory TestableVolumeFactory) {
555         v := factory(t)
556         defer v.Teardown()
557
558         v.PutRaw(TestHash, TestBlock)
559         v.PutRaw(TestHash2, TestBlock2)
560         v.PutRaw(TestHash3, TestBlock3)
561
562         sem := make(chan int)
563         go func(sem chan int) {
564                 buf, err := v.Get(TestHash)
565                 if err != nil {
566                         t.Errorf("err1: %v", err)
567                 }
568                 bufs.Put(buf)
569                 if bytes.Compare(buf, TestBlock) != 0 {
570                         t.Errorf("buf should be %s, is %s", string(TestBlock), string(buf))
571                 }
572                 sem <- 1
573         }(sem)
574
575         go func(sem chan int) {
576                 buf, err := v.Get(TestHash2)
577                 if err != nil {
578                         t.Errorf("err2: %v", err)
579                 }
580                 bufs.Put(buf)
581                 if bytes.Compare(buf, TestBlock2) != 0 {
582                         t.Errorf("buf should be %s, is %s", string(TestBlock2), string(buf))
583                 }
584                 sem <- 1
585         }(sem)
586
587         go func(sem chan int) {
588                 buf, err := v.Get(TestHash3)
589                 if err != nil {
590                         t.Errorf("err3: %v", err)
591                 }
592                 bufs.Put(buf)
593                 if bytes.Compare(buf, TestBlock3) != 0 {
594                         t.Errorf("buf should be %s, is %s", string(TestBlock3), string(buf))
595                 }
596                 sem <- 1
597         }(sem)
598
599         // Wait for all goroutines to finish
600         for done := 0; done < 3; {
601                 done += <-sem
602         }
603 }
604
605 // Launch concurrent Puts
606 // Test is intended for only writable volumes
607 func testPutConcurrent(t TB, factory TestableVolumeFactory) {
608         v := factory(t)
609         defer v.Teardown()
610
611         if v.Writable() == false {
612                 return
613         }
614
615         sem := make(chan int)
616         go func(sem chan int) {
617                 err := v.Put(TestHash, TestBlock)
618                 if err != nil {
619                         t.Errorf("err1: %v", err)
620                 }
621                 sem <- 1
622         }(sem)
623
624         go func(sem chan int) {
625                 err := v.Put(TestHash2, TestBlock2)
626                 if err != nil {
627                         t.Errorf("err2: %v", err)
628                 }
629                 sem <- 1
630         }(sem)
631
632         go func(sem chan int) {
633                 err := v.Put(TestHash3, TestBlock3)
634                 if err != nil {
635                         t.Errorf("err3: %v", err)
636                 }
637                 sem <- 1
638         }(sem)
639
640         // Wait for all goroutines to finish
641         for done := 0; done < 3; {
642                 done += <-sem
643         }
644
645         // Double check that we actually wrote the blocks we expected to write.
646         buf, err := v.Get(TestHash)
647         if err != nil {
648                 t.Errorf("Get #1: %v", err)
649         }
650         bufs.Put(buf)
651         if bytes.Compare(buf, TestBlock) != 0 {
652                 t.Errorf("Get #1: expected %s, got %s", string(TestBlock), string(buf))
653         }
654
655         buf, err = v.Get(TestHash2)
656         if err != nil {
657                 t.Errorf("Get #2: %v", err)
658         }
659         bufs.Put(buf)
660         if bytes.Compare(buf, TestBlock2) != 0 {
661                 t.Errorf("Get #2: expected %s, got %s", string(TestBlock2), string(buf))
662         }
663
664         buf, err = v.Get(TestHash3)
665         if err != nil {
666                 t.Errorf("Get #3: %v", err)
667         }
668         bufs.Put(buf)
669         if bytes.Compare(buf, TestBlock3) != 0 {
670                 t.Errorf("Get #3: expected %s, got %s", string(TestBlock3), string(buf))
671         }
672 }
673
674 // Write and read back a full size block
675 func testPutFullBlock(t TB, factory TestableVolumeFactory) {
676         v := factory(t)
677         defer v.Teardown()
678
679         if !v.Writable() {
680                 return
681         }
682
683         wdata := make([]byte, BlockSize)
684         wdata[0] = 'a'
685         wdata[BlockSize-1] = 'z'
686         hash := fmt.Sprintf("%x", md5.Sum(wdata))
687         err := v.Put(hash, wdata)
688         if err != nil {
689                 t.Fatal(err)
690         }
691         rdata, err := v.Get(hash)
692         if err != nil {
693                 t.Error(err)
694         } else {
695                 defer bufs.Put(rdata)
696         }
697         if bytes.Compare(rdata, wdata) != 0 {
698                 t.Error("rdata != wdata")
699         }
700 }
701
702 // With trashLifetime != 0, perform:
703 // Trash an old block - which either raises ErrNotImplemented or succeeds
704 // Untrash -  which either raises ErrNotImplemented or succeeds
705 // Get - which must succeed
706 func testTrashUntrash(t TB, factory TestableVolumeFactory) {
707         v := factory(t)
708         defer v.Teardown()
709         defer func() {
710                 trashLifetime = 0
711         }()
712
713         trashLifetime = 3600 * time.Second
714
715         // put block and backdate it
716         v.PutRaw(TestHash, TestBlock)
717         v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
718
719         buf, err := v.Get(TestHash)
720         if err != nil {
721                 t.Fatal(err)
722         }
723         if bytes.Compare(buf, TestBlock) != 0 {
724                 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
725         }
726         bufs.Put(buf)
727
728         // Trash
729         err = v.Trash(TestHash)
730         if v.Writable() == false {
731                 if err != MethodDisabledError {
732                         t.Error(err)
733                 }
734         } else if err != nil {
735                 if err != ErrNotImplemented {
736                         t.Error(err)
737                 }
738         } else {
739                 _, err = v.Get(TestHash)
740                 if err == nil || !os.IsNotExist(err) {
741                         t.Errorf("os.IsNotExist(%v) should have been true", err)
742                 }
743
744                 // Untrash
745                 err = v.Untrash(TestHash)
746                 if err != nil {
747                         t.Fatal(err)
748                 }
749         }
750
751         // Get the block - after trash and untrash sequence
752         buf, err = v.Get(TestHash)
753         if err != nil {
754                 t.Fatal(err)
755         }
756         if bytes.Compare(buf, TestBlock) != 0 {
757                 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
758         }
759         bufs.Put(buf)
760 }