Added TestPutBlockOK. (refs #2292, refs #2449)
[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 // ========================================
16 // GetBlock tests.
17 // ========================================
18
19 // Test simple block reads.
20 func TestGetBlockOK(t *testing.T) {
21         defer teardown()
22
23         // Create two test Keep volumes and store a block in each of them.
24         KeepVolumes = setup(t, 2)
25
26         for _, vol := range KeepVolumes {
27                 store(t, vol, TEST_HASH, TEST_BLOCK)
28         }
29
30         // Check that GetBlock returns success.
31         result, err := GetBlock(TEST_HASH)
32         if err != nil {
33                 t.Errorf("GetBlock error: %s", err)
34         }
35         if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
36                 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
37         }
38 }
39
40 // Test block reads when one Keep volume is missing.
41 func TestGetBlockOneKeepOK(t *testing.T) {
42         defer teardown()
43
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)
47
48         // Check that GetBlock returns success.
49         result, err := GetBlock(TEST_HASH)
50         if err != nil {
51                 t.Errorf("GetBlock error: %s", err)
52         }
53         if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
54                 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
55         }
56 }
57
58 // Test block read failure.
59 func TestGetBlockFail(t *testing.T) {
60         defer teardown()
61
62         // Create two empty test Keep volumes.
63         KeepVolumes = setup(t, 2)
64
65         // Check that GetBlock returns failure.
66         result, err := GetBlock(TEST_HASH)
67         if err == nil {
68                 t.Errorf("GetBlock incorrectly returned success: ", result)
69         }
70 }
71
72 // Test reading a corrupt block.
73 func TestGetBlockCorrupt(t *testing.T) {
74         defer teardown()
75
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)
81         }
82
83         // Check that GetBlock returns failure.
84         result, err := GetBlock(TEST_HASH)
85         if err == nil {
86                 t.Errorf("GetBlock incorrectly returned success: %s", result)
87         }
88 }
89
90 // ========================================
91 // PutBlock tests
92 // ========================================
93
94 // Test simple block stores.
95
96 func TestPutBlockOK(t *testing.T) {
97         defer teardown()
98
99         // Create two test Keep volumes.
100         KeepVolumes = setup(t, 2)
101
102         // Check that PutBlock stores the data as expected.
103         err := PutBlock(TEST_BLOCK, TEST_HASH)
104         if err == nil {
105                 t.Log("err is nil")
106         }
107         if err != nil {
108                 t.Fatalf("PutBlock: %v", err)
109         }
110
111         var result []byte
112         result, err = GetBlock(TEST_HASH)
113         t.Log("result = %v", result)
114         t.Log("err = %v", err)
115
116         if err != nil {
117                 t.Fatalf("GetBlock: %s", err.Error())
118         }
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))
123         }
124 }
125
126 // Test finding Keep volumes.
127 func TestFindKeepVolumes(t *testing.T) {
128         defer teardown()
129
130         // Initialize two keep volumes.
131         var tempVols []string = setup(t, 2)
132
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))
137                 }
138                 f.Close()
139                 PROC_MOUNTS = f.Name()
140
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))
146                 }
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])
151                         }
152                 }
153
154                 os.Remove(f.Name())
155         }
156 }
157
158 // Test that FindKeepVolumes returns an empty slice when no Keep volumes
159 // are present.
160 func TestFindKeepVolumesFail(t *testing.T) {
161         defer teardown()
162
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")
170                 f.Close()
171                 PROC_MOUNTS = f.Name()
172
173                 // Check that FindKeepVolumes returns an empty array.
174                 resultVols := FindKeepVolumes()
175                 if len(resultVols) != 0 {
176                         t.Fatalf("FindKeepVolumes returned %v", resultVols)
177                 }
178
179                 os.Remove(PROC_MOUNTS)
180         }
181 }
182
183 // setup
184 //     Create KeepVolumes for testing.
185 //     Returns a slice of pathnames to temporary Keep volumes.
186 //
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)
193                 } else {
194                         t.Fatal(err)
195                 }
196         }
197         return vols
198 }
199
200 // teardown
201 //     Cleanup to perform after each test.
202 //
203 func teardown() {
204         for _, vol := range KeepVolumes {
205                 os.RemoveAll(path.Dir(vol))
206         }
207 }
208
209 // store
210 //     Low-level code to write Keep blocks directly to disk for testing.
211 //
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 {
215                 t.Fatal(err)
216         }
217
218         blockpath := fmt.Sprintf("%s/%s", blockdir, filename)
219         if f, err := os.Create(blockpath); err == nil {
220                 f.Write(block)
221                 f.Close()
222         } else {
223                 t.Fatal(err)
224         }
225
226         return nil
227 }