12 var TEST_BLOCK = []byte("The quick brown fox jumps over the lazy dog.")
13 var TEST_HASH = "e4d909c290d0fb1ca068ffaddf22cbd0"
15 // Test simple block reads.
16 func TestGetBlockOK(t *testing.T) {
21 // Create two test Keep volumes and store a block in each of them.
22 if err := setup(2); err != nil {
25 for _, vol := range KeepVolumes {
26 if err := storeTestBlock(vol, TEST_BLOCK); err != nil {
31 // Check that GetBlock returns success.
32 result, err := GetBlock(TEST_HASH)
34 t.Errorf("GetBlock error: %s", err)
36 if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
37 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
41 // Test block reads when one Keep volume is missing.
42 func TestGetBlockOneKeepOK(t *testing.T) {
47 // Two test Keep volumes, only the second has a block.
48 if err := setup(2); err != nil {
51 if err := storeTestBlock(KeepVolumes[1], TEST_BLOCK); err != nil {
55 // Check that GetBlock returns success.
56 result, err := GetBlock(TEST_HASH)
58 t.Errorf("GetBlock error: %s", err)
60 if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
61 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
65 // Test block read failure.
66 func TestGetBlockFail(t *testing.T) {
71 // Create two empty test Keep volumes.
72 if err := setup(2); err != nil {
76 // Check that GetBlock returns failure.
77 result, err := GetBlock(TEST_HASH)
79 t.Errorf("GetBlock incorrectly returned success: ", result)
84 // Create KeepVolumes for testing.
85 func setup(nkeeps int) error {
86 KeepVolumes = make([]string, 2)
87 for i := range KeepVolumes {
88 if dir, err := ioutil.TempDir(os.TempDir(), "keeptest"); err == nil {
89 KeepVolumes[i] = dir + "/keep"
98 for _, vol := range KeepVolumes {
99 os.RemoveAll(path.Dir(vol))
103 func storeTestBlock(keepdir string, block []byte) error {
104 testhash := fmt.Sprintf("%x", md5.Sum(block))
106 blockdir := fmt.Sprintf("%s/%s", keepdir, testhash[:3])
107 if err := os.MkdirAll(blockdir, 0755); err != nil {
111 blockpath := fmt.Sprintf("%s/%s", blockdir, testhash)
112 if f, err := os.Create(blockpath); err == nil {