Added rudimentary GetNodeStatus test. (refs #2561)
[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         "regexp"
10         "testing"
11 )
12
13 var TEST_BLOCK = []byte("The quick brown fox jumps over the lazy dog.")
14 var TEST_HASH = "e4d909c290d0fb1ca068ffaddf22cbd0"
15
16 var TEST_BLOCK_2 = []byte("Pack my box with five dozen liquor jugs.")
17 var TEST_HASH_2 = "f15ac516f788aec4f30932ffb6395c39"
18
19 var TEST_BLOCK_3 = []byte("Now is the time for all good men to come to the aid of their country.")
20 var TEST_HASH_3 = "eed29bbffbc2dbe5e5ee0bb71888e61f"
21
22 // BAD_BLOCK is used to test collisions and corruption.
23 // It must not match any test hashes.
24 var BAD_BLOCK = []byte("The magic words are squeamish ossifrage.")
25
26 // TODO(twp): Tests still to be written
27 //
28 //   * TestPutBlockFull
29 //       - test that PutBlock returns 503 Full if the filesystem is full.
30 //         (must mock FreeDiskSpace or Statfs? use a tmpfs?)
31 //
32 //   * TestPutBlockWriteErr
33 //       - test the behavior when Write returns an error.
34 //           - Possible solutions: use a small tmpfs and a high
35 //             MIN_FREE_KILOBYTES to trick PutBlock into attempting
36 //             to write a block larger than the amount of space left
37 //           - use an interface to mock ioutil.TempFile with a File
38 //             object that always returns an error on write
39 //
40 // ========================================
41 // GetBlock tests.
42 // ========================================
43
44 // TestGetBlock
45 //     Test that simple block reads succeed.
46 //
47 func TestGetBlock(t *testing.T) {
48         defer teardown()
49
50         // Prepare two test Keep volumes. Our block is stored on the second volume.
51         KeepVolumes = setup(t, 2)
52         store(t, KeepVolumes[1], TEST_HASH, TEST_BLOCK)
53
54         // Check that GetBlock returns success.
55         result, err := GetBlock(TEST_HASH)
56         if err != nil {
57                 t.Errorf("GetBlock error: %s", err)
58         }
59         if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
60                 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
61         }
62 }
63
64 // TestGetBlockMissing
65 //     GetBlock must return an error when the block is not found.
66 //
67 func TestGetBlockMissing(t *testing.T) {
68         defer teardown()
69
70         // Create two empty test Keep volumes.
71         KeepVolumes = setup(t, 2)
72
73         // Check that GetBlock returns failure.
74         result, err := GetBlock(TEST_HASH)
75         if err != NotFoundError {
76                 t.Errorf("Expected NotFoundError, got %v", result)
77         }
78 }
79
80 // TestGetBlockCorrupt
81 //     GetBlock must return an error when a corrupted block is requested
82 //     (the contents of the file do not checksum to its hash).
83 //
84 func TestGetBlockCorrupt(t *testing.T) {
85         defer teardown()
86
87         // Create two test Keep volumes and store a block in each of them,
88         // but the hash of the block does not match the filename.
89         KeepVolumes = setup(t, 2)
90         for _, vol := range KeepVolumes {
91                 store(t, vol, TEST_HASH, BAD_BLOCK)
92         }
93
94         // Check that GetBlock returns failure.
95         result, err := GetBlock(TEST_HASH)
96         if err != CorruptError {
97                 t.Errorf("Expected CorruptError, got %v", result)
98         }
99 }
100
101 // ========================================
102 // PutBlock tests
103 // ========================================
104
105 // TestPutBlockOK
106 //     PutBlock can perform a simple block write and returns success.
107 //
108 func TestPutBlockOK(t *testing.T) {
109         defer teardown()
110
111         // Create two test Keep volumes.
112         KeepVolumes = setup(t, 2)
113
114         // Check that PutBlock stores the data as expected.
115         if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
116                 t.Fatalf("PutBlock: %v", err)
117         }
118
119         result, err := GetBlock(TEST_HASH)
120         if err != nil {
121                 t.Fatalf("GetBlock returned error: %v", err)
122         }
123         if string(result) != string(TEST_BLOCK) {
124                 t.Error("PutBlock/GetBlock mismatch")
125                 t.Fatalf("PutBlock stored '%s', GetBlock retrieved '%s'",
126                         string(TEST_BLOCK), string(result))
127         }
128 }
129
130 // TestPutBlockOneVol
131 //     PutBlock still returns success even when only one of the known
132 //     volumes is online.
133 //
134 func TestPutBlockOneVol(t *testing.T) {
135         defer teardown()
136
137         // Create two test Keep volumes, but cripple one of them.
138         KeepVolumes = setup(t, 2)
139         os.Chmod(KeepVolumes[0], 000)
140
141         // Check that PutBlock stores the data as expected.
142         if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
143                 t.Fatalf("PutBlock: %v", err)
144         }
145
146         result, err := GetBlock(TEST_HASH)
147         if err != nil {
148                 t.Fatalf("GetBlock: %v", err)
149         }
150         if string(result) != string(TEST_BLOCK) {
151                 t.Error("PutBlock/GetBlock mismatch")
152                 t.Fatalf("PutBlock stored '%s', GetBlock retrieved '%s'",
153                         string(TEST_BLOCK), string(result))
154         }
155 }
156
157 // TestPutBlockMD5Fail
158 //     Check that PutBlock returns an error if passed a block and hash that
159 //     do not match.
160 //
161 func TestPutBlockMD5Fail(t *testing.T) {
162         defer teardown()
163
164         // Create two test Keep volumes.
165         KeepVolumes = setup(t, 2)
166
167         // Check that PutBlock returns the expected error when the hash does
168         // not match the block.
169         if err := PutBlock(BAD_BLOCK, TEST_HASH); err != MD5Error {
170                 t.Error("Expected MD5Error, got %v", err)
171         }
172
173         // Confirm that GetBlock fails to return anything.
174         if result, err := GetBlock(TEST_HASH); err != NotFoundError {
175                 t.Errorf("GetBlock succeeded after a corrupt block store (result = %s, err = %v)",
176                         string(result), err)
177         }
178 }
179
180 // TestPutBlockCorrupt
181 //     PutBlock should overwrite corrupt blocks on disk when given
182 //     a PUT request with a good block.
183 //
184 func TestPutBlockCorrupt(t *testing.T) {
185         defer teardown()
186
187         // Create two test Keep volumes.
188         KeepVolumes = setup(t, 2)
189
190         // Store a corrupted block under TEST_HASH.
191         store(t, KeepVolumes[0], TEST_HASH, BAD_BLOCK)
192         if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
193                 t.Errorf("PutBlock: %v", err)
194         }
195
196         // The block on disk should now match TEST_BLOCK.
197         if block, err := GetBlock(TEST_HASH); err != nil {
198                 t.Errorf("GetBlock: %v", err)
199         } else if bytes.Compare(block, TEST_BLOCK) != 0 {
200                 t.Errorf("GetBlock returned: '%s'", string(block))
201         }
202 }
203
204 // PutBlockCollision
205 //     PutBlock returns a 400 Collision error when attempting to
206 //     store a block that collides with another block on disk.
207 //
208 func TestPutBlockCollision(t *testing.T) {
209         defer teardown()
210
211         // These blocks both hash to the MD5 digest cee9a457e790cf20d4bdaa6d69f01e41.
212         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")
213         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")
214         var locator = "cee9a457e790cf20d4bdaa6d69f01e41"
215
216         // Prepare two test Keep volumes. Store one block,
217         // then attempt to store the other.
218         KeepVolumes = setup(t, 2)
219         store(t, KeepVolumes[1], locator, b1)
220
221         if err := PutBlock(b2, locator); err == nil {
222                 t.Error("PutBlock did not report a collision")
223         } else if err != CollisionError {
224                 t.Errorf("PutBlock returned %v", err)
225         }
226 }
227
228 // ========================================
229 // FindKeepVolumes tests.
230 // ========================================
231
232 // TestFindKeepVolumes
233 //     Confirms that FindKeepVolumes finds tmpfs volumes with "/keep"
234 //     directories at the top level.
235 //
236 func TestFindKeepVolumes(t *testing.T) {
237         defer teardown()
238
239         // Initialize two keep volumes.
240         var tempVols []string = setup(t, 2)
241
242         // Set up a bogus PROC_MOUNTS file.
243         if f, err := ioutil.TempFile("", "keeptest"); err == nil {
244                 for _, vol := range tempVols {
245                         fmt.Fprintf(f, "tmpfs %s tmpfs opts\n", path.Dir(vol))
246                 }
247                 f.Close()
248                 PROC_MOUNTS = f.Name()
249
250                 // Check that FindKeepVolumes finds the temp volumes.
251                 resultVols := FindKeepVolumes()
252                 if len(tempVols) != len(resultVols) {
253                         t.Fatalf("set up %d volumes, FindKeepVolumes found %d\n",
254                                 len(tempVols), len(resultVols))
255                 }
256                 for i := range tempVols {
257                         if tempVols[i] != resultVols[i] {
258                                 t.Errorf("FindKeepVolumes returned %s, expected %s\n",
259                                         resultVols[i], tempVols[i])
260                         }
261                 }
262
263                 os.Remove(f.Name())
264         }
265 }
266
267 // TestFindKeepVolumesFail
268 //     When no Keep volumes are present, FindKeepVolumes returns an empty slice.
269 //
270 func TestFindKeepVolumesFail(t *testing.T) {
271         defer teardown()
272
273         // Set up a bogus PROC_MOUNTS file with no Keep vols.
274         if f, err := ioutil.TempFile("", "keeptest"); err == nil {
275                 fmt.Fprintln(f, "rootfs / rootfs opts 0 0")
276                 fmt.Fprintln(f, "sysfs /sys sysfs opts 0 0")
277                 fmt.Fprintln(f, "proc /proc proc opts 0 0")
278                 fmt.Fprintln(f, "udev /dev devtmpfs opts 0 0")
279                 fmt.Fprintln(f, "devpts /dev/pts devpts opts 0 0")
280                 f.Close()
281                 PROC_MOUNTS = f.Name()
282
283                 // Check that FindKeepVolumes returns an empty array.
284                 resultVols := FindKeepVolumes()
285                 if len(resultVols) != 0 {
286                         t.Fatalf("FindKeepVolumes returned %v", resultVols)
287                 }
288
289                 os.Remove(PROC_MOUNTS)
290         }
291 }
292
293 // TestIndex
294 //     Test an /index request.
295 func TestIndex(t *testing.T) {
296         defer teardown()
297
298         // Set up Keep volumes and populate them.
299         KeepVolumes = setup(t, 2)
300         store(t, KeepVolumes[0], TEST_HASH, TEST_BLOCK)
301         store(t, KeepVolumes[1], TEST_HASH_2, TEST_BLOCK_2)
302         store(t, KeepVolumes[0], TEST_HASH_3, TEST_BLOCK_3)
303
304         index := IndexLocators("")
305         expected := `^` + TEST_HASH + `\+\d+ \d+\n` +
306                 TEST_HASH_3 + `\+\d+ \d+\n` +
307                 TEST_HASH_2 + `\+\d+ \d+\n$`
308
309         match, err := regexp.MatchString(expected, index)
310         if err == nil {
311                 if !match {
312                         t.Errorf("IndexLocators returned:\n-----\n%s-----\n", index)
313                 }
314         } else {
315                 t.Errorf("regexp.MatchString: %s", err)
316         }
317 }
318
319 // TestNodeStatus
320 //     Test that GetNodeStatus returns valid info about available volumes.
321 //
322 //     TODO(twp): set up appropriate interfaces to permit more rigorous
323 //     testing.
324 //
325 func TestNodeStatus(t *testing.T) {
326         defer teardown()
327
328         // Set up test Keep volumes.
329         KeepVolumes = setup(t, 2)
330
331         // Get node status and make a basic sanity check.
332         st := GetNodeStatus()
333         for i, vol := range KeepVolumes {
334                 volinfo := st.Volumes[i]
335                 mtp := volinfo.MountPoint
336                 if mtp != vol {
337                         t.Errorf("GetNodeStatus mount_point %s != KeepVolume %s", mtp, vol)
338                 }
339                 if volinfo.BytesFree == 0 {
340                         t.Errorf("uninitialized bytes_free in %v", volinfo)
341                 }
342                 if volinfo.BytesUsed == 0 {
343                         t.Errorf("uninitialized bytes_used in %v", volinfo)
344                 }
345         }
346 }
347
348 // ========================================
349 // Helper functions for unit tests.
350 // ========================================
351
352 // setup
353 //     Create KeepVolumes for testing.
354 //     Returns a slice of pathnames to temporary Keep volumes.
355 //
356 func setup(t *testing.T, num_volumes int) []string {
357         vols := make([]string, num_volumes)
358         for i := range vols {
359                 if dir, err := ioutil.TempDir(os.TempDir(), "keeptest"); err == nil {
360                         vols[i] = dir + "/keep"
361                         os.Mkdir(vols[i], 0755)
362                 } else {
363                         t.Fatal(err)
364                 }
365         }
366         return vols
367 }
368
369 // teardown
370 //     Cleanup to perform after each test.
371 //
372 func teardown() {
373         for _, vol := range KeepVolumes {
374                 os.RemoveAll(path.Dir(vol))
375         }
376         KeepVolumes = nil
377 }
378
379 // store
380 //     Low-level code to write Keep blocks directly to disk for testing.
381 //
382 func store(t *testing.T, keepdir string, filename string, block []byte) {
383         blockdir := fmt.Sprintf("%s/%s", keepdir, filename[:3])
384         if err := os.MkdirAll(blockdir, 0755); err != nil {
385                 t.Fatal(err)
386         }
387
388         blockpath := fmt.Sprintf("%s/%s", blockdir, filename)
389         if f, err := os.Create(blockpath); err == nil {
390                 f.Write(block)
391                 f.Close()
392         } else {
393                 t.Fatal(err)
394         }
395 }