13 var TEST_BLOCK = []byte("The quick brown fox jumps over the lazy dog.")
14 var TEST_HASH = "e4d909c290d0fb1ca068ffaddf22cbd0"
16 var TEST_BLOCK_2 = []byte("Pack my box with five dozen liquor jugs.")
17 var TEST_HASH_2 = "f15ac516f788aec4f30932ffb6395c39"
19 var TEST_BLOCK_3 = []byte("Now is the time for all good men to come to the aid of their country.")
20 var TEST_HASH_3 = "eed29bbffbc2dbe5e5ee0bb71888e61f"
22 // BAD_BLOCK is used to test collisions and corruption.
23 // It must not match any test hashes.
24 var BAD_BLOCK = []byte("The magic words are squeamish ossifrage.")
26 // TODO(twp): Tests still to be written
29 // - test that PutBlock returns 503 Full if the filesystem is full.
30 // (must mock FreeDiskSpace or Statfs? use a tmpfs?)
32 // * TestPutBlockWriteErr
33 // - test the behavior when Write returns an error.
34 // - Possible solutions: use a small tmpfs and a high
35 // MIN_FREE_KILOBYTES to trick PutBlock into attempting
36 // to write a block larger than the amount of space left
37 // - use an interface to mock ioutil.TempFile with a File
38 // object that always returns an error on write
40 // ========================================
42 // ========================================
45 // Test that simple block reads succeed.
47 func TestGetBlock(t *testing.T) {
50 // Prepare two test Keep volumes. Our block is stored on the second volume.
51 KeepVolumes = setup(t, 2)
52 store(t, KeepVolumes[1], TEST_HASH, TEST_BLOCK)
54 // Check that GetBlock returns success.
55 result, err := GetBlock(TEST_HASH)
57 t.Errorf("GetBlock error: %s", err)
59 if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
60 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
64 // TestGetBlockMissing
65 // GetBlock must return an error when the block is not found.
67 func TestGetBlockMissing(t *testing.T) {
70 // Create two empty test Keep volumes.
71 KeepVolumes = setup(t, 2)
73 // Check that GetBlock returns failure.
74 result, err := GetBlock(TEST_HASH)
75 if err != NotFoundError {
76 t.Errorf("Expected NotFoundError, got %v", result)
80 // TestGetBlockCorrupt
81 // GetBlock must return an error when a corrupted block is requested
82 // (the contents of the file do not checksum to its hash).
84 func TestGetBlockCorrupt(t *testing.T) {
87 // Create two test Keep volumes and store a block in each of them,
88 // but the hash of the block does not match the filename.
89 KeepVolumes = setup(t, 2)
90 for _, vol := range KeepVolumes {
91 store(t, vol, TEST_HASH, BAD_BLOCK)
94 // Check that GetBlock returns failure.
95 result, err := GetBlock(TEST_HASH)
96 if err != CorruptError {
97 t.Errorf("Expected CorruptError, got %v", result)
101 // ========================================
103 // ========================================
106 // PutBlock can perform a simple block write and returns success.
108 func TestPutBlockOK(t *testing.T) {
111 // Create two test Keep volumes.
112 KeepVolumes = setup(t, 2)
114 // Check that PutBlock stores the data as expected.
115 if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
116 t.Fatalf("PutBlock: %v", err)
119 result, err := GetBlock(TEST_HASH)
121 t.Fatalf("GetBlock returned error: %v", err)
123 if string(result) != string(TEST_BLOCK) {
124 t.Error("PutBlock/GetBlock mismatch")
125 t.Fatalf("PutBlock stored '%s', GetBlock retrieved '%s'",
126 string(TEST_BLOCK), string(result))
130 // TestPutBlockOneVol
131 // PutBlock still returns success even when only one of the known
132 // volumes is online.
134 func TestPutBlockOneVol(t *testing.T) {
137 // Create two test Keep volumes, but cripple one of them.
138 KeepVolumes = setup(t, 2)
139 os.Chmod(KeepVolumes[0], 000)
141 // Check that PutBlock stores the data as expected.
142 if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
143 t.Fatalf("PutBlock: %v", err)
146 result, err := GetBlock(TEST_HASH)
148 t.Fatalf("GetBlock: %v", err)
150 if string(result) != string(TEST_BLOCK) {
151 t.Error("PutBlock/GetBlock mismatch")
152 t.Fatalf("PutBlock stored '%s', GetBlock retrieved '%s'",
153 string(TEST_BLOCK), string(result))
157 // TestPutBlockMD5Fail
158 // Check that PutBlock returns an error if passed a block and hash that
161 func TestPutBlockMD5Fail(t *testing.T) {
164 // Create two test Keep volumes.
165 KeepVolumes = setup(t, 2)
167 // Check that PutBlock returns the expected error when the hash does
168 // not match the block.
169 if err := PutBlock(BAD_BLOCK, TEST_HASH); err != MD5Error {
170 t.Error("Expected MD5Error, got %v", err)
173 // Confirm that GetBlock fails to return anything.
174 if result, err := GetBlock(TEST_HASH); err != NotFoundError {
175 t.Errorf("GetBlock succeeded after a corrupt block store (result = %s, err = %v)",
180 // TestPutBlockCorrupt
181 // PutBlock should overwrite corrupt blocks on disk when given
182 // a PUT request with a good block.
184 func TestPutBlockCorrupt(t *testing.T) {
187 // Create two test Keep volumes.
188 KeepVolumes = setup(t, 2)
190 // Store a corrupted block under TEST_HASH.
191 store(t, KeepVolumes[0], TEST_HASH, BAD_BLOCK)
192 if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
193 t.Errorf("PutBlock: %v", err)
196 // The block on disk should now match TEST_BLOCK.
197 if block, err := GetBlock(TEST_HASH); err != nil {
198 t.Errorf("GetBlock: %v", err)
199 } else if bytes.Compare(block, TEST_BLOCK) != 0 {
200 t.Errorf("GetBlock returned: '%s'", string(block))
205 // PutBlock returns a 400 Collision error when attempting to
206 // store a block that collides with another block on disk.
208 func TestPutBlockCollision(t *testing.T) {
211 // These blocks both hash to the MD5 digest cee9a457e790cf20d4bdaa6d69f01e41.
212 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")
213 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")
214 var locator = "cee9a457e790cf20d4bdaa6d69f01e41"
216 // Prepare two test Keep volumes. Store one block,
217 // then attempt to store the other.
218 KeepVolumes = setup(t, 2)
219 store(t, KeepVolumes[1], locator, b1)
221 if err := PutBlock(b2, locator); err == nil {
222 t.Error("PutBlock did not report a collision")
223 } else if err != CollisionError {
224 t.Errorf("PutBlock returned %v", err)
228 // ========================================
229 // FindKeepVolumes tests.
230 // ========================================
232 // TestFindKeepVolumes
233 // Confirms that FindKeepVolumes finds tmpfs volumes with "/keep"
234 // directories at the top level.
236 func TestFindKeepVolumes(t *testing.T) {
239 // Initialize two keep volumes.
240 var tempVols []string = setup(t, 2)
242 // Set up a bogus PROC_MOUNTS file.
243 if f, err := ioutil.TempFile("", "keeptest"); err == nil {
244 for _, vol := range tempVols {
245 fmt.Fprintf(f, "tmpfs %s tmpfs opts\n", path.Dir(vol))
248 PROC_MOUNTS = f.Name()
250 // Check that FindKeepVolumes finds the temp volumes.
251 resultVols := FindKeepVolumes()
252 if len(tempVols) != len(resultVols) {
253 t.Fatalf("set up %d volumes, FindKeepVolumes found %d\n",
254 len(tempVols), len(resultVols))
256 for i := range tempVols {
257 if tempVols[i] != resultVols[i] {
258 t.Errorf("FindKeepVolumes returned %s, expected %s\n",
259 resultVols[i], tempVols[i])
267 // TestFindKeepVolumesFail
268 // When no Keep volumes are present, FindKeepVolumes returns an empty slice.
270 func TestFindKeepVolumesFail(t *testing.T) {
273 // Set up a bogus PROC_MOUNTS file with no Keep vols.
274 if f, err := ioutil.TempFile("", "keeptest"); err == nil {
275 fmt.Fprintln(f, "rootfs / rootfs opts 0 0")
276 fmt.Fprintln(f, "sysfs /sys sysfs opts 0 0")
277 fmt.Fprintln(f, "proc /proc proc opts 0 0")
278 fmt.Fprintln(f, "udev /dev devtmpfs opts 0 0")
279 fmt.Fprintln(f, "devpts /dev/pts devpts opts 0 0")
281 PROC_MOUNTS = f.Name()
283 // Check that FindKeepVolumes returns an empty array.
284 resultVols := FindKeepVolumes()
285 if len(resultVols) != 0 {
286 t.Fatalf("FindKeepVolumes returned %v", resultVols)
289 os.Remove(PROC_MOUNTS)
294 // Test an /index request.
295 func TestIndex(t *testing.T) {
298 // Set up Keep volumes and populate them.
299 // Include multiple blocks on different volumes, and
300 // some metadata files.
301 KeepVolumes = setup(t, 2)
302 store(t, KeepVolumes[0], TEST_HASH, TEST_BLOCK)
303 store(t, KeepVolumes[1], TEST_HASH_2, TEST_BLOCK_2)
304 store(t, KeepVolumes[0], TEST_HASH_3, TEST_BLOCK_3)
305 store(t, KeepVolumes[0], TEST_HASH+".meta", []byte("metadata"))
306 store(t, KeepVolumes[1], TEST_HASH_2+".meta", []byte("metadata"))
308 index := IndexLocators("")
309 expected := `^` + TEST_HASH + `\+\d+ \d+\n` +
310 TEST_HASH_3 + `\+\d+ \d+\n` +
311 TEST_HASH_2 + `\+\d+ \d+\n$`
313 match, err := regexp.MatchString(expected, index)
316 t.Errorf("IndexLocators returned:\n-----\n%s-----\n", index)
319 t.Errorf("regexp.MatchString: %s", err)
324 // Test that GetNodeStatus returns valid info about available volumes.
326 // TODO(twp): set up appropriate interfaces to permit more rigorous
329 func TestNodeStatus(t *testing.T) {
332 // Set up test Keep volumes.
333 KeepVolumes = setup(t, 2)
335 // Get node status and make a basic sanity check.
336 st := GetNodeStatus()
337 for i, vol := range KeepVolumes {
338 volinfo := st.Volumes[i]
339 mtp := volinfo.MountPoint
341 t.Errorf("GetNodeStatus mount_point %s != KeepVolume %s", mtp, vol)
343 if volinfo.DeviceNum == 0 {
344 t.Errorf("uninitialized device_num in %v", volinfo)
346 if volinfo.BytesFree == 0 {
347 t.Errorf("uninitialized bytes_free in %v", volinfo)
349 if volinfo.BytesUsed == 0 {
350 t.Errorf("uninitialized bytes_used in %v", volinfo)
355 // ========================================
356 // Helper functions for unit tests.
357 // ========================================
360 // Create KeepVolumes for testing.
361 // Returns a slice of pathnames to temporary Keep volumes.
363 func setup(t *testing.T, num_volumes int) []string {
364 vols := make([]string, num_volumes)
365 for i := range vols {
366 if dir, err := ioutil.TempDir(os.TempDir(), "keeptest"); err == nil {
367 vols[i] = dir + "/keep"
368 os.Mkdir(vols[i], 0755)
377 // Cleanup to perform after each test.
380 for _, vol := range KeepVolumes {
381 os.RemoveAll(path.Dir(vol))
387 // Low-level code to write Keep blocks directly to disk for testing.
389 func store(t *testing.T, keepdir string, filename string, block []byte) {
390 blockdir := fmt.Sprintf("%s/%s", keepdir, filename[:3])
391 if err := os.MkdirAll(blockdir, 0755); err != nil {
395 blockpath := fmt.Sprintf("%s/%s", blockdir, filename)
396 if f, err := os.Create(blockpath); err == nil {