12 var TEST_BLOCK = []byte("The quick brown fox jumps over the lazy dog.")
13 var TEST_HASH = "e4d909c290d0fb1ca068ffaddf22cbd0"
14 var BAD_BLOCK = []byte("The magic words are squeamish ossifrage.")
16 // TODO(twp): Tests still to be written
19 // - test that PutBlock returns 503 Full if the filesystem is full.
20 // (must mock FreeDiskSpace or Statfs? use a tmpfs?)
23 // - test the behavior when Write returns an error.
24 // - Possible solutions: use a small tmpfs and a high
25 // MIN_FREE_KILOBYTES to trick PutBlock into attempting
26 // to write a block larger than the amount of space left
27 // - use an interface to mock ioutil.TempFile with a File
28 // object that always returns an error on write
30 // ========================================
32 // ========================================
35 // Test that simple block reads succeed.
37 func TestGetBlock(t *testing.T) {
40 // Prepare two test Keep volumes. Our block is stored on the second volume.
41 KeepVolumes = setup(t, 2)
42 store(t, KeepVolumes[1], TEST_HASH, TEST_BLOCK)
44 // Check that GetBlock returns success.
45 result, err := GetBlock(TEST_HASH)
47 t.Errorf("GetBlock error: %s", err)
49 if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
50 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
54 // TestGetBlockMissing
55 // GetBlock must return an error when the block is not found.
57 func TestGetBlockMissing(t *testing.T) {
60 // Create two empty test Keep volumes.
61 KeepVolumes = setup(t, 2)
63 // Check that GetBlock returns failure.
64 result, err := GetBlock(TEST_HASH)
65 if err != NotFoundError {
66 t.Errorf("Expected NotFoundError, got %v", result)
70 // TestGetBlockCorrupt
71 // GetBlock must return an error when a corrupted block is requested
72 // (the contents of the file do not checksum to its hash).
74 func TestGetBlockCorrupt(t *testing.T) {
77 // Create two test Keep volumes and store a block in each of them,
78 // but the hash of the block does not match the filename.
79 KeepVolumes = setup(t, 2)
80 for _, vol := range KeepVolumes {
81 store(t, vol, TEST_HASH, BAD_BLOCK)
84 // Check that GetBlock returns failure.
85 result, err := GetBlock(TEST_HASH)
86 if err != CorruptError {
87 t.Errorf("Expected CorruptError, got %v", result)
91 // ========================================
93 // ========================================
96 // PutBlock can perform a simple block write and returns success.
98 func TestPutBlockOK(t *testing.T) {
101 // Create two test Keep volumes.
102 KeepVolumes = setup(t, 2)
104 // Check that PutBlock stores the data as expected.
105 if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
106 t.Fatalf("PutBlock: %v", err)
109 result, err := GetBlock(TEST_HASH)
111 t.Fatalf("GetBlock returned error: %v", err)
113 if string(result) != string(TEST_BLOCK) {
114 t.Error("PutBlock/GetBlock mismatch")
115 t.Fatalf("PutBlock stored '%s', GetBlock retrieved '%s'",
116 string(TEST_BLOCK), string(result))
120 // TestPutBlockOneVol
121 // PutBlock still returns success even when only one of the known
122 // volumes is online.
124 func TestPutBlockOneVol(t *testing.T) {
127 // Create two test Keep volumes, but cripple one of them.
128 KeepVolumes = setup(t, 2)
129 os.Chmod(KeepVolumes[0], 000)
131 // Check that PutBlock stores the data as expected.
132 if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
133 t.Fatalf("PutBlock: %v", err)
136 result, err := GetBlock(TEST_HASH)
138 t.Fatalf("GetBlock: %v", err)
140 if string(result) != string(TEST_BLOCK) {
141 t.Error("PutBlock/GetBlock mismatch")
142 t.Fatalf("PutBlock stored '%s', GetBlock retrieved '%s'",
143 string(TEST_BLOCK), string(result))
147 // TestPutBlockMD5Fail
148 // Check that PutBlock returns an error if passed a block and hash that
151 func TestPutBlockMD5Fail(t *testing.T) {
154 // Create two test Keep volumes.
155 KeepVolumes = setup(t, 2)
157 // Check that PutBlock returns the expected error when the hash does
158 // not match the block.
159 if err := PutBlock(BAD_BLOCK, TEST_HASH); err != MD5Error {
160 t.Error("Expected MD5Error, got %v", err)
163 // Confirm that GetBlock fails to return anything.
164 if result, err := GetBlock(TEST_HASH); err != NotFoundError {
165 t.Errorf("GetBlock succeeded after a corrupt block store (result = %s, err = %v)",
170 // TestPutBlockCorrupt
171 // PutBlock should overwrite corrupt blocks on disk when given
172 // a PUT request with a good block.
174 func TestPutBlockCorrupt(t *testing.T) {
177 // Create two test Keep volumes.
178 KeepVolumes = setup(t, 2)
180 // Store a corrupted block under TEST_HASH.
181 store(t, KeepVolumes[0], TEST_HASH, BAD_BLOCK)
182 if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
183 t.Errorf("PutBlock: %v", err)
186 // The block on disk should now match TEST_BLOCK.
187 if block, err := GetBlock(TEST_HASH); err != nil {
188 t.Errorf("GetBlock: %v", err)
189 } else if bytes.Compare(block, TEST_BLOCK) != 0 {
190 t.Errorf("GetBlock returned: '%s'", string(block))
195 // PutBlock returns a 400 Collision error when attempting to
196 // store a block that collides with another block on disk.
198 func TestPutBlockCollision(t *testing.T) {
201 // These blocks both hash to the MD5 digest cee9a457e790cf20d4bdaa6d69f01e41.
202 var b1 = []byte("\x0e0eaU\x9a\xa7\x87\xd0\x0b\xc6\xf7\x0b\xbd\xfe4\x04\xcf\x03e\x9epO\x854\xc0\x0f\xfbe\x9cL\x87@\xcc\x94/\xeb-\xa1\x15\xa3\xf4\x15\\\xbb\x86\x07Is\x86em}\x1f4\xa4 Y\xd7\x8fZ\x8d\xd1\xef")
203 var b2 = []byte("\x0e0eaU\x9a\xa7\x87\xd0\x0b\xc6\xf7\x0b\xbd\xfe4\x04\xcf\x03e\x9etO\x854\xc0\x0f\xfbe\x9cL\x87@\xcc\x94/\xeb-\xa1\x15\xa3\xf4\x15\xdc\xbb\x86\x07Is\x86em}\x1f4\xa4 Y\xd7\x8fZ\x8d\xd1\xef")
204 var locator = "cee9a457e790cf20d4bdaa6d69f01e41"
206 // Prepare two test Keep volumes. Store one block,
207 // then attempt to store the other.
208 KeepVolumes = setup(t, 2)
209 store(t, KeepVolumes[1], locator, b1)
211 if err := PutBlock(b2, locator); err == nil {
212 t.Error("PutBlock did not report a collision")
213 } else if err != CollisionError {
214 t.Errorf("PutBlock returned %v", err)
218 // ========================================
219 // FindKeepVolumes tests.
220 // ========================================
222 // TestFindKeepVolumes
223 // Confirms that FindKeepVolumes finds tmpfs volumes with "/keep"
224 // directories at the top level.
226 func TestFindKeepVolumes(t *testing.T) {
229 // Initialize two keep volumes.
230 var tempVols []string = setup(t, 2)
232 // Set up a bogus PROC_MOUNTS file.
233 if f, err := ioutil.TempFile("", "keeptest"); err == nil {
234 for _, vol := range tempVols {
235 fmt.Fprintf(f, "tmpfs %s tmpfs opts\n", path.Dir(vol))
238 PROC_MOUNTS = f.Name()
240 // Check that FindKeepVolumes finds the temp volumes.
241 resultVols := FindKeepVolumes()
242 if len(tempVols) != len(resultVols) {
243 t.Fatalf("set up %d volumes, FindKeepVolumes found %d\n",
244 len(tempVols), len(resultVols))
246 for i := range tempVols {
247 if tempVols[i] != resultVols[i] {
248 t.Errorf("FindKeepVolumes returned %s, expected %s\n",
249 resultVols[i], tempVols[i])
257 // TestFindKeepVolumesFail
258 // When no Keep volumes are present, FindKeepVolumes returns an empty slice.
260 func TestFindKeepVolumesFail(t *testing.T) {
263 // Set up a bogus PROC_MOUNTS file with no Keep vols.
264 if f, err := ioutil.TempFile("", "keeptest"); err == nil {
265 fmt.Fprintln(f, "rootfs / rootfs opts 0 0")
266 fmt.Fprintln(f, "sysfs /sys sysfs opts 0 0")
267 fmt.Fprintln(f, "proc /proc proc opts 0 0")
268 fmt.Fprintln(f, "udev /dev devtmpfs opts 0 0")
269 fmt.Fprintln(f, "devpts /dev/pts devpts opts 0 0")
271 PROC_MOUNTS = f.Name()
273 // Check that FindKeepVolumes returns an empty array.
274 resultVols := FindKeepVolumes()
275 if len(resultVols) != 0 {
276 t.Fatalf("FindKeepVolumes returned %v", resultVols)
279 os.Remove(PROC_MOUNTS)
283 // ========================================
284 // Helper functions for unit tests.
285 // ========================================
288 // Create KeepVolumes for testing.
289 // Returns a slice of pathnames to temporary Keep volumes.
291 func setup(t *testing.T, num_volumes int) []string {
292 vols := make([]string, num_volumes)
293 for i := range vols {
294 if dir, err := ioutil.TempDir(os.TempDir(), "keeptest"); err == nil {
295 vols[i] = dir + "/keep"
296 os.Mkdir(vols[i], 0755)
305 // Cleanup to perform after each test.
308 for _, vol := range KeepVolumes {
309 os.RemoveAll(path.Dir(vol))
315 // Low-level code to write Keep blocks directly to disk for testing.
317 func store(t *testing.T, keepdir string, filename string, block []byte) {
318 blockdir := fmt.Sprintf("%s/%s", keepdir, filename[:3])
319 if err := os.MkdirAll(blockdir, 0755); err != nil {
323 blockpath := fmt.Sprintf("%s/%s", blockdir, filename)
324 if f, err := os.Create(blockpath); err == nil {