15 // A TestableVolumeFactory returns a new TestableVolume. The factory
16 // function, and the TestableVolume it returns, can use "t" to write
17 // logs, fail the current test, etc.
18 type TestableVolumeFactory func(t *testing.T) TestableVolume
20 // DoGenericVolumeTests runs a set of tests that every TestableVolume
21 // is expected to pass. It calls factory to create a new TestableVolume
22 // for each test case, to avoid leaking state between tests.
23 func DoGenericVolumeTests(t *testing.T, factory TestableVolumeFactory) {
25 testGetNoSuchBlock(t, factory)
27 testCompareNonexistent(t, factory)
28 testCompareSameContent(t, factory, TestHash, TestBlock)
29 testCompareSameContent(t, factory, EmptyHash, EmptyBlock)
30 testCompareWithCollision(t, factory, TestHash, TestBlock, []byte("baddata"))
31 testCompareWithCollision(t, factory, TestHash, TestBlock, EmptyBlock)
32 testCompareWithCollision(t, factory, EmptyHash, EmptyBlock, TestBlock)
33 testCompareWithCorruptStoredData(t, factory, TestHash, TestBlock, []byte("baddata"))
34 testCompareWithCorruptStoredData(t, factory, TestHash, TestBlock, EmptyBlock)
35 testCompareWithCorruptStoredData(t, factory, EmptyHash, EmptyBlock, []byte("baddata"))
37 testPutBlockWithSameContent(t, factory, TestHash, TestBlock)
38 testPutBlockWithSameContent(t, factory, EmptyHash, EmptyBlock)
39 testPutBlockWithDifferentContent(t, factory, TestHash, TestBlock, TestBlock2)
40 testPutBlockWithDifferentContent(t, factory, TestHash, EmptyBlock, TestBlock)
41 testPutBlockWithDifferentContent(t, factory, TestHash, TestBlock, EmptyBlock)
42 testPutBlockWithDifferentContent(t, factory, EmptyHash, EmptyBlock, TestBlock)
43 testPutMultipleBlocks(t, factory)
45 testPutAndTouch(t, factory)
46 testTouchNoSuchBlock(t, factory)
48 testMtimeNoSuchBlock(t, factory)
50 testIndexTo(t, factory)
52 testDeleteNewBlock(t, factory)
53 testDeleteOldBlock(t, factory)
54 testDeleteNoSuchBlock(t, factory)
56 testStatus(t, factory)
58 testString(t, factory)
60 testUpdateReadOnly(t, factory)
62 testGetConcurrent(t, factory)
63 testPutConcurrent(t, factory)
65 testPutFullBlock(t, factory)
68 // Put a test block, get it and verify content
69 // Test should pass for both writable and read-only volumes
70 func testGet(t *testing.T, factory TestableVolumeFactory) {
74 v.PutRaw(TestHash, TestBlock)
76 buf, err := v.Get(TestHash)
83 if bytes.Compare(buf, TestBlock) != 0 {
84 t.Errorf("expected %s, got %s", string(TestBlock), string(buf))
88 // Invoke get on a block that does not exist in volume; should result in error
89 // Test should pass for both writable and read-only volumes
90 func testGetNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
94 if _, err := v.Get(TestHash2); err == nil {
95 t.Errorf("Expected error while getting non-existing block %v", TestHash2)
99 // Compare() should return os.ErrNotExist if the block does not exist.
100 // Otherwise, writing new data causes CompareAndTouch() to generate
101 // error logs even though everything is working fine.
102 func testCompareNonexistent(t *testing.T, factory TestableVolumeFactory) {
106 err := v.Compare(TestHash, TestBlock)
107 if err != os.ErrNotExist {
108 t.Errorf("Got err %T %q, expected os.ErrNotExist", err, err)
112 // Put a test block and compare the locator with same content
113 // Test should pass for both writable and read-only volumes
114 func testCompareSameContent(t *testing.T, factory TestableVolumeFactory, testHash string, testData []byte) {
118 v.PutRaw(testHash, testData)
120 // Compare the block locator with same content
121 err := v.Compare(testHash, testData)
123 t.Errorf("Got err %q, expected nil", err)
127 // Test behavior of Compare() when stored data matches expected
128 // checksum but differs from new data we need to store. Requires
129 // testHash = md5(testDataA).
131 // Test should pass for both writable and read-only volumes
132 func testCompareWithCollision(t *testing.T, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
136 v.PutRaw(testHash, testDataA)
138 // Compare the block locator with different content; collision
139 err := v.Compare(TestHash, testDataB)
141 t.Errorf("Got err nil, expected error due to collision")
145 // Test behavior of Compare() when stored data has become
146 // corrupted. Requires testHash = md5(testDataA) != md5(testDataB).
148 // Test should pass for both writable and read-only volumes
149 func testCompareWithCorruptStoredData(t *testing.T, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
153 v.PutRaw(TestHash, testDataB)
155 err := v.Compare(testHash, testDataA)
156 if err == nil || err == CollisionError {
157 t.Errorf("Got err %+v, expected non-collision error", err)
161 // Put a block and put again with same content
162 // Test is intended for only writable volumes
163 func testPutBlockWithSameContent(t *testing.T, factory TestableVolumeFactory, testHash string, testData []byte) {
167 if v.Writable() == false {
171 err := v.Put(testHash, testData)
173 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
176 err = v.Put(testHash, testData)
178 t.Errorf("Got err putting block second time %q: %q, expected nil", TestBlock, err)
182 // Put a block and put again with different content
183 // Test is intended for only writable volumes
184 func testPutBlockWithDifferentContent(t *testing.T, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
188 if v.Writable() == false {
192 err := v.Put(testHash, testDataA)
194 t.Errorf("Got err putting block %q: %q, expected nil", testDataA, err)
197 putErr := v.Put(testHash, testDataB)
198 buf, getErr := v.Get(testHash)
200 // Put must not return a nil error unless it has
201 // overwritten the existing data.
202 if bytes.Compare(buf, testDataB) != 0 {
203 t.Errorf("Put succeeded but Get returned %+q, expected %+q", buf, testDataB)
206 // It is permissible for Put to fail, but it must
207 // leave us with either the original data, the new
208 // data, or nothing at all.
209 if getErr == nil && bytes.Compare(buf, testDataA) != 0 && bytes.Compare(buf, testDataB) != 0 {
210 t.Errorf("Put failed but Get returned %+q, which is neither %+q nor %+q", buf, testDataA, testDataB)
218 // Put and get multiple blocks
219 // Test is intended for only writable volumes
220 func testPutMultipleBlocks(t *testing.T, factory TestableVolumeFactory) {
224 if v.Writable() == false {
228 err := v.Put(TestHash, TestBlock)
230 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
233 err = v.Put(TestHash2, TestBlock2)
235 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock2, err)
238 err = v.Put(TestHash3, TestBlock3)
240 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock3, err)
243 data, err := v.Get(TestHash)
247 if bytes.Compare(data, TestBlock) != 0 {
248 t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock)
253 data, err = v.Get(TestHash2)
257 if bytes.Compare(data, TestBlock2) != 0 {
258 t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock2)
263 data, err = v.Get(TestHash3)
267 if bytes.Compare(data, TestBlock3) != 0 {
268 t.Errorf("Block present, but to %+q, expected %+q", data, TestBlock3)
275 // Test that when applying PUT to a block that already exists,
276 // the block's modification time is updated.
277 // Test is intended for only writable volumes
278 func testPutAndTouch(t *testing.T, factory TestableVolumeFactory) {
282 if v.Writable() == false {
286 if err := v.Put(TestHash, TestBlock); err != nil {
290 // We'll verify { t0 < threshold < t1 }, where t0 is the
291 // existing block's timestamp on disk before Put() and t1 is
292 // its timestamp after Put().
293 threshold := time.Now().Add(-time.Second)
295 // Set the stored block's mtime far enough in the past that we
296 // can see the difference between "timestamp didn't change"
297 // and "timestamp granularity is too low".
298 v.TouchWithDate(TestHash, time.Now().Add(-20*time.Second))
300 // Make sure v.Mtime() agrees the above Utime really worked.
301 if t0, err := v.Mtime(TestHash); err != nil || t0.IsZero() || !t0.Before(threshold) {
302 t.Errorf("Setting mtime failed: %v, %v", t0, err)
305 // Write the same block again.
306 if err := v.Put(TestHash, TestBlock); err != nil {
310 // Verify threshold < t1
311 if t1, err := v.Mtime(TestHash); err != nil {
313 } else if t1.Before(threshold) {
314 t.Errorf("t1 %v should be >= threshold %v after v.Put ", t1, threshold)
318 // Touching a non-existing block should result in error.
319 // Test should pass for both writable and read-only volumes
320 func testTouchNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
324 if err := v.Touch(TestHash); err == nil {
325 t.Error("Expected error when attempted to touch a non-existing block")
329 // Invoking Mtime on a non-existing block should result in error.
330 // Test should pass for both writable and read-only volumes
331 func testMtimeNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
335 if _, err := v.Mtime("12345678901234567890123456789012"); err == nil {
336 t.Error("Expected error when updating Mtime on a non-existing block")
340 // Put a few blocks and invoke IndexTo with:
343 // * with no such prefix
344 // Test should pass for both writable and read-only volumes
345 func testIndexTo(t *testing.T, factory TestableVolumeFactory) {
349 v.PutRaw(TestHash, TestBlock)
350 v.PutRaw(TestHash2, TestBlock2)
351 v.PutRaw(TestHash3, TestBlock3)
353 buf := new(bytes.Buffer)
355 indexRows := strings.Split(string(buf.Bytes()), "\n")
356 sort.Strings(indexRows)
357 sortedIndex := strings.Join(indexRows, "\n")
358 m, err := regexp.MatchString(
359 `^\n`+TestHash+`\+\d+ \d+\n`+
360 TestHash3+`\+\d+ \d+\n`+
361 TestHash2+`\+\d+ \d+$`,
366 t.Errorf("Got index %q for empty prefix", sortedIndex)
369 for _, prefix := range []string{"f", "f15", "f15ac"} {
370 buf = new(bytes.Buffer)
371 v.IndexTo(prefix, buf)
373 m, err := regexp.MatchString(`^`+TestHash2+`\+\d+ \d+\n$`, string(buf.Bytes()))
377 t.Errorf("Got index %q for prefix %s", string(buf.Bytes()), prefix)
381 for _, prefix := range []string{"zero", "zip", "zilch"} {
382 buf = new(bytes.Buffer)
383 v.IndexTo(prefix, buf)
385 t.Errorf("Got error on IndexTo with no such prefix %v", err.Error())
386 } else if buf.Len() != 0 {
387 t.Errorf("Expected empty list for IndexTo with no such prefix %s", prefix)
392 // Calling Delete() for a block immediately after writing it (not old enough)
393 // should neither delete the data nor return an error.
394 // Test is intended for only writable volumes
395 func testDeleteNewBlock(t *testing.T, factory TestableVolumeFactory) {
398 blobSignatureTTL = 300 * time.Second
400 if v.Writable() == false {
404 v.Put(TestHash, TestBlock)
406 if err := v.Delete(TestHash); err != nil {
409 data, err := v.Get(TestHash)
413 if bytes.Compare(data, TestBlock) != 0 {
414 t.Errorf("Got data %+q, expected %+q", data, TestBlock)
420 // Calling Delete() for a block with a timestamp older than
421 // blobSignatureTTL seconds in the past should delete the data.
422 // Test is intended for only writable volumes
423 func testDeleteOldBlock(t *testing.T, factory TestableVolumeFactory) {
426 blobSignatureTTL = 300 * time.Second
428 if v.Writable() == false {
432 v.Put(TestHash, TestBlock)
433 v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
435 if err := v.Delete(TestHash); err != nil {
438 if _, err := v.Get(TestHash); err == nil || !os.IsNotExist(err) {
439 t.Errorf("os.IsNotExist(%v) should have been true", err)
443 // Calling Delete() for a block that does not exist should result in error.
444 // Test should pass for both writable and read-only volumes
445 func testDeleteNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
449 if err := v.Delete(TestHash2); err == nil {
450 t.Errorf("Expected error when attempting to delete a non-existing block")
454 // Invoke Status and verify that VolumeStatus is returned
455 // Test should pass for both writable and read-only volumes
456 func testStatus(t *testing.T, factory TestableVolumeFactory) {
460 // Get node status and make a basic sanity check.
462 if status.DeviceNum == 0 {
463 t.Errorf("uninitialized device_num in %v", status)
466 if status.BytesFree == 0 {
467 t.Errorf("uninitialized bytes_free in %v", status)
470 if status.BytesUsed == 0 {
471 t.Errorf("uninitialized bytes_used in %v", status)
475 // Invoke String for the volume; expect non-empty result
476 // Test should pass for both writable and read-only volumes
477 func testString(t *testing.T, factory TestableVolumeFactory) {
481 if id := v.String(); len(id) == 0 {
482 t.Error("Got empty string for v.String()")
486 // Putting, updating, touching, and deleting blocks from a read-only volume result in error.
487 // Test is intended for only read-only volumes
488 func testUpdateReadOnly(t *testing.T, factory TestableVolumeFactory) {
492 if v.Writable() == true {
496 v.PutRaw(TestHash, TestBlock)
498 // Get from read-only volume should succeed
499 _, err := v.Get(TestHash)
501 t.Errorf("got err %v, expected nil", err)
504 // Put a new block to read-only volume should result in error
505 err = v.Put(TestHash2, TestBlock2)
507 t.Errorf("Expected error when putting block in a read-only volume")
509 _, err = v.Get(TestHash2)
511 t.Errorf("Expected error when getting block whose put in read-only volume failed")
514 // Touch a block in read-only volume should result in error
515 err = v.Touch(TestHash)
517 t.Errorf("Expected error when touching block in a read-only volume")
520 // Delete a block from a read-only volume should result in error
521 err = v.Delete(TestHash)
523 t.Errorf("Expected error when deleting block from a read-only volume")
526 // Overwriting an existing block in read-only volume should result in error
527 err = v.Put(TestHash, TestBlock)
529 t.Errorf("Expected error when putting block in a read-only volume")
533 // Launch concurrent Gets
534 // Test should pass for both writable and read-only volumes
535 func testGetConcurrent(t *testing.T, factory TestableVolumeFactory) {
539 v.PutRaw(TestHash, TestBlock)
540 v.PutRaw(TestHash2, TestBlock2)
541 v.PutRaw(TestHash3, TestBlock3)
543 sem := make(chan int)
544 go func(sem chan int) {
545 buf, err := v.Get(TestHash)
547 t.Errorf("err1: %v", err)
550 if bytes.Compare(buf, TestBlock) != 0 {
551 t.Errorf("buf should be %s, is %s", string(TestBlock), string(buf))
556 go func(sem chan int) {
557 buf, err := v.Get(TestHash2)
559 t.Errorf("err2: %v", err)
562 if bytes.Compare(buf, TestBlock2) != 0 {
563 t.Errorf("buf should be %s, is %s", string(TestBlock2), string(buf))
568 go func(sem chan int) {
569 buf, err := v.Get(TestHash3)
571 t.Errorf("err3: %v", err)
574 if bytes.Compare(buf, TestBlock3) != 0 {
575 t.Errorf("buf should be %s, is %s", string(TestBlock3), string(buf))
580 // Wait for all goroutines to finish
581 for done := 0; done < 3; {
586 // Launch concurrent Puts
587 // Test is intended for only writable volumes
588 func testPutConcurrent(t *testing.T, factory TestableVolumeFactory) {
592 if v.Writable() == false {
596 sem := make(chan int)
597 go func(sem chan int) {
598 err := v.Put(TestHash, TestBlock)
600 t.Errorf("err1: %v", err)
605 go func(sem chan int) {
606 err := v.Put(TestHash2, TestBlock2)
608 t.Errorf("err2: %v", err)
613 go func(sem chan int) {
614 err := v.Put(TestHash3, TestBlock3)
616 t.Errorf("err3: %v", err)
621 // Wait for all goroutines to finish
622 for done := 0; done < 3; {
626 // Double check that we actually wrote the blocks we expected to write.
627 buf, err := v.Get(TestHash)
629 t.Errorf("Get #1: %v", err)
632 if bytes.Compare(buf, TestBlock) != 0 {
633 t.Errorf("Get #1: expected %s, got %s", string(TestBlock), string(buf))
636 buf, err = v.Get(TestHash2)
638 t.Errorf("Get #2: %v", err)
641 if bytes.Compare(buf, TestBlock2) != 0 {
642 t.Errorf("Get #2: expected %s, got %s", string(TestBlock2), string(buf))
645 buf, err = v.Get(TestHash3)
647 t.Errorf("Get #3: %v", err)
650 if bytes.Compare(buf, TestBlock3) != 0 {
651 t.Errorf("Get #3: expected %s, got %s", string(TestBlock3), string(buf))
655 // Write and read back a full size block
656 func testPutFullBlock(t *testing.T, factory TestableVolumeFactory) {
664 wdata := make([]byte, BlockSize)
666 wdata[BlockSize-1] = 'z'
667 hash := fmt.Sprintf("%x", md5.Sum(wdata))
668 err := v.Put(hash, wdata)
672 rdata, err := v.Get(hash)
676 defer bufs.Put(rdata)
678 if bytes.Compare(rdata, wdata) != 0 {
679 t.Error("rdata != wdata")