13 // A TestableVolumeFactory returns a new TestableVolume. The factory
14 // function, and the TestableVolume it returns, can use "t" to write
15 // logs, fail the current test, etc.
16 type TestableVolumeFactory func(t *testing.T) TestableVolume
18 // DoGenericVolumeTests runs a set of tests that every TestableVolume
19 // is expected to pass. It calls factory to create a new TestableVolume
20 // for each test case, to avoid leaking state between tests.
21 func DoGenericVolumeTests(t *testing.T, factory TestableVolumeFactory) {
23 testGetNoSuchBlock(t, factory)
25 testCompareSameContent(t, factory, TestHash, TestBlock)
26 testCompareSameContent(t, factory, EmptyHash, EmptyBlock)
27 testCompareWithCollision(t, factory, TestHash, TestBlock, []byte("baddata"))
28 testCompareWithCollision(t, factory, TestHash, TestBlock, EmptyBlock)
29 testCompareWithCollision(t, factory, EmptyHash, EmptyBlock, TestBlock)
30 testCompareWithCorruptStoredData(t, factory, TestHash, TestBlock, []byte("baddata"))
31 testCompareWithCorruptStoredData(t, factory, TestHash, TestBlock, EmptyBlock)
32 testCompareWithCorruptStoredData(t, factory, EmptyHash, EmptyBlock, []byte("baddata"))
34 testPutBlockWithSameContent(t, factory, TestHash, TestBlock)
35 testPutBlockWithSameContent(t, factory, EmptyHash, EmptyBlock)
36 testPutBlockWithDifferentContent(t, factory, TestHash, TestBlock, TestBlock2)
37 testPutBlockWithDifferentContent(t, factory, TestHash, EmptyBlock, TestBlock)
38 testPutBlockWithDifferentContent(t, factory, TestHash, TestBlock, EmptyBlock)
39 testPutBlockWithDifferentContent(t, factory, EmptyHash, EmptyBlock, TestBlock)
40 testPutMultipleBlocks(t, factory)
42 testPutAndTouch(t, factory)
43 testTouchNoSuchBlock(t, factory)
45 testMtimeNoSuchBlock(t, factory)
47 testIndexTo(t, factory)
49 testDeleteNewBlock(t, factory)
50 testDeleteOldBlock(t, factory)
51 testDeleteNoSuchBlock(t, factory)
53 testStatus(t, factory)
55 testString(t, factory)
57 testUpdateReadOnly(t, factory)
59 testGetConcurrent(t, factory)
60 testPutConcurrent(t, factory)
63 // Put a test block, get it and verify content
64 // Test should pass for both writable and read-only volumes
65 func testGet(t *testing.T, factory TestableVolumeFactory) {
69 v.PutRaw(TestHash, TestBlock)
71 buf, err := v.Get(TestHash)
78 if bytes.Compare(buf, TestBlock) != 0 {
79 t.Errorf("expected %s, got %s", string(TestBlock), string(buf))
83 // Invoke get on a block that does not exist in volume; should result in error
84 // Test should pass for both writable and read-only volumes
85 func testGetNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
89 if _, err := v.Get(TestHash2); err == nil {
90 t.Errorf("Expected error while getting non-existing block %v", TestHash2)
94 // Put a test block and compare the locator with same content
95 // Test should pass for both writable and read-only volumes
96 func testCompareSameContent(t *testing.T, factory TestableVolumeFactory, testHash string, testData []byte) {
100 v.PutRaw(testHash, testData)
102 // Compare the block locator with same content
103 err := v.Compare(testHash, testData)
105 t.Errorf("Got err %q, expected nil", err)
109 // Test behavior of Compare() when stored data matches expected
110 // checksum but differs from new data we need to store. Requires
111 // testHash = md5(testDataA).
113 // Test should pass for both writable and read-only volumes
114 func testCompareWithCollision(t *testing.T, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
118 v.PutRaw(testHash, testDataA)
120 // Compare the block locator with different content; collision
121 err := v.Compare(TestHash, testDataB)
123 t.Errorf("Got err nil, expected error due to collision")
127 // Test behavior of Compare() when stored data has become
128 // corrupted. Requires testHash = md5(testDataA) != md5(testDataB).
130 // Test should pass for both writable and read-only volumes
131 func testCompareWithCorruptStoredData(t *testing.T, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
135 v.PutRaw(TestHash, testDataB)
137 err := v.Compare(testHash, testDataA)
138 if err == nil || err == CollisionError {
139 t.Errorf("Got err %+v, expected non-collision error", err)
143 // Put a block and put again with same content
144 // Test is intended for only writable volumes
145 func testPutBlockWithSameContent(t *testing.T, factory TestableVolumeFactory, testHash string, testData []byte) {
149 if v.Writable() == false {
153 err := v.Put(testHash, testData)
155 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
158 err = v.Put(testHash, testData)
160 t.Errorf("Got err putting block second time %q: %q, expected nil", TestBlock, err)
164 // Put a block and put again with different content
165 // Test is intended for only writable volumes
166 func testPutBlockWithDifferentContent(t *testing.T, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
170 if v.Writable() == false {
174 err := v.Put(testHash, testDataA)
176 t.Errorf("Got err putting block %q: %q, expected nil", testDataA, err)
179 putErr := v.Put(testHash, testDataB)
180 buf, getErr := v.Get(testHash)
182 // Put must not return a nil error unless it has
183 // overwritten the existing data.
184 if bytes.Compare(buf, testDataB) != 0 {
185 t.Errorf("Put succeeded but Get returned %+q, expected %+q", buf, testDataB)
188 // It is permissible for Put to fail, but it must
189 // leave us with either the original data, the new
190 // data, or nothing at all.
191 if getErr == nil && bytes.Compare(buf, testDataA) != 0 && bytes.Compare(buf, testDataB) != 0 {
192 t.Errorf("Put failed but Get returned %+q, which is neither %+q nor %+q", buf, testDataA, testDataB)
200 // Put and get multiple blocks
201 // Test is intended for only writable volumes
202 func testPutMultipleBlocks(t *testing.T, factory TestableVolumeFactory) {
206 if v.Writable() == false {
210 err := v.Put(TestHash, TestBlock)
212 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
215 err = v.Put(TestHash2, TestBlock2)
217 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock2, err)
220 err = v.Put(TestHash3, TestBlock3)
222 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock3, err)
225 data, err := v.Get(TestHash)
229 if bytes.Compare(data, TestBlock) != 0 {
230 t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock)
235 data, err = v.Get(TestHash2)
239 if bytes.Compare(data, TestBlock2) != 0 {
240 t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock2)
245 data, err = v.Get(TestHash3)
249 if bytes.Compare(data, TestBlock3) != 0 {
250 t.Errorf("Block present, but to %+q, expected %+q", data, TestBlock3)
257 // Test that when applying PUT to a block that already exists,
258 // the block's modification time is updated.
259 // Test is intended for only writable volumes
260 func testPutAndTouch(t *testing.T, factory TestableVolumeFactory) {
264 if v.Writable() == false {
268 if err := v.Put(TestHash, TestBlock); err != nil {
272 // We'll verify { t0 < threshold < t1 }, where t0 is the
273 // existing block's timestamp on disk before Put() and t1 is
274 // its timestamp after Put().
275 threshold := time.Now().Add(-time.Second)
277 // Set the stored block's mtime far enough in the past that we
278 // can see the difference between "timestamp didn't change"
279 // and "timestamp granularity is too low".
280 v.TouchWithDate(TestHash, time.Now().Add(-20*time.Second))
282 // Make sure v.Mtime() agrees the above Utime really worked.
283 if t0, err := v.Mtime(TestHash); err != nil || t0.IsZero() || !t0.Before(threshold) {
284 t.Errorf("Setting mtime failed: %v, %v", t0, err)
287 // Write the same block again.
288 if err := v.Put(TestHash, TestBlock); err != nil {
292 // Verify threshold < t1
293 if t1, err := v.Mtime(TestHash); err != nil {
295 } else if t1.Before(threshold) {
296 t.Errorf("t1 %v should be >= threshold %v after v.Put ", t1, threshold)
300 // Touching a non-existing block should result in error.
301 // Test should pass for both writable and read-only volumes
302 func testTouchNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
306 if err := v.Touch(TestHash); err == nil {
307 t.Error("Expected error when attempted to touch a non-existing block")
311 // Invoking Mtime on a non-existing block should result in error.
312 // Test should pass for both writable and read-only volumes
313 func testMtimeNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
317 if _, err := v.Mtime("12345678901234567890123456789012"); err == nil {
318 t.Error("Expected error when updating Mtime on a non-existing block")
322 // Put a few blocks and invoke IndexTo with:
325 // * with no such prefix
326 // Test should pass for both writable and read-only volumes
327 func testIndexTo(t *testing.T, factory TestableVolumeFactory) {
331 v.PutRaw(TestHash, TestBlock)
332 v.PutRaw(TestHash2, TestBlock2)
333 v.PutRaw(TestHash3, TestBlock3)
335 buf := new(bytes.Buffer)
337 indexRows := strings.Split(string(buf.Bytes()), "\n")
338 sort.Strings(indexRows)
339 sortedIndex := strings.Join(indexRows, "\n")
340 m, err := regexp.MatchString(
341 `^\n`+TestHash+`\+\d+ \d+\n`+
342 TestHash3+`\+\d+ \d+\n`+
343 TestHash2+`\+\d+ \d+$`,
348 t.Errorf("Got index %q for empty prefix", sortedIndex)
351 for _, prefix := range []string{"f", "f15", "f15ac"} {
352 buf = new(bytes.Buffer)
353 v.IndexTo(prefix, buf)
355 m, err := regexp.MatchString(`^`+TestHash2+`\+\d+ \d+\n$`, string(buf.Bytes()))
359 t.Errorf("Got index %q for prefix %s", string(buf.Bytes()), prefix)
363 for _, prefix := range []string{"zero", "zip", "zilch"} {
364 buf = new(bytes.Buffer)
365 v.IndexTo(prefix, buf)
367 t.Errorf("Got error on IndexTo with no such prefix %v", err.Error())
368 } else if buf.Len() != 0 {
369 t.Errorf("Expected empty list for IndexTo with no such prefix %s", prefix)
374 // Calling Delete() for a block immediately after writing it (not old enough)
375 // should neither delete the data nor return an error.
376 // Test is intended for only writable volumes
377 func testDeleteNewBlock(t *testing.T, factory TestableVolumeFactory) {
380 blobSignatureTTL = 300 * time.Second
382 if v.Writable() == false {
386 v.Put(TestHash, TestBlock)
388 if err := v.Delete(TestHash); err != nil {
391 data, err := v.Get(TestHash)
395 if bytes.Compare(data, TestBlock) != 0 {
396 t.Errorf("Got data %+q, expected %+q", data, TestBlock)
402 // Calling Delete() for a block with a timestamp older than
403 // blobSignatureTTL seconds in the past should delete the data.
404 // Test is intended for only writable volumes
405 func testDeleteOldBlock(t *testing.T, factory TestableVolumeFactory) {
408 blobSignatureTTL = 300 * time.Second
410 if v.Writable() == false {
414 v.Put(TestHash, TestBlock)
415 v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
417 if err := v.Delete(TestHash); err != nil {
420 if _, err := v.Get(TestHash); err == nil || !os.IsNotExist(err) {
421 t.Errorf("os.IsNotExist(%v) should have been true", err)
425 // Calling Delete() for a block that does not exist should result in error.
426 // Test should pass for both writable and read-only volumes
427 func testDeleteNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
431 if err := v.Delete(TestHash2); err == nil {
432 t.Errorf("Expected error when attempting to delete a non-existing block")
436 // Invoke Status and verify that VolumeStatus is returned
437 // Test should pass for both writable and read-only volumes
438 func testStatus(t *testing.T, factory TestableVolumeFactory) {
442 // Get node status and make a basic sanity check.
444 if status.DeviceNum == 0 {
445 t.Errorf("uninitialized device_num in %v", status)
448 if status.BytesFree == 0 {
449 t.Errorf("uninitialized bytes_free in %v", status)
452 if status.BytesUsed == 0 {
453 t.Errorf("uninitialized bytes_used in %v", status)
457 // Invoke String for the volume; expect non-empty result
458 // Test should pass for both writable and read-only volumes
459 func testString(t *testing.T, factory TestableVolumeFactory) {
463 if id := v.String(); len(id) == 0 {
464 t.Error("Got empty string for v.String()")
468 // Putting, updating, touching, and deleting blocks from a read-only volume result in error.
469 // Test is intended for only read-only volumes
470 func testUpdateReadOnly(t *testing.T, factory TestableVolumeFactory) {
474 if v.Writable() == true {
478 v.PutRaw(TestHash, TestBlock)
480 // Get from read-only volume should succeed
481 _, err := v.Get(TestHash)
483 t.Errorf("got err %v, expected nil", err)
486 // Put a new block to read-only volume should result in error
487 err = v.Put(TestHash2, TestBlock2)
489 t.Errorf("Expected error when putting block in a read-only volume")
491 _, err = v.Get(TestHash2)
493 t.Errorf("Expected error when getting block whose put in read-only volume failed")
496 // Touch a block in read-only volume should result in error
497 err = v.Touch(TestHash)
499 t.Errorf("Expected error when touching block in a read-only volume")
502 // Delete a block from a read-only volume should result in error
503 err = v.Delete(TestHash)
505 t.Errorf("Expected error when deleting block from a read-only volume")
508 // Overwriting an existing block in read-only volume should result in error
509 err = v.Put(TestHash, TestBlock)
511 t.Errorf("Expected error when putting block in a read-only volume")
515 // Launch concurrent Gets
516 // Test should pass for both writable and read-only volumes
517 func testGetConcurrent(t *testing.T, factory TestableVolumeFactory) {
521 v.PutRaw(TestHash, TestBlock)
522 v.PutRaw(TestHash2, TestBlock2)
523 v.PutRaw(TestHash3, TestBlock3)
525 sem := make(chan int)
526 go func(sem chan int) {
527 buf, err := v.Get(TestHash)
529 t.Errorf("err1: %v", err)
532 if bytes.Compare(buf, TestBlock) != 0 {
533 t.Errorf("buf should be %s, is %s", string(TestBlock), string(buf))
538 go func(sem chan int) {
539 buf, err := v.Get(TestHash2)
541 t.Errorf("err2: %v", err)
544 if bytes.Compare(buf, TestBlock2) != 0 {
545 t.Errorf("buf should be %s, is %s", string(TestBlock2), string(buf))
550 go func(sem chan int) {
551 buf, err := v.Get(TestHash3)
553 t.Errorf("err3: %v", err)
556 if bytes.Compare(buf, TestBlock3) != 0 {
557 t.Errorf("buf should be %s, is %s", string(TestBlock3), string(buf))
562 // Wait for all goroutines to finish
563 for done := 0; done < 3; {
568 // Launch concurrent Puts
569 // Test is intended for only writable volumes
570 func testPutConcurrent(t *testing.T, factory TestableVolumeFactory) {
574 if v.Writable() == false {
578 sem := make(chan int)
579 go func(sem chan int) {
580 err := v.Put(TestHash, TestBlock)
582 t.Errorf("err1: %v", err)
587 go func(sem chan int) {
588 err := v.Put(TestHash2, TestBlock2)
590 t.Errorf("err2: %v", err)
595 go func(sem chan int) {
596 err := v.Put(TestHash3, TestBlock3)
598 t.Errorf("err3: %v", err)
603 // Wait for all goroutines to finish
604 for done := 0; done < 3; {
608 // Double check that we actually wrote the blocks we expected to write.
609 buf, err := v.Get(TestHash)
611 t.Errorf("Get #1: %v", err)
614 if bytes.Compare(buf, TestBlock) != 0 {
615 t.Errorf("Get #1: expected %s, got %s", string(TestBlock), string(buf))
618 buf, err = v.Get(TestHash2)
620 t.Errorf("Get #2: %v", err)
623 if bytes.Compare(buf, TestBlock2) != 0 {
624 t.Errorf("Get #2: expected %s, got %s", string(TestBlock2), string(buf))
627 buf, err = v.Get(TestHash3)
629 t.Errorf("Get #3: %v", err)
632 if bytes.Compare(buf, TestBlock3) != 0 {
633 t.Errorf("Get #3: expected %s, got %s", string(TestBlock3), string(buf))