8724: some more cleanup of tests.
[arvados.git] / tools / keep-block-check / keep-block-check_test.go
1 package main
2
3 import (
4         "bytes"
5         "fmt"
6         "io"
7         "io/ioutil"
8         "log"
9         "os"
10         "regexp"
11         "strings"
12         "testing"
13
14         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
15         "git.curoverse.com/arvados.git/sdk/go/keepclient"
16
17         . "gopkg.in/check.v1"
18 )
19
20 // Gocheck boilerplate
21 func Test(t *testing.T) {
22         TestingT(t)
23 }
24
25 // Gocheck boilerplate
26 var _ = Suite(&ServerRequiredSuite{})
27 var _ = Suite(&DoMainTestSuite{})
28
29 type ServerRequiredSuite struct{}
30 type DoMainTestSuite struct{}
31
32 var kc *keepclient.KeepClient
33 var logBuffer bytes.Buffer
34
35 var TestHash = "aaaa09c290d0fb1ca068ffaddf22cbd0"
36 var TestHash2 = "aaaac516f788aec4f30932ffb6395c39"
37
38 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
39         arvadostest.StartAPI()
40 }
41
42 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
43         arvadostest.StopAPI()
44         arvadostest.ResetEnv()
45 }
46
47 func (s *ServerRequiredSuite) SetUpTest(c *C) {
48         logOutput := io.MultiWriter(&logBuffer)
49         log.SetOutput(logOutput)
50 }
51
52 func (s *ServerRequiredSuite) TearDownTest(c *C) {
53         arvadostest.StopKeep(2)
54         log.SetOutput(os.Stdout)
55         log.Printf("%v", logBuffer.String())
56 }
57
58 func (s *DoMainTestSuite) SetUpSuite(c *C) {
59 }
60
61 func (s *DoMainTestSuite) SetUpTest(c *C) {
62         logOutput := io.MultiWriter(&logBuffer)
63         log.SetOutput(logOutput)
64 }
65
66 func (s *DoMainTestSuite) TearDownTest(c *C) {
67         log.SetOutput(os.Stdout)
68         log.Printf("%v", logBuffer.String())
69 }
70
71 func setupKeepBlockCheck(c *C, enforcePermissions bool, keepServicesJSON string) {
72         var config apiConfig
73         config.APIHost = os.Getenv("ARVADOS_API_HOST")
74         config.APIToken = arvadostest.DataManagerToken
75         config.APIHostInsecure = matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE"))
76
77         // Start Keep servers
78         arvadostest.StartKeep(2, enforcePermissions)
79
80         // setup keepclients
81         var err error
82         kc, err = setupKeepClient(config, keepServicesJSON)
83         c.Check(err, IsNil)
84 }
85
86 // Setup test data
87 func setupTestData(c *C) []string {
88         allLocators := []string{}
89
90         // Put a few blocks
91         for i := 0; i < 5; i++ {
92                 hash, _, err := kc.PutB([]byte(fmt.Sprintf("keep-block-check-test-data-%d", i)))
93                 c.Check(err, IsNil)
94                 allLocators = append(allLocators, strings.Split(hash, "+A")[0])
95         }
96
97         return allLocators
98 }
99
100 func setupConfigFile(c *C, fileName string) string {
101         // Setup a config file
102         file, err := ioutil.TempFile(os.TempDir(), fileName)
103         c.Check(err, IsNil)
104
105         // Add config to file. While at it, throw some extra white space
106         fileContent := "ARVADOS_API_HOST=" + os.Getenv("ARVADOS_API_HOST") + "\n"
107         fileContent += "ARVADOS_API_TOKEN=" + arvadostest.DataManagerToken + "\n"
108         fileContent += "\n"
109         fileContent += "ARVADOS_API_HOST_INSECURE=" + os.Getenv("ARVADOS_API_HOST_INSECURE") + "\n"
110         fileContent += " ARVADOS_EXTERNAL_CLIENT = false \n"
111         fileContent += " NotANameValuePairAndShouldGetIgnored \n"
112         fileContent += "ARVADOS_BLOB_SIGNING_KEY=abcdefg\n"
113
114         _, err = file.Write([]byte(fileContent))
115         c.Check(err, IsNil)
116
117         return file.Name()
118 }
119
120 func setupBlockHashFile(c *C, name string, blocks []string) string {
121         // Setup a block hash file
122         file, err := ioutil.TempFile(os.TempDir(), name)
123         c.Check(err, IsNil)
124
125         // Add the hashes to the file. While at it, throw some extra white space
126         fileContent := ""
127         for _, hash := range blocks {
128                 fileContent += fmt.Sprintf(" %s \n", hash)
129         }
130         fileContent += "\n"
131         _, err = file.Write([]byte(fileContent))
132         c.Check(err, IsNil)
133
134         return file.Name()
135 }
136
137 func checkErrorLog(c *C, blocks []string, prefix, suffix string) {
138         for _, hash := range blocks {
139                 expected := prefix + `.*` + hash + `.*` + suffix
140                 match, _ := regexp.MatchString(expected, logBuffer.String())
141                 c.Assert(match, Equals, true)
142         }
143 }
144
145 func checkNoErrorsLogged(c *C, prefix, suffix string) {
146         expected := prefix + `.*` + suffix
147         match, _ := regexp.MatchString(expected, logBuffer.String())
148         c.Assert(match, Equals, false)
149 }
150
151 func (s *ServerRequiredSuite) TestBlockCheck(c *C) {
152         setupKeepBlockCheck(c, false, "")
153         allLocators := setupTestData(c)
154         err := performKeepBlockCheck(kc, "", allLocators, true)
155         c.Check(err, IsNil)
156         checkNoErrorsLogged(c, "Error verifying block", "Block not found")
157 }
158
159 func (s *ServerRequiredSuite) TestBlockCheckWithBlobSigning(c *C) {
160         setupKeepBlockCheck(c, true, "")
161         allLocators := setupTestData(c)
162         err := performKeepBlockCheck(kc, arvadostest.BlobSigningKey, allLocators, true)
163         c.Check(err, IsNil)
164         checkNoErrorsLogged(c, "Error verifying block", "Block not found")
165 }
166
167 func (s *ServerRequiredSuite) TestBlockCheck_NoSuchBlock(c *C) {
168         setupKeepBlockCheck(c, false, "")
169         allLocators := setupTestData(c)
170         allLocators = append(allLocators, TestHash)
171         allLocators = append(allLocators, TestHash2)
172         err := performKeepBlockCheck(kc, "", allLocators, true)
173         c.Check(err, NotNil)
174         c.Assert(err.Error(), Equals, "Block verification failed for 2 out of 7 blocks with matching prefix.")
175         checkErrorLog(c, []string{TestHash, TestHash2}, "Error verifying block", "Block not found")
176 }
177
178 func (s *ServerRequiredSuite) TestBlockCheck_NoSuchBlock_WithMatchingPrefix(c *C) {
179         setupKeepBlockCheck(c, false, "")
180         allLocators := setupTestData(c)
181         allLocators = append(allLocators, TestHash)
182         allLocators = append(allLocators, TestHash2)
183         locatorFile := setupBlockHashFile(c, "block-hash", allLocators)
184         defer os.Remove(locatorFile)
185         locators, err := getBlockLocators(locatorFile, "aaa")
186         c.Check(err, IsNil)
187         err = performKeepBlockCheck(kc, "", locators, true)
188         c.Check(err, NotNil)
189         // Of the 7 blocks in allLocators, only two match the prefix and hence only those are checked
190         c.Assert(err.Error(), Equals, "Block verification failed for 2 out of 2 blocks with matching prefix.")
191         checkErrorLog(c, []string{TestHash, TestHash2}, "Error verifying block", "Block not found")
192 }
193
194 func (s *ServerRequiredSuite) TestBlockCheck_NoSuchBlock_WithPrefixMismatch(c *C) {
195         setupKeepBlockCheck(c, false, "")
196         allLocators := setupTestData(c)
197         allLocators = append(allLocators, TestHash)
198         allLocators = append(allLocators, TestHash2)
199         locatorFile := setupBlockHashFile(c, "block-hash", allLocators)
200         defer os.Remove(locatorFile)
201         locators, err := getBlockLocators(locatorFile, "999")
202         c.Check(err, IsNil)
203         err = performKeepBlockCheck(kc, "", locators, true)
204         c.Check(err, IsNil) // there were no matching locators in file and hence nothing was checked
205 }
206
207 func (s *ServerRequiredSuite) TestBlockCheck_BadSignature(c *C) {
208         setupKeepBlockCheck(c, true, "")
209         setupTestData(c)
210         err := performKeepBlockCheck(kc, "badblobsigningkey", []string{TestHash, TestHash2}, false)
211         c.Assert(err.Error(), Equals, "Block verification failed for 2 out of 2 blocks with matching prefix.")
212         checkErrorLog(c, []string{TestHash, TestHash2}, "Error verifying block", "HTTP 403")
213         // verbose logging not requested
214         c.Assert(strings.Contains(logBuffer.String(), "Verifying block 1 of 2"), Equals, false)
215 }
216
217 var testKeepServicesJSON = `{
218   "kind":"arvados#keepServiceList",
219   "etag":"",
220   "self_link":"",
221   "offset":null, "limit":null,
222   "items":[
223     {"href":"/keep_services/zzzzz-bi6l4-123456789012340",
224      "kind":"arvados#keepService",
225      "uuid":"zzzzz-bi6l4-123456789012340",
226      "service_host":"keep0.zzzzz.arvadosapi.com",
227      "service_port":25107,
228      "service_ssl_flag":false,
229      "service_type":"disk",
230      "read_only":false },
231     {"href":"/keep_services/zzzzz-bi6l4-123456789012341",
232      "kind":"arvados#keepService",
233      "uuid":"zzzzz-bi6l4-123456789012341",
234      "service_host":"keep0.zzzzz.arvadosapi.com",
235      "service_port":25108,
236      "service_ssl_flag":false,
237      "service_type":"disk",
238      "read_only":false }
239     ],
240   "items_available":2 }`
241
242 // Setup block-check using keepServicesJSON with fake keepservers.
243 // Expect error during performKeepBlockCheck due to unreachable keepservers.
244 func (s *ServerRequiredSuite) TestErrorDuringKeepBlockCheck_FakeKeepservers(c *C) {
245         setupKeepBlockCheck(c, false, testKeepServicesJSON)
246         err := performKeepBlockCheck(kc, "", []string{TestHash, TestHash2}, true)
247         c.Assert(err.Error(), Equals, "Block verification failed for 2 out of 2 blocks with matching prefix.")
248         checkErrorLog(c, []string{TestHash, TestHash2}, "Error verifying block", "")
249 }
250
251 // Test keep-block-check initialization with keepServicesJSON
252 func (s *ServerRequiredSuite) TestKeepBlockCheck_InitializeWithKeepServicesJSON(c *C) {
253         setupKeepBlockCheck(c, false, testKeepServicesJSON)
254         found := 0
255         for k := range kc.LocalRoots() {
256                 if k == "zzzzz-bi6l4-123456789012340" || k == "zzzzz-bi6l4-123456789012341" {
257                         found++
258                 }
259         }
260         c.Check(found, Equals, 2)
261 }
262
263 // Test loadConfig func
264 func (s *ServerRequiredSuite) TestLoadConfig(c *C) {
265         // Setup config file
266         configFile := setupConfigFile(c, "config")
267         defer os.Remove(configFile)
268
269         // load configuration from the file
270         config, blobSigningKey, err := loadConfig(configFile)
271         c.Check(err, IsNil)
272
273         c.Assert(config.APIHost, Equals, os.Getenv("ARVADOS_API_HOST"))
274         c.Assert(config.APIToken, Equals, arvadostest.DataManagerToken)
275         c.Assert(config.APIHostInsecure, Equals, matchTrue.MatchString(os.Getenv("ARVADOS_API_HOST_INSECURE")))
276         c.Assert(config.ExternalClient, Equals, false)
277         c.Assert(blobSigningKey, Equals, "abcdefg")
278 }
279
280 func (s *DoMainTestSuite) Test_doMain_WithNoConfig(c *C) {
281         args := []string{"-prefix", "a"}
282         err := doMain(args)
283         c.Check(err, NotNil)
284         c.Assert(strings.Contains(err.Error(), "config file not specified"), Equals, true)
285 }
286
287 func (s *DoMainTestSuite) Test_doMain_WithNoSuchConfigFile(c *C) {
288         args := []string{"-config", "no-such-file"}
289         err := doMain(args)
290         c.Check(err, NotNil)
291         c.Assert(strings.Contains(err.Error(), "no such file or directory"), Equals, true)
292 }
293
294 func (s *DoMainTestSuite) Test_doMain_WithNoBlockHashFile(c *C) {
295         config := setupConfigFile(c, "config")
296         defer os.Remove(config)
297
298         // Start keepservers.
299         arvadostest.StartKeep(2, false)
300         defer arvadostest.StopKeep(2)
301
302         args := []string{"-config", config}
303         err := doMain(args)
304         c.Assert(strings.Contains(err.Error(), "block-hash-file not specified"), Equals, true)
305 }
306
307 func (s *DoMainTestSuite) Test_doMain_WithNoSuchBlockHashFile(c *C) {
308         config := setupConfigFile(c, "config")
309         defer os.Remove(config)
310
311         arvadostest.StartKeep(2, false)
312         defer arvadostest.StopKeep(2)
313
314         args := []string{"-config", config, "-block-hash-file", "no-such-file"}
315         err := doMain(args)
316         c.Assert(strings.Contains(err.Error(), "no such file or directory"), Equals, true)
317 }
318
319 func (s *DoMainTestSuite) Test_doMain(c *C) {
320         // Start keepservers.
321         arvadostest.StartKeep(2, false)
322         defer arvadostest.StopKeep(2)
323
324         config := setupConfigFile(c, "config")
325         defer os.Remove(config)
326
327         locatorFile := setupBlockHashFile(c, "block-hash", []string{TestHash, TestHash2})
328         defer os.Remove(locatorFile)
329
330         args := []string{"-config", config, "-block-hash-file", locatorFile, "-v"}
331         err := doMain(args)
332         c.Check(err, NotNil)
333         c.Assert(err.Error(), Equals, "Block verification failed for 2 out of 2 blocks with matching prefix.")
334         checkErrorLog(c, []string{TestHash, TestHash2}, "Error verifying block", "Block not found")
335         c.Assert(strings.Contains(logBuffer.String(), "Verifying block 1 of 2"), Equals, true)
336 }