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 testEmptyTrashTrashLifetime0s(t, factory)
82 testEmptyTrashTrashLifetime3600s(t, factory)
83 testEmptyTrashTrashLifetime1s(t, factory)
86 // Put a test block, get it and verify content
87 // Test should pass for both writable and read-only volumes
88 func testGet(t TB, factory TestableVolumeFactory) {
92 v.PutRaw(TestHash, TestBlock)
94 buf, err := v.Get(TestHash)
101 if bytes.Compare(buf, TestBlock) != 0 {
102 t.Errorf("expected %s, got %s", string(TestBlock), string(buf))
106 // Invoke get on a block that does not exist in volume; should result in error
107 // Test should pass for both writable and read-only volumes
108 func testGetNoSuchBlock(t TB, factory TestableVolumeFactory) {
112 if _, err := v.Get(TestHash2); err == nil {
113 t.Errorf("Expected error while getting non-existing block %v", TestHash2)
117 // Compare() should return os.ErrNotExist if the block does not exist.
118 // Otherwise, writing new data causes CompareAndTouch() to generate
119 // error logs even though everything is working fine.
120 func testCompareNonexistent(t TB, factory TestableVolumeFactory) {
124 err := v.Compare(TestHash, TestBlock)
125 if err != os.ErrNotExist {
126 t.Errorf("Got err %T %q, expected os.ErrNotExist", err, err)
130 // Put a test block and compare the locator with same content
131 // Test should pass for both writable and read-only volumes
132 func testCompareSameContent(t TB, factory TestableVolumeFactory, testHash string, testData []byte) {
136 v.PutRaw(testHash, testData)
138 // Compare the block locator with same content
139 err := v.Compare(testHash, testData)
141 t.Errorf("Got err %q, expected nil", err)
145 // Test behavior of Compare() when stored data matches expected
146 // checksum but differs from new data we need to store. Requires
147 // testHash = md5(testDataA).
149 // Test should pass for both writable and read-only volumes
150 func testCompareWithCollision(t TB, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
154 v.PutRaw(testHash, testDataA)
156 // Compare the block locator with different content; collision
157 err := v.Compare(TestHash, testDataB)
159 t.Errorf("Got err nil, expected error due to collision")
163 // Test behavior of Compare() when stored data has become
164 // corrupted. Requires testHash = md5(testDataA) != md5(testDataB).
166 // Test should pass for both writable and read-only volumes
167 func testCompareWithCorruptStoredData(t TB, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
171 v.PutRaw(TestHash, testDataB)
173 err := v.Compare(testHash, testDataA)
174 if err == nil || err == CollisionError {
175 t.Errorf("Got err %+v, expected non-collision error", err)
179 // Put a block and put again with same content
180 // Test is intended for only writable volumes
181 func testPutBlockWithSameContent(t TB, factory TestableVolumeFactory, testHash string, testData []byte) {
185 if v.Writable() == false {
189 err := v.Put(testHash, testData)
191 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
194 err = v.Put(testHash, testData)
196 t.Errorf("Got err putting block second time %q: %q, expected nil", TestBlock, err)
200 // Put a block and put again with different content
201 // Test is intended for only writable volumes
202 func testPutBlockWithDifferentContent(t TB, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
206 if v.Writable() == false {
210 v.PutRaw(testHash, testDataA)
212 putErr := v.Put(testHash, testDataB)
213 buf, getErr := v.Get(testHash)
215 // Put must not return a nil error unless it has
216 // overwritten the existing data.
217 if bytes.Compare(buf, testDataB) != 0 {
218 t.Errorf("Put succeeded but Get returned %+q, expected %+q", buf, testDataB)
221 // It is permissible for Put to fail, but it must
222 // leave us with either the original data, the new
223 // data, or nothing at all.
224 if getErr == nil && bytes.Compare(buf, testDataA) != 0 && bytes.Compare(buf, testDataB) != 0 {
225 t.Errorf("Put failed but Get returned %+q, which is neither %+q nor %+q", buf, testDataA, testDataB)
233 // Put and get multiple blocks
234 // Test is intended for only writable volumes
235 func testPutMultipleBlocks(t TB, factory TestableVolumeFactory) {
239 if v.Writable() == false {
243 err := v.Put(TestHash, TestBlock)
245 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
248 err = v.Put(TestHash2, TestBlock2)
250 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock2, err)
253 err = v.Put(TestHash3, TestBlock3)
255 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock3, err)
258 data, err := v.Get(TestHash)
262 if bytes.Compare(data, TestBlock) != 0 {
263 t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock)
268 data, err = v.Get(TestHash2)
272 if bytes.Compare(data, TestBlock2) != 0 {
273 t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock2)
278 data, err = v.Get(TestHash3)
282 if bytes.Compare(data, TestBlock3) != 0 {
283 t.Errorf("Block present, but to %+q, expected %+q", data, TestBlock3)
290 // Test that when applying PUT to a block that already exists,
291 // the block's modification time is updated.
292 // Test is intended for only writable volumes
293 func testPutAndTouch(t TB, factory TestableVolumeFactory) {
297 if v.Writable() == false {
301 if err := v.Put(TestHash, TestBlock); err != nil {
305 // We'll verify { t0 < threshold < t1 }, where t0 is the
306 // existing block's timestamp on disk before Put() and t1 is
307 // its timestamp after Put().
308 threshold := time.Now().Add(-time.Second)
310 // Set the stored block's mtime far enough in the past that we
311 // can see the difference between "timestamp didn't change"
312 // and "timestamp granularity is too low".
313 v.TouchWithDate(TestHash, time.Now().Add(-20*time.Second))
315 // Make sure v.Mtime() agrees the above Utime really worked.
316 if t0, err := v.Mtime(TestHash); err != nil || t0.IsZero() || !t0.Before(threshold) {
317 t.Errorf("Setting mtime failed: %v, %v", t0, err)
320 // Write the same block again.
321 if err := v.Put(TestHash, TestBlock); err != nil {
325 // Verify threshold < t1
326 if t1, err := v.Mtime(TestHash); err != nil {
328 } else if t1.Before(threshold) {
329 t.Errorf("t1 %v should be >= threshold %v after v.Put ", t1, threshold)
333 // Touching a non-existing block should result in error.
334 // Test should pass for both writable and read-only volumes
335 func testTouchNoSuchBlock(t TB, factory TestableVolumeFactory) {
339 if err := v.Touch(TestHash); err == nil {
340 t.Error("Expected error when attempted to touch a non-existing block")
344 // Invoking Mtime on a non-existing block should result in error.
345 // Test should pass for both writable and read-only volumes
346 func testMtimeNoSuchBlock(t TB, factory TestableVolumeFactory) {
350 if _, err := v.Mtime("12345678901234567890123456789012"); err == nil {
351 t.Error("Expected error when updating Mtime on a non-existing block")
355 // Put a few blocks and invoke IndexTo with:
358 // * with no such prefix
359 // Test should pass for both writable and read-only volumes
360 func testIndexTo(t TB, factory TestableVolumeFactory) {
364 v.PutRaw(TestHash, TestBlock)
365 v.PutRaw(TestHash2, TestBlock2)
366 v.PutRaw(TestHash3, TestBlock3)
368 // Blocks whose names aren't Keep hashes should be omitted from
370 v.PutRaw("fffffffffnotreallyahashfffffffff", nil)
371 v.PutRaw("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", nil)
372 v.PutRaw("f0000000000000000000000000000000f", nil)
375 buf := new(bytes.Buffer)
377 indexRows := strings.Split(string(buf.Bytes()), "\n")
378 sort.Strings(indexRows)
379 sortedIndex := strings.Join(indexRows, "\n")
380 m, err := regexp.MatchString(
381 `^\n`+TestHash+`\+\d+ \d+\n`+
382 TestHash3+`\+\d+ \d+\n`+
383 TestHash2+`\+\d+ \d+$`,
388 t.Errorf("Got index %q for empty prefix", sortedIndex)
391 for _, prefix := range []string{"f", "f15", "f15ac"} {
392 buf = new(bytes.Buffer)
393 v.IndexTo(prefix, buf)
395 m, err := regexp.MatchString(`^`+TestHash2+`\+\d+ \d+\n$`, string(buf.Bytes()))
399 t.Errorf("Got index %q for prefix %s", string(buf.Bytes()), prefix)
403 for _, prefix := range []string{"zero", "zip", "zilch"} {
404 buf = new(bytes.Buffer)
405 v.IndexTo(prefix, buf)
407 t.Errorf("Got error on IndexTo with no such prefix %v", err.Error())
408 } else if buf.Len() != 0 {
409 t.Errorf("Expected empty list for IndexTo with no such prefix %s", prefix)
414 // Calling Delete() for a block immediately after writing it (not old enough)
415 // should neither delete the data nor return an error.
416 // Test is intended for only writable volumes
417 func testDeleteNewBlock(t TB, factory TestableVolumeFactory) {
420 blobSignatureTTL = 300 * time.Second
422 if v.Writable() == false {
426 v.Put(TestHash, TestBlock)
428 if err := v.Trash(TestHash); err != nil {
431 data, err := v.Get(TestHash)
435 if bytes.Compare(data, TestBlock) != 0 {
436 t.Errorf("Got data %+q, expected %+q", data, TestBlock)
442 // Calling Delete() for a block with a timestamp older than
443 // blobSignatureTTL seconds in the past should delete the data.
444 // Test is intended for only writable volumes
445 func testDeleteOldBlock(t TB, factory TestableVolumeFactory) {
448 blobSignatureTTL = 300 * time.Second
450 if v.Writable() == false {
454 v.Put(TestHash, TestBlock)
455 v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
457 if err := v.Trash(TestHash); err != nil {
460 if _, err := v.Get(TestHash); err == nil || !os.IsNotExist(err) {
461 t.Errorf("os.IsNotExist(%v) should have been true", err)
465 // Calling Delete() for a block that does not exist should result in error.
466 // Test should pass for both writable and read-only volumes
467 func testDeleteNoSuchBlock(t TB, factory TestableVolumeFactory) {
471 if err := v.Trash(TestHash2); err == nil {
472 t.Errorf("Expected error when attempting to delete a non-existing block")
476 // Invoke Status and verify that VolumeStatus is returned
477 // Test should pass for both writable and read-only volumes
478 func testStatus(t TB, factory TestableVolumeFactory) {
482 // Get node status and make a basic sanity check.
484 if status.DeviceNum == 0 {
485 t.Errorf("uninitialized device_num in %v", status)
488 if status.BytesFree == 0 {
489 t.Errorf("uninitialized bytes_free in %v", status)
492 if status.BytesUsed == 0 {
493 t.Errorf("uninitialized bytes_used in %v", status)
497 // Invoke String for the volume; expect non-empty result
498 // Test should pass for both writable and read-only volumes
499 func testString(t TB, factory TestableVolumeFactory) {
503 if id := v.String(); len(id) == 0 {
504 t.Error("Got empty string for v.String()")
508 // Putting, updating, touching, and deleting blocks from a read-only volume result in error.
509 // Test is intended for only read-only volumes
510 func testUpdateReadOnly(t TB, factory TestableVolumeFactory) {
514 if v.Writable() == true {
518 v.PutRaw(TestHash, TestBlock)
520 // Get from read-only volume should succeed
521 _, err := v.Get(TestHash)
523 t.Errorf("got err %v, expected nil", err)
526 // Put a new block to read-only volume should result in error
527 err = v.Put(TestHash2, TestBlock2)
529 t.Errorf("Expected error when putting block in a read-only volume")
531 _, err = v.Get(TestHash2)
533 t.Errorf("Expected error when getting block whose put in read-only volume failed")
536 // Touch a block in read-only volume should result in error
537 err = v.Touch(TestHash)
539 t.Errorf("Expected error when touching block in a read-only volume")
542 // Delete a block from a read-only volume should result in error
543 err = v.Trash(TestHash)
545 t.Errorf("Expected error when deleting block from a read-only volume")
548 // Overwriting an existing block in read-only volume should result in error
549 err = v.Put(TestHash, TestBlock)
551 t.Errorf("Expected error when putting block in a read-only volume")
555 // Launch concurrent Gets
556 // Test should pass for both writable and read-only volumes
557 func testGetConcurrent(t TB, factory TestableVolumeFactory) {
561 v.PutRaw(TestHash, TestBlock)
562 v.PutRaw(TestHash2, TestBlock2)
563 v.PutRaw(TestHash3, TestBlock3)
565 sem := make(chan int)
566 go func(sem chan int) {
567 buf, err := v.Get(TestHash)
569 t.Errorf("err1: %v", err)
572 if bytes.Compare(buf, TestBlock) != 0 {
573 t.Errorf("buf should be %s, is %s", string(TestBlock), string(buf))
578 go func(sem chan int) {
579 buf, err := v.Get(TestHash2)
581 t.Errorf("err2: %v", err)
584 if bytes.Compare(buf, TestBlock2) != 0 {
585 t.Errorf("buf should be %s, is %s", string(TestBlock2), string(buf))
590 go func(sem chan int) {
591 buf, err := v.Get(TestHash3)
593 t.Errorf("err3: %v", err)
596 if bytes.Compare(buf, TestBlock3) != 0 {
597 t.Errorf("buf should be %s, is %s", string(TestBlock3), string(buf))
602 // Wait for all goroutines to finish
603 for done := 0; done < 3; {
608 // Launch concurrent Puts
609 // Test is intended for only writable volumes
610 func testPutConcurrent(t TB, factory TestableVolumeFactory) {
614 if v.Writable() == false {
618 sem := make(chan int)
619 go func(sem chan int) {
620 err := v.Put(TestHash, TestBlock)
622 t.Errorf("err1: %v", err)
627 go func(sem chan int) {
628 err := v.Put(TestHash2, TestBlock2)
630 t.Errorf("err2: %v", err)
635 go func(sem chan int) {
636 err := v.Put(TestHash3, TestBlock3)
638 t.Errorf("err3: %v", err)
643 // Wait for all goroutines to finish
644 for done := 0; done < 3; {
648 // Double check that we actually wrote the blocks we expected to write.
649 buf, err := v.Get(TestHash)
651 t.Errorf("Get #1: %v", err)
654 if bytes.Compare(buf, TestBlock) != 0 {
655 t.Errorf("Get #1: expected %s, got %s", string(TestBlock), string(buf))
658 buf, err = v.Get(TestHash2)
660 t.Errorf("Get #2: %v", err)
663 if bytes.Compare(buf, TestBlock2) != 0 {
664 t.Errorf("Get #2: expected %s, got %s", string(TestBlock2), string(buf))
667 buf, err = v.Get(TestHash3)
669 t.Errorf("Get #3: %v", err)
672 if bytes.Compare(buf, TestBlock3) != 0 {
673 t.Errorf("Get #3: expected %s, got %s", string(TestBlock3), string(buf))
677 // Write and read back a full size block
678 func testPutFullBlock(t TB, factory TestableVolumeFactory) {
686 wdata := make([]byte, BlockSize)
688 wdata[BlockSize-1] = 'z'
689 hash := fmt.Sprintf("%x", md5.Sum(wdata))
690 err := v.Put(hash, wdata)
694 rdata, err := v.Get(hash)
698 defer bufs.Put(rdata)
700 if bytes.Compare(rdata, wdata) != 0 {
701 t.Error("rdata != wdata")
705 // With trashLifetime != 0, perform:
706 // Trash an old block - which either raises ErrNotImplemented or succeeds
707 // Untrash - which either raises ErrNotImplemented or succeeds
708 // Get - which must succeed
709 func testTrashUntrash(t TB, factory TestableVolumeFactory) {
718 // put block and backdate it
719 v.PutRaw(TestHash, TestBlock)
720 v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
722 buf, err := v.Get(TestHash)
726 if bytes.Compare(buf, TestBlock) != 0 {
727 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
732 err = v.Trash(TestHash)
733 if v.Writable() == false {
734 if err != MethodDisabledError {
737 } else if err != nil {
738 if err != ErrNotImplemented {
742 _, err = v.Get(TestHash)
743 if err == nil || !os.IsNotExist(err) {
744 t.Errorf("os.IsNotExist(%v) should have been true", err)
748 err = v.Untrash(TestHash)
754 // Get the block - after trash and untrash sequence
755 buf, err = v.Get(TestHash)
759 if bytes.Compare(buf, TestBlock) != 0 {
760 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
765 // With trashLifetime == 0, perform:
766 // Trash an old block - which either raises ErrNotImplemented or succeeds to delete it
767 // Untrash - which either raises ErrNotImplemented or is a no-op for the deleted block
768 // Get - which must fail to find the block, since it was deleted and hence not untrashed
769 func testEmptyTrashTrashLifetime0s(t TB, factory TestableVolumeFactory) {
774 doneEmptyingTrash <- true
778 trashCheckInterval = 1
780 go emptyTrash(trashCheckInterval)
782 // Trash old block; since trashLifetime = 0, Trash actually deletes the block
783 err := trashUntrashOldBlock(t, v, 0)
785 // Get it; for writable volumes, this should not find the block since it was deleted
786 buf, err := v.Get(TestHash)
788 if !os.IsNotExist(err) {
789 t.Errorf("os.IsNotExist(%v) should have been true", err)
792 if bytes.Compare(buf, TestBlock) != 0 {
793 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
799 // With large trashLifetime, perform:
800 // Run emptyTrash goroutine with much smaller trashCheckInterval
801 // Trash an old block - which either raises ErrNotImplemented or succeeds
802 // Untrash - which either raises ErrNotImplemented or succeeds
803 // Get - which must find the block
804 func testEmptyTrashTrashLifetime3600s(t TB, factory TestableVolumeFactory) {
809 doneEmptyingTrash <- true
813 trashCheckInterval = 1
815 go emptyTrash(trashCheckInterval)
818 err := trashUntrashOldBlock(t, v, 2)
820 // Get is expected to succeed after untrash before EmptyTrash
821 // It is still found on readonly volumes
822 buf, err := v.Get(TestHash)
824 if !os.IsNotExist(err) {
825 t.Errorf("os.IsNotExist(%v) should have been true", err)
828 if bytes.Compare(buf, TestBlock) != 0 {
829 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
835 // With trashLifetime = 1, perform:
836 // Run emptyTrash goroutine
837 // Trash an old block - which either raises ErrNotImplemented or succeeds
838 // Untrash - after emptyTrash goroutine ticks, and hence does not actually untrash
839 // Get - which must fail to find the block
840 func testEmptyTrashTrashLifetime1s(t TB, factory TestableVolumeFactory) {
845 doneEmptyingTrash <- true
848 volumes = append(volumes, v)
851 trashCheckInterval = 1
853 go emptyTrash(trashCheckInterval)
855 // Trash old block and untrash a little after first trashCheckInterval
856 err := trashUntrashOldBlock(t, v, 3)
858 // Get is expected to fail due to EmptyTrash before Untrash
859 // It is still found on readonly volumes
860 buf, err := v.Get(TestHash)
862 if !os.IsNotExist(err) {
863 t.Errorf("os.IsNotExist(%v) should have been true", err)
866 if bytes.Compare(buf, TestBlock) != 0 {
867 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
873 // Put a block, backdate it, trash it, untrash it after the untrashAfter seconds
874 func trashUntrashOldBlock(t TB, v TestableVolume, untrashAfter int) error {
875 // put block and backdate it
876 v.PutRaw(TestHash, TestBlock)
877 v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
879 buf, err := v.Get(TestHash)
883 if bytes.Compare(buf, TestBlock) != 0 {
884 t.Fatalf("Got data %+q, expected %+q", buf, TestBlock)
889 err = v.Trash(TestHash)
891 if err != ErrNotImplemented && err != MethodDisabledError {
894 // To test emptyTrash goroutine effectively, we need to give the
895 // ticker a couple rounds, adding some sleep time to the test.
896 // This delay is unnecessary for volumes that are currently
897 // not yet supporting trashLifetime > 0 (this case is already
898 // covered in the testTrashUntrash already)
902 _, err = v.Get(TestHash)
903 if err == nil || !os.IsNotExist(err) {
904 t.Fatalf("os.IsNotExist(%v) should have been true", err)
908 // Untrash after give wait time
909 time.Sleep(time.Duration(untrashAfter) * time.Second)
910 err = v.Untrash(TestHash)
912 if err != ErrNotImplemented && err != MethodDisabledError {