Merge branch 'master' into 1971-show-image-thumbnails
[arvados.git] / services / keep / keep_test.go
1 package main
2
3 import (
4         "bytes"
5         "fmt"
6         "io/ioutil"
7         "os"
8         "path"
9         "testing"
10 )
11
12 var TEST_BLOCK = []byte("The quick brown fox jumps over the lazy dog.")
13 var TEST_HASH = "e4d909c290d0fb1ca068ffaddf22cbd0"
14 var BAD_BLOCK = []byte("The magic words are squeamish ossifrage.")
15
16 // TODO(twp): Tests still to be written
17 //
18 //   * PutBlockFull
19 //       - test that PutBlock returns 503 Full if the filesystem is full.
20 //         (must mock FreeDiskSpace or Statfs? use a tmpfs?)
21 //
22 //   * PutBlockWriteErr
23 //       - test the behavior when Write returns an error.
24 //           - Possible solutions: use a small tmpfs and a high
25 //             MIN_FREE_KILOBYTES to trick PutBlock into attempting
26 //             to write a block larger than the amount of space left
27 //           - use an interface to mock ioutil.TempFile with a File
28 //             object that always returns an error on write
29 //
30 // ========================================
31 // GetBlock tests.
32 // ========================================
33
34 // TestGetBlock
35 //     Test that simple block reads succeed.
36 //
37 func TestGetBlock(t *testing.T) {
38         defer teardown()
39
40         // Prepare two test Keep volumes. Our block is stored on the second volume.
41         KeepVolumes = setup(t, 2)
42         store(t, KeepVolumes[1], TEST_HASH, TEST_BLOCK)
43
44         // Check that GetBlock returns success.
45         result, err := GetBlock(TEST_HASH)
46         if err != nil {
47                 t.Errorf("GetBlock error: %s", err)
48         }
49         if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
50                 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
51         }
52 }
53
54 // TestGetBlockMissing
55 //     GetBlock must return an error when the block is not found.
56 //
57 func TestGetBlockMissing(t *testing.T) {
58         defer teardown()
59
60         // Create two empty test Keep volumes.
61         KeepVolumes = setup(t, 2)
62
63         // Check that GetBlock returns failure.
64         result, err := GetBlock(TEST_HASH)
65         if err != NotFoundError {
66                 t.Errorf("Expected NotFoundError, got %v", result)
67         }
68 }
69
70 // TestGetBlockCorrupt
71 //     GetBlock must return an error when a corrupted block is requested
72 //     (the contents of the file do not checksum to its hash).
73 //
74 func TestGetBlockCorrupt(t *testing.T) {
75         defer teardown()
76
77         // Create two test Keep volumes and store a block in each of them,
78         // but the hash of the block does not match the filename.
79         KeepVolumes = setup(t, 2)
80         for _, vol := range KeepVolumes {
81                 store(t, vol, TEST_HASH, BAD_BLOCK)
82         }
83
84         // Check that GetBlock returns failure.
85         result, err := GetBlock(TEST_HASH)
86         if err != CorruptError {
87                 t.Errorf("Expected CorruptError, got %v", result)
88         }
89 }
90
91 // ========================================
92 // PutBlock tests
93 // ========================================
94
95 // TestPutBlockOK
96 //     PutBlock can perform a simple block write and returns success.
97 //
98 func TestPutBlockOK(t *testing.T) {
99         defer teardown()
100
101         // Create two test Keep volumes.
102         KeepVolumes = setup(t, 2)
103
104         // Check that PutBlock stores the data as expected.
105         if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
106                 t.Fatalf("PutBlock: %v", err)
107         }
108
109         result, err := GetBlock(TEST_HASH)
110         if err != nil {
111                 t.Fatalf("GetBlock returned error: %v", err)
112         }
113         if string(result) != string(TEST_BLOCK) {
114                 t.Error("PutBlock/GetBlock mismatch")
115                 t.Fatalf("PutBlock stored '%s', GetBlock retrieved '%s'",
116                         string(TEST_BLOCK), string(result))
117         }
118 }
119
120 // TestPutBlockOneVol
121 //     PutBlock still returns success even when only one of the known
122 //     volumes is online.
123 //
124 func TestPutBlockOneVol(t *testing.T) {
125         defer teardown()
126
127         // Create two test Keep volumes, but cripple one of them.
128         KeepVolumes = setup(t, 2)
129         os.Chmod(KeepVolumes[0], 000)
130
131         // Check that PutBlock stores the data as expected.
132         if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
133                 t.Fatalf("PutBlock: %v", err)
134         }
135
136         result, err := GetBlock(TEST_HASH)
137         if err != nil {
138                 t.Fatalf("GetBlock: %v", err)
139         }
140         if string(result) != string(TEST_BLOCK) {
141                 t.Error("PutBlock/GetBlock mismatch")
142                 t.Fatalf("PutBlock stored '%s', GetBlock retrieved '%s'",
143                         string(TEST_BLOCK), string(result))
144         }
145 }
146
147 // TestPutBlockMD5Fail
148 //     Check that PutBlock returns an error if passed a block and hash that
149 //     do not match.
150 //
151 func TestPutBlockMD5Fail(t *testing.T) {
152         defer teardown()
153
154         // Create two test Keep volumes.
155         KeepVolumes = setup(t, 2)
156
157         // Check that PutBlock returns the expected error when the hash does
158         // not match the block.
159         if err := PutBlock(BAD_BLOCK, TEST_HASH); err != MD5Error {
160                 t.Error("Expected MD5Error, got %v", err)
161         }
162
163         // Confirm that GetBlock fails to return anything.
164         if result, err := GetBlock(TEST_HASH); err != NotFoundError {
165                 t.Errorf("GetBlock succeeded after a corrupt block store (result = %s, err = %v)",
166                         string(result), err)
167         }
168 }
169
170 // TestPutBlockCorrupt
171 //     PutBlock should overwrite corrupt blocks on disk when given
172 //     a PUT request with a good block.
173 //
174 func TestPutBlockCorrupt(t *testing.T) {
175         defer teardown()
176
177         // Create two test Keep volumes.
178         KeepVolumes = setup(t, 2)
179
180         // Store a corrupted block under TEST_HASH.
181         store(t, KeepVolumes[0], TEST_HASH, BAD_BLOCK)
182         if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
183                 t.Errorf("PutBlock: %v", err)
184         }
185
186         // The block on disk should now match TEST_BLOCK.
187         if block, err := GetBlock(TEST_HASH); err != nil {
188                 t.Errorf("GetBlock: %v", err)
189         } else if bytes.Compare(block, TEST_BLOCK) != 0 {
190                 t.Errorf("GetBlock returned: '%s'", string(block))
191         }
192 }
193
194 // PutBlockCollision
195 //     PutBlock returns a 400 Collision error when attempting to
196 //     store a block that collides with another block on disk.
197 //
198 func TestPutBlockCollision(t *testing.T) {
199         defer teardown()
200
201         // These blocks both hash to the MD5 digest cee9a457e790cf20d4bdaa6d69f01e41.
202         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")
203         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")
204         var locator = "cee9a457e790cf20d4bdaa6d69f01e41"
205
206         // Prepare two test Keep volumes. Store one block,
207         // then attempt to store the other.
208         KeepVolumes = setup(t, 2)
209         store(t, KeepVolumes[1], locator, b1)
210
211         if err := PutBlock(b2, locator); err == nil {
212                 t.Error("PutBlock did not report a collision")
213         } else if err != CollisionError {
214                 t.Errorf("PutBlock returned %v", err)
215         }
216 }
217
218 // ========================================
219 // FindKeepVolumes tests.
220 // ========================================
221
222 // TestFindKeepVolumes
223 //     Confirms that FindKeepVolumes finds tmpfs volumes with "/keep"
224 //     directories at the top level.
225 //
226 func TestFindKeepVolumes(t *testing.T) {
227         defer teardown()
228
229         // Initialize two keep volumes.
230         var tempVols []string = setup(t, 2)
231
232         // Set up a bogus PROC_MOUNTS file.
233         if f, err := ioutil.TempFile("", "keeptest"); err == nil {
234                 for _, vol := range tempVols {
235                         fmt.Fprintf(f, "tmpfs %s tmpfs opts\n", path.Dir(vol))
236                 }
237                 f.Close()
238                 PROC_MOUNTS = f.Name()
239
240                 // Check that FindKeepVolumes finds the temp volumes.
241                 resultVols := FindKeepVolumes()
242                 if len(tempVols) != len(resultVols) {
243                         t.Fatalf("set up %d volumes, FindKeepVolumes found %d\n",
244                                 len(tempVols), len(resultVols))
245                 }
246                 for i := range tempVols {
247                         if tempVols[i] != resultVols[i] {
248                                 t.Errorf("FindKeepVolumes returned %s, expected %s\n",
249                                         resultVols[i], tempVols[i])
250                         }
251                 }
252
253                 os.Remove(f.Name())
254         }
255 }
256
257 // TestFindKeepVolumesFail
258 //     When no Keep volumes are present, FindKeepVolumes returns an empty slice.
259 //
260 func TestFindKeepVolumesFail(t *testing.T) {
261         defer teardown()
262
263         // Set up a bogus PROC_MOUNTS file with no Keep vols.
264         if f, err := ioutil.TempFile("", "keeptest"); err == nil {
265                 fmt.Fprintln(f, "rootfs / rootfs opts 0 0")
266                 fmt.Fprintln(f, "sysfs /sys sysfs opts 0 0")
267                 fmt.Fprintln(f, "proc /proc proc opts 0 0")
268                 fmt.Fprintln(f, "udev /dev devtmpfs opts 0 0")
269                 fmt.Fprintln(f, "devpts /dev/pts devpts opts 0 0")
270                 f.Close()
271                 PROC_MOUNTS = f.Name()
272
273                 // Check that FindKeepVolumes returns an empty array.
274                 resultVols := FindKeepVolumes()
275                 if len(resultVols) != 0 {
276                         t.Fatalf("FindKeepVolumes returned %v", resultVols)
277                 }
278
279                 os.Remove(PROC_MOUNTS)
280         }
281 }
282
283 // ========================================
284 // Helper functions for unit tests.
285 // ========================================
286
287 // setup
288 //     Create KeepVolumes for testing.
289 //     Returns a slice of pathnames to temporary Keep volumes.
290 //
291 func setup(t *testing.T, num_volumes int) []string {
292         vols := make([]string, num_volumes)
293         for i := range vols {
294                 if dir, err := ioutil.TempDir(os.TempDir(), "keeptest"); err == nil {
295                         vols[i] = dir + "/keep"
296                         os.Mkdir(vols[i], 0755)
297                 } else {
298                         t.Fatal(err)
299                 }
300         }
301         return vols
302 }
303
304 // teardown
305 //     Cleanup to perform after each test.
306 //
307 func teardown() {
308         for _, vol := range KeepVolumes {
309                 os.RemoveAll(path.Dir(vol))
310         }
311         KeepVolumes = nil
312 }
313
314 // store
315 //     Low-level code to write Keep blocks directly to disk for testing.
316 //
317 func store(t *testing.T, keepdir string, filename string, block []byte) {
318         blockdir := fmt.Sprintf("%s/%s", keepdir, filename[:3])
319         if err := os.MkdirAll(blockdir, 0755); err != nil {
320                 t.Fatal(err)
321         }
322
323         blockpath := fmt.Sprintf("%s/%s", blockdir, filename)
324         if f, err := os.Create(blockpath); err == nil {
325                 f.Write(block)
326                 f.Close()
327         } else {
328                 t.Fatal(err)
329         }
330 }