Merge branch '2291-new-keepd-read-blocks'
[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 // Test simple block reads.
16 func TestGetBlockOK(t *testing.T) {
17         defer teardown()
18
19         // Create two test Keep volumes and store a block in each of them.
20         KeepVolumes = setup(t, 2)
21         fmt.Println("KeepVolumes = ", KeepVolumes)
22
23         for _, vol := range KeepVolumes {
24                 store(t, vol, TEST_HASH, TEST_BLOCK)
25         }
26
27         // Check that GetBlock returns success.
28         result, err := GetBlock(TEST_HASH)
29         if err != nil {
30                 t.Errorf("GetBlock error: %s", err)
31         }
32         if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
33                 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
34         }
35 }
36
37 // Test block reads when one Keep volume is missing.
38 func TestGetBlockOneKeepOK(t *testing.T) {
39         defer teardown()
40
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)
44
45         // Check that GetBlock returns success.
46         result, err := GetBlock(TEST_HASH)
47         if err != nil {
48                 t.Errorf("GetBlock error: %s", err)
49         }
50         if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
51                 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
52         }
53 }
54
55 // Test block read failure.
56 func TestGetBlockFail(t *testing.T) {
57         defer teardown()
58
59         // Create two empty test Keep volumes.
60         KeepVolumes = setup(t, 2)
61
62         // Check that GetBlock returns failure.
63         result, err := GetBlock(TEST_HASH)
64         if err == nil {
65                 t.Errorf("GetBlock incorrectly returned success: ", result)
66         }
67 }
68
69 // Test reading a corrupt block.
70 func TestGetBlockCorrupt(t *testing.T) {
71         defer teardown()
72
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)
78         }
79
80         // Check that GetBlock returns failure.
81         result, err := GetBlock(TEST_HASH)
82         if err == nil {
83                 t.Errorf("GetBlock incorrectly returned success: %s", result)
84         }
85 }
86
87 // Test finding Keep volumes.
88 func TestFindKeepVolumes(t *testing.T) {
89         defer teardown()
90
91         // Initialize two keep volumes.
92         var tempVols []string = setup(t, 2)
93
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))
98                 }
99                 f.Close()
100                 PROC_MOUNTS = f.Name()
101
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))
107                 }
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])
112                         }
113                 }
114
115                 os.Remove(f.Name())
116         }
117 }
118
119 // Test that FindKeepVolumes returns an empty slice when no Keep volumes
120 // are present.
121 func TestFindKeepVolumesFail(t *testing.T) {
122         defer teardown()
123
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")
131                 f.Close()
132                 PROC_MOUNTS = f.Name()
133
134                 // Check that FindKeepVolumes returns an empty array.
135                 resultVols := FindKeepVolumes()
136                 if len(resultVols) != 0 {
137                         t.Fatalf("FindKeepVolumes returned %v", resultVols)
138                 }
139
140                 os.Remove(PROC_MOUNTS)
141         }
142 }
143
144 // setup
145 //     Create KeepVolumes for testing.
146 //     Returns a slice of pathnames to temporary Keep volumes.
147 //
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)
154                 } else {
155                         t.Fatal(err)
156                 }
157         }
158         return vols
159 }
160
161 // teardown
162 //     Cleanup to perform after each test.
163 //
164 func teardown() {
165         for _, vol := range KeepVolumes {
166                 os.RemoveAll(path.Dir(vol))
167         }
168 }
169
170 // store
171 //
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 {
175                 t.Fatal(err)
176         }
177
178         blockpath := fmt.Sprintf("%s/%s", blockdir, filename)
179         if f, err := os.Create(blockpath); err == nil {
180                 f.Write(block)
181                 f.Close()
182         } else {
183                 t.Fatal(err)
184         }
185
186         return nil
187 }