)
// A TestableVolumeFactory returns a new TestableVolume. The factory
-// function, and the TestableVolume it returns, can use t to write
+// function, and the TestableVolume it returns, can use "t" to write
// logs, fail the current test, etc.
type TestableVolumeFactory func(t *testing.T) TestableVolume
// DoGenericVolumeTests runs a set of tests that every TestableVolume
-// is expected to pass. It calls factory to create a new writable
-// TestableVolume for each test case, to avoid leaking state between
-// tests.
+// is expected to pass. It calls factory to create a new TestableVolume
+// for each test case, to avoid leaking state between tests.
func DoGenericVolumeTests(t *testing.T, factory TestableVolumeFactory) {
testGet(t, factory)
testGetNoSuchBlock(t, factory)
testString(t, factory)
- testWritableTrue(t, factory)
-
- testGetSerialized(t, factory)
- testPutSerialized(t, factory)
-}
-
-// DoGenericReadOnlyVolumeTests runs a set of tests that every
-// read-only TestableVolume is expected to pass. It calls factory
-// to create a new read-only TestableVolume for each test case,
-// to avoid leaking state between tests.
-func DoGenericReadOnlyVolumeTests(t *testing.T, factory TestableVolumeFactory) {
- testWritableFalse(t, factory)
testUpdateReadOnly(t, factory)
+
+ testGetConcurrent(t, factory)
+ testPutConcurrent(t, factory)
}
// Put a test block, get it and verify content
+// Test should pass for both writable and read-only volumes
func testGet(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
- v.Put(TEST_HASH, TEST_BLOCK)
+
+ v.PutRaw(TEST_HASH, TEST_BLOCK)
buf, err := v.Get(TEST_HASH)
if err != nil {
t.Error(err)
}
+
+ bufs.Put(buf)
+
if bytes.Compare(buf, TEST_BLOCK) != 0 {
t.Errorf("expected %s, got %s", string(TEST_BLOCK), string(buf))
}
}
// Invoke get on a block that does not exist in volume; should result in error
+// Test should pass for both writable and read-only volumes
func testGetNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
- v.Put(TEST_HASH, TEST_BLOCK)
if _, err := v.Get(TEST_HASH_2); err == nil {
t.Errorf("Expected error while getting non-existing block %v", TEST_HASH_2)
}
// Put a test block and compare the locator with same content
+// Test should pass for both writable and read-only volumes
func testCompareSameContent(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
- v.Put(TEST_HASH, TEST_BLOCK)
+ v.PutRaw(TEST_HASH, TEST_BLOCK)
// Compare the block locator with same content
err := v.Compare(TEST_HASH, TEST_BLOCK)
// Put a test block and compare the locator with a different content
// Expect error due to collision
+// Test should pass for both writable and read-only volumes
func testCompareWithDifferentContent(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
- v.Put(TEST_HASH, TEST_BLOCK)
+ v.PutRaw(TEST_HASH, TEST_BLOCK)
// Compare the block locator with different content; collision
err := v.Compare(TEST_HASH, []byte("baddata"))
}
// Put a test block with bad data (hash does not match, but Put does not verify)
-// Compare the locator with good data whose has matches with locator
+// Compare the locator with good data whose hash matches with locator
// Expect error due to corruption.
+// Test should pass for both writable and read-only volumes
func testCompareWithBadData(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
- v.Put(TEST_HASH, []byte("baddata"))
+ v.PutRaw(TEST_HASH, []byte("baddata"))
err := v.Compare(TEST_HASH, TEST_BLOCK)
if err == nil {
}
// Put a block and put again with same content
+// Test is intended for only writable volumes
func testPutBlockWithSameContent(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
+ if v.Writable() == false {
+ return
+ }
+
err := v.Put(TEST_HASH, TEST_BLOCK)
if err != nil {
t.Errorf("Got err putting block %q: %q, expected nil", TEST_BLOCK, err)
}
// Put a block and put again with different content
+// Test is intended for only writable volumes
func testPutBlockWithDifferentContent(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
+ if v.Writable() == false {
+ return
+ }
+
err := v.Put(TEST_HASH, TEST_BLOCK)
if err != nil {
t.Errorf("Got err putting block %q: %q, expected nil", TEST_BLOCK, err)
}
- // Whether Put with the same loc with different content fails or succeeds
- // is implementation dependent. So, just check loc exists after overwriting.
- // We also do not want to see if loc has block1 or block2, for the same reason.
- if err = v.Put(TEST_HASH, TEST_BLOCK_2); err != nil {
- t.Errorf("Got err putting block with different content %q: %q, expected nil", TEST_BLOCK, err)
+ putErr := v.Put(TEST_HASH, TEST_BLOCK_2)
+ buf, getErr := v.Get(TEST_HASH)
+ if putErr == nil {
+ // Put must not return a nil error unless it has
+ // overwritten the existing data.
+ if bytes.Compare(buf, TEST_BLOCK_2) != 0 {
+ t.Errorf("Put succeeded but Get returned %+v, expected %+v", buf, TEST_BLOCK_2)
+ }
+ } else {
+ // It is permissible for Put to fail, but it must
+ // leave us with either the original data, the new
+ // data, or nothing at all.
+ if getErr == nil && bytes.Compare(buf, TEST_BLOCK) != 0 && bytes.Compare(buf, TEST_BLOCK_2) != 0 {
+ t.Errorf("Put failed but Get returned %+v, which is neither %+v nor %+v", buf, TEST_BLOCK, TEST_BLOCK_2)
+ }
}
- if _, err := v.Get(TEST_HASH); err != nil {
- t.Errorf("Got err getting block %q: %q, expected nil", TEST_BLOCK, err)
+ if getErr == nil {
+ bufs.Put(buf)
}
}
// Put and get multiple blocks
+// Test is intended for only writable volumes
func testPutMultipleBlocks(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
+ if v.Writable() == false {
+ return
+ }
+
err := v.Put(TEST_HASH, TEST_BLOCK)
if err != nil {
t.Errorf("Got err putting block %q: %q, expected nil", TEST_BLOCK, err)
t.Errorf("Got err putting block %q: %q, expected nil", TEST_BLOCK_3, err)
}
- if data, err := v.Get(TEST_HASH); err != nil {
+ data, err := v.Get(TEST_HASH)
+ if err != nil {
t.Error(err)
} else if bytes.Compare(data, TEST_BLOCK) != 0 {
t.Errorf("Block present, but content is incorrect: Expected: %v Found: %v", data, TEST_BLOCK)
}
+ bufs.Put(data)
- if data, err := v.Get(TEST_HASH_2); err != nil {
+ data, err = v.Get(TEST_HASH_2)
+ if err != nil {
t.Error(err)
} else if bytes.Compare(data, TEST_BLOCK_2) != 0 {
t.Errorf("Block present, but content is incorrect: Expected: %v Found: %v", data, TEST_BLOCK_2)
}
+ bufs.Put(data)
- if data, err := v.Get(TEST_HASH_3); err != nil {
+ data, err = v.Get(TEST_HASH_3)
+ if err != nil {
t.Error(err)
} else if bytes.Compare(data, TEST_BLOCK_3) != 0 {
t.Errorf("Block present, but content is incorrect: Expected: %v Found: %v", data, TEST_BLOCK_3)
}
+ bufs.Put(data)
}
// testPutAndTouch
// Test that when applying PUT to a block that already exists,
// the block's modification time is updated.
+// Test is intended for only writable volumes
func testPutAndTouch(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
+ if v.Writable() == false {
+ return
+ }
+
if err := v.Put(TEST_HASH, TEST_BLOCK); err != nil {
t.Error(err)
}
}
// Touching a non-existing block should result in error.
+// Test should pass for both writable and read-only volumes
func testTouchNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
- if err := v.Put(TEST_HASH, TEST_BLOCK); err != nil {
- t.Error(err)
- }
-
- if err := v.Touch(TEST_HASH); err != nil {
+ if err := v.Touch(TEST_HASH); err == nil {
t.Error("Expected error when attempted to touch a non-existing block")
}
}
// Invoking Mtime on a non-existing block should result in error.
+// Test should pass for both writable and read-only volumes
func testMtimeNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
// * no prefix
// * with a prefix
// * with no such prefix
+// Test should pass for both writable and read-only volumes
func testIndexTo(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
- v.Put(TEST_HASH, TEST_BLOCK)
- v.Put(TEST_HASH_2, TEST_BLOCK_2)
- v.Put(TEST_HASH_3, TEST_BLOCK_3)
+ v.PutRaw(TEST_HASH, TEST_BLOCK)
+ v.PutRaw(TEST_HASH_2, TEST_BLOCK_2)
+ v.PutRaw(TEST_HASH_3, TEST_BLOCK_3)
buf := new(bytes.Buffer)
v.IndexTo("", buf)
- index_rows := strings.Split(string(buf.Bytes()), "\n")
- sort.Strings(index_rows)
- sorted_index := strings.Join(index_rows, "\n")
+ indexRows := strings.Split(string(buf.Bytes()), "\n")
+ sort.Strings(indexRows)
+ sortedIndex := strings.Join(indexRows, "\n")
m, err := regexp.MatchString(
`^\n`+TEST_HASH+`\+\d+ \d+\n`+
TEST_HASH_3+`\+\d+ \d+\n`+
TEST_HASH_2+`\+\d+ \d+$`,
- sorted_index)
+ sortedIndex)
if err != nil {
t.Error(err)
} else if !m {
- t.Errorf("Got index %q for empty prefix", sorted_index)
+ t.Errorf("Got index %q for empty prefix", sortedIndex)
}
for _, prefix := range []string{"f", "f15", "f15ac"} {
// Calling Delete() for a block immediately after writing it (not old enough)
// should neither delete the data nor return an error.
+// Test is intended for only writable volumes
func testDeleteNewBlock(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
+
+ if v.Writable() == false {
+ return
+ }
+
v.Put(TEST_HASH, TEST_BLOCK)
if err := v.Delete(TEST_HASH); err != nil {
t.Error(err)
}
- if data, err := v.Get(TEST_HASH); err != nil {
+ data, err := v.Get(TEST_HASH)
+ if err != nil {
t.Error(err)
} else if bytes.Compare(data, TEST_BLOCK) != 0 {
t.Error("Block still present, but content is incorrect: %+v != %+v", data, TEST_BLOCK)
}
+ bufs.Put(data)
}
// Calling Delete() for a block with a timestamp older than
// blob_signature_ttl seconds in the past should delete the data.
+// Test is intended for only writable volumes
func testDeleteOldBlock(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
+
+ if v.Writable() == false {
+ return
+ }
+
v.Put(TEST_HASH, TEST_BLOCK)
v.TouchWithDate(TEST_HASH, time.Now().Add(-2*blob_signature_ttl*time.Second))
}
// Calling Delete() for a block that does not exist should result in error.
+// Test should pass for both writable and read-only volumes
func testDeleteNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
- v.Put(TEST_HASH, TEST_BLOCK)
if err := v.Delete(TEST_HASH_2); err == nil {
t.Errorf("Expected error when attempting to delete a non-existing block")
}
// Invoke Status and verify that VolumeStatus is returned
+// Test should pass for both writable and read-only volumes
func testStatus(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
}
// Invoke String for the volume; expect non-empty result
+// Test should pass for both writable and read-only volumes
func testString(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
}
}
-// Verify Writable is true on a writable volume
-func testWritableTrue(t *testing.T, factory TestableVolumeFactory) {
- v := factory(t)
- defer v.Teardown()
-
- if v.Writable() == false {
- t.Errorf("Expected writable to be true on a writable volume")
- }
-}
-
-// Verify Writable is false on a read-only volume
-func testWritableFalse(t *testing.T, factory TestableVolumeFactory) {
+// Putting, updating, touching, and deleting blocks from a read-only volume result in error.
+// Test is intended for only read-only volumes
+func testUpdateReadOnly(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
- if v.Writable() != false {
- t.Errorf("Expected writable to be false on a read-only volume")
+ if v.Writable() == true {
+ return
}
-}
-
-// Updating, touching, and deleting blocks from a read-only volume result in error.
-func testUpdateReadOnly(t *testing.T, factory TestableVolumeFactory) {
- v := factory(t)
- defer v.Teardown()
v.PutRaw(TEST_HASH, TEST_BLOCK)
+ // Get from read-only volume should succeed
_, err := v.Get(TEST_HASH)
if err != nil {
t.Errorf("got err %v, expected nil", err)
}
- err = v.Put(TEST_HASH, TEST_BLOCK)
+ // Put a new block to read-only volume should result in error
+ err = v.Put(TEST_HASH_2, TEST_BLOCK_2)
if err == nil {
t.Errorf("Expected error when putting block in a read-only volume")
}
+ _, err = v.Get(TEST_HASH_2)
+ if err == nil {
+ t.Errorf("Expected error when getting block whose put in read-only volume failed")
+ }
+ // Touch a block in read-only volume should result in error
err = v.Touch(TEST_HASH)
if err == nil {
t.Errorf("Expected error when touching block in a read-only volume")
}
+ // Delete a block from a read-only volume should result in error
err = v.Delete(TEST_HASH)
if err == nil {
t.Errorf("Expected error when deleting block from a read-only volume")
}
+
+ // Overwriting an existing block in read-only volume should result in error
+ err = v.Put(TEST_HASH, TEST_BLOCK)
+ if err == nil {
+ t.Errorf("Expected error when putting block in a read-only volume")
+ }
}
-// Serialization tests: launch a bunch of concurrent
-//
-// TODO(twp): show that the underlying Read/Write operations executed
-// serially and not concurrently. The easiest way to do this is
-// probably to activate verbose or debug logging, capture log output
-// and examine it to confirm that Reads and Writes did not overlap.
-//
-// TODO(twp): a proper test of I/O serialization requires that a
-// second request start while the first one is still underway.
-// Guaranteeing that the test behaves this way requires some tricky
-// synchronization and mocking. For now we'll just launch a bunch of
-// requests simultaenously in goroutines and demonstrate that they
-// return accurate results.
-//
-
-func testGetSerialized(t *testing.T, factory TestableVolumeFactory) {
+// Launch concurrent Gets
+// Test should pass for both writable and read-only volumes
+func testGetConcurrent(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
- v.Put(TEST_HASH, TEST_BLOCK)
- v.Put(TEST_HASH_2, TEST_BLOCK_2)
- v.Put(TEST_HASH_3, TEST_BLOCK_3)
+ v.PutRaw(TEST_HASH, TEST_BLOCK)
+ v.PutRaw(TEST_HASH_2, TEST_BLOCK_2)
+ v.PutRaw(TEST_HASH_3, TEST_BLOCK_3)
sem := make(chan int)
go func(sem chan int) {
if err != nil {
t.Errorf("err1: %v", err)
}
+ bufs.Put(buf)
if bytes.Compare(buf, TEST_BLOCK) != 0 {
t.Errorf("buf should be %s, is %s", string(TEST_BLOCK), string(buf))
}
if err != nil {
t.Errorf("err2: %v", err)
}
+ bufs.Put(buf)
if bytes.Compare(buf, TEST_BLOCK_2) != 0 {
t.Errorf("buf should be %s, is %s", string(TEST_BLOCK_2), string(buf))
}
if err != nil {
t.Errorf("err3: %v", err)
}
+ bufs.Put(buf)
if bytes.Compare(buf, TEST_BLOCK_3) != 0 {
t.Errorf("buf should be %s, is %s", string(TEST_BLOCK_3), string(buf))
}
}
}
-func testPutSerialized(t *testing.T, factory TestableVolumeFactory) {
+// Launch concurrent Puts
+// Test is intended for only writable volumes
+func testPutConcurrent(t *testing.T, factory TestableVolumeFactory) {
v := factory(t)
defer v.Teardown()
+ if v.Writable() == false {
+ return
+ }
+
sem := make(chan int)
go func(sem chan int) {
err := v.Put(TEST_HASH, TEST_BLOCK)
if err != nil {
t.Errorf("Get #1: %v", err)
}
+ bufs.Put(buf)
if bytes.Compare(buf, TEST_BLOCK) != 0 {
t.Errorf("Get #1: expected %s, got %s", string(TEST_BLOCK), string(buf))
}
if err != nil {
t.Errorf("Get #2: %v", err)
}
+ bufs.Put(buf)
if bytes.Compare(buf, TEST_BLOCK_2) != 0 {
t.Errorf("Get #2: expected %s, got %s", string(TEST_BLOCK_2), string(buf))
}
if err != nil {
t.Errorf("Get #3: %v", err)
}
+ bufs.Put(buf)
if bytes.Compare(buf, TEST_BLOCK_3) != 0 {
t.Errorf("Get #3: expected %s, got %s", string(TEST_BLOCK_3), string(buf))
}