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 writable
20 // TestableVolume for each test case, to avoid leaking state between
22 func DoGenericVolumeTests(t *testing.T, factory TestableVolumeFactory) {
24 testGetNoSuchBlock(t, factory)
26 testCompareSameContent(t, factory)
27 testCompareWithDifferentContent(t, factory)
28 testCompareWithBadData(t, factory)
30 testPutBlockWithSameContent(t, factory)
31 testPutBlockWithDifferentContent(t, factory)
32 testPutMultipleBlocks(t, factory)
34 testPutAndTouch(t, factory)
35 testTouchNoSuchBlock(t, factory)
37 testMtimeNoSuchBlock(t, factory)
39 testIndexTo(t, factory)
41 testDeleteNewBlock(t, factory)
42 testDeleteOldBlock(t, factory)
43 testDeleteNoSuchBlock(t, factory)
45 testStatus(t, factory)
47 testString(t, factory)
49 testWritableTrue(t, factory)
51 testGetConcurrent(t, factory)
52 testPutConcurrent(t, factory)
55 // DoGenericReadOnlyVolumeTests runs a set of tests that every
56 // read-only TestableVolume is expected to pass. It calls factory
57 // to create a new read-only TestableVolume for each test case,
58 // to avoid leaking state between tests.
59 func DoGenericReadOnlyVolumeTests(t *testing.T, factory TestableVolumeFactory) {
60 testWritableFalse(t, factory)
61 testUpdateReadOnly(t, factory)
64 // Put a test block, get it and verify content
65 func testGet(t *testing.T, factory TestableVolumeFactory) {
68 v.Put(TEST_HASH, TEST_BLOCK)
70 buf, err := v.Get(TEST_HASH)
77 if bytes.Compare(buf, TEST_BLOCK) != 0 {
78 t.Errorf("expected %s, got %s", string(TEST_BLOCK), string(buf))
82 // Invoke get on a block that does not exist in volume; should result in error
83 func testGetNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
86 v.Put(TEST_HASH, TEST_BLOCK)
88 if _, err := v.Get(TEST_HASH_2); err == nil {
89 t.Errorf("Expected error while getting non-existing block %v", TEST_HASH_2)
93 // Put a test block and compare the locator with same content
94 func testCompareSameContent(t *testing.T, factory TestableVolumeFactory) {
98 v.Put(TEST_HASH, TEST_BLOCK)
100 // Compare the block locator with same content
101 err := v.Compare(TEST_HASH, TEST_BLOCK)
103 t.Errorf("Got err %q, expected nil", err)
107 // Put a test block and compare the locator with a different content
108 // Expect error due to collision
109 func testCompareWithDifferentContent(t *testing.T, factory TestableVolumeFactory) {
113 v.Put(TEST_HASH, TEST_BLOCK)
115 // Compare the block locator with different content; collision
116 err := v.Compare(TEST_HASH, []byte("baddata"))
118 t.Errorf("Expected error due to collision")
122 // Put a test block with bad data (hash does not match, but Put does not verify)
123 // Compare the locator with good data whose has matches with locator
124 // Expect error due to corruption.
125 func testCompareWithBadData(t *testing.T, factory TestableVolumeFactory) {
129 v.Put(TEST_HASH, []byte("baddata"))
131 err := v.Compare(TEST_HASH, TEST_BLOCK)
133 t.Errorf("Expected error due to corruption")
137 // Put a block and put again with same content
138 func testPutBlockWithSameContent(t *testing.T, factory TestableVolumeFactory) {
142 err := v.Put(TEST_HASH, TEST_BLOCK)
144 t.Errorf("Got err putting block %q: %q, expected nil", TEST_BLOCK, err)
147 err = v.Put(TEST_HASH, TEST_BLOCK)
149 t.Errorf("Got err putting block second time %q: %q, expected nil", TEST_BLOCK, err)
153 // Put a block and put again with different content
154 func testPutBlockWithDifferentContent(t *testing.T, factory TestableVolumeFactory) {
158 err := v.Put(TEST_HASH, TEST_BLOCK)
160 t.Errorf("Got err putting block %q: %q, expected nil", TEST_BLOCK, err)
163 putErr := v.Put(TEST_HASH, TEST_BLOCK_2)
164 buf, getErr := v.Get(TEST_HASH)
166 // Put must not return a nil error unless it has
167 // overwritten the existing data.
168 if bytes.Compare(buf, TEST_BLOCK_2) != 0 {
169 t.Errorf("Put succeeded but Get returned %+v, expected %+v", buf, TEST_BLOCK_2)
172 // It is permissible for Put to fail, but it must
173 // leave us with either the original data, the new
174 // data, or nothing at all.
175 if getErr == nil && bytes.Compare(buf, TEST_BLOCK) != 0 && bytes.Compare(buf, TEST_BLOCK_2) != 0 {
176 t.Errorf("Put failed but Get returned %+v, which is neither %+v nor %+v", buf, TEST_BLOCK, TEST_BLOCK_2)
184 // Put and get multiple blocks
185 func testPutMultipleBlocks(t *testing.T, factory TestableVolumeFactory) {
189 err := v.Put(TEST_HASH, TEST_BLOCK)
191 t.Errorf("Got err putting block %q: %q, expected nil", TEST_BLOCK, err)
194 err = v.Put(TEST_HASH_2, TEST_BLOCK_2)
196 t.Errorf("Got err putting block %q: %q, expected nil", TEST_BLOCK_2, err)
199 err = v.Put(TEST_HASH_3, TEST_BLOCK_3)
201 t.Errorf("Got err putting block %q: %q, expected nil", TEST_BLOCK_3, err)
204 data, err := v.Get(TEST_HASH)
207 } else if bytes.Compare(data, TEST_BLOCK) != 0 {
208 t.Errorf("Block present, but content is incorrect: Expected: %v Found: %v", data, TEST_BLOCK)
212 data, err = v.Get(TEST_HASH_2)
215 } else if bytes.Compare(data, TEST_BLOCK_2) != 0 {
216 t.Errorf("Block present, but content is incorrect: Expected: %v Found: %v", data, TEST_BLOCK_2)
220 data, err = v.Get(TEST_HASH_3)
223 } else if bytes.Compare(data, TEST_BLOCK_3) != 0 {
224 t.Errorf("Block present, but content is incorrect: Expected: %v Found: %v", data, TEST_BLOCK_3)
230 // Test that when applying PUT to a block that already exists,
231 // the block's modification time is updated.
232 func testPutAndTouch(t *testing.T, factory TestableVolumeFactory) {
236 if err := v.Put(TEST_HASH, TEST_BLOCK); err != nil {
240 // We'll verify { t0 < threshold < t1 }, where t0 is the
241 // existing block's timestamp on disk before Put() and t1 is
242 // its timestamp after Put().
243 threshold := time.Now().Add(-time.Second)
245 // Set the stored block's mtime far enough in the past that we
246 // can see the difference between "timestamp didn't change"
247 // and "timestamp granularity is too low".
248 v.TouchWithDate(TEST_HASH, time.Now().Add(-20*time.Second))
250 // Make sure v.Mtime() agrees the above Utime really worked.
251 if t0, err := v.Mtime(TEST_HASH); err != nil || t0.IsZero() || !t0.Before(threshold) {
252 t.Errorf("Setting mtime failed: %v, %v", t0, err)
255 // Write the same block again.
256 if err := v.Put(TEST_HASH, TEST_BLOCK); err != nil {
260 // Verify threshold < t1
261 if t1, err := v.Mtime(TEST_HASH); err != nil {
263 } else if t1.Before(threshold) {
264 t.Errorf("t1 %v should be >= threshold %v after v.Put ", t1, threshold)
268 // Touching a non-existing block should result in error.
269 func testTouchNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
273 if err := v.Touch(TEST_HASH); err == nil {
274 t.Error("Expected error when attempted to touch a non-existing block")
278 // Invoking Mtime on a non-existing block should result in error.
279 func testMtimeNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
283 if _, err := v.Mtime("12345678901234567890123456789012"); err == nil {
284 t.Error("Expected error when updating Mtime on a non-existing block")
288 // Put a few blocks and invoke IndexTo with:
291 // * with no such prefix
292 func testIndexTo(t *testing.T, factory TestableVolumeFactory) {
296 v.Put(TEST_HASH, TEST_BLOCK)
297 v.Put(TEST_HASH_2, TEST_BLOCK_2)
298 v.Put(TEST_HASH_3, TEST_BLOCK_3)
300 buf := new(bytes.Buffer)
302 indexRows := strings.Split(string(buf.Bytes()), "\n")
303 sort.Strings(indexRows)
304 sortedIndex := strings.Join(indexRows, "\n")
305 m, err := regexp.MatchString(
306 `^\n`+TEST_HASH+`\+\d+ \d+\n`+
307 TEST_HASH_3+`\+\d+ \d+\n`+
308 TEST_HASH_2+`\+\d+ \d+$`,
313 t.Errorf("Got index %q for empty prefix", sortedIndex)
316 for _, prefix := range []string{"f", "f15", "f15ac"} {
317 buf = new(bytes.Buffer)
318 v.IndexTo(prefix, buf)
320 m, err := regexp.MatchString(`^`+TEST_HASH_2+`\+\d+ \d+\n$`, string(buf.Bytes()))
324 t.Errorf("Got index %q for prefix %s", string(buf.Bytes()), prefix)
328 for _, prefix := range []string{"zero", "zip", "zilch"} {
329 buf = new(bytes.Buffer)
330 v.IndexTo(prefix, buf)
332 t.Errorf("Got error on IndexTo with no such prefix %v", err.Error())
333 } else if buf.Len() != 0 {
334 t.Errorf("Expected empty list for IndexTo with no such prefix %s", prefix)
339 // Calling Delete() for a block immediately after writing it (not old enough)
340 // should neither delete the data nor return an error.
341 func testDeleteNewBlock(t *testing.T, factory TestableVolumeFactory) {
344 v.Put(TEST_HASH, TEST_BLOCK)
346 if err := v.Delete(TEST_HASH); err != nil {
349 data, err := v.Get(TEST_HASH)
352 } else if bytes.Compare(data, TEST_BLOCK) != 0 {
353 t.Error("Block still present, but content is incorrect: %+v != %+v", data, TEST_BLOCK)
358 // Calling Delete() for a block with a timestamp older than
359 // blob_signature_ttl seconds in the past should delete the data.
360 func testDeleteOldBlock(t *testing.T, factory TestableVolumeFactory) {
363 v.Put(TEST_HASH, TEST_BLOCK)
364 v.TouchWithDate(TEST_HASH, time.Now().Add(-2*blob_signature_ttl*time.Second))
366 if err := v.Delete(TEST_HASH); err != nil {
369 if _, err := v.Get(TEST_HASH); err == nil || !os.IsNotExist(err) {
370 t.Errorf("os.IsNotExist(%v) should have been true", err.Error())
374 // Calling Delete() for a block that does not exist should result in error.
375 func testDeleteNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
378 v.Put(TEST_HASH, TEST_BLOCK)
380 if err := v.Delete(TEST_HASH_2); err == nil {
381 t.Errorf("Expected error when attempting to delete a non-existing block")
385 // Invoke Status and verify that VolumeStatus is returned
386 func testStatus(t *testing.T, factory TestableVolumeFactory) {
390 // Get node status and make a basic sanity check.
392 if status.DeviceNum == 0 {
393 t.Errorf("uninitialized device_num in %v", status)
396 if status.BytesFree == 0 {
397 t.Errorf("uninitialized bytes_free in %v", status)
400 if status.BytesUsed == 0 {
401 t.Errorf("uninitialized bytes_used in %v", status)
405 // Invoke String for the volume; expect non-empty result
406 func testString(t *testing.T, factory TestableVolumeFactory) {
410 if id := v.String(); len(id) == 0 {
411 t.Error("Got empty string for v.String()")
415 // Verify Writable is true on a writable volume
416 func testWritableTrue(t *testing.T, factory TestableVolumeFactory) {
420 if v.Writable() == false {
421 t.Errorf("Expected writable to be true on a writable volume")
425 // Verify Writable is false on a read-only volume
426 func testWritableFalse(t *testing.T, factory TestableVolumeFactory) {
430 if v.Writable() != false {
431 t.Errorf("Expected writable to be false on a read-only volume")
435 // Updating, touching, and deleting blocks from a read-only volume result in error.
436 func testUpdateReadOnly(t *testing.T, factory TestableVolumeFactory) {
440 v.PutRaw(TEST_HASH, TEST_BLOCK)
442 _, err := v.Get(TEST_HASH)
444 t.Errorf("got err %v, expected nil", err)
447 err = v.Put(TEST_HASH_2, TEST_BLOCK_2)
449 t.Errorf("Expected error when putting block in a read-only volume")
451 _, err = v.Get(TEST_HASH_2)
453 t.Errorf("Expected error when getting block whose put in read-only volume failed")
456 err = v.Touch(TEST_HASH)
458 t.Errorf("Expected error when touching block in a read-only volume")
461 err = v.Delete(TEST_HASH)
463 t.Errorf("Expected error when deleting block from a read-only volume")
467 // Launch concurrent Gets
468 func testGetConcurrent(t *testing.T, factory TestableVolumeFactory) {
472 v.Put(TEST_HASH, TEST_BLOCK)
473 v.Put(TEST_HASH_2, TEST_BLOCK_2)
474 v.Put(TEST_HASH_3, TEST_BLOCK_3)
476 sem := make(chan int)
477 go func(sem chan int) {
478 buf, err := v.Get(TEST_HASH)
480 t.Errorf("err1: %v", err)
483 if bytes.Compare(buf, TEST_BLOCK) != 0 {
484 t.Errorf("buf should be %s, is %s", string(TEST_BLOCK), string(buf))
489 go func(sem chan int) {
490 buf, err := v.Get(TEST_HASH_2)
492 t.Errorf("err2: %v", err)
495 if bytes.Compare(buf, TEST_BLOCK_2) != 0 {
496 t.Errorf("buf should be %s, is %s", string(TEST_BLOCK_2), string(buf))
501 go func(sem chan int) {
502 buf, err := v.Get(TEST_HASH_3)
504 t.Errorf("err3: %v", err)
507 if bytes.Compare(buf, TEST_BLOCK_3) != 0 {
508 t.Errorf("buf should be %s, is %s", string(TEST_BLOCK_3), string(buf))
513 // Wait for all goroutines to finish
514 for done := 0; done < 3; {
519 // Launch concurrent Puts
520 func testPutConcurrent(t *testing.T, factory TestableVolumeFactory) {
524 sem := make(chan int)
525 go func(sem chan int) {
526 err := v.Put(TEST_HASH, TEST_BLOCK)
528 t.Errorf("err1: %v", err)
533 go func(sem chan int) {
534 err := v.Put(TEST_HASH_2, TEST_BLOCK_2)
536 t.Errorf("err2: %v", err)
541 go func(sem chan int) {
542 err := v.Put(TEST_HASH_3, TEST_BLOCK_3)
544 t.Errorf("err3: %v", err)
549 // Wait for all goroutines to finish
550 for done := 0; done < 3; {
554 // Double check that we actually wrote the blocks we expected to write.
555 buf, err := v.Get(TEST_HASH)
557 t.Errorf("Get #1: %v", err)
560 if bytes.Compare(buf, TEST_BLOCK) != 0 {
561 t.Errorf("Get #1: expected %s, got %s", string(TEST_BLOCK), string(buf))
564 buf, err = v.Get(TEST_HASH_2)
566 t.Errorf("Get #2: %v", err)
569 if bytes.Compare(buf, TEST_BLOCK_2) != 0 {
570 t.Errorf("Get #2: expected %s, got %s", string(TEST_BLOCK_2), string(buf))
573 buf, err = v.Get(TEST_HASH_3)
575 t.Errorf("Get #3: %v", err)
578 if bytes.Compare(buf, TEST_BLOCK_3) != 0 {
579 t.Errorf("Get #3: expected %s, got %s", string(TEST_BLOCK_3), string(buf))