Merge branch '7491-keepclient-bugs' refs #7491
[arvados.git] / services / keepstore / volume_generic_test.go
1 package main
2
3 import (
4         "bytes"
5         "os"
6         "regexp"
7         "sort"
8         "strings"
9         "testing"
10         "time"
11 )
12
13 // A TestableVolumeFactory returns a new TestableVolume. The factory
14 // function, and the TestableVolume it returns, can use "t" to write
15 // logs, fail the current test, etc.
16 type TestableVolumeFactory func(t *testing.T) TestableVolume
17
18 // DoGenericVolumeTests runs a set of tests that every TestableVolume
19 // is expected to pass. It calls factory to create a new TestableVolume
20 // for each test case, to avoid leaking state between tests.
21 func DoGenericVolumeTests(t *testing.T, factory TestableVolumeFactory) {
22         testGet(t, factory)
23         testGetNoSuchBlock(t, factory)
24
25         testCompareSameContent(t, factory, TestHash, TestBlock)
26         testCompareSameContent(t, factory, EmptyHash, EmptyBlock)
27         testCompareWithCollision(t, factory, TestHash, TestBlock, []byte("baddata"))
28         testCompareWithCollision(t, factory, TestHash, TestBlock, EmptyBlock)
29         testCompareWithCollision(t, factory, EmptyHash, EmptyBlock, TestBlock)
30         testCompareWithCorruptStoredData(t, factory, TestHash, TestBlock, []byte("baddata"))
31         testCompareWithCorruptStoredData(t, factory, TestHash, TestBlock, EmptyBlock)
32         testCompareWithCorruptStoredData(t, factory, EmptyHash, EmptyBlock, []byte("baddata"))
33
34         testPutBlockWithSameContent(t, factory, TestHash, TestBlock)
35         testPutBlockWithSameContent(t, factory, EmptyHash, EmptyBlock)
36         testPutBlockWithDifferentContent(t, factory, TestHash, TestBlock, TestBlock2)
37         testPutBlockWithDifferentContent(t, factory, TestHash, EmptyBlock, TestBlock)
38         testPutBlockWithDifferentContent(t, factory, TestHash, TestBlock, EmptyBlock)
39         testPutBlockWithDifferentContent(t, factory, EmptyHash, EmptyBlock, TestBlock)
40         testPutMultipleBlocks(t, factory)
41
42         testPutAndTouch(t, factory)
43         testTouchNoSuchBlock(t, factory)
44
45         testMtimeNoSuchBlock(t, factory)
46
47         testIndexTo(t, factory)
48
49         testDeleteNewBlock(t, factory)
50         testDeleteOldBlock(t, factory)
51         testDeleteNoSuchBlock(t, factory)
52
53         testStatus(t, factory)
54
55         testString(t, factory)
56
57         testUpdateReadOnly(t, factory)
58
59         testGetConcurrent(t, factory)
60         testPutConcurrent(t, factory)
61 }
62
63 // Put a test block, get it and verify content
64 // Test should pass for both writable and read-only volumes
65 func testGet(t *testing.T, factory TestableVolumeFactory) {
66         v := factory(t)
67         defer v.Teardown()
68
69         v.PutRaw(TestHash, TestBlock)
70
71         buf, err := v.Get(TestHash)
72         if err != nil {
73                 t.Fatal(err)
74         }
75
76         bufs.Put(buf)
77
78         if bytes.Compare(buf, TestBlock) != 0 {
79                 t.Errorf("expected %s, got %s", string(TestBlock), string(buf))
80         }
81 }
82
83 // Invoke get on a block that does not exist in volume; should result in error
84 // Test should pass for both writable and read-only volumes
85 func testGetNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
86         v := factory(t)
87         defer v.Teardown()
88
89         if _, err := v.Get(TestHash2); err == nil {
90                 t.Errorf("Expected error while getting non-existing block %v", TestHash2)
91         }
92 }
93
94 // Put a test block and compare the locator with same content
95 // Test should pass for both writable and read-only volumes
96 func testCompareSameContent(t *testing.T, factory TestableVolumeFactory, testHash string, testData []byte) {
97         v := factory(t)
98         defer v.Teardown()
99
100         v.PutRaw(testHash, testData)
101
102         // Compare the block locator with same content
103         err := v.Compare(testHash, testData)
104         if err != nil {
105                 t.Errorf("Got err %q, expected nil", err)
106         }
107 }
108
109 // Test behavior of Compare() when stored data matches expected
110 // checksum but differs from new data we need to store. Requires
111 // testHash = md5(testDataA).
112 //
113 // Test should pass for both writable and read-only volumes
114 func testCompareWithCollision(t *testing.T, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
115         v := factory(t)
116         defer v.Teardown()
117
118         v.PutRaw(testHash, testDataA)
119
120         // Compare the block locator with different content; collision
121         err := v.Compare(TestHash, testDataB)
122         if err == nil {
123                 t.Errorf("Got err nil, expected error due to collision")
124         }
125 }
126
127 // Test behavior of Compare() when stored data has become
128 // corrupted. Requires testHash = md5(testDataA) != md5(testDataB).
129 //
130 // Test should pass for both writable and read-only volumes
131 func testCompareWithCorruptStoredData(t *testing.T, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
132         v := factory(t)
133         defer v.Teardown()
134
135         v.PutRaw(TestHash, testDataB)
136
137         err := v.Compare(testHash, testDataA)
138         if err == nil || err == CollisionError {
139                 t.Errorf("Got err %+v, expected non-collision error", err)
140         }
141 }
142
143 // Put a block and put again with same content
144 // Test is intended for only writable volumes
145 func testPutBlockWithSameContent(t *testing.T, factory TestableVolumeFactory, testHash string, testData []byte) {
146         v := factory(t)
147         defer v.Teardown()
148
149         if v.Writable() == false {
150                 return
151         }
152
153         err := v.Put(testHash, testData)
154         if err != nil {
155                 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
156         }
157
158         err = v.Put(testHash, testData)
159         if err != nil {
160                 t.Errorf("Got err putting block second time %q: %q, expected nil", TestBlock, err)
161         }
162 }
163
164 // Put a block and put again with different content
165 // Test is intended for only writable volumes
166 func testPutBlockWithDifferentContent(t *testing.T, factory TestableVolumeFactory, testHash string, testDataA, testDataB []byte) {
167         v := factory(t)
168         defer v.Teardown()
169
170         if v.Writable() == false {
171                 return
172         }
173
174         err := v.Put(testHash, testDataA)
175         if err != nil {
176                 t.Errorf("Got err putting block %q: %q, expected nil", testDataA, err)
177         }
178
179         putErr := v.Put(testHash, testDataB)
180         buf, getErr := v.Get(testHash)
181         if putErr == nil {
182                 // Put must not return a nil error unless it has
183                 // overwritten the existing data.
184                 if bytes.Compare(buf, testDataB) != 0 {
185                         t.Errorf("Put succeeded but Get returned %+q, expected %+q", buf, testDataB)
186                 }
187         } else {
188                 // It is permissible for Put to fail, but it must
189                 // leave us with either the original data, the new
190                 // data, or nothing at all.
191                 if getErr == nil && bytes.Compare(buf, testDataA) != 0 && bytes.Compare(buf, testDataB) != 0 {
192                         t.Errorf("Put failed but Get returned %+q, which is neither %+q nor %+q", buf, testDataA, testDataB)
193                 }
194         }
195         if getErr == nil {
196                 bufs.Put(buf)
197         }
198 }
199
200 // Put and get multiple blocks
201 // Test is intended for only writable volumes
202 func testPutMultipleBlocks(t *testing.T, factory TestableVolumeFactory) {
203         v := factory(t)
204         defer v.Teardown()
205
206         if v.Writable() == false {
207                 return
208         }
209
210         err := v.Put(TestHash, TestBlock)
211         if err != nil {
212                 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock, err)
213         }
214
215         err = v.Put(TestHash2, TestBlock2)
216         if err != nil {
217                 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock2, err)
218         }
219
220         err = v.Put(TestHash3, TestBlock3)
221         if err != nil {
222                 t.Errorf("Got err putting block %q: %q, expected nil", TestBlock3, err)
223         }
224
225         data, err := v.Get(TestHash)
226         if err != nil {
227                 t.Error(err)
228         } else {
229                 if bytes.Compare(data, TestBlock) != 0 {
230                         t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock)
231                 }
232                 bufs.Put(data)
233         }
234
235         data, err = v.Get(TestHash2)
236         if err != nil {
237                 t.Error(err)
238         } else {
239                 if bytes.Compare(data, TestBlock2) != 0 {
240                         t.Errorf("Block present, but got %+q, expected %+q", data, TestBlock2)
241                 }
242                 bufs.Put(data)
243         }
244
245         data, err = v.Get(TestHash3)
246         if err != nil {
247                 t.Error(err)
248         } else {
249                 if bytes.Compare(data, TestBlock3) != 0 {
250                         t.Errorf("Block present, but to %+q, expected %+q", data, TestBlock3)
251                 }
252                 bufs.Put(data)
253         }
254 }
255
256 // testPutAndTouch
257 //   Test that when applying PUT to a block that already exists,
258 //   the block's modification time is updated.
259 // Test is intended for only writable volumes
260 func testPutAndTouch(t *testing.T, factory TestableVolumeFactory) {
261         v := factory(t)
262         defer v.Teardown()
263
264         if v.Writable() == false {
265                 return
266         }
267
268         if err := v.Put(TestHash, TestBlock); err != nil {
269                 t.Error(err)
270         }
271
272         // We'll verify { t0 < threshold < t1 }, where t0 is the
273         // existing block's timestamp on disk before Put() and t1 is
274         // its timestamp after Put().
275         threshold := time.Now().Add(-time.Second)
276
277         // Set the stored block's mtime far enough in the past that we
278         // can see the difference between "timestamp didn't change"
279         // and "timestamp granularity is too low".
280         v.TouchWithDate(TestHash, time.Now().Add(-20*time.Second))
281
282         // Make sure v.Mtime() agrees the above Utime really worked.
283         if t0, err := v.Mtime(TestHash); err != nil || t0.IsZero() || !t0.Before(threshold) {
284                 t.Errorf("Setting mtime failed: %v, %v", t0, err)
285         }
286
287         // Write the same block again.
288         if err := v.Put(TestHash, TestBlock); err != nil {
289                 t.Error(err)
290         }
291
292         // Verify threshold < t1
293         if t1, err := v.Mtime(TestHash); err != nil {
294                 t.Error(err)
295         } else if t1.Before(threshold) {
296                 t.Errorf("t1 %v should be >= threshold %v after v.Put ", t1, threshold)
297         }
298 }
299
300 // Touching a non-existing block should result in error.
301 // Test should pass for both writable and read-only volumes
302 func testTouchNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
303         v := factory(t)
304         defer v.Teardown()
305
306         if err := v.Touch(TestHash); err == nil {
307                 t.Error("Expected error when attempted to touch a non-existing block")
308         }
309 }
310
311 // Invoking Mtime on a non-existing block should result in error.
312 // Test should pass for both writable and read-only volumes
313 func testMtimeNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
314         v := factory(t)
315         defer v.Teardown()
316
317         if _, err := v.Mtime("12345678901234567890123456789012"); err == nil {
318                 t.Error("Expected error when updating Mtime on a non-existing block")
319         }
320 }
321
322 // Put a few blocks and invoke IndexTo with:
323 // * no prefix
324 // * with a prefix
325 // * with no such prefix
326 // Test should pass for both writable and read-only volumes
327 func testIndexTo(t *testing.T, factory TestableVolumeFactory) {
328         v := factory(t)
329         defer v.Teardown()
330
331         v.PutRaw(TestHash, TestBlock)
332         v.PutRaw(TestHash2, TestBlock2)
333         v.PutRaw(TestHash3, TestBlock3)
334
335         buf := new(bytes.Buffer)
336         v.IndexTo("", buf)
337         indexRows := strings.Split(string(buf.Bytes()), "\n")
338         sort.Strings(indexRows)
339         sortedIndex := strings.Join(indexRows, "\n")
340         m, err := regexp.MatchString(
341                 `^\n`+TestHash+`\+\d+ \d+\n`+
342                         TestHash3+`\+\d+ \d+\n`+
343                         TestHash2+`\+\d+ \d+$`,
344                 sortedIndex)
345         if err != nil {
346                 t.Error(err)
347         } else if !m {
348                 t.Errorf("Got index %q for empty prefix", sortedIndex)
349         }
350
351         for _, prefix := range []string{"f", "f15", "f15ac"} {
352                 buf = new(bytes.Buffer)
353                 v.IndexTo(prefix, buf)
354
355                 m, err := regexp.MatchString(`^`+TestHash2+`\+\d+ \d+\n$`, string(buf.Bytes()))
356                 if err != nil {
357                         t.Error(err)
358                 } else if !m {
359                         t.Errorf("Got index %q for prefix %s", string(buf.Bytes()), prefix)
360                 }
361         }
362
363         for _, prefix := range []string{"zero", "zip", "zilch"} {
364                 buf = new(bytes.Buffer)
365                 v.IndexTo(prefix, buf)
366                 if err != nil {
367                         t.Errorf("Got error on IndexTo with no such prefix %v", err.Error())
368                 } else if buf.Len() != 0 {
369                         t.Errorf("Expected empty list for IndexTo with no such prefix %s", prefix)
370                 }
371         }
372 }
373
374 // Calling Delete() for a block immediately after writing it (not old enough)
375 // should neither delete the data nor return an error.
376 // Test is intended for only writable volumes
377 func testDeleteNewBlock(t *testing.T, factory TestableVolumeFactory) {
378         v := factory(t)
379         defer v.Teardown()
380         blobSignatureTTL = 300 * time.Second
381
382         if v.Writable() == false {
383                 return
384         }
385
386         v.Put(TestHash, TestBlock)
387
388         if err := v.Delete(TestHash); err != nil {
389                 t.Error(err)
390         }
391         data, err := v.Get(TestHash)
392         if err != nil {
393                 t.Error(err)
394         } else {
395                 if bytes.Compare(data, TestBlock) != 0 {
396                         t.Errorf("Got data %+q, expected %+q", data, TestBlock)
397                 }
398                 bufs.Put(data)
399         }
400 }
401
402 // Calling Delete() for a block with a timestamp older than
403 // blobSignatureTTL seconds in the past should delete the data.
404 // Test is intended for only writable volumes
405 func testDeleteOldBlock(t *testing.T, factory TestableVolumeFactory) {
406         v := factory(t)
407         defer v.Teardown()
408         blobSignatureTTL = 300 * time.Second
409
410         if v.Writable() == false {
411                 return
412         }
413
414         v.Put(TestHash, TestBlock)
415         v.TouchWithDate(TestHash, time.Now().Add(-2*blobSignatureTTL))
416
417         if err := v.Delete(TestHash); err != nil {
418                 t.Error(err)
419         }
420         if _, err := v.Get(TestHash); err == nil || !os.IsNotExist(err) {
421                 t.Errorf("os.IsNotExist(%v) should have been true", err)
422         }
423 }
424
425 // Calling Delete() for a block that does not exist should result in error.
426 // Test should pass for both writable and read-only volumes
427 func testDeleteNoSuchBlock(t *testing.T, factory TestableVolumeFactory) {
428         v := factory(t)
429         defer v.Teardown()
430
431         if err := v.Delete(TestHash2); err == nil {
432                 t.Errorf("Expected error when attempting to delete a non-existing block")
433         }
434 }
435
436 // Invoke Status and verify that VolumeStatus is returned
437 // Test should pass for both writable and read-only volumes
438 func testStatus(t *testing.T, factory TestableVolumeFactory) {
439         v := factory(t)
440         defer v.Teardown()
441
442         // Get node status and make a basic sanity check.
443         status := v.Status()
444         if status.DeviceNum == 0 {
445                 t.Errorf("uninitialized device_num in %v", status)
446         }
447
448         if status.BytesFree == 0 {
449                 t.Errorf("uninitialized bytes_free in %v", status)
450         }
451
452         if status.BytesUsed == 0 {
453                 t.Errorf("uninitialized bytes_used in %v", status)
454         }
455 }
456
457 // Invoke String for the volume; expect non-empty result
458 // Test should pass for both writable and read-only volumes
459 func testString(t *testing.T, factory TestableVolumeFactory) {
460         v := factory(t)
461         defer v.Teardown()
462
463         if id := v.String(); len(id) == 0 {
464                 t.Error("Got empty string for v.String()")
465         }
466 }
467
468 // Putting, updating, touching, and deleting blocks from a read-only volume result in error.
469 // Test is intended for only read-only volumes
470 func testUpdateReadOnly(t *testing.T, factory TestableVolumeFactory) {
471         v := factory(t)
472         defer v.Teardown()
473
474         if v.Writable() == true {
475                 return
476         }
477
478         v.PutRaw(TestHash, TestBlock)
479
480         // Get from read-only volume should succeed
481         _, err := v.Get(TestHash)
482         if err != nil {
483                 t.Errorf("got err %v, expected nil", err)
484         }
485
486         // Put a new block to read-only volume should result in error
487         err = v.Put(TestHash2, TestBlock2)
488         if err == nil {
489                 t.Errorf("Expected error when putting block in a read-only volume")
490         }
491         _, err = v.Get(TestHash2)
492         if err == nil {
493                 t.Errorf("Expected error when getting block whose put in read-only volume failed")
494         }
495
496         // Touch a block in read-only volume should result in error
497         err = v.Touch(TestHash)
498         if err == nil {
499                 t.Errorf("Expected error when touching block in a read-only volume")
500         }
501
502         // Delete a block from a read-only volume should result in error
503         err = v.Delete(TestHash)
504         if err == nil {
505                 t.Errorf("Expected error when deleting block from a read-only volume")
506         }
507
508         // Overwriting an existing block in read-only volume should result in error
509         err = v.Put(TestHash, TestBlock)
510         if err == nil {
511                 t.Errorf("Expected error when putting block in a read-only volume")
512         }
513 }
514
515 // Launch concurrent Gets
516 // Test should pass for both writable and read-only volumes
517 func testGetConcurrent(t *testing.T, factory TestableVolumeFactory) {
518         v := factory(t)
519         defer v.Teardown()
520
521         v.PutRaw(TestHash, TestBlock)
522         v.PutRaw(TestHash2, TestBlock2)
523         v.PutRaw(TestHash3, TestBlock3)
524
525         sem := make(chan int)
526         go func(sem chan int) {
527                 buf, err := v.Get(TestHash)
528                 if err != nil {
529                         t.Errorf("err1: %v", err)
530                 }
531                 bufs.Put(buf)
532                 if bytes.Compare(buf, TestBlock) != 0 {
533                         t.Errorf("buf should be %s, is %s", string(TestBlock), string(buf))
534                 }
535                 sem <- 1
536         }(sem)
537
538         go func(sem chan int) {
539                 buf, err := v.Get(TestHash2)
540                 if err != nil {
541                         t.Errorf("err2: %v", err)
542                 }
543                 bufs.Put(buf)
544                 if bytes.Compare(buf, TestBlock2) != 0 {
545                         t.Errorf("buf should be %s, is %s", string(TestBlock2), string(buf))
546                 }
547                 sem <- 1
548         }(sem)
549
550         go func(sem chan int) {
551                 buf, err := v.Get(TestHash3)
552                 if err != nil {
553                         t.Errorf("err3: %v", err)
554                 }
555                 bufs.Put(buf)
556                 if bytes.Compare(buf, TestBlock3) != 0 {
557                         t.Errorf("buf should be %s, is %s", string(TestBlock3), string(buf))
558                 }
559                 sem <- 1
560         }(sem)
561
562         // Wait for all goroutines to finish
563         for done := 0; done < 3; {
564                 done += <-sem
565         }
566 }
567
568 // Launch concurrent Puts
569 // Test is intended for only writable volumes
570 func testPutConcurrent(t *testing.T, factory TestableVolumeFactory) {
571         v := factory(t)
572         defer v.Teardown()
573
574         if v.Writable() == false {
575                 return
576         }
577
578         sem := make(chan int)
579         go func(sem chan int) {
580                 err := v.Put(TestHash, TestBlock)
581                 if err != nil {
582                         t.Errorf("err1: %v", err)
583                 }
584                 sem <- 1
585         }(sem)
586
587         go func(sem chan int) {
588                 err := v.Put(TestHash2, TestBlock2)
589                 if err != nil {
590                         t.Errorf("err2: %v", err)
591                 }
592                 sem <- 1
593         }(sem)
594
595         go func(sem chan int) {
596                 err := v.Put(TestHash3, TestBlock3)
597                 if err != nil {
598                         t.Errorf("err3: %v", err)
599                 }
600                 sem <- 1
601         }(sem)
602
603         // Wait for all goroutines to finish
604         for done := 0; done < 3; {
605                 done += <-sem
606         }
607
608         // Double check that we actually wrote the blocks we expected to write.
609         buf, err := v.Get(TestHash)
610         if err != nil {
611                 t.Errorf("Get #1: %v", err)
612         }
613         bufs.Put(buf)
614         if bytes.Compare(buf, TestBlock) != 0 {
615                 t.Errorf("Get #1: expected %s, got %s", string(TestBlock), string(buf))
616         }
617
618         buf, err = v.Get(TestHash2)
619         if err != nil {
620                 t.Errorf("Get #2: %v", err)
621         }
622         bufs.Put(buf)
623         if bytes.Compare(buf, TestBlock2) != 0 {
624                 t.Errorf("Get #2: expected %s, got %s", string(TestBlock2), string(buf))
625         }
626
627         buf, err = v.Get(TestHash3)
628         if err != nil {
629                 t.Errorf("Get #3: %v", err)
630         }
631         bufs.Put(buf)
632         if bytes.Compare(buf, TestBlock3) != 0 {
633                 t.Errorf("Get #3: expected %s, got %s", string(TestBlock3), string(buf))
634         }
635 }