1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.curoverse.com/arvados.git/sdk/go/arvados"
20 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
24 Error(args ...interface{})
25 Errorf(format string, args ...interface{})
29 Fatal(args ...interface{})
30 Fatalf(format string, args ...interface{})
31 Log(args ...interface{})
32 Logf(format string, args ...interface{})
35 // A TestableVolumeFactory returns a new TestableVolume. The factory
36 // function, and the TestableVolume it returns, can use "t" to write
37 // logs, fail the current test, etc.
38 type TestableVolumeFactory func(t TB) TestableVolume
40 // DoGenericVolumeTests runs a set of tests that every TestableVolume
41 // is expected to pass. It calls factory to create a new TestableVolume
42 // for each test case, to avoid leaking state between tests.
43 func DoGenericVolumeTests(t TB, factory TestableVolumeFactory) {
45 testGetNoSuchBlock(t, factory)
47 testCompareNonexistent(t, factory)
48 testCompareSameContent(t, factory, TestHash, TestBlock)
49 testCompareSameContent(t, factory, EmptyHash, EmptyBlock)
50 testCompareWithCollision(t, factory, TestHash, TestBlock, []byte("baddata"))
51 testCompareWithCollision(t, factory, TestHash, TestBlock, EmptyBlock)
52 testCompareWithCollision(t, factory, EmptyHash, EmptyBlock, TestBlock)
53 testCompareWithCorruptStoredData(t, factory, TestHash, TestBlock, []byte("baddata"))
54 testCompareWithCorruptStoredData(t, factory, TestHash, TestBlock, EmptyBlock)
55 testCompareWithCorruptStoredData(t, factory, EmptyHash, EmptyBlock, []byte("baddata"))
57 testPutBlockWithSameContent(t, factory, TestHash, TestBlock)
58 testPutBlockWithSameContent(t, factory, EmptyHash, EmptyBlock)
59 testPutBlockWithDifferentContent(t, factory, arvadostest.MD5CollisionMD5, arvadostest.MD5CollisionData[0], arvadostest.MD5CollisionData[1])
60 testPutBlockWithDifferentContent(t, factory, arvadostest.MD5CollisionMD5, EmptyBlock, arvadostest.MD5CollisionData[0])
61 testPutBlockWithDifferentContent(t, factory, arvadostest.MD5CollisionMD5, arvadostest.MD5CollisionData[0], EmptyBlock)
62 testPutBlockWithDifferentContent(t, factory, EmptyHash, EmptyBlock, arvadostest.MD5CollisionData[0])
63 testPutMultipleBlocks(t, factory)
65 testPutAndTouch(t, factory)
66 testTouchNoSuchBlock(t, factory)
68 testMtimeNoSuchBlock(t, factory)
70 testIndexTo(t, factory)
72 testDeleteNewBlock(t, factory)
73 testDeleteOldBlock(t, factory)
74 testDeleteNoSuchBlock(t, factory)
76 testStatus(t, factory)
78 testString(t, factory)
80 testUpdateReadOnly(t, factory)
82 testGetConcurrent(t, factory)
83 testPutConcurrent(t, factory)
85 testPutFullBlock(t, factory)
87 testTrashUntrash(t, factory)
88 testTrashEmptyTrashUntrash(t, factory)
91 // Put a test block, get it and verify content
92 // Test should pass for both writable and read-only volumes
93 func testGet(t TB, factory TestableVolumeFactory) {
97 v.PutRaw(TestHash, TestBlock)
99 buf := make([]byte, BlockSize)
100 n, err := v.Get(context.Background(), TestHash, buf)
105 if bytes.Compare(buf[:n], TestBlock) != 0 {
106 t.Errorf("expected %s, got %s", string(TestBlock), string(buf))
110 // Invoke get on a block that does not exist in volume; should result in error
111 // Test should pass for both writable and read-only volumes
112 func testGetNoSuchBlock(t TB, factory TestableVolumeFactory) {
116 buf := make([]byte, BlockSize)
117 if _, err := v.Get(context.Background(), TestHash2, buf); err == nil {
118 t.Errorf("Expected error while getting non-existing block %v", TestHash2)
122 // Compare() should return os.ErrNotExist if the block does not exist.
123 // Otherwise, writing new data causes CompareAndTouch() to generate
124 // error logs even though everything is working fine.
125 func testCompareNonexistent(t TB, factory TestableVolumeFactory) {
129 err := v.Compare(context.Background(), TestHash, TestBlock)
130 if err != os.ErrNotExist {
131 t.Errorf("Got err %T %q, expected os.ErrNotExist", err, err)
135 // Put a test block and compare the locator with same content
136 // Test should pass for both writable and read-only volumes
137 func testCompareSameContent(t TB, factory TestableVolumeFactory, testHash string, testData []byte) {
141 v.PutRaw(testHash, testData)
143 // Compare the block locator with same content
144 err := v.Compare(context.Background(), testHash, testData)
146 t.Errorf("Got err %q, expected nil", err)
150 // Test behavior of Compare() when stored data matches expected
151 // checksum but differs from new data we need to store. Requires
152 // testHash = md5(testDataA).
154 // Test should pass for both writable and read-only volumes
155 func testCompareWithCollision(t TB, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
159 v.PutRaw(testHash, testDataA)
161 // Compare the block locator with different content; collision
162 err := v.Compare(context.Background(), TestHash, testDataB)
164 t.Errorf("Got err nil, expected error due to collision")
168 // Test behavior of Compare() when stored data has become
169 // corrupted. Requires testHash = md5(testDataA) != md5(testDataB).
171 // Test should pass for both writable and read-only volumes
172 func testCompareWithCorruptStoredData(t TB, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
176 v.PutRaw(TestHash, testDataB)
178 err := v.Compare(context.Background(), testHash, testDataA)
179 if err == nil || err == CollisionError {
180 t.Errorf("Got err %+v, expected non-collision error", err)
184 // Put a block and put again with same content
185 // Test is intended for only writable volumes
186 func testPutBlockWithSameContent(t TB, factory TestableVolumeFactory, testHash string, testData []byte) {
190 if v.Writable() == false {
194 err := v.Put(context.Background(), testHash, testData)
196 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
199 err = v.Put(context.Background(), testHash, testData)
201 t.Errorf("Got err putting block second time %q: %q, expected nil", TestBlock, err)
205 // Put a block and put again with different content
206 // Test is intended for only writable volumes
207 func testPutBlockWithDifferentContent(t TB, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
211 if v.Writable() == false {
215 v.PutRaw(testHash, testDataA)
217 putErr := v.Put(context.Background(), testHash, testDataB)
218 buf := make([]byte, BlockSize)
219 n, getErr := v.Get(context.Background(), testHash, buf)
221 // Put must not return a nil error unless it has
222 // overwritten the existing data.
223 if bytes.Compare(buf[:n], testDataB) != 0 {
224 t.Errorf("Put succeeded but Get returned %+q, expected %+q", buf[:n], testDataB)
227 // It is permissible for Put to fail, but it must
228 // leave us with either the original data, the new
229 // data, or nothing at all.
230 if getErr == nil && bytes.Compare(buf[:n], testDataA) != 0 && bytes.Compare(buf[:n], testDataB) != 0 {
231 t.Errorf("Put failed but Get returned %+q, which is neither %+q nor %+q", buf[:n], testDataA, testDataB)
236 // Put and get multiple blocks
237 // Test is intended for only writable volumes
238 func testPutMultipleBlocks(t TB, factory TestableVolumeFactory) {
242 if v.Writable() == false {
246 err := v.Put(context.Background(), TestHash, TestBlock)
248 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
251 err = v.Put(context.Background(), TestHash2, TestBlock2)
253 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock2, err)
256 err = v.Put(context.Background(), TestHash3, TestBlock3)
258 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock3, err)
261 data := make([]byte, BlockSize)
262 n, err := v.Get(context.Background(), TestHash, data)
266 if bytes.Compare(data[:n], TestBlock) != 0 {
267 t.Errorf("Block present, but got %+q, expected %+q", data[:n], TestBlock)
271 n, err = v.Get(context.Background(), TestHash2, data)
275 if bytes.Compare(data[:n], TestBlock2) != 0 {
276 t.Errorf("Block present, but got %+q, expected %+q", data[:n], TestBlock2)
280 n, err = v.Get(context.Background(), TestHash3, data)
284 if bytes.Compare(data[:n], TestBlock3) != 0 {
285 t.Errorf("Block present, but to %+q, expected %+q", data[:n], TestBlock3)
291 // Test that when applying PUT to a block that already exists,
292 // the block's modification time is updated.
293 // Test is intended for only writable volumes
294 func testPutAndTouch(t TB, factory TestableVolumeFactory) {
298 if v.Writable() == false {
302 if err := v.Put(context.Background(), TestHash, TestBlock); err != nil {
306 // We'll verify { t0 < threshold < t1 }, where t0 is the
307 // existing block's timestamp on disk before Put() and t1 is
308 // its timestamp after Put().
309 threshold := time.Now().Add(-time.Second)
311 // Set the stored block's mtime far enough in the past that we
312 // can see the difference between "timestamp didn't change"
313 // and "timestamp granularity is too low".
314 v.TouchWithDate(TestHash, time.Now().Add(-20*time.Second))
316 // Make sure v.Mtime() agrees the above Utime really worked.
317 if t0, err := v.Mtime(TestHash); err != nil || t0.IsZero() || !t0.Before(threshold) {
318 t.Errorf("Setting mtime failed: %v, %v", t0, err)
321 // Write the same block again.
322 if err := v.Put(context.Background(), TestHash, TestBlock); err != nil {
326 // Verify threshold < t1
327 if t1, err := v.Mtime(TestHash); err != nil {
329 } else if t1.Before(threshold) {
330 t.Errorf("t1 %v should be >= threshold %v after v.Put ", t1, threshold)
334 // Touching a non-existing block should result in error.
335 // Test should pass for both writable and read-only volumes
336 func testTouchNoSuchBlock(t TB, factory TestableVolumeFactory) {
340 if err := v.Touch(TestHash); err == nil {
341 t.Error("Expected error when attempted to touch a non-existing block")
345 // Invoking Mtime on a non-existing block should result in error.
346 // Test should pass for both writable and read-only volumes
347 func testMtimeNoSuchBlock(t TB, factory TestableVolumeFactory) {
351 if _, err := v.Mtime("12345678901234567890123456789012"); err == nil {
352 t.Error("Expected error when updating Mtime on a non-existing block")
356 // Put a few blocks and invoke IndexTo with:
359 // * with no such prefix
360 // Test should pass for both writable and read-only volumes
361 func testIndexTo(t TB, factory TestableVolumeFactory) {
365 // minMtime and maxMtime are the minimum and maximum
366 // acceptable values the index can report for our test
367 // blocks. 1-second precision is acceptable.
368 minMtime := time.Now().UTC().UnixNano()
369 minMtime -= minMtime % 1e9
371 v.PutRaw(TestHash, TestBlock)
372 v.PutRaw(TestHash2, TestBlock2)
373 v.PutRaw(TestHash3, TestBlock3)
375 maxMtime := time.Now().UTC().UnixNano()
376 if maxMtime%1e9 > 0 {
377 maxMtime -= maxMtime % 1e9
381 // Blocks whose names aren't Keep hashes should be omitted from
383 v.PutRaw("fffffffffnotreallyahashfffffffff", nil)
384 v.PutRaw("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", nil)
385 v.PutRaw("f0000000000000000000000000000000f", nil)
388 buf := new(bytes.Buffer)
390 indexRows := strings.Split(string(buf.Bytes()), "\n")
391 sort.Strings(indexRows)
392 sortedIndex := strings.Join(indexRows, "\n")
393 m := regexp.MustCompile(
394 `^\n` + TestHash + `\+\d+ (\d+)\n` +
395 TestHash3 + `\+\d+ \d+\n` +
396 TestHash2 + `\+\d+ \d+$`,
397 ).FindStringSubmatch(sortedIndex)
399 t.Errorf("Got index %q for empty prefix", sortedIndex)
401 mtime, err := strconv.ParseInt(m[1], 10, 64)
404 } else if mtime < minMtime || mtime > maxMtime {
405 t.Errorf("got %d for TestHash timestamp, expected %d <= t <= %d",
406 mtime, minMtime, maxMtime)
410 for _, prefix := range []string{"f", "f15", "f15ac"} {
411 buf = new(bytes.Buffer)
412 v.IndexTo(prefix, buf)
414 m, err := regexp.MatchString(`^`+TestHash2+`\+\d+ \d+\n$`, string(buf.Bytes()))
418 t.Errorf("Got index %q for prefix %s", string(buf.Bytes()), prefix)
422 for _, prefix := range []string{"zero", "zip", "zilch"} {
423 buf = new(bytes.Buffer)
424 err := v.IndexTo(prefix, buf)
426 t.Errorf("Got error on IndexTo with no such prefix %v", err.Error())
427 } else if buf.Len() != 0 {
428 t.Errorf("Expected empty list for IndexTo with no such prefix %s", prefix)
433 // Calling Delete() for a block immediately after writing it (not old enough)
434 // should neither delete the data nor return an error.
435 // Test is intended for only writable volumes
436 func testDeleteNewBlock(t TB, factory TestableVolumeFactory) {
439 theConfig.BlobSignatureTTL.Set("5m")
441 if v.Writable() == false {
445 v.Put(context.Background(), TestHash, TestBlock)
447 if err := v.Trash(TestHash); err != nil {
450 data := make([]byte, BlockSize)
451 n, err := v.Get(context.Background(), TestHash, data)
454 } else if bytes.Compare(data[:n], TestBlock) != 0 {
455 t.Errorf("Got data %+q, expected %+q", data[:n], TestBlock)
459 // Calling Delete() for a block with a timestamp older than
460 // BlobSignatureTTL seconds in the past should delete the data.
461 // Test is intended for only writable volumes
462 func testDeleteOldBlock(t TB, factory TestableVolumeFactory) {
465 theConfig.BlobSignatureTTL.Set("5m")
467 if v.Writable() == false {
471 v.Put(context.Background(), TestHash, TestBlock)
472 v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration()))
474 if err := v.Trash(TestHash); err != nil {
477 data := make([]byte, BlockSize)
478 if _, err := v.Get(context.Background(), TestHash, data); err == nil || !os.IsNotExist(err) {
479 t.Errorf("os.IsNotExist(%v) should have been true", err)
482 _, err := v.Mtime(TestHash)
483 if err == nil || !os.IsNotExist(err) {
484 t.Fatalf("os.IsNotExist(%v) should have been true", err)
487 err = v.Compare(context.Background(), TestHash, TestBlock)
488 if err == nil || !os.IsNotExist(err) {
489 t.Fatalf("os.IsNotExist(%v) should have been true", err)
492 indexBuf := new(bytes.Buffer)
493 v.IndexTo("", indexBuf)
494 if strings.Contains(string(indexBuf.Bytes()), TestHash) {
495 t.Fatalf("Found trashed block in IndexTo")
498 err = v.Touch(TestHash)
499 if err == nil || !os.IsNotExist(err) {
500 t.Fatalf("os.IsNotExist(%v) should have been true", err)
504 // Calling Delete() for a block that does not exist should result in error.
505 // Test should pass for both writable and read-only volumes
506 func testDeleteNoSuchBlock(t TB, factory TestableVolumeFactory) {
510 if err := v.Trash(TestHash2); err == nil {
511 t.Errorf("Expected error when attempting to delete a non-existing block")
515 // Invoke Status and verify that VolumeStatus is returned
516 // Test should pass for both writable and read-only volumes
517 func testStatus(t TB, factory TestableVolumeFactory) {
521 // Get node status and make a basic sanity check.
523 if status.DeviceNum == 0 {
524 t.Errorf("uninitialized device_num in %v", status)
527 if status.BytesFree == 0 {
528 t.Errorf("uninitialized bytes_free in %v", status)
531 if status.BytesUsed == 0 {
532 t.Errorf("uninitialized bytes_used in %v", status)
536 // Invoke String for the volume; expect non-empty result
537 // Test should pass for both writable and read-only volumes
538 func testString(t TB, factory TestableVolumeFactory) {
542 if id := v.String(); len(id) == 0 {
543 t.Error("Got empty string for v.String()")
547 // Putting, updating, touching, and deleting blocks from a read-only volume result in error.
548 // Test is intended for only read-only volumes
549 func testUpdateReadOnly(t TB, factory TestableVolumeFactory) {
553 if v.Writable() == true {
557 v.PutRaw(TestHash, TestBlock)
558 buf := make([]byte, BlockSize)
560 // Get from read-only volume should succeed
561 _, err := v.Get(context.Background(), TestHash, buf)
563 t.Errorf("got err %v, expected nil", err)
566 // Put a new block to read-only volume should result in error
567 err = v.Put(context.Background(), TestHash2, TestBlock2)
569 t.Errorf("Expected error when putting block in a read-only volume")
571 _, err = v.Get(context.Background(), TestHash2, buf)
573 t.Errorf("Expected error when getting block whose put in read-only volume failed")
576 // Touch a block in read-only volume should result in error
577 err = v.Touch(TestHash)
579 t.Errorf("Expected error when touching block in a read-only volume")
582 // Delete a block from a read-only volume should result in error
583 err = v.Trash(TestHash)
585 t.Errorf("Expected error when deleting block from a read-only volume")
588 // Overwriting an existing block in read-only volume should result in error
589 err = v.Put(context.Background(), TestHash, TestBlock)
591 t.Errorf("Expected error when putting block in a read-only volume")
595 // Launch concurrent Gets
596 // Test should pass for both writable and read-only volumes
597 func testGetConcurrent(t TB, factory TestableVolumeFactory) {
601 v.PutRaw(TestHash, TestBlock)
602 v.PutRaw(TestHash2, TestBlock2)
603 v.PutRaw(TestHash3, TestBlock3)
605 sem := make(chan int)
607 buf := make([]byte, BlockSize)
608 n, err := v.Get(context.Background(), TestHash, buf)
610 t.Errorf("err1: %v", err)
612 if bytes.Compare(buf[:n], TestBlock) != 0 {
613 t.Errorf("buf should be %s, is %s", string(TestBlock), string(buf[:n]))
619 buf := make([]byte, BlockSize)
620 n, err := v.Get(context.Background(), TestHash2, buf)
622 t.Errorf("err2: %v", err)
624 if bytes.Compare(buf[:n], TestBlock2) != 0 {
625 t.Errorf("buf should be %s, is %s", string(TestBlock2), string(buf[:n]))
631 buf := make([]byte, BlockSize)
632 n, err := v.Get(context.Background(), TestHash3, buf)
634 t.Errorf("err3: %v", err)
636 if bytes.Compare(buf[:n], TestBlock3) != 0 {
637 t.Errorf("buf should be %s, is %s", string(TestBlock3), string(buf[:n]))
642 // Wait for all goroutines to finish
643 for done := 0; done < 3; done++ {
648 // Launch concurrent Puts
649 // Test is intended for only writable volumes
650 func testPutConcurrent(t TB, factory TestableVolumeFactory) {
654 if v.Writable() == false {
658 sem := make(chan int)
659 go func(sem chan int) {
660 err := v.Put(context.Background(), TestHash, TestBlock)
662 t.Errorf("err1: %v", err)
667 go func(sem chan int) {
668 err := v.Put(context.Background(), TestHash2, TestBlock2)
670 t.Errorf("err2: %v", err)
675 go func(sem chan int) {
676 err := v.Put(context.Background(), TestHash3, TestBlock3)
678 t.Errorf("err3: %v", err)
683 // Wait for all goroutines to finish
684 for done := 0; done < 3; done++ {
688 // Double check that we actually wrote the blocks we expected to write.
689 buf := make([]byte, BlockSize)
690 n, err := v.Get(context.Background(), TestHash, buf)
692 t.Errorf("Get #1: %v", err)
694 if bytes.Compare(buf[:n], TestBlock) != 0 {
695 t.Errorf("Get #1: expected %s, got %s", string(TestBlock), string(buf[:n]))
698 n, err = v.Get(context.Background(), TestHash2, buf)
700 t.Errorf("Get #2: %v", err)
702 if bytes.Compare(buf[:n], TestBlock2) != 0 {
703 t.Errorf("Get #2: expected %s, got %s", string(TestBlock2), string(buf[:n]))
706 n, err = v.Get(context.Background(), TestHash3, buf)
708 t.Errorf("Get #3: %v", err)
710 if bytes.Compare(buf[:n], TestBlock3) != 0 {
711 t.Errorf("Get #3: expected %s, got %s", string(TestBlock3), string(buf[:n]))
715 // Write and read back a full size block
716 func testPutFullBlock(t TB, factory TestableVolumeFactory) {
724 wdata := make([]byte, BlockSize)
726 wdata[BlockSize-1] = 'z'
727 hash := fmt.Sprintf("%x", md5.Sum(wdata))
728 err := v.Put(context.Background(), hash, wdata)
732 buf := make([]byte, BlockSize)
733 n, err := v.Get(context.Background(), hash, buf)
737 if bytes.Compare(buf[:n], wdata) != 0 {
738 t.Error("buf %+q != wdata %+q", buf[:n], wdata)
742 // With TrashLifetime != 0, perform:
743 // Trash an old block - which either raises ErrNotImplemented or succeeds
744 // Untrash - which either raises ErrNotImplemented or succeeds
745 // Get - which must succeed
746 func testTrashUntrash(t TB, factory TestableVolumeFactory) {
750 theConfig.TrashLifetime = 0
753 theConfig.TrashLifetime.Set("1h")
755 // put block and backdate it
756 v.PutRaw(TestHash, TestBlock)
757 v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration()))
759 buf := make([]byte, BlockSize)
760 n, err := v.Get(context.Background(), TestHash, buf)
764 if bytes.Compare(buf[:n], TestBlock) != 0 {
765 t.Errorf("Got data %+q, expected %+q", buf[:n], TestBlock)
769 err = v.Trash(TestHash)
770 if v.Writable() == false {
771 if err != MethodDisabledError {
774 } else if err != nil {
775 if err != ErrNotImplemented {
779 _, err = v.Get(context.Background(), TestHash, buf)
780 if err == nil || !os.IsNotExist(err) {
781 t.Errorf("os.IsNotExist(%v) should have been true", err)
785 err = v.Untrash(TestHash)
791 // Get the block - after trash and untrash sequence
792 n, err = v.Get(context.Background(), TestHash, buf)
796 if bytes.Compare(buf[:n], TestBlock) != 0 {
797 t.Errorf("Got data %+q, expected %+q", buf[:n], TestBlock)
801 func testTrashEmptyTrashUntrash(t TB, factory TestableVolumeFactory) {
804 defer func(orig arvados.Duration) {
805 theConfig.TrashLifetime = orig
806 }(theConfig.TrashLifetime)
808 checkGet := func() error {
809 buf := make([]byte, BlockSize)
810 n, err := v.Get(context.Background(), TestHash, buf)
814 if bytes.Compare(buf[:n], TestBlock) != 0 {
815 t.Fatalf("Got data %+q, expected %+q", buf[:n], TestBlock)
818 _, err = v.Mtime(TestHash)
823 err = v.Compare(context.Background(), TestHash, TestBlock)
828 indexBuf := new(bytes.Buffer)
829 v.IndexTo("", indexBuf)
830 if !strings.Contains(string(indexBuf.Bytes()), TestHash) {
831 return os.ErrNotExist
837 // First set: EmptyTrash before reaching the trash deadline.
839 theConfig.TrashLifetime.Set("1h")
841 v.PutRaw(TestHash, TestBlock)
842 v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration()))
850 err = v.Trash(TestHash)
851 if err == MethodDisabledError || err == ErrNotImplemented {
852 // Skip the trash tests for read-only volumes, and
853 // volume types that don't support TrashLifetime>0.
858 if err == nil || !os.IsNotExist(err) {
859 t.Fatalf("os.IsNotExist(%v) should have been true", err)
862 err = v.Touch(TestHash)
863 if err == nil || !os.IsNotExist(err) {
864 t.Fatalf("os.IsNotExist(%v) should have been true", err)
869 // Even after emptying the trash, we can untrash our block
870 // because the deadline hasn't been reached.
871 err = v.Untrash(TestHash)
881 err = v.Touch(TestHash)
886 // Because we Touch'ed, need to backdate again for next set of tests
887 v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration()))
889 // If the only block in the trash has already been untrashed,
890 // most volumes will fail a subsequent Untrash with a 404, but
891 // it's also acceptable for Untrash to succeed.
892 err = v.Untrash(TestHash)
893 if err != nil && !os.IsNotExist(err) {
894 t.Fatalf("Expected success or os.IsNotExist(), but got: %v", err)
897 // The additional Untrash should not interfere with our
898 // already-untrashed copy.
904 // Untrash might have updated the timestamp, so backdate again
905 v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration()))
907 // Second set: EmptyTrash after the trash deadline has passed.
909 theConfig.TrashLifetime.Set("1ns")
911 err = v.Trash(TestHash)
916 if err == nil || !os.IsNotExist(err) {
917 t.Fatalf("os.IsNotExist(%v) should have been true", err)
920 // Even though 1ns has passed, we can untrash because we
921 // haven't called EmptyTrash yet.
922 err = v.Untrash(TestHash)
931 // Trash it again, and this time call EmptyTrash so it really
933 // (In Azure volumes, un/trash changes Mtime, so first backdate again)
934 v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration()))
935 err = v.Trash(TestHash)
937 if err == nil || !os.IsNotExist(err) {
938 t.Fatalf("os.IsNotExist(%v) should have been true", err)
942 // Untrash won't find it
943 err = v.Untrash(TestHash)
944 if err == nil || !os.IsNotExist(err) {
945 t.Fatalf("os.IsNotExist(%v) should have been true", err)
948 // Get block won't find it
950 if err == nil || !os.IsNotExist(err) {
951 t.Fatalf("os.IsNotExist(%v) should have been true", err)
954 // Third set: If the same data block gets written again after
955 // being trashed, and then the trash gets emptied, the newer
956 // un-trashed copy doesn't get deleted along with it.
958 v.PutRaw(TestHash, TestBlock)
959 v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration()))
961 theConfig.TrashLifetime.Set("1ns")
962 err = v.Trash(TestHash)
967 if err == nil || !os.IsNotExist(err) {
968 t.Fatalf("os.IsNotExist(%v) should have been true", err)
971 v.PutRaw(TestHash, TestBlock)
972 v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration()))
974 // EmptyTrash should not delete the untrashed copy.
981 // Fourth set: If the same data block gets trashed twice with
982 // different deadlines A and C, and then the trash is emptied
983 // at intermediate time B (A < B < C), it is still possible to
984 // untrash the block whose deadline is "C".
986 v.PutRaw(TestHash, TestBlock)
987 v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration()))
989 theConfig.TrashLifetime.Set("1ns")
990 err = v.Trash(TestHash)
995 v.PutRaw(TestHash, TestBlock)
996 v.TouchWithDate(TestHash, time.Now().Add(-2*theConfig.BlobSignatureTTL.Duration()))
998 theConfig.TrashLifetime.Set("1h")
999 err = v.Trash(TestHash)
1004 // EmptyTrash should not prevent us from recovering the
1005 // time.Hour ("C") trash
1007 err = v.Untrash(TestHash)