13 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
17 Error(args ...interface{})
18 Errorf(format string, args ...interface{})
22 Fatal(args ...interface{})
23 Fatalf(format string, args ...interface{})
24 Log(args ...interface{})
25 Logf(format string, args ...interface{})
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
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) {
38 testGetNoSuchBlock(t, factory)
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"))
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)
58 testPutAndTouch(t, factory)
59 testTouchNoSuchBlock(t, factory)
61 testMtimeNoSuchBlock(t, factory)
63 testIndexTo(t, factory)
65 testDeleteNewBlock(t, factory)
66 testDeleteOldBlock(t, factory)
67 testDeleteNoSuchBlock(t, factory)
69 testStatus(t, factory)
71 testString(t, factory)
73 testUpdateReadOnly(t, factory)
75 testGetConcurrent(t, factory)
76 testPutConcurrent(t, factory)
78 testPutFullBlock(t, factory)
80 testTrashUntrash(t, factory)
81 testTrashEmptyTrashUntrash(t, factory)
84 // Put a test block, get it and verify content
85 // Test should pass for both writable and read-only volumes
86 func testGet(t TB, factory TestableVolumeFactory) {
90 v.PutRaw(TestHash, TestBlock)
92 buf, err := v.Get(TestHash)
99 if bytes.Compare(buf, TestBlock) != 0 {
100 t.Errorf("expected %s, got %s", string(TestBlock), string(buf))
104 // Invoke get on a block that does not exist in volume; should result in error
105 // Test should pass for both writable and read-only volumes
106 func testGetNoSuchBlock(t TB, factory TestableVolumeFactory) {
110 if _, err := v.Get(TestHash2); err == nil {
111 t.Errorf("Expected error while getting non-existing block %v", TestHash2)
115 // Compare() should return os.ErrNotExist if the block does not exist.
116 // Otherwise, writing new data causes CompareAndTouch() to generate
117 // error logs even though everything is working fine.
118 func testCompareNonexistent(t TB, factory TestableVolumeFactory) {
122 err := v.Compare(TestHash, TestBlock)
123 if err != os.ErrNotExist {
124 t.Errorf("Got err %T %q, expected os.ErrNotExist", err, err)
128 // Put a test block and compare the locator with same content
129 // Test should pass for both writable and read-only volumes
130 func testCompareSameContent(t TB, factory TestableVolumeFactory, testHash string, testData []byte) {
134 v.PutRaw(testHash, testData)
136 // Compare the block locator with same content
137 err := v.Compare(testHash, testData)
139 t.Errorf("Got err %q, expected nil", err)
143 // Test behavior of Compare() when stored data matches expected
144 // checksum but differs from new data we need to store. Requires
145 // testHash = md5(testDataA).
147 // Test should pass for both writable and read-only volumes
148 func testCompareWithCollision(t TB, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
152 v.PutRaw(testHash, testDataA)
154 // Compare the block locator with different content; collision
155 err := v.Compare(TestHash, testDataB)
157 t.Errorf("Got err nil, expected error due to collision")
161 // Test behavior of Compare() when stored data has become
162 // corrupted. Requires testHash = md5(testDataA) != md5(testDataB).
164 // Test should pass for both writable and read-only volumes
165 func testCompareWithCorruptStoredData(t TB, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
169 v.PutRaw(TestHash, testDataB)
171 err := v.Compare(testHash, testDataA)
172 if err == nil || err == CollisionError {
173 t.Errorf("Got err %+v, expected non-collision error", err)
177 // Put a block and put again with same content
178 // Test is intended for only writable volumes
179 func testPutBlockWithSameContent(t TB, factory TestableVolumeFactory, testHash string, testData []byte) {
183 if v.Writable() == false {
187 err := v.Put(testHash, testData)
189 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
192 err = v.Put(testHash, testData)
194 t.Errorf("Got err putting block second time %q: %q, expected nil", TestBlock, err)
198 // Put a block and put again with different content
199 // Test is intended for only writable volumes
200 func testPutBlockWithDifferentContent(t TB, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
204 if v.Writable() == false {
208 v.PutRaw(testHash, testDataA)
210 putErr := v.Put(testHash, testDataB)
211 buf, getErr := v.Get(testHash)
213 // Put must not return a nil error unless it has
214 // overwritten the existing data.
215 if bytes.Compare(buf, testDataB) != 0 {
216 t.Errorf("Put succeeded but Get returned %+q, expected %+q", buf, testDataB)
219 // It is permissible for Put to fail, but it must
220 // leave us with either the original data, the new
221 // data, or nothing at all.
222 if getErr == nil && bytes.Compare(buf, testDataA) != 0 && bytes.Compare(buf, testDataB) != 0 {
223 t.Errorf("Put failed but Get returned %+q, which is neither %+q nor %+q", buf, testDataA, testDataB)
231 // Put and get multiple blocks
232 // Test is intended for only writable volumes
233 func testPutMultipleBlocks(t TB, factory TestableVolumeFactory) {
237 if v.Writable() == false {
241 err := v.Put(TestHash, TestBlock)
243 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
246 err = v.Put(TestHash2, TestBlock2)
248 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock2, err)
251 err = v.Put(TestHash3, TestBlock3)
253 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock3, err)
256 data, err := v.Get(TestHash)
260 if bytes.Compare(data, TestBlock) != 0 {
261 t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock)
266 data, err = v.Get(TestHash2)
270 if bytes.Compare(data, TestBlock2) != 0 {
271 t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock2)
276 data, err = v.Get(TestHash3)
280 if bytes.Compare(data, TestBlock3) != 0 {
281 t.Errorf("Block present, but to %+q, expected %+q", data, TestBlock3)
288 // Test that when applying PUT to a block that already exists,
289 // the block's modification time is updated.
290 // Test is intended for only writable volumes
291 func testPutAndTouch(t TB, factory TestableVolumeFactory) {
295 if v.Writable() == false {
299 if err := v.Put(TestHash, TestBlock); err != nil {
303 // We'll verify { t0 < threshold < t1 }, where t0 is the
304 // existing block's timestamp on disk before Put() and t1 is
305 // its timestamp after Put().
306 threshold := time.Now().Add(-time.Second)
308 // Set the stored block's mtime far enough in the past that we
309 // can see the difference between "timestamp didn't change"
310 // and "timestamp granularity is too low".
311 v.TouchWithDate(TestHash, time.Now().Add(-20*time.Second))
313 // Make sure v.Mtime() agrees the above Utime really worked.
314 if t0, err := v.Mtime(TestHash); err != nil || t0.IsZero() || !t0.Before(threshold) {
315 t.Errorf("Setting mtime failed: %v, %v", t0, err)
318 // Write the same block again.
319 if err := v.Put(TestHash, TestBlock); err != nil {
323 // Verify threshold < t1
324 if t1, err := v.Mtime(TestHash); err != nil {
326 } else if t1.Before(threshold) {
327 t.Errorf("t1 %v should be >= threshold %v after v.Put ", t1, threshold)
331 // Touching a non-existing block should result in error.
332 // Test should pass for both writable and read-only volumes
333 func testTouchNoSuchBlock(t TB, factory TestableVolumeFactory) {
337 if err := v.Touch(TestHash); err == nil {
338 t.Error("Expected error when attempted to touch a non-existing block")
342 // Invoking Mtime on a non-existing block should result in error.
343 // Test should pass for both writable and read-only volumes
344 func testMtimeNoSuchBlock(t TB, factory TestableVolumeFactory) {
348 if _, err := v.Mtime("12345678901234567890123456789012"); err == nil {
349 t.Error("Expected error when updating Mtime on a non-existing block")
353 // Put a few blocks and invoke IndexTo with:
356 // * with no such prefix
357 // Test should pass for both writable and read-only volumes
358 func testIndexTo(t TB, factory TestableVolumeFactory) {
362 v.PutRaw(TestHash, TestBlock)
363 v.PutRaw(TestHash2, TestBlock2)
364 v.PutRaw(TestHash3, TestBlock3)
366 // Blocks whose names aren't Keep hashes should be omitted from
368 v.PutRaw("fffffffffnotreallyahashfffffffff", nil)
369 v.PutRaw("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", nil)
370 v.PutRaw("f0000000000000000000000000000000f", nil)
373 buf := new(bytes.Buffer)
375 indexRows := strings.Split(string(buf.Bytes()), "\n")
376 sort.Strings(indexRows)
377 sortedIndex := strings.Join(indexRows, "\n")
378 m, err := regexp.MatchString(
379 `^\n`+TestHash+`\+\d+ \d+\n`+
380 TestHash3+`\+\d+ \d+\n`+
381 TestHash2+`\+\d+ \d+$`,
386 t.Errorf("Got index %q for empty prefix", sortedIndex)
389 for _, prefix := range []string{"f", "f15", "f15ac"} {
390 buf = new(bytes.Buffer)
391 v.IndexTo(prefix, buf)
393 m, err := regexp.MatchString(`^`+TestHash2+`\+\d+ \d+\n$`, string(buf.Bytes()))
397 t.Errorf("Got index %q for prefix %s", string(buf.Bytes()), prefix)
401 for _, prefix := range []string{"zero", "zip", "zilch"} {
402 buf = new(bytes.Buffer)
403 v.IndexTo(prefix, buf)
405 t.Errorf("Got error on IndexTo with no such prefix %v", err.Error())
406 } else if buf.Len() != 0 {
407 t.Errorf("Expected empty list for IndexTo with no such prefix %s", prefix)
412 // Calling Delete() for a block immediately after writing it (not old enough)
413 // should neither delete the data nor return an error.
414 // Test is intended for only writable volumes
415 func testDeleteNewBlock(t TB, factory TestableVolumeFactory) {
418 blobSignatureTTL = 300 * time.Second
420 if v.Writable() == false {
424 v.Put(TestHash, TestBlock)
426 if err := v.Trash(TestHash); err != nil {
429 data, err := v.Get(TestHash)
433 if bytes.Compare(data, TestBlock) != 0 {
434 t.Errorf("Got data %+q, expected %+q", data, TestBlock)
440 // Calling Delete() for a block with a timestamp older than
441 // blobSignatureTTL seconds in the past should delete the data.
442 // Test is intended for only writable volumes
443 func testDeleteOldBlock(t TB, factory TestableVolumeFactory) {
446 blobSignatureTTL = 300 * time.Second
448 if v.Writable() == false {
452 v.Put(TestHash, TestBlock)
453 v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
455 if err := v.Trash(TestHash); err != nil {
458 if _, err := v.Get(TestHash); err == nil || !os.IsNotExist(err) {
459 t.Errorf("os.IsNotExist(%v) should have been true", err)
463 // Calling Delete() for a block that does not exist should result in error.
464 // Test should pass for both writable and read-only volumes
465 func testDeleteNoSuchBlock(t TB, factory TestableVolumeFactory) {
469 if err := v.Trash(TestHash2); err == nil {
470 t.Errorf("Expected error when attempting to delete a non-existing block")
474 // Invoke Status and verify that VolumeStatus is returned
475 // Test should pass for both writable and read-only volumes
476 func testStatus(t TB, factory TestableVolumeFactory) {
480 // Get node status and make a basic sanity check.
482 if status.DeviceNum == 0 {
483 t.Errorf("uninitialized device_num in %v", status)
486 if status.BytesFree == 0 {
487 t.Errorf("uninitialized bytes_free in %v", status)
490 if status.BytesUsed == 0 {
491 t.Errorf("uninitialized bytes_used in %v", status)
495 // Invoke String for the volume; expect non-empty result
496 // Test should pass for both writable and read-only volumes
497 func testString(t TB, factory TestableVolumeFactory) {
501 if id := v.String(); len(id) == 0 {
502 t.Error("Got empty string for v.String()")
506 // Putting, updating, touching, and deleting blocks from a read-only volume result in error.
507 // Test is intended for only read-only volumes
508 func testUpdateReadOnly(t TB, factory TestableVolumeFactory) {
512 if v.Writable() == true {
516 v.PutRaw(TestHash, TestBlock)
518 // Get from read-only volume should succeed
519 _, err := v.Get(TestHash)
521 t.Errorf("got err %v, expected nil", err)
524 // Put a new block to read-only volume should result in error
525 err = v.Put(TestHash2, TestBlock2)
527 t.Errorf("Expected error when putting block in a read-only volume")
529 _, err = v.Get(TestHash2)
531 t.Errorf("Expected error when getting block whose put in read-only volume failed")
534 // Touch a block in read-only volume should result in error
535 err = v.Touch(TestHash)
537 t.Errorf("Expected error when touching block in a read-only volume")
540 // Delete a block from a read-only volume should result in error
541 err = v.Trash(TestHash)
543 t.Errorf("Expected error when deleting block from a read-only volume")
546 // Overwriting an existing block in read-only volume should result in error
547 err = v.Put(TestHash, TestBlock)
549 t.Errorf("Expected error when putting block in a read-only volume")
553 // Launch concurrent Gets
554 // Test should pass for both writable and read-only volumes
555 func testGetConcurrent(t TB, factory TestableVolumeFactory) {
559 v.PutRaw(TestHash, TestBlock)
560 v.PutRaw(TestHash2, TestBlock2)
561 v.PutRaw(TestHash3, TestBlock3)
563 sem := make(chan int)
564 go func(sem chan int) {
565 buf, err := v.Get(TestHash)
567 t.Errorf("err1: %v", err)
570 if bytes.Compare(buf, TestBlock) != 0 {
571 t.Errorf("buf should be %s, is %s", string(TestBlock), string(buf))
576 go func(sem chan int) {
577 buf, err := v.Get(TestHash2)
579 t.Errorf("err2: %v", err)
582 if bytes.Compare(buf, TestBlock2) != 0 {
583 t.Errorf("buf should be %s, is %s", string(TestBlock2), string(buf))
588 go func(sem chan int) {
589 buf, err := v.Get(TestHash3)
591 t.Errorf("err3: %v", err)
594 if bytes.Compare(buf, TestBlock3) != 0 {
595 t.Errorf("buf should be %s, is %s", string(TestBlock3), string(buf))
600 // Wait for all goroutines to finish
601 for done := 0; done < 3; {
606 // Launch concurrent Puts
607 // Test is intended for only writable volumes
608 func testPutConcurrent(t TB, factory TestableVolumeFactory) {
612 if v.Writable() == false {
616 sem := make(chan int)
617 go func(sem chan int) {
618 err := v.Put(TestHash, TestBlock)
620 t.Errorf("err1: %v", err)
625 go func(sem chan int) {
626 err := v.Put(TestHash2, TestBlock2)
628 t.Errorf("err2: %v", err)
633 go func(sem chan int) {
634 err := v.Put(TestHash3, TestBlock3)
636 t.Errorf("err3: %v", err)
641 // Wait for all goroutines to finish
642 for done := 0; done < 3; {
646 // Double check that we actually wrote the blocks we expected to write.
647 buf, err := v.Get(TestHash)
649 t.Errorf("Get #1: %v", err)
652 if bytes.Compare(buf, TestBlock) != 0 {
653 t.Errorf("Get #1: expected %s, got %s", string(TestBlock), string(buf))
656 buf, err = v.Get(TestHash2)
658 t.Errorf("Get #2: %v", err)
661 if bytes.Compare(buf, TestBlock2) != 0 {
662 t.Errorf("Get #2: expected %s, got %s", string(TestBlock2), string(buf))
665 buf, err = v.Get(TestHash3)
667 t.Errorf("Get #3: %v", err)
670 if bytes.Compare(buf, TestBlock3) != 0 {
671 t.Errorf("Get #3: expected %s, got %s", string(TestBlock3), string(buf))
675 // Write and read back a full size block
676 func testPutFullBlock(t TB, factory TestableVolumeFactory) {
684 wdata := make([]byte, BlockSize)
686 wdata[BlockSize-1] = 'z'
687 hash := fmt.Sprintf("%x", md5.Sum(wdata))
688 err := v.Put(hash, wdata)
692 rdata, err := v.Get(hash)
696 defer bufs.Put(rdata)
698 if bytes.Compare(rdata, wdata) != 0 {
699 t.Error("rdata != wdata")
703 // With trashLifetime != 0, perform:
704 // Trash an old block - which either raises ErrNotImplemented or succeeds
705 // Untrash - which either raises ErrNotImplemented or succeeds
706 // Get - which must succeed
707 func testTrashUntrash(t TB, factory TestableVolumeFactory) {
711 trashLifetime = 0 * time.Second
714 trashLifetime = 3600 * time.Second
716 // put block and backdate it
717 v.PutRaw(TestHash, TestBlock)
718 v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
720 buf, err := v.Get(TestHash)
724 if bytes.Compare(buf, TestBlock) != 0 {
725 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
730 err = v.Trash(TestHash)
731 if v.Writable() == false {
732 if err != MethodDisabledError {
735 } else if err != nil {
736 if err != ErrNotImplemented {
740 _, err = v.Get(TestHash)
741 if err == nil || !os.IsNotExist(err) {
742 t.Errorf("os.IsNotExist(%v) should have been true", err)
746 err = v.Untrash(TestHash)
752 // Get the block - after trash and untrash sequence
753 buf, err = v.Get(TestHash)
757 if bytes.Compare(buf, TestBlock) != 0 {
758 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
763 func testTrashEmptyTrashUntrash(t TB, factory TestableVolumeFactory) {
766 defer func(orig time.Duration) {
770 checkGet := func() error {
771 buf, err := v.Get(TestHash)
775 if bytes.Compare(buf, TestBlock) != 0 {
776 t.Fatalf("Got data %+q, expected %+q", buf, TestBlock)
782 // First set: EmptyTrash before reaching the trash deadline.
784 trashLifetime = 1 * time.Hour
786 v.PutRaw(TestHash, TestBlock)
787 v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
794 err = v.Trash(TestHash)
795 if err == MethodDisabledError || err == ErrNotImplemented {
796 // Skip the trash tests for read-only volumes, and
797 // volume types that don't support trashLifetime>0.
802 if err == nil || !os.IsNotExist(err) {
803 t.Fatalf("os.IsNotExist(%v) should have been true", err)
808 // Even after emptying the trash, we can untrash our block
809 // because the deadline hasn't been reached.
810 err = v.Untrash(TestHash)
819 // Untrash should fail if the only block in the trash has
820 // already been untrashed.
821 err = v.Untrash(TestHash)
822 if err == nil || !os.IsNotExist(err) {
823 t.Fatalf("os.IsNotExist(%v) should have been true", err)
826 // The failed Untrash should not interfere with our
827 // already-untrashed copy.
833 // Second set: EmptyTrash after the trash deadline has passed.
835 trashLifetime = 1 * time.Nanosecond
837 err = v.Trash(TestHash)
842 if err == nil || !os.IsNotExist(err) {
843 t.Fatalf("os.IsNotExist(%v) should have been true", err)
846 // Even though 1ns has passed, we can untrash because we
847 // haven't called EmptyTrash yet.
848 err = v.Untrash(TestHash)
857 // Trash it again, and this time call EmptyTrash so it really
859 err = v.Trash(TestHash)
861 if err == nil || !os.IsNotExist(err) {
862 t.Errorf("os.IsNotExist(%v) should have been true", err)
866 // Untrash won't find it
867 err = v.Untrash(TestHash)
868 if err == nil || !os.IsNotExist(err) {
869 t.Fatalf("os.IsNotExist(%v) should have been true", err)
872 // Get block won't find it
874 if err == nil || !os.IsNotExist(err) {
875 t.Fatalf("os.IsNotExist(%v) should have been true", err)
878 // Third set: If the same data block gets written again after
879 // being trashed, and then the trash gets emptied, the newer
880 // un-trashed copy doesn't get deleted along with it.
882 v.PutRaw(TestHash, TestBlock)
883 v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
885 trashLifetime = time.Nanosecond
886 err = v.Trash(TestHash)
891 if err == nil || !os.IsNotExist(err) {
892 t.Fatalf("os.IsNotExist(%v) should have been true", err)
895 v.PutRaw(TestHash, TestBlock)
896 v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
898 // EmptyTrash should not delete the untrashed copy.
905 // Fourth set: If the same data block gets trashed twice with
906 // different deadlines A and C, and then the trash is emptied
907 // at intermediate time B (A < B < C), it is still possible to
908 // untrash the block whose deadline is "C".
910 v.PutRaw(TestHash, TestBlock)
911 v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
913 trashLifetime = time.Nanosecond
914 err = v.Trash(TestHash)
919 v.PutRaw(TestHash, TestBlock)
920 v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
922 trashLifetime = time.Hour
923 err = v.Trash(TestHash)
928 // EmptyTrash should not prevent us from recovering the
929 // time.Hour ("C") trash
931 err = v.Untrash(TestHash)