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)
26 testCompareWithDifferentContent(t, factory)
27 testCompareWithBadData(t, factory)
29 testPutBlockWithSameContent(t, factory)
30 testPutBlockWithDifferentContent(t, factory)
31 testPutMultipleBlocks(t, factory)
33 testPutAndTouch(t, factory)
34 testTouchNoSuchBlock(t, factory)
36 testMtimeNoSuchBlock(t, factory)
38 testIndexTo(t, factory)
40 testDeleteNewBlock(t, factory)
41 testDeleteOldBlock(t, factory)
42 testDeleteNoSuchBlock(t, factory)
44 testStatus(t, factory)
46 testString(t, factory)
48 testUpdateReadOnly(t, factory)
50 testGetConcurrent(t, factory)
51 testPutConcurrent(t, factory)
54 // Put a test block, get it and verify content
55 // Test should pass for both writable and read-only volumes
56 func testGet(t *testing.T, factory TestableVolumeFactory) {
60 v.PutRaw(TestHash, TestBlock)
62 buf, err := v.Get(TestHash)
69 if bytes.Compare(buf, TestBlock) != 0 {
70 t.Errorf("expected %s, got %s", string(TestBlock), string(buf))
74 // Invoke get on a block that does not exist in volume; should result in error
75 // Test should pass for both writable and read-only volumes
76 func testGetNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
80 if _, err := v.Get(TestHash2); err == nil {
81 t.Errorf("Expected error while getting non-existing block %v", TestHash2)
85 // Put a test block and compare the locator with same content
86 // Test should pass for both writable and read-only volumes
87 func testCompareSameContent(t *testing.T, factory TestableVolumeFactory) {
91 v.PutRaw(TestHash, TestBlock)
93 // Compare the block locator with same content
94 err := v.Compare(TestHash, TestBlock)
96 t.Errorf("Got err %q, expected nil", err)
100 // Put a test block and compare the locator with a different content
101 // Expect error due to collision
102 // Test should pass for both writable and read-only volumes
103 func testCompareWithDifferentContent(t *testing.T, factory TestableVolumeFactory) {
107 v.PutRaw(TestHash, TestBlock)
109 // Compare the block locator with different content; collision
110 err := v.Compare(TestHash, []byte("baddata"))
112 t.Errorf("Expected error due to collision")
116 // Put a test block with bad data (hash does not match, but Put does not verify)
117 // Compare the locator with good data whose hash matches with locator
118 // Expect error due to corruption.
119 // Test should pass for both writable and read-only volumes
120 func testCompareWithBadData(t *testing.T, factory TestableVolumeFactory) {
124 v.PutRaw(TestHash, []byte("baddata"))
126 err := v.Compare(TestHash, TestBlock)
128 t.Errorf("Expected error due to corruption")
132 // Put a block and put again with same content
133 // Test is intended for only writable volumes
134 func testPutBlockWithSameContent(t *testing.T, factory TestableVolumeFactory) {
138 if v.Writable() == false {
142 err := v.Put(TestHash, TestBlock)
144 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
147 err = v.Put(TestHash, TestBlock)
149 t.Errorf("Got err putting block second time %q: %q, expected nil", TestBlock, err)
153 // Put a block and put again with different content
154 // Test is intended for only writable volumes
155 func testPutBlockWithDifferentContent(t *testing.T, factory TestableVolumeFactory) {
159 if v.Writable() == false {
163 err := v.Put(TestHash, TestBlock)
165 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
168 putErr := v.Put(TestHash, TestBlock2)
169 buf, getErr := v.Get(TestHash)
171 // Put must not return a nil error unless it has
172 // overwritten the existing data.
173 if bytes.Compare(buf, TestBlock2) != 0 {
174 t.Errorf("Put succeeded but Get returned %+q, expected %+q", buf, TestBlock2)
177 // It is permissible for Put to fail, but it must
178 // leave us with either the original data, the new
179 // data, or nothing at all.
180 if getErr == nil && bytes.Compare(buf, TestBlock) != 0 && bytes.Compare(buf, TestBlock2) != 0 {
181 t.Errorf("Put failed but Get returned %+q, which is neither %+q nor %+q", buf, TestBlock, TestBlock2)
189 // Put and get multiple blocks
190 // Test is intended for only writable volumes
191 func testPutMultipleBlocks(t *testing.T, factory TestableVolumeFactory) {
195 if v.Writable() == false {
199 err := v.Put(TestHash, TestBlock)
201 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
204 err = v.Put(TestHash2, TestBlock2)
206 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock2, err)
209 err = v.Put(TestHash3, TestBlock3)
211 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock3, err)
214 data, err := v.Get(TestHash)
218 if bytes.Compare(data, TestBlock) != 0 {
219 t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock)
224 data, err = v.Get(TestHash2)
228 if bytes.Compare(data, TestBlock2) != 0 {
229 t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock2)
234 data, err = v.Get(TestHash3)
238 if bytes.Compare(data, TestBlock3) != 0 {
239 t.Errorf("Block present, but to %+q, expected %+q", data, TestBlock3)
246 // Test that when applying PUT to a block that already exists,
247 // the block's modification time is updated.
248 // Test is intended for only writable volumes
249 func testPutAndTouch(t *testing.T, factory TestableVolumeFactory) {
253 if v.Writable() == false {
257 if err := v.Put(TestHash, TestBlock); err != nil {
261 // We'll verify { t0 < threshold < t1 }, where t0 is the
262 // existing block's timestamp on disk before Put() and t1 is
263 // its timestamp after Put().
264 threshold := time.Now().Add(-time.Second)
266 // Set the stored block's mtime far enough in the past that we
267 // can see the difference between "timestamp didn't change"
268 // and "timestamp granularity is too low".
269 v.TouchWithDate(TestHash, time.Now().Add(-20*time.Second))
271 // Make sure v.Mtime() agrees the above Utime really worked.
272 if t0, err := v.Mtime(TestHash); err != nil || t0.IsZero() || !t0.Before(threshold) {
273 t.Errorf("Setting mtime failed: %v, %v", t0, err)
276 // Write the same block again.
277 if err := v.Put(TestHash, TestBlock); err != nil {
281 // Verify threshold < t1
282 if t1, err := v.Mtime(TestHash); err != nil {
284 } else if t1.Before(threshold) {
285 t.Errorf("t1 %v should be >= threshold %v after v.Put ", t1, threshold)
289 // Touching a non-existing block should result in error.
290 // Test should pass for both writable and read-only volumes
291 func testTouchNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
295 if err := v.Touch(TestHash); err == nil {
296 t.Error("Expected error when attempted to touch a non-existing block")
300 // Invoking Mtime on a non-existing block should result in error.
301 // Test should pass for both writable and read-only volumes
302 func testMtimeNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
306 if _, err := v.Mtime("12345678901234567890123456789012"); err == nil {
307 t.Error("Expected error when updating Mtime on a non-existing block")
311 // Put a few blocks and invoke IndexTo with:
314 // * with no such prefix
315 // Test should pass for both writable and read-only volumes
316 func testIndexTo(t *testing.T, factory TestableVolumeFactory) {
320 v.PutRaw(TestHash, TestBlock)
321 v.PutRaw(TestHash2, TestBlock2)
322 v.PutRaw(TestHash3, TestBlock3)
324 buf := new(bytes.Buffer)
326 indexRows := strings.Split(string(buf.Bytes()), "\n")
327 sort.Strings(indexRows)
328 sortedIndex := strings.Join(indexRows, "\n")
329 m, err := regexp.MatchString(
330 `^\n`+TestHash+`\+\d+ \d+\n`+
331 TestHash3+`\+\d+ \d+\n`+
332 TestHash2+`\+\d+ \d+$`,
337 t.Errorf("Got index %q for empty prefix", sortedIndex)
340 for _, prefix := range []string{"f", "f15", "f15ac"} {
341 buf = new(bytes.Buffer)
342 v.IndexTo(prefix, buf)
344 m, err := regexp.MatchString(`^`+TestHash2+`\+\d+ \d+\n$`, string(buf.Bytes()))
348 t.Errorf("Got index %q for prefix %s", string(buf.Bytes()), prefix)
352 for _, prefix := range []string{"zero", "zip", "zilch"} {
353 buf = new(bytes.Buffer)
354 v.IndexTo(prefix, buf)
356 t.Errorf("Got error on IndexTo with no such prefix %v", err.Error())
357 } else if buf.Len() != 0 {
358 t.Errorf("Expected empty list for IndexTo with no such prefix %s", prefix)
363 // Calling Delete() for a block immediately after writing it (not old enough)
364 // should neither delete the data nor return an error.
365 // Test is intended for only writable volumes
366 func testDeleteNewBlock(t *testing.T, factory TestableVolumeFactory) {
369 blobSignatureTTL = 300 * time.Second
371 if v.Writable() == false {
375 v.Put(TestHash, TestBlock)
377 if err := v.Delete(TestHash); err != nil {
380 data, err := v.Get(TestHash)
384 if bytes.Compare(data, TestBlock) != 0 {
385 t.Errorf("Got data %+q, expected %+q", data, TestBlock)
391 // Calling Delete() for a block with a timestamp older than
392 // blobSignatureTTL seconds in the past should delete the data.
393 // Test is intended for only writable volumes
394 func testDeleteOldBlock(t *testing.T, factory TestableVolumeFactory) {
397 blobSignatureTTL = 300 * time.Second
399 if v.Writable() == false {
403 v.Put(TestHash, TestBlock)
404 v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
406 if err := v.Delete(TestHash); err != nil {
409 if _, err := v.Get(TestHash); err == nil || !os.IsNotExist(err) {
410 t.Errorf("os.IsNotExist(%v) should have been true", err)
414 // Calling Delete() for a block that does not exist should result in error.
415 // Test should pass for both writable and read-only volumes
416 func testDeleteNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
420 if err := v.Delete(TestHash2); err == nil {
421 t.Errorf("Expected error when attempting to delete a non-existing block")
425 // Invoke Status and verify that VolumeStatus is returned
426 // Test should pass for both writable and read-only volumes
427 func testStatus(t *testing.T, factory TestableVolumeFactory) {
431 // Get node status and make a basic sanity check.
433 if status.DeviceNum == 0 {
434 t.Errorf("uninitialized device_num in %v", status)
437 if status.BytesFree == 0 {
438 t.Errorf("uninitialized bytes_free in %v", status)
441 if status.BytesUsed == 0 {
442 t.Errorf("uninitialized bytes_used in %v", status)
446 // Invoke String for the volume; expect non-empty result
447 // Test should pass for both writable and read-only volumes
448 func testString(t *testing.T, factory TestableVolumeFactory) {
452 if id := v.String(); len(id) == 0 {
453 t.Error("Got empty string for v.String()")
457 // Putting, updating, touching, and deleting blocks from a read-only volume result in error.
458 // Test is intended for only read-only volumes
459 func testUpdateReadOnly(t *testing.T, factory TestableVolumeFactory) {
463 if v.Writable() == true {
467 v.PutRaw(TestHash, TestBlock)
469 // Get from read-only volume should succeed
470 _, err := v.Get(TestHash)
472 t.Errorf("got err %v, expected nil", err)
475 // Put a new block to read-only volume should result in error
476 err = v.Put(TestHash2, TestBlock2)
478 t.Errorf("Expected error when putting block in a read-only volume")
480 _, err = v.Get(TestHash2)
482 t.Errorf("Expected error when getting block whose put in read-only volume failed")
485 // Touch a block in read-only volume should result in error
486 err = v.Touch(TestHash)
488 t.Errorf("Expected error when touching block in a read-only volume")
491 // Delete a block from a read-only volume should result in error
492 err = v.Delete(TestHash)
494 t.Errorf("Expected error when deleting block from a read-only volume")
497 // Overwriting an existing block in read-only volume should result in error
498 err = v.Put(TestHash, TestBlock)
500 t.Errorf("Expected error when putting block in a read-only volume")
504 // Launch concurrent Gets
505 // Test should pass for both writable and read-only volumes
506 func testGetConcurrent(t *testing.T, factory TestableVolumeFactory) {
510 v.PutRaw(TestHash, TestBlock)
511 v.PutRaw(TestHash2, TestBlock2)
512 v.PutRaw(TestHash3, TestBlock3)
514 sem := make(chan int)
515 go func(sem chan int) {
516 buf, err := v.Get(TestHash)
518 t.Errorf("err1: %v", err)
521 if bytes.Compare(buf, TestBlock) != 0 {
522 t.Errorf("buf should be %s, is %s", string(TestBlock), string(buf))
527 go func(sem chan int) {
528 buf, err := v.Get(TestHash2)
530 t.Errorf("err2: %v", err)
533 if bytes.Compare(buf, TestBlock2) != 0 {
534 t.Errorf("buf should be %s, is %s", string(TestBlock2), string(buf))
539 go func(sem chan int) {
540 buf, err := v.Get(TestHash3)
542 t.Errorf("err3: %v", err)
545 if bytes.Compare(buf, TestBlock3) != 0 {
546 t.Errorf("buf should be %s, is %s", string(TestBlock3), string(buf))
551 // Wait for all goroutines to finish
552 for done := 0; done < 3; {
557 // Launch concurrent Puts
558 // Test is intended for only writable volumes
559 func testPutConcurrent(t *testing.T, factory TestableVolumeFactory) {
563 if v.Writable() == false {
567 sem := make(chan int)
568 go func(sem chan int) {
569 err := v.Put(TestHash, TestBlock)
571 t.Errorf("err1: %v", err)
576 go func(sem chan int) {
577 err := v.Put(TestHash2, TestBlock2)
579 t.Errorf("err2: %v", err)
584 go func(sem chan int) {
585 err := v.Put(TestHash3, TestBlock3)
587 t.Errorf("err3: %v", err)
592 // Wait for all goroutines to finish
593 for done := 0; done < 3; {
597 // Double check that we actually wrote the blocks we expected to write.
598 buf, err := v.Get(TestHash)
600 t.Errorf("Get #1: %v", err)
603 if bytes.Compare(buf, TestBlock) != 0 {
604 t.Errorf("Get #1: expected %s, got %s", string(TestBlock), string(buf))
607 buf, err = v.Get(TestHash2)
609 t.Errorf("Get #2: %v", err)
612 if bytes.Compare(buf, TestBlock2) != 0 {
613 t.Errorf("Get #2: expected %s, got %s", string(TestBlock2), string(buf))
616 buf, err = v.Get(TestHash3)
618 t.Errorf("Get #3: %v", err)
621 if bytes.Compare(buf, TestBlock3) != 0 {
622 t.Errorf("Get #3: expected %s, got %s", string(TestBlock3), string(buf))