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)
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) {
89 v.PutRaw(TestHash, TestBlock)
91 buf, err := v.Get(TestHash)
98 if bytes.Compare(buf, TestBlock) != 0 {
99 t.Errorf("expected %s, got %s", string(TestBlock), string(buf))
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) {
109 if _, err := v.Get(TestHash2); err == nil {
110 t.Errorf("Expected error while getting non-existing block %v", TestHash2)
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) {
121 err := v.Compare(TestHash, TestBlock)
122 if err != os.ErrNotExist {
123 t.Errorf("Got err %T %q, expected os.ErrNotExist", err, err)
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) {
133 v.PutRaw(testHash, testData)
135 // Compare the block locator with same content
136 err := v.Compare(testHash, testData)
138 t.Errorf("Got err %q, expected nil", err)
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).
146 // Test should pass for both writable and read-only volumes
147 func testCompareWithCollision(t TB, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
151 v.PutRaw(testHash, testDataA)
153 // Compare the block locator with different content; collision
154 err := v.Compare(TestHash, testDataB)
156 t.Errorf("Got err nil, expected error due to collision")
160 // Test behavior of Compare() when stored data has become
161 // corrupted. Requires testHash = md5(testDataA) != md5(testDataB).
163 // Test should pass for both writable and read-only volumes
164 func testCompareWithCorruptStoredData(t TB, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
168 v.PutRaw(TestHash, testDataB)
170 err := v.Compare(testHash, testDataA)
171 if err == nil || err == CollisionError {
172 t.Errorf("Got err %+v, expected non-collision error", err)
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) {
182 if v.Writable() == false {
186 err := v.Put(testHash, testData)
188 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
191 err = v.Put(testHash, testData)
193 t.Errorf("Got err putting block second time %q: %q, expected nil", TestBlock, err)
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) {
203 if v.Writable() == false {
207 v.PutRaw(testHash, testDataA)
209 putErr := v.Put(testHash, testDataB)
210 buf, getErr := v.Get(testHash)
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)
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)
230 // Put and get multiple blocks
231 // Test is intended for only writable volumes
232 func testPutMultipleBlocks(t TB, factory TestableVolumeFactory) {
236 if v.Writable() == false {
240 err := v.Put(TestHash, TestBlock)
242 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
245 err = v.Put(TestHash2, TestBlock2)
247 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock2, err)
250 err = v.Put(TestHash3, TestBlock3)
252 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock3, err)
255 data, err := v.Get(TestHash)
259 if bytes.Compare(data, TestBlock) != 0 {
260 t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock)
265 data, err = v.Get(TestHash2)
269 if bytes.Compare(data, TestBlock2) != 0 {
270 t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock2)
275 data, err = v.Get(TestHash3)
279 if bytes.Compare(data, TestBlock3) != 0 {
280 t.Errorf("Block present, but to %+q, expected %+q", data, TestBlock3)
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) {
294 if v.Writable() == false {
298 if err := v.Put(TestHash, TestBlock); err != nil {
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)
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))
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)
317 // Write the same block again.
318 if err := v.Put(TestHash, TestBlock); err != nil {
322 // Verify threshold < t1
323 if t1, err := v.Mtime(TestHash); err != nil {
325 } else if t1.Before(threshold) {
326 t.Errorf("t1 %v should be >= threshold %v after v.Put ", t1, threshold)
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) {
336 if err := v.Touch(TestHash); err == nil {
337 t.Error("Expected error when attempted to touch a non-existing block")
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) {
347 if _, err := v.Mtime("12345678901234567890123456789012"); err == nil {
348 t.Error("Expected error when updating Mtime on a non-existing block")
352 // Put a few blocks and invoke IndexTo with:
355 // * with no such prefix
356 // Test should pass for both writable and read-only volumes
357 func testIndexTo(t TB, factory TestableVolumeFactory) {
361 v.PutRaw(TestHash, TestBlock)
362 v.PutRaw(TestHash2, TestBlock2)
363 v.PutRaw(TestHash3, TestBlock3)
365 // Blocks whose names aren't Keep hashes should be omitted from
367 v.PutRaw("fffffffffnotreallyahashfffffffff", nil)
368 v.PutRaw("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", nil)
369 v.PutRaw("f0000000000000000000000000000000f", nil)
372 buf := new(bytes.Buffer)
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+$`,
385 t.Errorf("Got index %q for empty prefix", sortedIndex)
388 for _, prefix := range []string{"f", "f15", "f15ac"} {
389 buf = new(bytes.Buffer)
390 v.IndexTo(prefix, buf)
392 m, err := regexp.MatchString(`^`+TestHash2+`\+\d+ \d+\n$`, string(buf.Bytes()))
396 t.Errorf("Got index %q for prefix %s", string(buf.Bytes()), prefix)
400 for _, prefix := range []string{"zero", "zip", "zilch"} {
401 buf = new(bytes.Buffer)
402 v.IndexTo(prefix, buf)
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)
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) {
417 blobSignatureTTL = 300 * time.Second
419 if v.Writable() == false {
423 v.Put(TestHash, TestBlock)
425 if err := v.Trash(TestHash); err != nil {
428 data, err := v.Get(TestHash)
432 if bytes.Compare(data, TestBlock) != 0 {
433 t.Errorf("Got data %+q, expected %+q", data, TestBlock)
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) {
445 blobSignatureTTL = 300 * time.Second
447 if v.Writable() == false {
451 v.Put(TestHash, TestBlock)
452 v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
454 if err := v.Trash(TestHash); err != nil {
457 if _, err := v.Get(TestHash); err == nil || !os.IsNotExist(err) {
458 t.Errorf("os.IsNotExist(%v) should have been true", err)
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) {
468 if err := v.Trash(TestHash2); err == nil {
469 t.Errorf("Expected error when attempting to delete a non-existing block")
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) {
479 // Get node status and make a basic sanity check.
481 if status.DeviceNum == 0 {
482 t.Errorf("uninitialized device_num in %v", status)
485 if status.BytesFree == 0 {
486 t.Errorf("uninitialized bytes_free in %v", status)
489 if status.BytesUsed == 0 {
490 t.Errorf("uninitialized bytes_used in %v", status)
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) {
500 if id := v.String(); len(id) == 0 {
501 t.Error("Got empty string for v.String()")
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) {
511 if v.Writable() == true {
515 v.PutRaw(TestHash, TestBlock)
517 // Get from read-only volume should succeed
518 _, err := v.Get(TestHash)
520 t.Errorf("got err %v, expected nil", err)
523 // Put a new block to read-only volume should result in error
524 err = v.Put(TestHash2, TestBlock2)
526 t.Errorf("Expected error when putting block in a read-only volume")
528 _, err = v.Get(TestHash2)
530 t.Errorf("Expected error when getting block whose put in read-only volume failed")
533 // Touch a block in read-only volume should result in error
534 err = v.Touch(TestHash)
536 t.Errorf("Expected error when touching block in a read-only volume")
539 // Delete a block from a read-only volume should result in error
540 err = v.Trash(TestHash)
542 t.Errorf("Expected error when deleting block from a read-only volume")
545 // Overwriting an existing block in read-only volume should result in error
546 err = v.Put(TestHash, TestBlock)
548 t.Errorf("Expected error when putting block in a read-only volume")
552 // Launch concurrent Gets
553 // Test should pass for both writable and read-only volumes
554 func testGetConcurrent(t TB, factory TestableVolumeFactory) {
558 v.PutRaw(TestHash, TestBlock)
559 v.PutRaw(TestHash2, TestBlock2)
560 v.PutRaw(TestHash3, TestBlock3)
562 sem := make(chan int)
563 go func(sem chan int) {
564 buf, err := v.Get(TestHash)
566 t.Errorf("err1: %v", err)
569 if bytes.Compare(buf, TestBlock) != 0 {
570 t.Errorf("buf should be %s, is %s", string(TestBlock), string(buf))
575 go func(sem chan int) {
576 buf, err := v.Get(TestHash2)
578 t.Errorf("err2: %v", err)
581 if bytes.Compare(buf, TestBlock2) != 0 {
582 t.Errorf("buf should be %s, is %s", string(TestBlock2), string(buf))
587 go func(sem chan int) {
588 buf, err := v.Get(TestHash3)
590 t.Errorf("err3: %v", err)
593 if bytes.Compare(buf, TestBlock3) != 0 {
594 t.Errorf("buf should be %s, is %s", string(TestBlock3), string(buf))
599 // Wait for all goroutines to finish
600 for done := 0; done < 3; {
605 // Launch concurrent Puts
606 // Test is intended for only writable volumes
607 func testPutConcurrent(t TB, factory TestableVolumeFactory) {
611 if v.Writable() == false {
615 sem := make(chan int)
616 go func(sem chan int) {
617 err := v.Put(TestHash, TestBlock)
619 t.Errorf("err1: %v", err)
624 go func(sem chan int) {
625 err := v.Put(TestHash2, TestBlock2)
627 t.Errorf("err2: %v", err)
632 go func(sem chan int) {
633 err := v.Put(TestHash3, TestBlock3)
635 t.Errorf("err3: %v", err)
640 // Wait for all goroutines to finish
641 for done := 0; done < 3; {
645 // Double check that we actually wrote the blocks we expected to write.
646 buf, err := v.Get(TestHash)
648 t.Errorf("Get #1: %v", err)
651 if bytes.Compare(buf, TestBlock) != 0 {
652 t.Errorf("Get #1: expected %s, got %s", string(TestBlock), string(buf))
655 buf, err = v.Get(TestHash2)
657 t.Errorf("Get #2: %v", err)
660 if bytes.Compare(buf, TestBlock2) != 0 {
661 t.Errorf("Get #2: expected %s, got %s", string(TestBlock2), string(buf))
664 buf, err = v.Get(TestHash3)
666 t.Errorf("Get #3: %v", err)
669 if bytes.Compare(buf, TestBlock3) != 0 {
670 t.Errorf("Get #3: expected %s, got %s", string(TestBlock3), string(buf))
674 // Write and read back a full size block
675 func testPutFullBlock(t TB, factory TestableVolumeFactory) {
683 wdata := make([]byte, BlockSize)
685 wdata[BlockSize-1] = 'z'
686 hash := fmt.Sprintf("%x", md5.Sum(wdata))
687 err := v.Put(hash, wdata)
691 rdata, err := v.Get(hash)
695 defer bufs.Put(rdata)
697 if bytes.Compare(rdata, wdata) != 0 {
698 t.Error("rdata != wdata")
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) {
713 trashLifetime = 3600 * time.Second
715 // put block and backdate it
716 v.PutRaw(TestHash, TestBlock)
717 v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
719 buf, err := v.Get(TestHash)
723 if bytes.Compare(buf, TestBlock) != 0 {
724 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)
729 err = v.Trash(TestHash)
730 if v.Writable() == false {
731 if err != MethodDisabledError {
734 } else if err != nil {
735 if err != ErrNotImplemented {
739 _, err = v.Get(TestHash)
740 if err == nil || !os.IsNotExist(err) {
741 t.Errorf("os.IsNotExist(%v) should have been true", err)
745 err = v.Untrash(TestHash)
751 // Get the block - after trash and untrash sequence
752 buf, err = v.Get(TestHash)
756 if bytes.Compare(buf, TestBlock) != 0 {
757 t.Errorf("Got data %+q, expected %+q", buf, TestBlock)