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.")
15 // ========================================
17 // ========================================
20 // Test that a simple block read can be executed successfully.
22 func TestGetBlockOK(t *testing.T) {
25 // Create two test Keep volumes and store a block in each of them.
26 KeepVolumes = setup(t, 2)
28 for _, vol := range KeepVolumes {
29 store(t, vol, TEST_HASH, TEST_BLOCK)
32 // Check that GetBlock returns success.
33 result, err := GetBlock(TEST_HASH)
35 t.Errorf("GetBlock error: %s", err)
37 if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
38 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
42 // TestGetBlockOneKeepOK
43 // Test that block reads succeed even when the block is found only
44 // on one Keep volume.
46 func TestGetBlockOneKeepOK(t *testing.T) {
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)
53 // Check that GetBlock returns success.
54 result, err := GetBlock(TEST_HASH)
56 t.Errorf("GetBlock error: %s", err)
58 if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
59 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
63 // TestGetBlockMissing
64 // GetBlock must return an error when the block is not found.
66 func TestGetBlockMissing(t *testing.T) {
69 // Create two empty test Keep volumes.
70 KeepVolumes = setup(t, 2)
72 // Check that GetBlock returns failure.
73 result, err := GetBlock(TEST_HASH)
75 t.Errorf("GetBlock incorrectly returned success: ", result)
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).
83 func TestGetBlockCorrupt(t *testing.T) {
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)
93 // Check that GetBlock returns failure.
94 result, err := GetBlock(TEST_HASH)
96 t.Errorf("GetBlock incorrectly returned success: %s", result)
100 // ========================================
102 // ========================================
105 // PutBlock can perform a simple block write and returns success.
107 func TestPutBlockOK(t *testing.T) {
110 // Create two test Keep volumes.
111 KeepVolumes = setup(t, 2)
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)
118 result, err := GetBlock(TEST_HASH)
120 t.Fatalf("GetBlock: %s", err.Error())
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))
129 // TestPutBlockOneVol
130 // PutBlock still returns success even when only one of the known
131 // volumes is online.
133 func TestPutBlockOneVol(t *testing.T) {
136 // Create two test Keep volumes, but cripple one of them.
137 KeepVolumes = setup(t, 2)
138 os.Chmod(KeepVolumes[0], 000)
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)
145 result, err := GetBlock(TEST_HASH)
147 t.Fatalf("GetBlock: %s", err.Error())
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))
156 // TestPutBlockCorrupt
157 // Check that PutBlock returns an error if passed a block and hash that
160 func TestPutBlockCorrupt(t *testing.T) {
163 // Create two test Keep volumes.
164 KeepVolumes = setup(t, 2)
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")
171 ke := err.(*KeepError)
172 if ke.HTTPCode != 401 || ke.Err.Error() != "MD5Fail" {
173 t.Errorf("PutBlock returned the wrong error (%v)", ke)
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'",
184 // TestFindKeepVolumes
185 // Confirms that FindKeepVolumes finds tmpfs volumes with "/keep"
186 // directories at the top level.
188 func TestFindKeepVolumes(t *testing.T) {
191 // Initialize two keep volumes.
192 var tempVols []string = setup(t, 2)
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))
200 PROC_MOUNTS = f.Name()
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))
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])
219 // TestFindKeepVolumesFail
220 // When no Keep volumes are present, FindKeepVolumes returns an empty slice.
222 func TestFindKeepVolumesFail(t *testing.T) {
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")
233 PROC_MOUNTS = f.Name()
235 // Check that FindKeepVolumes returns an empty array.
236 resultVols := FindKeepVolumes()
237 if len(resultVols) != 0 {
238 t.Fatalf("FindKeepVolumes returned %v", resultVols)
241 os.Remove(PROC_MOUNTS)
245 // ========================================
246 // Helper functions for unit tests.
247 // ========================================
250 // Create KeepVolumes for testing.
251 // Returns a slice of pathnames to temporary Keep volumes.
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)
267 // Cleanup to perform after each test.
270 for _, vol := range KeepVolumes {
271 os.RemoveAll(path.Dir(vol))
276 // Low-level code to write Keep blocks directly to disk for testing.
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 {
284 blockpath := fmt.Sprintf("%s/%s", blockdir, filename)
285 if f, err := os.Create(blockpath); err == nil {