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 // ========================================
19 // Test simple block reads.
20 func TestGetBlockOK(t *testing.T) {
23 // Create two test Keep volumes and store a block in each of them.
24 KeepVolumes = setup(t, 2)
26 for _, vol := range KeepVolumes {
27 store(t, vol, TEST_HASH, TEST_BLOCK)
30 // Check that GetBlock returns success.
31 result, err := GetBlock(TEST_HASH)
33 t.Errorf("GetBlock error: %s", err)
35 if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
36 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
40 // Test block reads when one Keep volume is missing.
41 func TestGetBlockOneKeepOK(t *testing.T) {
44 // Two test Keep volumes, only the second has a block.
45 KeepVolumes = setup(t, 2)
46 store(t, KeepVolumes[1], TEST_HASH, TEST_BLOCK)
48 // Check that GetBlock returns success.
49 result, err := GetBlock(TEST_HASH)
51 t.Errorf("GetBlock error: %s", err)
53 if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
54 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
58 // Test block read failure.
59 func TestGetBlockFail(t *testing.T) {
62 // Create two empty test Keep volumes.
63 KeepVolumes = setup(t, 2)
65 // Check that GetBlock returns failure.
66 result, err := GetBlock(TEST_HASH)
68 t.Errorf("GetBlock incorrectly returned success: ", result)
72 // Test reading a corrupt block.
73 func TestGetBlockCorrupt(t *testing.T) {
76 // Create two test Keep volumes and store a block in each of them,
77 // but the hash of the block does not match the filename.
78 KeepVolumes = setup(t, 2)
79 for _, vol := range KeepVolumes {
80 store(t, vol, TEST_HASH, BAD_BLOCK)
83 // Check that GetBlock returns failure.
84 result, err := GetBlock(TEST_HASH)
86 t.Errorf("GetBlock incorrectly returned success: %s", result)
90 // ========================================
92 // ========================================
94 // Test simple block stores.
96 func TestPutBlockOK(t *testing.T) {
99 // Create two test Keep volumes.
100 KeepVolumes = setup(t, 2)
102 // Check that PutBlock stores the data as expected.
103 if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
104 t.Fatalf("PutBlock: %v", err)
107 result, err := GetBlock(TEST_HASH)
109 t.Fatalf("GetBlock: %s", err.Error())
111 if string(result) != string(TEST_BLOCK) {
112 t.Error("PutBlock/GetBlock mismatch")
113 t.Fatalf("PutBlock stored '%s', GetBlock retrieved '%s'",
114 string(TEST_BLOCK), string(result))
118 // Test storing a block when only one volume (of many) is not available.
119 func TestPutBlockOneVol(t *testing.T) {
122 // Create two test Keep volumes, but cripple one of them.
123 KeepVolumes = setup(t, 2)
124 os.Chmod(KeepVolumes[0], 000)
126 // Check that PutBlock stores the data as expected.
127 if err := PutBlock(TEST_BLOCK, TEST_HASH); err != nil {
128 t.Fatalf("PutBlock: %v", err)
131 result, err := GetBlock(TEST_HASH)
133 t.Fatalf("GetBlock: %s", err.Error())
135 if string(result) != string(TEST_BLOCK) {
136 t.Error("PutBlock/GetBlock mismatch")
137 t.Fatalf("PutBlock stored '%s', GetBlock retrieved '%s'",
138 string(TEST_BLOCK), string(result))
142 // TestPutBlockCorrupt
143 // Check that PutBlock returns an error if passed a block and hash that
146 func TestPutBlockCorrupt(t *testing.T) {
149 // Create two test Keep volumes.
150 KeepVolumes = setup(t, 2)
152 // Check that PutBlock returns the expected error when the hash does
153 // not match the block.
154 if err := PutBlock(BAD_BLOCK, TEST_HASH); err == nil {
155 t.Error("PutBlock succeeded despite a block mismatch")
157 ke := err.(*KeepError)
158 if ke.HTTPCode != 401 || ke.Err.Error() != "MD5Fail" {
159 t.Errorf("PutBlock returned the wrong error (%v)", ke)
163 // Confirm that GetBlock fails to return anything.
164 if result, err := GetBlock(TEST_HASH); err == nil {
165 t.Errorf("GetBlock succeded after a corrupt block store, returned '%s'",
170 // Test finding Keep volumes.
171 func TestFindKeepVolumes(t *testing.T) {
174 // Initialize two keep volumes.
175 var tempVols []string = setup(t, 2)
177 // Set up a bogus PROC_MOUNTS file.
178 if f, err := ioutil.TempFile("", "keeptest"); err == nil {
179 for _, vol := range tempVols {
180 fmt.Fprintf(f, "tmpfs %s tmpfs opts\n", path.Dir(vol))
183 PROC_MOUNTS = f.Name()
185 // Check that FindKeepVolumes finds the temp volumes.
186 resultVols := FindKeepVolumes()
187 if len(tempVols) != len(resultVols) {
188 t.Fatalf("set up %d volumes, FindKeepVolumes found %d\n",
189 len(tempVols), len(resultVols))
191 for i := range tempVols {
192 if tempVols[i] != resultVols[i] {
193 t.Errorf("FindKeepVolumes returned %s, expected %s\n",
194 resultVols[i], tempVols[i])
202 // Test that FindKeepVolumes returns an empty slice when no Keep volumes
204 func TestFindKeepVolumesFail(t *testing.T) {
207 // Set up a bogus PROC_MOUNTS file with no Keep vols.
208 if f, err := ioutil.TempFile("", "keeptest"); err == nil {
209 fmt.Fprintln(f, "rootfs / rootfs opts 0 0")
210 fmt.Fprintln(f, "sysfs /sys sysfs opts 0 0")
211 fmt.Fprintln(f, "proc /proc proc opts 0 0")
212 fmt.Fprintln(f, "udev /dev devtmpfs opts 0 0")
213 fmt.Fprintln(f, "devpts /dev/pts devpts opts 0 0")
215 PROC_MOUNTS = f.Name()
217 // Check that FindKeepVolumes returns an empty array.
218 resultVols := FindKeepVolumes()
219 if len(resultVols) != 0 {
220 t.Fatalf("FindKeepVolumes returned %v", resultVols)
223 os.Remove(PROC_MOUNTS)
228 // Create KeepVolumes for testing.
229 // Returns a slice of pathnames to temporary Keep volumes.
231 func setup(t *testing.T, num_volumes int) []string {
232 vols := make([]string, num_volumes)
233 for i := range vols {
234 if dir, err := ioutil.TempDir(os.TempDir(), "keeptest"); err == nil {
235 vols[i] = dir + "/keep"
236 os.Mkdir(vols[i], 0755)
245 // Cleanup to perform after each test.
248 for _, vol := range KeepVolumes {
249 os.RemoveAll(path.Dir(vol))
254 // Low-level code to write Keep blocks directly to disk for testing.
256 func store(t *testing.T, keepdir string, filename string, block []byte) error {
257 blockdir := fmt.Sprintf("%s/%s", keepdir, filename[:3])
258 if err := os.MkdirAll(blockdir, 0755); err != nil {
262 blockpath := fmt.Sprintf("%s/%s", blockdir, filename)
263 if f, err := os.Create(blockpath); err == nil {