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 err := PutBlock(TEST_BLOCK, TEST_HASH)
108 t.Fatalf("PutBlock: %v", err)
112 result, err = GetBlock(TEST_HASH)
113 t.Log("result = %v", result)
114 t.Log("err = %v", err)
117 t.Fatalf("GetBlock: %s", err.Error())
119 if string(result) != string(TEST_BLOCK) {
120 t.Error("PutBlock/GetBlock mismatch")
121 t.Fatalf("PutBlock stored '%s', GetBlock retrieved '%s'",
122 string(TEST_BLOCK), string(result))
126 // Test finding Keep volumes.
127 func TestFindKeepVolumes(t *testing.T) {
130 // Initialize two keep volumes.
131 var tempVols []string = setup(t, 2)
133 // Set up a bogus PROC_MOUNTS file.
134 if f, err := ioutil.TempFile("", "keeptest"); err == nil {
135 for _, vol := range tempVols {
136 fmt.Fprintf(f, "tmpfs %s tmpfs opts\n", path.Dir(vol))
139 PROC_MOUNTS = f.Name()
141 // Check that FindKeepVolumes finds the temp volumes.
142 resultVols := FindKeepVolumes()
143 if len(tempVols) != len(resultVols) {
144 t.Fatalf("set up %d volumes, FindKeepVolumes found %d\n",
145 len(tempVols), len(resultVols))
147 for i := range tempVols {
148 if tempVols[i] != resultVols[i] {
149 t.Errorf("FindKeepVolumes returned %s, expected %s\n",
150 resultVols[i], tempVols[i])
158 // Test that FindKeepVolumes returns an empty slice when no Keep volumes
160 func TestFindKeepVolumesFail(t *testing.T) {
163 // Set up a bogus PROC_MOUNTS file with no Keep vols.
164 if f, err := ioutil.TempFile("", "keeptest"); err == nil {
165 fmt.Fprintln(f, "rootfs / rootfs opts 0 0")
166 fmt.Fprintln(f, "sysfs /sys sysfs opts 0 0")
167 fmt.Fprintln(f, "proc /proc proc opts 0 0")
168 fmt.Fprintln(f, "udev /dev devtmpfs opts 0 0")
169 fmt.Fprintln(f, "devpts /dev/pts devpts opts 0 0")
171 PROC_MOUNTS = f.Name()
173 // Check that FindKeepVolumes returns an empty array.
174 resultVols := FindKeepVolumes()
175 if len(resultVols) != 0 {
176 t.Fatalf("FindKeepVolumes returned %v", resultVols)
179 os.Remove(PROC_MOUNTS)
184 // Create KeepVolumes for testing.
185 // Returns a slice of pathnames to temporary Keep volumes.
187 func setup(t *testing.T, num_volumes int) []string {
188 vols := make([]string, num_volumes)
189 for i := range vols {
190 if dir, err := ioutil.TempDir(os.TempDir(), "keeptest"); err == nil {
191 vols[i] = dir + "/keep"
192 os.Mkdir(vols[i], 0755)
201 // Cleanup to perform after each test.
204 for _, vol := range KeepVolumes {
205 os.RemoveAll(path.Dir(vol))
210 // Low-level code to write Keep blocks directly to disk for testing.
212 func store(t *testing.T, keepdir string, filename string, block []byte) error {
213 blockdir := fmt.Sprintf("%s/%s", keepdir, filename[:3])
214 if err := os.MkdirAll(blockdir, 0755); err != nil {
218 blockpath := fmt.Sprintf("%s/%s", blockdir, filename)
219 if f, err := os.Create(blockpath); err == nil {