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 // Test simple block reads.
16 func TestGetBlockOK(t *testing.T) {
19 // Create two test Keep volumes and store a block in each of them.
20 KeepVolumes = setup(t, 2)
21 fmt.Println("KeepVolumes = ", KeepVolumes)
23 for _, vol := range KeepVolumes {
24 store(t, vol, TEST_HASH, TEST_BLOCK)
27 // Check that GetBlock returns success.
28 result, err := GetBlock(TEST_HASH)
30 t.Errorf("GetBlock error: %s", err)
32 if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
33 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
37 // Test block reads when one Keep volume is missing.
38 func TestGetBlockOneKeepOK(t *testing.T) {
41 // Two test Keep volumes, only the second has a block.
42 KeepVolumes = setup(t, 2)
43 store(t, KeepVolumes[1], TEST_HASH, TEST_BLOCK)
45 // Check that GetBlock returns success.
46 result, err := GetBlock(TEST_HASH)
48 t.Errorf("GetBlock error: %s", err)
50 if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
51 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
55 // Test block read failure.
56 func TestGetBlockFail(t *testing.T) {
59 // Create two empty test Keep volumes.
60 KeepVolumes = setup(t, 2)
62 // Check that GetBlock returns failure.
63 result, err := GetBlock(TEST_HASH)
65 t.Errorf("GetBlock incorrectly returned success: ", result)
69 // Test reading a corrupt block.
70 func TestGetBlockCorrupt(t *testing.T) {
73 // Create two test Keep volumes and store a block in each of them,
74 // but the hash of the block does not match the filename.
75 KeepVolumes = setup(t, 2)
76 for _, vol := range KeepVolumes {
77 store(t, vol, TEST_HASH, BAD_BLOCK)
80 // Check that GetBlock returns failure.
81 result, err := GetBlock(TEST_HASH)
83 t.Errorf("GetBlock incorrectly returned success: %s", result)
87 // Test finding Keep volumes.
88 func TestFindKeepVolumes(t *testing.T) {
91 // Initialize two keep volumes.
92 var tempVols []string = setup(t, 2)
94 // Set up a bogus PROC_MOUNTS file.
95 if f, err := ioutil.TempFile("", "keeptest"); err == nil {
96 for _, vol := range tempVols {
97 fmt.Fprintf(f, "tmpfs %s tmpfs opts\n", path.Dir(vol))
100 PROC_MOUNTS = f.Name()
102 // Check that FindKeepVolumes finds the temp volumes.
103 resultVols := FindKeepVolumes()
104 if len(tempVols) != len(resultVols) {
105 t.Fatalf("set up %d volumes, FindKeepVolumes found %d\n",
106 len(tempVols), len(resultVols))
108 for i := range tempVols {
109 if tempVols[i] != resultVols[i] {
110 t.Errorf("FindKeepVolumes returned %s, expected %s\n",
111 resultVols[i], tempVols[i])
119 // Test that FindKeepVolumes returns an empty slice when no Keep volumes
121 func TestFindKeepVolumesFail(t *testing.T) {
124 // Set up a bogus PROC_MOUNTS file with no Keep vols.
125 if f, err := ioutil.TempFile("", "keeptest"); err == nil {
126 fmt.Fprintln(f, "rootfs / rootfs opts 0 0")
127 fmt.Fprintln(f, "sysfs /sys sysfs opts 0 0")
128 fmt.Fprintln(f, "proc /proc proc opts 0 0")
129 fmt.Fprintln(f, "udev /dev devtmpfs opts 0 0")
130 fmt.Fprintln(f, "devpts /dev/pts devpts opts 0 0")
132 PROC_MOUNTS = f.Name()
134 // Check that FindKeepVolumes returns an empty array.
135 resultVols := FindKeepVolumes()
136 if len(resultVols) != 0 {
137 t.Fatalf("FindKeepVolumes returned %v", resultVols)
140 os.Remove(PROC_MOUNTS)
145 // Create KeepVolumes for testing.
146 // Returns a slice of pathnames to temporary Keep volumes.
148 func setup(t *testing.T, num_volumes int) []string {
149 vols := make([]string, num_volumes)
150 for i := range vols {
151 if dir, err := ioutil.TempDir(os.TempDir(), "keeptest"); err == nil {
152 vols[i] = dir + "/keep"
153 os.Mkdir(vols[i], 0755)
162 // Cleanup to perform after each test.
165 for _, vol := range KeepVolumes {
166 os.RemoveAll(path.Dir(vol))
172 func store(t *testing.T, keepdir string, filename string, block []byte) error {
173 blockdir := fmt.Sprintf("%s/%s", keepdir, filename[:3])
174 if err := os.MkdirAll(blockdir, 0755); err != nil {
178 blockpath := fmt.Sprintf("%s/%s", blockdir, filename)
179 if f, err := os.Create(blockpath); err == nil {