1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
20 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
23 var TestBlock = []byte("The quick brown fox jumps over the lazy dog.")
24 var TestHash = "e4d909c290d0fb1ca068ffaddf22cbd0"
25 var TestHashPutResp = "e4d909c290d0fb1ca068ffaddf22cbd0+44\n"
27 var TestBlock2 = []byte("Pack my box with five dozen liquor jugs.")
28 var TestHash2 = "f15ac516f788aec4f30932ffb6395c39"
30 var TestBlock3 = []byte("Now is the time for all good men to come to the aid of their country.")
31 var TestHash3 = "eed29bbffbc2dbe5e5ee0bb71888e61f"
33 // BadBlock is used to test collisions and corruption.
34 // It must not match any test hashes.
35 var BadBlock = []byte("The magic words are squeamish ossifrage.")
38 var EmptyHash = "d41d8cd98f00b204e9800998ecf8427e"
39 var EmptyBlock = []byte("")
41 // TODO(twp): Tests still to be written
44 // - test that PutBlock returns 503 Full if the filesystem is full.
45 // (must mock FreeDiskSpace or Statfs? use a tmpfs?)
47 // * TestPutBlockWriteErr
48 // - test the behavior when Write returns an error.
49 // - Possible solutions: use a small tmpfs and a high
50 // MIN_FREE_KILOBYTES to trick PutBlock into attempting
51 // to write a block larger than the amount of space left
52 // - use an interface to mock ioutil.TempFile with a File
53 // object that always returns an error on write
55 // ========================================
57 // ========================================
60 // Test that simple block reads succeed.
62 func TestGetBlock(t *testing.T) {
65 // Prepare two test Keep volumes. Our block is stored on the second volume.
66 KeepVM = MakeTestVolumeManager(2)
69 vols := KeepVM.AllReadable()
70 if err := vols[1].Put(context.Background(), TestHash, TestBlock); err != nil {
74 // Check that GetBlock returns success.
75 buf := make([]byte, BlockSize)
76 size, err := GetBlock(context.Background(), TestHash, buf, nil)
78 t.Errorf("GetBlock error: %s", err)
80 if bytes.Compare(buf[:size], TestBlock) != 0 {
81 t.Errorf("got %v, expected %v", buf[:size], TestBlock)
85 // TestGetBlockMissing
86 // GetBlock must return an error when the block is not found.
88 func TestGetBlockMissing(t *testing.T) {
91 // Create two empty test Keep volumes.
92 KeepVM = MakeTestVolumeManager(2)
95 // Check that GetBlock returns failure.
96 buf := make([]byte, BlockSize)
97 size, err := GetBlock(context.Background(), TestHash, buf, nil)
98 if err != NotFoundError {
99 t.Errorf("Expected NotFoundError, got %v, err %v", buf[:size], err)
103 // TestGetBlockCorrupt
104 // GetBlock must return an error when a corrupted block is requested
105 // (the contents of the file do not checksum to its hash).
107 func TestGetBlockCorrupt(t *testing.T) {
110 // Create two test Keep volumes and store a corrupt block in one.
111 KeepVM = MakeTestVolumeManager(2)
114 vols := KeepVM.AllReadable()
115 vols[0].Put(context.Background(), TestHash, BadBlock)
117 // Check that GetBlock returns failure.
118 buf := make([]byte, BlockSize)
119 size, err := GetBlock(context.Background(), TestHash, buf, nil)
120 if err != DiskHashError {
121 t.Errorf("Expected DiskHashError, got %v (buf: %v)", err, buf[:size])
125 // ========================================
127 // ========================================
130 // PutBlock can perform a simple block write and returns success.
132 func TestPutBlockOK(t *testing.T) {
135 // Create two test Keep volumes.
136 KeepVM = MakeTestVolumeManager(2)
139 // Check that PutBlock stores the data as expected.
140 if n, err := PutBlock(context.Background(), TestBlock, TestHash); err != nil || n < 1 {
141 t.Fatalf("PutBlock: n %d err %v", n, err)
144 vols := KeepVM.AllReadable()
145 buf := make([]byte, BlockSize)
146 n, err := vols[1].Get(context.Background(), TestHash, buf)
148 t.Fatalf("Volume #0 Get returned error: %v", err)
150 if string(buf[:n]) != string(TestBlock) {
151 t.Fatalf("PutBlock stored '%s', Get retrieved '%s'",
152 string(TestBlock), string(buf[:n]))
156 // TestPutBlockOneVol
157 // PutBlock still returns success even when only one of the known
158 // volumes is online.
160 func TestPutBlockOneVol(t *testing.T) {
163 // Create two test Keep volumes, but cripple one of them.
164 KeepVM = MakeTestVolumeManager(2)
167 vols := KeepVM.AllWritable()
168 vols[0].(*MockVolume).Bad = true
169 vols[0].(*MockVolume).BadVolumeError = errors.New("Bad volume")
171 // Check that PutBlock stores the data as expected.
172 if n, err := PutBlock(context.Background(), TestBlock, TestHash); err != nil || n < 1 {
173 t.Fatalf("PutBlock: n %d err %v", n, err)
176 buf := make([]byte, BlockSize)
177 size, err := GetBlock(context.Background(), TestHash, buf, nil)
179 t.Fatalf("GetBlock: %v", err)
181 if bytes.Compare(buf[:size], TestBlock) != 0 {
182 t.Fatalf("PutBlock stored %+q, GetBlock retrieved %+q",
183 TestBlock, buf[:size])
187 // TestPutBlockMD5Fail
188 // Check that PutBlock returns an error if passed a block and hash that
191 func TestPutBlockMD5Fail(t *testing.T) {
194 // Create two test Keep volumes.
195 KeepVM = MakeTestVolumeManager(2)
198 // Check that PutBlock returns the expected error when the hash does
199 // not match the block.
200 if _, err := PutBlock(context.Background(), BadBlock, TestHash); err != RequestHashError {
201 t.Errorf("Expected RequestHashError, got %v", err)
204 // Confirm that GetBlock fails to return anything.
205 if result, err := GetBlock(context.Background(), TestHash, make([]byte, BlockSize), nil); err != NotFoundError {
206 t.Errorf("GetBlock succeeded after a corrupt block store (result = %s, err = %v)",
211 // TestPutBlockCorrupt
212 // PutBlock should overwrite corrupt blocks on disk when given
213 // a PUT request with a good block.
215 func TestPutBlockCorrupt(t *testing.T) {
218 // Create two test Keep volumes.
219 KeepVM = MakeTestVolumeManager(2)
222 // Store a corrupted block under TestHash.
223 vols := KeepVM.AllWritable()
224 vols[0].Put(context.Background(), TestHash, BadBlock)
225 if n, err := PutBlock(context.Background(), TestBlock, TestHash); err != nil || n < 1 {
226 t.Errorf("PutBlock: n %d err %v", n, err)
229 // The block on disk should now match TestBlock.
230 buf := make([]byte, BlockSize)
231 if size, err := GetBlock(context.Background(), TestHash, buf, nil); err != nil {
232 t.Errorf("GetBlock: %v", err)
233 } else if bytes.Compare(buf[:size], TestBlock) != 0 {
234 t.Errorf("Got %+q, expected %+q", buf[:size], TestBlock)
238 // TestPutBlockCollision
239 // PutBlock returns a 400 Collision error when attempting to
240 // store a block that collides with another block on disk.
242 func TestPutBlockCollision(t *testing.T) {
245 // These blocks both hash to the MD5 digest cee9a457e790cf20d4bdaa6d69f01e41.
246 b1 := arvadostest.MD5CollisionData[0]
247 b2 := arvadostest.MD5CollisionData[1]
248 locator := arvadostest.MD5CollisionMD5
250 // Prepare two test Keep volumes.
251 KeepVM = MakeTestVolumeManager(2)
254 // Store one block, then attempt to store the other. Confirm that
255 // PutBlock reported a CollisionError.
256 if _, err := PutBlock(context.Background(), b1, locator); err != nil {
259 if _, err := PutBlock(context.Background(), b2, locator); err == nil {
260 t.Error("PutBlock did not report a collision")
261 } else if err != CollisionError {
262 t.Errorf("PutBlock returned %v", err)
266 // TestPutBlockTouchFails
267 // When PutBlock is asked to PUT an existing block, but cannot
268 // modify the timestamp, it should write a second block.
270 func TestPutBlockTouchFails(t *testing.T) {
273 // Prepare two test Keep volumes.
274 KeepVM = MakeTestVolumeManager(2)
276 vols := KeepVM.AllWritable()
278 // Store a block and then make the underlying volume bad,
279 // so a subsequent attempt to update the file timestamp
281 vols[0].Put(context.Background(), TestHash, BadBlock)
282 oldMtime, err := vols[0].Mtime(TestHash)
284 t.Fatalf("vols[0].Mtime(%s): %s\n", TestHash, err)
287 // vols[0].Touch will fail on the next call, so the volume
288 // manager will store a copy on vols[1] instead.
289 vols[0].(*MockVolume).Touchable = false
290 if n, err := PutBlock(context.Background(), TestBlock, TestHash); err != nil || n < 1 {
291 t.Fatalf("PutBlock: n %d err %v", n, err)
293 vols[0].(*MockVolume).Touchable = true
295 // Now the mtime on the block on vols[0] should be unchanged, and
296 // there should be a copy of the block on vols[1].
297 newMtime, err := vols[0].Mtime(TestHash)
299 t.Fatalf("vols[0].Mtime(%s): %s\n", TestHash, err)
301 if !newMtime.Equal(oldMtime) {
302 t.Errorf("mtime was changed on vols[0]:\noldMtime = %v\nnewMtime = %v\n",
305 buf := make([]byte, BlockSize)
306 n, err := vols[1].Get(context.Background(), TestHash, buf)
308 t.Fatalf("vols[1]: %v", err)
310 if bytes.Compare(buf[:n], TestBlock) != 0 {
311 t.Errorf("new block does not match test block\nnew block = %v\n", buf[:n])
315 func TestDiscoverTmpfs(t *testing.T) {
316 var tempVols [4]string
319 // Create some directories suitable for using as keep volumes.
320 for i := range tempVols {
321 if tempVols[i], err = ioutil.TempDir("", "findvol"); err != nil {
324 defer os.RemoveAll(tempVols[i])
325 tempVols[i] = tempVols[i] + "/keep"
326 if err = os.Mkdir(tempVols[i], 0755); err != nil {
331 // Set up a bogus ProcMounts file.
332 f, err := ioutil.TempFile("", "keeptest")
336 defer os.Remove(f.Name())
337 for i, vol := range tempVols {
338 // Add readonly mount points at odd indexes.
342 opts = "rw,nosuid,nodev,noexec"
344 opts = "nosuid,nodev,noexec,ro"
346 fmt.Fprintf(f, "tmpfs %s tmpfs %s 0 0\n", path.Dir(vol), opts)
349 ProcMounts = f.Name()
352 added := (&unixVolumeAdder{cfg}).Discover()
354 if added != len(cfg.Volumes) {
355 t.Errorf("Discover returned %d, but added %d volumes",
356 added, len(cfg.Volumes))
358 if added != len(tempVols) {
359 t.Errorf("Discover returned %d but we set up %d volumes",
360 added, len(tempVols))
362 for i, tmpdir := range tempVols {
363 if tmpdir != cfg.Volumes[i].(*UnixVolume).Root {
364 t.Errorf("Discover returned %s, expected %s\n",
365 cfg.Volumes[i].(*UnixVolume).Root, tmpdir)
367 if expectReadonly := i%2 == 1; expectReadonly != cfg.Volumes[i].(*UnixVolume).ReadOnly {
368 t.Errorf("Discover added %s with readonly=%v, should be %v",
369 tmpdir, !expectReadonly, expectReadonly)
374 func TestDiscoverNone(t *testing.T) {
377 // Set up a bogus ProcMounts file with no Keep vols.
378 f, err := ioutil.TempFile("", "keeptest")
382 defer os.Remove(f.Name())
383 fmt.Fprintln(f, "rootfs / rootfs opts 0 0")
384 fmt.Fprintln(f, "sysfs /sys sysfs opts 0 0")
385 fmt.Fprintln(f, "proc /proc proc opts 0 0")
386 fmt.Fprintln(f, "udev /dev devtmpfs opts 0 0")
387 fmt.Fprintln(f, "devpts /dev/pts devpts opts 0 0")
389 ProcMounts = f.Name()
392 added := (&unixVolumeAdder{cfg}).Discover()
393 if added != 0 || len(cfg.Volumes) != 0 {
394 t.Fatalf("got %d, %v; expected 0, []", added, cfg.Volumes)
399 // Test an /index request.
400 func TestIndex(t *testing.T) {
403 // Set up Keep volumes and populate them.
404 // Include multiple blocks on different volumes, and
405 // some metadata files.
406 KeepVM = MakeTestVolumeManager(2)
409 vols := KeepVM.AllReadable()
410 vols[0].Put(context.Background(), TestHash, TestBlock)
411 vols[1].Put(context.Background(), TestHash2, TestBlock2)
412 vols[0].Put(context.Background(), TestHash3, TestBlock3)
413 vols[0].Put(context.Background(), TestHash+".meta", []byte("metadata"))
414 vols[1].Put(context.Background(), TestHash2+".meta", []byte("metadata"))
416 buf := new(bytes.Buffer)
417 vols[0].IndexTo("", buf)
418 vols[1].IndexTo("", buf)
419 indexRows := strings.Split(string(buf.Bytes()), "\n")
420 sort.Strings(indexRows)
421 sortedIndex := strings.Join(indexRows, "\n")
422 expected := `^\n` + TestHash + `\+\d+ \d+\n` +
423 TestHash3 + `\+\d+ \d+\n` +
424 TestHash2 + `\+\d+ \d+$`
426 match, err := regexp.MatchString(expected, sortedIndex)
429 t.Errorf("IndexLocators returned:\n%s", string(buf.Bytes()))
432 t.Errorf("regexp.MatchString: %s", err)
436 // ========================================
437 // Helper functions for unit tests.
438 // ========================================
440 // MakeTestVolumeManager returns a RRVolumeManager with the specified
441 // number of MockVolumes.
442 func MakeTestVolumeManager(numVolumes int) VolumeManager {
443 vols := make([]Volume, numVolumes)
444 for i := range vols {
445 vols[i] = CreateMockVolume()
447 return MakeRRVolumeManager(vols)
450 // teardown cleans up after each test.
452 theConfig.systemAuthToken = ""
453 theConfig.RequireSignatures = false
454 theConfig.blobSigningKey = nil