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 %+v, expected %+v", 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 %+v, which is neither %+v nor %+v", 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)
228 } else if bytes.Compare(data, TestBlock) != 0 {
229 t.Errorf("Block present, but content is incorrect: Expected: %v Found: %v", data, TestBlock)
233 data, err = v.Get(TestHash2)
236 } else if bytes.Compare(data, TestBlock2) != 0 {
237 t.Errorf("Block present, but content is incorrect: Expected: %v Found: %v", data, TestBlock2)
241 data, err = v.Get(TestHash3)
244 } else if bytes.Compare(data, TestBlock3) != 0 {
245 t.Errorf("Block present, but content is incorrect: Expected: %v Found: %v", data, TestBlock3)
251 // Test that when applying PUT to a block that already exists,
252 // the block's modification time is updated.
253 // Test is intended for only writable volumes
254 func testPutAndTouch(t *testing.T, factory TestableVolumeFactory) {
258 if v.Writable() == false {
262 if err := v.Put(TestHash, TestBlock); err != nil {
266 // We'll verify { t0 < threshold < t1 }, where t0 is the
267 // existing block's timestamp on disk before Put() and t1 is
268 // its timestamp after Put().
269 threshold := time.Now().Add(-time.Second)
271 // Set the stored block's mtime far enough in the past that we
272 // can see the difference between "timestamp didn't change"
273 // and "timestamp granularity is too low".
274 v.TouchWithDate(TestHash, time.Now().Add(-20*time.Second))
276 // Make sure v.Mtime() agrees the above Utime really worked.
277 if t0, err := v.Mtime(TestHash); err != nil || t0.IsZero() || !t0.Before(threshold) {
278 t.Errorf("Setting mtime failed: %v, %v", t0, err)
281 // Write the same block again.
282 if err := v.Put(TestHash, TestBlock); err != nil {
286 // Verify threshold < t1
287 if t1, err := v.Mtime(TestHash); err != nil {
289 } else if t1.Before(threshold) {
290 t.Errorf("t1 %v should be >= threshold %v after v.Put ", t1, threshold)
294 // Touching a non-existing block should result in error.
295 // Test should pass for both writable and read-only volumes
296 func testTouchNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
300 if err := v.Touch(TestHash); err == nil {
301 t.Error("Expected error when attempted to touch a non-existing block")
305 // Invoking Mtime on a non-existing block should result in error.
306 // Test should pass for both writable and read-only volumes
307 func testMtimeNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
311 if _, err := v.Mtime("12345678901234567890123456789012"); err == nil {
312 t.Error("Expected error when updating Mtime on a non-existing block")
316 // Put a few blocks and invoke IndexTo with:
319 // * with no such prefix
320 // Test should pass for both writable and read-only volumes
321 func testIndexTo(t *testing.T, factory TestableVolumeFactory) {
325 v.PutRaw(TestHash, TestBlock)
326 v.PutRaw(TestHash2, TestBlock2)
327 v.PutRaw(TestHash3, TestBlock3)
329 buf := new(bytes.Buffer)
331 indexRows := strings.Split(string(buf.Bytes()), "\n")
332 sort.Strings(indexRows)
333 sortedIndex := strings.Join(indexRows, "\n")
334 m, err := regexp.MatchString(
335 `^\n`+TestHash+`\+\d+ \d+\n`+
336 TestHash3+`\+\d+ \d+\n`+
337 TestHash2+`\+\d+ \d+$`,
342 t.Errorf("Got index %q for empty prefix", sortedIndex)
345 for _, prefix := range []string{"f", "f15", "f15ac"} {
346 buf = new(bytes.Buffer)
347 v.IndexTo(prefix, buf)
349 m, err := regexp.MatchString(`^`+TestHash2+`\+\d+ \d+\n$`, string(buf.Bytes()))
353 t.Errorf("Got index %q for prefix %s", string(buf.Bytes()), prefix)
357 for _, prefix := range []string{"zero", "zip", "zilch"} {
358 buf = new(bytes.Buffer)
359 v.IndexTo(prefix, buf)
361 t.Errorf("Got error on IndexTo with no such prefix %v", err.Error())
362 } else if buf.Len() != 0 {
363 t.Errorf("Expected empty list for IndexTo with no such prefix %s", prefix)
368 // Calling Delete() for a block immediately after writing it (not old enough)
369 // should neither delete the data nor return an error.
370 // Test is intended for only writable volumes
371 func testDeleteNewBlock(t *testing.T, factory TestableVolumeFactory) {
375 if v.Writable() == false {
379 v.Put(TestHash, TestBlock)
381 if err := v.Delete(TestHash); err != nil {
384 data, err := v.Get(TestHash)
387 } else if bytes.Compare(data, TestBlock) != 0 {
388 t.Error("Block still present, but content is incorrect: %+v != %+v", data, TestBlock)
393 // Calling Delete() for a block with a timestamp older than
394 // blobSignatureTTL seconds in the past should delete the data.
395 // Test is intended for only writable volumes
396 func testDeleteOldBlock(t *testing.T, factory TestableVolumeFactory) {
400 if v.Writable() == false {
404 v.Put(TestHash, TestBlock)
405 v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL*time.Second))
407 if err := v.Delete(TestHash); err != nil {
410 if _, err := v.Get(TestHash); err == nil || !os.IsNotExist(err) {
411 t.Errorf("os.IsNotExist(%v) should have been true", err.Error())
415 // Calling Delete() for a block that does not exist should result in error.
416 // Test should pass for both writable and read-only volumes
417 func testDeleteNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
421 if err := v.Delete(TestHash2); err == nil {
422 t.Errorf("Expected error when attempting to delete a non-existing block")
426 // Invoke Status and verify that VolumeStatus is returned
427 // Test should pass for both writable and read-only volumes
428 func testStatus(t *testing.T, factory TestableVolumeFactory) {
432 // Get node status and make a basic sanity check.
434 if status.DeviceNum == 0 {
435 t.Errorf("uninitialized device_num in %v", status)
438 if status.BytesFree == 0 {
439 t.Errorf("uninitialized bytes_free in %v", status)
442 if status.BytesUsed == 0 {
443 t.Errorf("uninitialized bytes_used in %v", status)
447 // Invoke String for the volume; expect non-empty result
448 // Test should pass for both writable and read-only volumes
449 func testString(t *testing.T, factory TestableVolumeFactory) {
453 if id := v.String(); len(id) == 0 {
454 t.Error("Got empty string for v.String()")
458 // Putting, updating, touching, and deleting blocks from a read-only volume result in error.
459 // Test is intended for only read-only volumes
460 func testUpdateReadOnly(t *testing.T, factory TestableVolumeFactory) {
464 if v.Writable() == true {
468 v.PutRaw(TestHash, TestBlock)
470 // Get from read-only volume should succeed
471 _, err := v.Get(TestHash)
473 t.Errorf("got err %v, expected nil", err)
476 // Put a new block to read-only volume should result in error
477 err = v.Put(TestHash2, TestBlock2)
479 t.Errorf("Expected error when putting block in a read-only volume")
481 _, err = v.Get(TestHash2)
483 t.Errorf("Expected error when getting block whose put in read-only volume failed")
486 // Touch a block in read-only volume should result in error
487 err = v.Touch(TestHash)
489 t.Errorf("Expected error when touching block in a read-only volume")
492 // Delete a block from a read-only volume should result in error
493 err = v.Delete(TestHash)
495 t.Errorf("Expected error when deleting block from a read-only volume")
498 // Overwriting an existing block in read-only volume should result in error
499 err = v.Put(TestHash, TestBlock)
501 t.Errorf("Expected error when putting block in a read-only volume")
505 // Launch concurrent Gets
506 // Test should pass for both writable and read-only volumes
507 func testGetConcurrent(t *testing.T, factory TestableVolumeFactory) {
511 v.PutRaw(TestHash, TestBlock)
512 v.PutRaw(TestHash2, TestBlock2)
513 v.PutRaw(TestHash3, TestBlock3)
515 sem := make(chan int)
516 go func(sem chan int) {
517 buf, err := v.Get(TestHash)
519 t.Errorf("err1: %v", err)
522 if bytes.Compare(buf, TestBlock) != 0 {
523 t.Errorf("buf should be %s, is %s", string(TestBlock), string(buf))
528 go func(sem chan int) {
529 buf, err := v.Get(TestHash2)
531 t.Errorf("err2: %v", err)
534 if bytes.Compare(buf, TestBlock2) != 0 {
535 t.Errorf("buf should be %s, is %s", string(TestBlock2), string(buf))
540 go func(sem chan int) {
541 buf, err := v.Get(TestHash3)
543 t.Errorf("err3: %v", err)
546 if bytes.Compare(buf, TestBlock3) != 0 {
547 t.Errorf("buf should be %s, is %s", string(TestBlock3), string(buf))
552 // Wait for all goroutines to finish
553 for done := 0; done < 3; {
558 // Launch concurrent Puts
559 // Test is intended for only writable volumes
560 func testPutConcurrent(t *testing.T, factory TestableVolumeFactory) {
564 if v.Writable() == false {
568 sem := make(chan int)
569 go func(sem chan int) {
570 err := v.Put(TestHash, TestBlock)
572 t.Errorf("err1: %v", err)
577 go func(sem chan int) {
578 err := v.Put(TestHash2, TestBlock2)
580 t.Errorf("err2: %v", err)
585 go func(sem chan int) {
586 err := v.Put(TestHash3, TestBlock3)
588 t.Errorf("err3: %v", err)
593 // Wait for all goroutines to finish
594 for done := 0; done < 3; {
598 // Double check that we actually wrote the blocks we expected to write.
599 buf, err := v.Get(TestHash)
601 t.Errorf("Get #1: %v", err)
604 if bytes.Compare(buf, TestBlock) != 0 {
605 t.Errorf("Get #1: expected %s, got %s", string(TestBlock), string(buf))
608 buf, err = v.Get(TestHash2)
610 t.Errorf("Get #2: %v", err)
613 if bytes.Compare(buf, TestBlock2) != 0 {
614 t.Errorf("Get #2: expected %s, got %s", string(TestBlock2), string(buf))
617 buf, err = v.Get(TestHash3)
619 t.Errorf("Get #3: %v", err)
622 if bytes.Compare(buf, TestBlock3) != 0 {
623 t.Errorf("Get #3: expected %s, got %s", string(TestBlock3), string(buf))