11 var TEST_BLOCK = []byte("The quick brown fox jumps over the lazy dog.")
12 var TEST_HASH = "e4d909c290d0fb1ca068ffaddf22cbd0"
13 var BAD_BLOCK = []byte("The magic words are squeamish ossifrage.")
15 // Test simple block reads.
16 func TestGetBlockOK(t *testing.T) {
19 // Create two test Keep volumes and store a block in each of them.
21 for _, vol := range KeepVolumes {
22 store(t, vol, TEST_HASH, TEST_BLOCK)
25 // Check that GetBlock returns success.
26 result, err := GetBlock(TEST_HASH)
28 t.Errorf("GetBlock error: %s", err)
30 if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
31 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
35 // Test block reads when one Keep volume is missing.
36 func TestGetBlockOneKeepOK(t *testing.T) {
39 // Two test Keep volumes, only the second has a block.
41 store(t, KeepVolumes[1], TEST_HASH, TEST_BLOCK)
43 // Check that GetBlock returns success.
44 result, err := GetBlock(TEST_HASH)
46 t.Errorf("GetBlock error: %s", err)
48 if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
49 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
53 // Test block read failure.
54 func TestGetBlockFail(t *testing.T) {
57 // Create two empty test Keep volumes.
60 // Check that GetBlock returns failure.
61 result, err := GetBlock(TEST_HASH)
63 t.Errorf("GetBlock incorrectly returned success: ", result)
67 // Test reading a corrupt block.
68 func TestGetBlockCorrupt(t *testing.T) {
71 // Create two test Keep volumes and store a block in each of them,
72 // but the hash of the block does not match the filename.
74 for _, vol := range KeepVolumes {
75 store(t, vol, TEST_HASH, BAD_BLOCK)
78 // Check that GetBlock returns failure.
79 result, err := GetBlock(TEST_HASH)
81 t.Errorf("GetBlock incorrectly returned success: %s", result)
86 // Create KeepVolumes for testing.
88 func setup(t *testing.T, num_volumes int) {
89 KeepVolumes = make([]string, num_volumes)
90 for i := range KeepVolumes {
91 if dir, err := ioutil.TempDir(os.TempDir(), "keeptest"); err == nil {
92 KeepVolumes[i] = dir + "/keep"
100 // Cleanup to perform after each test.
103 for _, vol := range KeepVolumes {
104 os.RemoveAll(path.Dir(vol))
110 func store(t *testing.T, keepdir string, filename string, block []byte) error {
111 blockdir := fmt.Sprintf("%s/%s", keepdir, filename[:3])
112 if err := os.MkdirAll(blockdir, 0755); err != nil {
116 blockpath := fmt.Sprintf("%s/%s", blockdir, filename)
117 if f, err := os.Create(blockpath); err == nil {