15 var TEST_BLOCK = []byte("The quick brown fox jumps over the lazy dog.")
16 var TEST_HASH = "e4d909c290d0fb1ca068ffaddf22cbd0"
17 var TEST_HASH_PUT_RESPONSE = "e4d909c290d0fb1ca068ffaddf22cbd0+44\n"
19 var TEST_BLOCK_2 = []byte("Pack my box with five dozen liquor jugs.")
20 var TEST_HASH_2 = "f15ac516f788aec4f30932ffb6395c39"
22 var TEST_BLOCK_3 = []byte("Now is the time for all good men to come to the aid of their country.")
23 var TEST_HASH_3 = "eed29bbffbc2dbe5e5ee0bb71888e61f"
25 // BAD_BLOCK is used to test collisions and corruption.
26 // It must not match any test hashes.
27 var BAD_BLOCK = []byte("The magic words are squeamish ossifrage.")
29 // TODO(twp): Tests still to be written
32 // - test that PutBlock returns 503 Full if the filesystem is full.
33 // (must mock FreeDiskSpace or Statfs? use a tmpfs?)
35 // * TestPutBlockWriteErr
36 // - test the behavior when Write returns an error.
37 // - Possible solutions: use a small tmpfs and a high
38 // MIN_FREE_KILOBYTES to trick PutBlock into attempting
39 // to write a block larger than the amount of space left
40 // - use an interface to mock ioutil.TempFile with a File
41 // object that always returns an error on write
43 // ========================================
45 // ========================================
48 // Test that simple block reads succeed.
50 func TestGetBlock(t *testing.T) {
53 // Prepare two test Keep volumes. Our block is stored on the second volume.
54 KeepVM = MakeTestVolumeManager(2)
55 defer func() { KeepVM.Quit() }()
57 vols := KeepVM.Volumes()
58 if err := vols[1].Put(TEST_HASH, TEST_BLOCK); err != nil {
62 // Check that GetBlock returns success.
63 result, err := GetBlock(TEST_HASH, false)
65 t.Errorf("GetBlock error: %s", err)
67 if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
68 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
72 // TestGetBlockMissing
73 // GetBlock must return an error when the block is not found.
75 func TestGetBlockMissing(t *testing.T) {
78 // Create two empty test Keep volumes.
79 KeepVM = MakeTestVolumeManager(2)
80 defer func() { KeepVM.Quit() }()
82 // Check that GetBlock returns failure.
83 result, err := GetBlock(TEST_HASH, false)
84 if err != NotFoundError {
85 t.Errorf("Expected NotFoundError, got %v", result)
89 // TestGetBlockCorrupt
90 // GetBlock must return an error when a corrupted block is requested
91 // (the contents of the file do not checksum to its hash).
93 func TestGetBlockCorrupt(t *testing.T) {
96 // Create two test Keep volumes and store a corrupt block in one.
97 KeepVM = MakeTestVolumeManager(2)
98 defer func() { KeepVM.Quit() }()
100 vols := KeepVM.Volumes()
101 vols[0].Put(TEST_HASH, BAD_BLOCK)
103 // Check that GetBlock returns failure.
104 result, err := GetBlock(TEST_HASH, false)
105 if err != DiskHashError {
106 t.Errorf("Expected DiskHashError, got %v (buf: %v)", err, result)
110 // ========================================
112 // ========================================
115 // PutBlock can perform a simple block write and returns success.
117 func TestPutBlockOK(t *testing.T) {
120 // Create two test Keep volumes.
121 KeepVM = MakeTestVolumeManager(2)
122 defer func() { KeepVM.Quit() }()
124 // Check that PutBlock stores the data as expected.
125 if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
126 t.Fatalf("PutBlock: %v", err)
129 vols := KeepVM.Volumes()
130 result, err := vols[0].Get(TEST_HASH)
132 t.Fatalf("Volume #0 Get returned error: %v", err)
134 if string(result) != string(TEST_BLOCK) {
135 t.Fatalf("PutBlock stored '%s', Get retrieved '%s'",
136 string(TEST_BLOCK), string(result))
140 // TestPutBlockOneVol
141 // PutBlock still returns success even when only one of the known
142 // volumes is online.
144 func TestPutBlockOneVol(t *testing.T) {
147 // Create two test Keep volumes, but cripple one of them.
148 KeepVM = MakeTestVolumeManager(2)
149 defer func() { KeepVM.Quit() }()
151 vols := KeepVM.Volumes()
152 vols[0].(*MockVolume).Bad = true
154 // Check that PutBlock stores the data as expected.
155 if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
156 t.Fatalf("PutBlock: %v", err)
159 result, err := GetBlock(TEST_HASH, false)
161 t.Fatalf("GetBlock: %v", err)
163 if string(result) != string(TEST_BLOCK) {
164 t.Error("PutBlock/GetBlock mismatch")
165 t.Fatalf("PutBlock stored '%s', GetBlock retrieved '%s'",
166 string(TEST_BLOCK), string(result))
170 // TestPutBlockMD5Fail
171 // Check that PutBlock returns an error if passed a block and hash that
174 func TestPutBlockMD5Fail(t *testing.T) {
177 // Create two test Keep volumes.
178 KeepVM = MakeTestVolumeManager(2)
179 defer func() { KeepVM.Quit() }()
181 // Check that PutBlock returns the expected error when the hash does
182 // not match the block.
183 if err := PutBlock(BAD_BLOCK, TEST_HASH); err != RequestHashError {
184 t.Error("Expected RequestHashError, got %v", err)
187 // Confirm that GetBlock fails to return anything.
188 if result, err := GetBlock(TEST_HASH, false); err != NotFoundError {
189 t.Errorf("GetBlock succeeded after a corrupt block store (result = %s, err = %v)",
194 // TestPutBlockCorrupt
195 // PutBlock should overwrite corrupt blocks on disk when given
196 // a PUT request with a good block.
198 func TestPutBlockCorrupt(t *testing.T) {
201 // Create two test Keep volumes.
202 KeepVM = MakeTestVolumeManager(2)
203 defer func() { KeepVM.Quit() }()
205 // Store a corrupted block under TEST_HASH.
206 vols := KeepVM.Volumes()
207 vols[0].Put(TEST_HASH, BAD_BLOCK)
208 if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
209 t.Errorf("PutBlock: %v", err)
212 // The block on disk should now match TEST_BLOCK.
213 if block, err := GetBlock(TEST_HASH, false); err != nil {
214 t.Errorf("GetBlock: %v", err)
215 } else if bytes.Compare(block, TEST_BLOCK) != 0 {
216 t.Errorf("GetBlock returned: '%s'", string(block))
220 // TestPutBlockCollision
221 // PutBlock returns a 400 Collision error when attempting to
222 // store a block that collides with another block on disk.
224 func TestPutBlockCollision(t *testing.T) {
227 // These blocks both hash to the MD5 digest cee9a457e790cf20d4bdaa6d69f01e41.
228 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")
229 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")
230 var locator = "cee9a457e790cf20d4bdaa6d69f01e41"
232 // Prepare two test Keep volumes.
233 KeepVM = MakeTestVolumeManager(2)
234 defer func() { KeepVM.Quit() }()
236 // Store one block, then attempt to store the other. Confirm that
237 // PutBlock reported a CollisionError.
238 if err := PutBlock(b1, locator); err != nil {
241 if err := PutBlock(b2, locator); err == nil {
242 t.Error("PutBlock did not report a collision")
243 } else if err != CollisionError {
244 t.Errorf("PutBlock returned %v", err)
248 // TestPutBlockTouchFails
249 // When PutBlock is asked to PUT an existing block, but cannot
250 // modify the timestamp, it should write a second block.
252 func TestPutBlockTouchFails(t *testing.T) {
255 // Prepare two test Keep volumes.
256 KeepVM = MakeTestVolumeManager(2)
257 defer func() { KeepVM.Quit() }()
258 vols := KeepVM.Volumes()
260 // Store a block and then make the underlying volume bad,
261 // so a subsequent attempt to update the file timestamp
263 vols[0].Put(TEST_HASH, BAD_BLOCK)
264 old_mtime, err := vols[0].Mtime(TEST_HASH)
266 t.Fatalf("vols[0].Mtime(%s): %s\n", TEST_HASH, err)
269 // vols[0].Touch will fail on the next call, so the volume
270 // manager will store a copy on vols[1] instead.
271 vols[0].(*MockVolume).Touchable = false
272 if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
273 t.Fatalf("PutBlock: %v", err)
275 vols[0].(*MockVolume).Touchable = true
277 // Now the mtime on the block on vols[0] should be unchanged, and
278 // there should be a copy of the block on vols[1].
279 new_mtime, err := vols[0].Mtime(TEST_HASH)
281 t.Fatalf("vols[0].Mtime(%s): %s\n", TEST_HASH, err)
283 if !new_mtime.Equal(old_mtime) {
284 t.Errorf("mtime was changed on vols[0]:\nold_mtime = %v\nnew_mtime = %v\n",
285 old_mtime, new_mtime)
287 result, err := vols[1].Get(TEST_HASH)
289 t.Fatalf("vols[1]: %v", err)
291 if bytes.Compare(result, TEST_BLOCK) != 0 {
292 t.Errorf("new block does not match test block\nnew block = %v\n", result)
296 // ========================================
297 // FindKeepVolumes tests.
298 // ========================================
300 // TestFindKeepVolumes
301 // Confirms that FindKeepVolumes finds tmpfs volumes with "/keep"
302 // directories at the top level.
304 func TestFindKeepVolumes(t *testing.T) {
305 var tempVols [2]string
309 for _, path := range tempVols {
314 // Create two directories suitable for using as keep volumes.
315 for i := range tempVols {
316 if tempVols[i], err = ioutil.TempDir("", "findvol"); err != nil {
319 tempVols[i] = tempVols[i] + "/keep"
320 if err = os.Mkdir(tempVols[i], 0755); err != nil {
325 // Set up a bogus PROC_MOUNTS file.
326 if f, err := ioutil.TempFile("", "keeptest"); err == nil {
327 for _, vol := range tempVols {
328 fmt.Fprintf(f, "tmpfs %s tmpfs opts\n", path.Dir(vol))
331 PROC_MOUNTS = f.Name()
333 // Check that FindKeepVolumes finds the temp volumes.
334 resultVols := FindKeepVolumes()
335 if len(tempVols) != len(resultVols) {
336 t.Fatalf("set up %d volumes, FindKeepVolumes found %d\n",
337 len(tempVols), len(resultVols))
339 for i := range tempVols {
340 if tempVols[i] != resultVols[i] {
341 t.Errorf("FindKeepVolumes returned %s, expected %s\n",
342 resultVols[i], tempVols[i])
350 // TestFindKeepVolumesFail
351 // When no Keep volumes are present, FindKeepVolumes returns an empty slice.
353 func TestFindKeepVolumesFail(t *testing.T) {
356 // Set up a bogus PROC_MOUNTS file with no Keep vols.
357 if f, err := ioutil.TempFile("", "keeptest"); err == nil {
358 fmt.Fprintln(f, "rootfs / rootfs opts 0 0")
359 fmt.Fprintln(f, "sysfs /sys sysfs opts 0 0")
360 fmt.Fprintln(f, "proc /proc proc opts 0 0")
361 fmt.Fprintln(f, "udev /dev devtmpfs opts 0 0")
362 fmt.Fprintln(f, "devpts /dev/pts devpts opts 0 0")
364 PROC_MOUNTS = f.Name()
366 // Check that FindKeepVolumes returns an empty array.
367 resultVols := FindKeepVolumes()
368 if len(resultVols) != 0 {
369 t.Fatalf("FindKeepVolumes returned %v", resultVols)
372 os.Remove(PROC_MOUNTS)
377 // Test an /index request.
378 func TestIndex(t *testing.T) {
381 // Set up Keep volumes and populate them.
382 // Include multiple blocks on different volumes, and
383 // some metadata files.
384 KeepVM = MakeTestVolumeManager(2)
385 defer func() { KeepVM.Quit() }()
387 vols := KeepVM.Volumes()
388 vols[0].Put(TEST_HASH, TEST_BLOCK)
389 vols[1].Put(TEST_HASH_2, TEST_BLOCK_2)
390 vols[0].Put(TEST_HASH_3, TEST_BLOCK_3)
391 vols[0].Put(TEST_HASH+".meta", []byte("metadata"))
392 vols[1].Put(TEST_HASH_2+".meta", []byte("metadata"))
394 index := vols[0].Index("") + vols[1].Index("")
395 index_rows := strings.Split(index, "\n")
396 sort.Strings(index_rows)
397 sorted_index := strings.Join(index_rows, "\n")
398 expected := `^\n` + TEST_HASH + `\+\d+ \d+\n` +
399 TEST_HASH_3 + `\+\d+ \d+\n` +
400 TEST_HASH_2 + `\+\d+ \d+$`
402 match, err := regexp.MatchString(expected, sorted_index)
405 t.Errorf("IndexLocators returned:\n%s", index)
408 t.Errorf("regexp.MatchString: %s", err)
413 // Test that GetNodeStatus returns valid info about available volumes.
415 // TODO(twp): set up appropriate interfaces to permit more rigorous
418 func TestNodeStatus(t *testing.T) {
421 // Set up test Keep volumes with some blocks.
422 KeepVM = MakeTestVolumeManager(2)
423 defer func() { KeepVM.Quit() }()
425 vols := KeepVM.Volumes()
426 vols[0].Put(TEST_HASH, TEST_BLOCK)
427 vols[1].Put(TEST_HASH_2, TEST_BLOCK_2)
429 // Get node status and make a basic sanity check.
430 st := GetNodeStatus()
431 for i := range vols {
432 volinfo := st.Volumes[i]
433 mtp := volinfo.MountPoint
435 t.Errorf("GetNodeStatus mount_point %s, expected /bogo", mtp)
437 if volinfo.DeviceNum == 0 {
438 t.Errorf("uninitialized device_num in %v", volinfo)
440 if volinfo.BytesFree == 0 {
441 t.Errorf("uninitialized bytes_free in %v", volinfo)
443 if volinfo.BytesUsed == 0 {
444 t.Errorf("uninitialized bytes_used in %v", volinfo)
449 // ========================================
450 // Helper functions for unit tests.
451 // ========================================
453 // MakeTestVolumeManager
454 // Creates and returns a RRVolumeManager with the specified number
457 func MakeTestVolumeManager(num_volumes int) VolumeManager {
458 vols := make([]Volume, num_volumes)
459 for i := range vols {
460 vols[i] = CreateMockVolume()
462 return MakeRRVolumeManager(vols)
466 // Cleanup to perform after each test.
469 data_manager_token = ""
470 enforce_permissions = false
471 PermissionSecret = nil