Doc fixes.
[arvados.git] / services / keep / keep_test.go
1 package main
2
3 import (
4         "fmt"
5         "io/ioutil"
6         "os"
7         "path"
8         "testing"
9 )
10
11 var TEST_BLOCK = []byte("The quick brown fox jumps over the lazy dog.")
12 var TEST_HASH = "e4d909c290d0fb1ca068ffaddf22cbd0"
13 var BAD_BLOCK = []byte("The magic words are squeamish ossifrage.")
14
15 // ========================================
16 // GetBlock tests.
17 // ========================================
18
19 // TestGetBlockOK
20 //     Test that a simple block read can be executed successfully.
21 //
22 func TestGetBlockOK(t *testing.T) {
23         defer teardown()
24
25         // Create two test Keep volumes and store a block in each of them.
26         KeepVolumes = setup(t, 2)
27
28         for _, vol := range KeepVolumes {
29                 store(t, vol, TEST_HASH, TEST_BLOCK)
30         }
31
32         // Check that GetBlock returns success.
33         result, err := GetBlock(TEST_HASH)
34         if err != nil {
35                 t.Errorf("GetBlock error: %s", err)
36         }
37         if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
38                 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
39         }
40 }
41
42 // TestGetBlockOneKeepOK
43 //     Test that block reads succeed even when the block is found only
44 //     on one Keep volume.
45 //
46 func TestGetBlockOneKeepOK(t *testing.T) {
47         defer teardown()
48
49         // Two test Keep volumes, only the second has a block.
50         KeepVolumes = setup(t, 2)
51         store(t, KeepVolumes[1], TEST_HASH, TEST_BLOCK)
52
53         // Check that GetBlock returns success.
54         result, err := GetBlock(TEST_HASH)
55         if err != nil {
56                 t.Errorf("GetBlock error: %s", err)
57         }
58         if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
59                 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
60         }
61 }
62
63 // TestGetBlockMissing
64 //     GetBlock must return an error when the block is not found.
65 //
66 func TestGetBlockMissing(t *testing.T) {
67         defer teardown()
68
69         // Create two empty test Keep volumes.
70         KeepVolumes = setup(t, 2)
71
72         // Check that GetBlock returns failure.
73         result, err := GetBlock(TEST_HASH)
74         if err == nil {
75                 t.Errorf("GetBlock incorrectly returned success: ", result)
76         }
77 }
78
79 // TestGetBlockCorrupt
80 //     GetBlock must return an error when a corrupted block is requested
81 //     (the contents of the file do not checksum to its hash).
82 //
83 func TestGetBlockCorrupt(t *testing.T) {
84         defer teardown()
85
86         // Create two test Keep volumes and store a block in each of them,
87         // but the hash of the block does not match the filename.
88         KeepVolumes = setup(t, 2)
89         for _, vol := range KeepVolumes {
90                 store(t, vol, TEST_HASH, BAD_BLOCK)
91         }
92
93         // Check that GetBlock returns failure.
94         result, err := GetBlock(TEST_HASH)
95         if err == nil {
96                 t.Errorf("GetBlock incorrectly returned success: %s", result)
97         }
98 }
99
100 // ========================================
101 // PutBlock tests
102 // ========================================
103
104 // TestPutBlockOK
105 //     PutBlock can perform a simple block write and returns success.
106 //
107 func TestPutBlockOK(t *testing.T) {
108         defer teardown()
109
110         // Create two test Keep volumes.
111         KeepVolumes = setup(t, 2)
112
113         // Check that PutBlock stores the data as expected.
114         if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
115                 t.Fatalf("PutBlock: %v", err)
116         }
117
118         result, err := GetBlock(TEST_HASH)
119         if err != nil {
120                 t.Fatalf("GetBlock: %s", err.Error())
121         }
122         if string(result) != string(TEST_BLOCK) {
123                 t.Error("PutBlock/GetBlock mismatch")
124                 t.Fatalf("PutBlock stored '%s', GetBlock retrieved '%s'",
125                         string(TEST_BLOCK), string(result))
126         }
127 }
128
129 // TestPutBlockOneVol
130 //     PutBlock still returns success even when only one of the known
131 //     volumes is online.
132 //
133 func TestPutBlockOneVol(t *testing.T) {
134         defer teardown()
135
136         // Create two test Keep volumes, but cripple one of them.
137         KeepVolumes = setup(t, 2)
138         os.Chmod(KeepVolumes[0], 000)
139
140         // Check that PutBlock stores the data as expected.
141         if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
142                 t.Fatalf("PutBlock: %v", err)
143         }
144
145         result, err := GetBlock(TEST_HASH)
146         if err != nil {
147                 t.Fatalf("GetBlock: %s", err.Error())
148         }
149         if string(result) != string(TEST_BLOCK) {
150                 t.Error("PutBlock/GetBlock mismatch")
151                 t.Fatalf("PutBlock stored '%s', GetBlock retrieved '%s'",
152                         string(TEST_BLOCK), string(result))
153         }
154 }
155
156 // TestPutBlockCorrupt
157 //     Check that PutBlock returns an error if passed a block and hash that
158 //     do not match.
159 //
160 func TestPutBlockCorrupt(t *testing.T) {
161         defer teardown()
162
163         // Create two test Keep volumes.
164         KeepVolumes = setup(t, 2)
165
166         // Check that PutBlock returns the expected error when the hash does
167         // not match the block.
168         if err := PutBlock(BAD_BLOCK, TEST_HASH); err == nil {
169                 t.Error("PutBlock succeeded despite a block mismatch")
170         } else {
171                 ke := err.(*KeepError)
172                 if ke.HTTPCode != 401 || ke.Err.Error() != "MD5Fail" {
173                         t.Errorf("PutBlock returned the wrong error (%v)", ke)
174                 }
175         }
176
177         // Confirm that GetBlock fails to return anything.
178         if result, err := GetBlock(TEST_HASH); err == nil {
179                 t.Errorf("GetBlock succeded after a corrupt block store, returned '%s'",
180                         string(result))
181         }
182 }
183
184 // TestFindKeepVolumes
185 //     Confirms that FindKeepVolumes finds tmpfs volumes with "/keep"
186 //     directories at the top level.
187 //
188 func TestFindKeepVolumes(t *testing.T) {
189         defer teardown()
190
191         // Initialize two keep volumes.
192         var tempVols []string = setup(t, 2)
193
194         // Set up a bogus PROC_MOUNTS file.
195         if f, err := ioutil.TempFile("", "keeptest"); err == nil {
196                 for _, vol := range tempVols {
197                         fmt.Fprintf(f, "tmpfs %s tmpfs opts\n", path.Dir(vol))
198                 }
199                 f.Close()
200                 PROC_MOUNTS = f.Name()
201
202                 // Check that FindKeepVolumes finds the temp volumes.
203                 resultVols := FindKeepVolumes()
204                 if len(tempVols) != len(resultVols) {
205                         t.Fatalf("set up %d volumes, FindKeepVolumes found %d\n",
206                                 len(tempVols), len(resultVols))
207                 }
208                 for i := range tempVols {
209                         if tempVols[i] != resultVols[i] {
210                                 t.Errorf("FindKeepVolumes returned %s, expected %s\n",
211                                         resultVols[i], tempVols[i])
212                         }
213                 }
214
215                 os.Remove(f.Name())
216         }
217 }
218
219 // TestFindKeepVolumesFail
220 //     When no Keep volumes are present, FindKeepVolumes returns an empty slice.
221 //
222 func TestFindKeepVolumesFail(t *testing.T) {
223         defer teardown()
224
225         // Set up a bogus PROC_MOUNTS file with no Keep vols.
226         if f, err := ioutil.TempFile("", "keeptest"); err == nil {
227                 fmt.Fprintln(f, "rootfs / rootfs opts 0 0")
228                 fmt.Fprintln(f, "sysfs /sys sysfs opts 0 0")
229                 fmt.Fprintln(f, "proc /proc proc opts 0 0")
230                 fmt.Fprintln(f, "udev /dev devtmpfs opts 0 0")
231                 fmt.Fprintln(f, "devpts /dev/pts devpts opts 0 0")
232                 f.Close()
233                 PROC_MOUNTS = f.Name()
234
235                 // Check that FindKeepVolumes returns an empty array.
236                 resultVols := FindKeepVolumes()
237                 if len(resultVols) != 0 {
238                         t.Fatalf("FindKeepVolumes returned %v", resultVols)
239                 }
240
241                 os.Remove(PROC_MOUNTS)
242         }
243 }
244
245 // ========================================
246 // Helper functions for unit tests.
247 // ========================================
248
249 // setup
250 //     Create KeepVolumes for testing.
251 //     Returns a slice of pathnames to temporary Keep volumes.
252 //
253 func setup(t *testing.T, num_volumes int) []string {
254         vols := make([]string, num_volumes)
255         for i := range vols {
256                 if dir, err := ioutil.TempDir(os.TempDir(), "keeptest"); err == nil {
257                         vols[i] = dir + "/keep"
258                         os.Mkdir(vols[i], 0755)
259                 } else {
260                         t.Fatal(err)
261                 }
262         }
263         return vols
264 }
265
266 // teardown
267 //     Cleanup to perform after each test.
268 //
269 func teardown() {
270         for _, vol := range KeepVolumes {
271                 os.RemoveAll(path.Dir(vol))
272         }
273 }
274
275 // store
276 //     Low-level code to write Keep blocks directly to disk for testing.
277 //
278 func store(t *testing.T, keepdir string, filename string, block []byte) error {
279         blockdir := fmt.Sprintf("%s/%s", keepdir, filename[:3])
280         if err := os.MkdirAll(blockdir, 0755); err != nil {
281                 t.Fatal(err)
282         }
283
284         blockpath := fmt.Sprintf("%s/%s", blockdir, filename)
285         if f, err := os.Create(blockpath); err == nil {
286                 f.Write(block)
287                 f.Close()
288         } else {
289                 t.Fatal(err)
290         }
291
292         return nil
293 }