8784: Fix test for latest firefox.
[arvados.git] / services / keepstore / handlers_with_generic_volume_test.go
1 package main
2
3 import (
4         "bytes"
5         "context"
6 )
7
8 // A TestableVolumeManagerFactory creates a volume manager with at least two TestableVolume instances.
9 // The factory function, and the TestableVolume instances it returns, can use "t" to write
10 // logs, fail the current test, etc.
11 type TestableVolumeManagerFactory func(t TB) (*RRVolumeManager, []TestableVolume)
12
13 // DoHandlersWithGenericVolumeTests runs a set of handler tests with a
14 // Volume Manager comprised of TestableVolume instances.
15 // It calls factory to create a volume manager with TestableVolume
16 // instances for each test case, to avoid leaking state between tests.
17 func DoHandlersWithGenericVolumeTests(t TB, factory TestableVolumeManagerFactory) {
18         testGetBlock(t, factory, TestHash, TestBlock)
19         testGetBlock(t, factory, EmptyHash, EmptyBlock)
20         testPutRawBadDataGetBlock(t, factory, TestHash, TestBlock, []byte("baddata"))
21         testPutRawBadDataGetBlock(t, factory, EmptyHash, EmptyBlock, []byte("baddata"))
22         testPutBlock(t, factory, TestHash, TestBlock)
23         testPutBlock(t, factory, EmptyHash, EmptyBlock)
24         testPutBlockCorrupt(t, factory, TestHash, TestBlock, []byte("baddata"))
25         testPutBlockCorrupt(t, factory, EmptyHash, EmptyBlock, []byte("baddata"))
26 }
27
28 // Setup RRVolumeManager with TestableVolumes
29 func setupHandlersWithGenericVolumeTest(t TB, factory TestableVolumeManagerFactory) []TestableVolume {
30         vm, testableVolumes := factory(t)
31         KeepVM = vm
32
33         for _, v := range testableVolumes {
34                 defer v.Teardown()
35         }
36         defer KeepVM.Close()
37
38         return testableVolumes
39 }
40
41 // Put a block using PutRaw in just one volume and Get it using GetBlock
42 func testGetBlock(t TB, factory TestableVolumeManagerFactory, testHash string, testBlock []byte) {
43         testableVolumes := setupHandlersWithGenericVolumeTest(t, factory)
44
45         // Put testBlock in one volume
46         testableVolumes[1].PutRaw(testHash, testBlock)
47
48         // Get should pass
49         buf := make([]byte, len(testBlock))
50         n, err := GetBlock(context.Background(), testHash, buf, nil)
51         if err != nil {
52                 t.Fatalf("Error while getting block %s", err)
53         }
54         if bytes.Compare(buf[:n], testBlock) != 0 {
55                 t.Errorf("Put succeeded but Get returned %+v, expected %+v", buf[:n], testBlock)
56         }
57 }
58
59 // Put a bad block using PutRaw and get it.
60 func testPutRawBadDataGetBlock(t TB, factory TestableVolumeManagerFactory,
61         testHash string, testBlock []byte, badData []byte) {
62         testableVolumes := setupHandlersWithGenericVolumeTest(t, factory)
63
64         // Put bad data for testHash in both volumes
65         testableVolumes[0].PutRaw(testHash, badData)
66         testableVolumes[1].PutRaw(testHash, badData)
67
68         // Get should fail
69         buf := make([]byte, BlockSize)
70         size, err := GetBlock(context.Background(), testHash, buf, nil)
71         if err == nil {
72                 t.Fatalf("Got %+q, expected error while getting corrupt block %v", buf[:size], testHash)
73         }
74 }
75
76 // Invoke PutBlock twice to ensure CompareAndTouch path is tested.
77 func testPutBlock(t TB, factory TestableVolumeManagerFactory, testHash string, testBlock []byte) {
78         setupHandlersWithGenericVolumeTest(t, factory)
79
80         // PutBlock
81         if _, err := PutBlock(context.Background(), testBlock, testHash); err != nil {
82                 t.Fatalf("Error during PutBlock: %s", err)
83         }
84
85         // Check that PutBlock succeeds again even after CompareAndTouch
86         if _, err := PutBlock(context.Background(), testBlock, testHash); err != nil {
87                 t.Fatalf("Error during PutBlock: %s", err)
88         }
89
90         // Check that PutBlock stored the data as expected
91         buf := make([]byte, BlockSize)
92         size, err := GetBlock(context.Background(), testHash, buf, nil)
93         if err != nil {
94                 t.Fatalf("Error during GetBlock for %q: %s", testHash, err)
95         } else if bytes.Compare(buf[:size], testBlock) != 0 {
96                 t.Errorf("Get response incorrect. Expected %q; found %q", testBlock, buf[:size])
97         }
98 }
99
100 // Put a bad block using PutRaw, overwrite it using PutBlock and get it.
101 func testPutBlockCorrupt(t TB, factory TestableVolumeManagerFactory,
102         testHash string, testBlock []byte, badData []byte) {
103         testableVolumes := setupHandlersWithGenericVolumeTest(t, factory)
104
105         // Put bad data for testHash in both volumes
106         testableVolumes[0].PutRaw(testHash, badData)
107         testableVolumes[1].PutRaw(testHash, badData)
108
109         // Check that PutBlock with good data succeeds
110         if _, err := PutBlock(context.Background(), testBlock, testHash); err != nil {
111                 t.Fatalf("Error during PutBlock for %q: %s", testHash, err)
112         }
113
114         // Put succeeded and overwrote the badData in one volume,
115         // and Get should return the testBlock now, ignoring the bad data.
116         buf := make([]byte, BlockSize)
117         size, err := GetBlock(context.Background(), testHash, buf, nil)
118         if err != nil {
119                 t.Fatalf("Error during GetBlock for %q: %s", testHash, err)
120         } else if bytes.Compare(buf[:size], testBlock) != 0 {
121                 t.Errorf("Get response incorrect. Expected %q; found %q", testBlock, buf[:size])
122         }
123 }