6 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
7 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
8 "git.curoverse.com/arvados.git/sdk/go/keepclient"
20 // Gocheck boilerplate
21 func Test(t *testing.T) {
25 // Gocheck boilerplate
26 var _ = Suite(&ServerRequiredSuite{})
28 // Tests that require the Keep server running
29 type ServerRequiredSuite struct{}
31 // Gocheck boilerplate
32 var _ = Suite(&NoKeepServerSuite{})
34 // Test with no keepserver to simulate errors
35 type NoKeepServerSuite struct{}
37 var TestProxyUUID = "zzzzz-bi6l4-lrixqc4fxofbmzz"
39 // Wait (up to 1 second) for keepproxy to listen on a port. This
40 // avoids a race condition where we hit a "connection refused" error
41 // because we start testing the proxy too soon.
42 func waitForListener() {
46 for i := 0; listener == nil && i < 1000; i += ms {
47 time.Sleep(ms * time.Millisecond)
50 log.Fatalf("Timed out waiting for listener to start")
54 func closeListener() {
60 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
61 arvadostest.StartAPI()
62 arvadostest.StartKeep(2, false)
65 func (s *ServerRequiredSuite) SetUpTest(c *C) {
66 arvadostest.ResetEnv()
69 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
70 arvadostest.StopKeep(2)
74 func (s *NoKeepServerSuite) SetUpSuite(c *C) {
75 arvadostest.StartAPI()
76 // We need API to have some keep services listed, but the
77 // services themselves should be unresponsive.
78 arvadostest.StartKeep(2, false)
79 arvadostest.StopKeep(2)
82 func (s *NoKeepServerSuite) SetUpTest(c *C) {
83 arvadostest.ResetEnv()
86 func (s *NoKeepServerSuite) TearDownSuite(c *C) {
90 func runProxy(c *C, args []string, bogusClientToken bool) *keepclient.KeepClient {
91 args = append([]string{"keepproxy"}, args...)
92 os.Args = append(args, "-listen=:0")
97 arv, err := arvadosclient.MakeArvadosClient()
98 c.Assert(err, Equals, nil)
100 arv.ApiToken = "bogus-token"
102 kc := keepclient.New(&arv)
103 sr := map[string]string{
104 TestProxyUUID: "http://" + listener.Addr().String(),
106 kc.SetServiceRoots(sr, sr, sr)
107 kc.Arvados.External = true
108 kc.Using_proxy = true
113 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
114 kc := runProxy(c, nil, false)
115 defer closeListener()
117 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
121 _, _, err := kc.Ask(hash)
122 c.Check(err, Equals, keepclient.BlockNotFound)
123 log.Print("Finished Ask (expected BlockNotFound)")
127 reader, _, _, err := kc.Get(hash)
128 c.Check(reader, Equals, nil)
129 c.Check(err, Equals, keepclient.BlockNotFound)
130 log.Print("Finished Get (expected BlockNotFound)")
133 // Note in bug #5309 among other errors keepproxy would set
134 // Content-Length incorrectly on the 404 BlockNotFound response, this
135 // would result in a protocol violation that would prevent reuse of the
136 // connection, which would manifest by the next attempt to use the
137 // connection (in this case the PutB below) failing. So to test for
138 // that bug it's necessary to trigger an error response (such as
139 // BlockNotFound) and then do something else with the same httpClient
145 hash2, rep, err = kc.PutB([]byte("foo"))
146 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+3(\+.+)?$`, hash))
147 c.Check(rep, Equals, 2)
148 c.Check(err, Equals, nil)
149 log.Print("Finished PutB (expected success)")
153 blocklen, _, err := kc.Ask(hash2)
154 c.Assert(err, Equals, nil)
155 c.Check(blocklen, Equals, int64(3))
156 log.Print("Finished Ask (expected success)")
160 reader, blocklen, _, err := kc.Get(hash2)
161 c.Assert(err, Equals, nil)
162 all, err := ioutil.ReadAll(reader)
163 c.Check(all, DeepEquals, []byte("foo"))
164 c.Check(blocklen, Equals, int64(3))
165 log.Print("Finished Get (expected success)")
171 hash2, rep, err = kc.PutB([]byte(""))
172 c.Check(hash2, Matches, `^d41d8cd98f00b204e9800998ecf8427e\+0(\+.+)?$`)
173 c.Check(rep, Equals, 2)
174 c.Check(err, Equals, nil)
175 log.Print("Finished PutB zero block")
179 reader, blocklen, _, err := kc.Get("d41d8cd98f00b204e9800998ecf8427e")
180 c.Assert(err, Equals, nil)
181 all, err := ioutil.ReadAll(reader)
182 c.Check(all, DeepEquals, []byte(""))
183 c.Check(blocklen, Equals, int64(0))
184 log.Print("Finished Get zero block")
188 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
189 kc := runProxy(c, nil, true)
190 defer closeListener()
192 hash := fmt.Sprintf("%x", md5.Sum([]byte("bar")))
195 _, _, err := kc.Ask(hash)
196 errNotFound, _ := err.(keepclient.ErrNotFound)
197 c.Check(errNotFound, NotNil)
198 c.Assert(strings.Contains(err.Error(), "HTTP 403"), Equals, true)
203 hash2, rep, err := kc.PutB([]byte("bar"))
204 c.Check(hash2, Equals, "")
205 c.Check(rep, Equals, 0)
206 c.Check(err, Equals, keepclient.InsufficientReplicasError)
211 blocklen, _, err := kc.Ask(hash)
212 errNotFound, _ := err.(keepclient.ErrNotFound)
213 c.Check(errNotFound, NotNil)
214 c.Assert(strings.Contains(err.Error(), "HTTP 403"), Equals, true)
215 c.Check(blocklen, Equals, int64(0))
220 _, blocklen, _, err := kc.Get(hash)
221 errNotFound, _ := err.(keepclient.ErrNotFound)
222 c.Check(errNotFound, NotNil)
223 c.Assert(strings.Contains(err.Error(), "HTTP 403"), Equals, true)
224 c.Check(blocklen, Equals, int64(0))
229 func (s *ServerRequiredSuite) TestGetDisabled(c *C) {
230 kc := runProxy(c, []string{"-no-get"}, false)
231 defer closeListener()
233 hash := fmt.Sprintf("%x", md5.Sum([]byte("baz")))
236 _, _, err := kc.Ask(hash)
237 errNotFound, _ := err.(keepclient.ErrNotFound)
238 c.Check(errNotFound, NotNil)
239 c.Assert(strings.Contains(err.Error(), "HTTP 400"), Equals, true)
244 hash2, rep, err := kc.PutB([]byte("baz"))
245 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+3(\+.+)?$`, hash))
246 c.Check(rep, Equals, 2)
247 c.Check(err, Equals, nil)
252 blocklen, _, err := kc.Ask(hash)
253 errNotFound, _ := err.(keepclient.ErrNotFound)
254 c.Check(errNotFound, NotNil)
255 c.Assert(strings.Contains(err.Error(), "HTTP 400"), Equals, true)
256 c.Check(blocklen, Equals, int64(0))
261 _, blocklen, _, err := kc.Get(hash)
262 errNotFound, _ := err.(keepclient.ErrNotFound)
263 c.Check(errNotFound, NotNil)
264 c.Assert(strings.Contains(err.Error(), "HTTP 400"), Equals, true)
265 c.Check(blocklen, Equals, int64(0))
270 func (s *ServerRequiredSuite) TestPutDisabled(c *C) {
271 kc := runProxy(c, []string{"-no-put"}, false)
272 defer closeListener()
274 hash2, rep, err := kc.PutB([]byte("quux"))
275 c.Check(hash2, Equals, "")
276 c.Check(rep, Equals, 0)
277 c.Check(err, Equals, keepclient.InsufficientReplicasError)
280 func (s *ServerRequiredSuite) TestCorsHeaders(c *C) {
281 runProxy(c, nil, false)
282 defer closeListener()
285 client := http.Client{}
286 req, err := http.NewRequest("OPTIONS",
287 fmt.Sprintf("http://%s/%x+3", listener.Addr().String(), md5.Sum([]byte("foo"))),
289 req.Header.Add("Access-Control-Request-Method", "PUT")
290 req.Header.Add("Access-Control-Request-Headers", "Authorization, X-Keep-Desired-Replicas")
291 resp, err := client.Do(req)
292 c.Check(err, Equals, nil)
293 c.Check(resp.StatusCode, Equals, 200)
294 body, err := ioutil.ReadAll(resp.Body)
295 c.Check(string(body), Equals, "")
296 c.Check(resp.Header.Get("Access-Control-Allow-Methods"), Equals, "GET, HEAD, POST, PUT, OPTIONS")
297 c.Check(resp.Header.Get("Access-Control-Allow-Origin"), Equals, "*")
301 resp, err := http.Get(
302 fmt.Sprintf("http://%s/%x+3", listener.Addr().String(), md5.Sum([]byte("foo"))))
303 c.Check(err, Equals, nil)
304 c.Check(resp.Header.Get("Access-Control-Allow-Headers"), Equals, "Authorization, Content-Length, Content-Type, X-Keep-Desired-Replicas")
305 c.Check(resp.Header.Get("Access-Control-Allow-Origin"), Equals, "*")
309 func (s *ServerRequiredSuite) TestPostWithoutHash(c *C) {
310 runProxy(c, nil, false)
311 defer closeListener()
314 client := http.Client{}
315 req, err := http.NewRequest("POST",
316 "http://"+listener.Addr().String()+"/",
317 strings.NewReader("qux"))
318 req.Header.Add("Authorization", "OAuth2 4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
319 req.Header.Add("Content-Type", "application/octet-stream")
320 resp, err := client.Do(req)
321 c.Check(err, Equals, nil)
322 body, err := ioutil.ReadAll(resp.Body)
323 c.Check(err, Equals, nil)
324 c.Check(string(body), Matches,
325 fmt.Sprintf(`^%x\+3(\+.+)?$`, md5.Sum([]byte("qux"))))
329 func (s *ServerRequiredSuite) TestStripHint(c *C) {
330 c.Check(removeHint.ReplaceAllString("http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73+K@zzzzz", "$1"),
332 "http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73")
333 c.Check(removeHint.ReplaceAllString("http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+K@zzzzz+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73", "$1"),
335 "http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73")
336 c.Check(removeHint.ReplaceAllString("http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73+K@zzzzz-zzzzz-zzzzzzzzzzzzzzz", "$1"),
338 "http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73+K@zzzzz-zzzzz-zzzzzzzzzzzzzzz")
339 c.Check(removeHint.ReplaceAllString("http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+K@zzzzz-zzzzz-zzzzzzzzzzzzzzz+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73", "$1"),
341 "http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+K@zzzzz-zzzzz-zzzzzzzzzzzzzzz+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73")
346 // Put one block, with 2 replicas
347 // With no prefix (expect the block locator, twice)
348 // With an existing prefix (expect the block locator, twice)
349 // With a valid but non-existing prefix (expect "\n")
350 // With an invalid prefix (expect error)
351 func (s *ServerRequiredSuite) TestGetIndex(c *C) {
352 kc := runProxy(c, nil, false)
353 defer closeListener()
355 // Put "index-data" blocks
356 data := []byte("index-data")
357 hash := fmt.Sprintf("%x", md5.Sum(data))
359 hash2, rep, err := kc.PutB(data)
360 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+10(\+.+)?$`, hash))
361 c.Check(rep, Equals, 2)
362 c.Check(err, Equals, nil)
364 reader, blocklen, _, err := kc.Get(hash)
365 c.Assert(err, Equals, nil)
366 c.Check(blocklen, Equals, int64(10))
367 all, err := ioutil.ReadAll(reader)
368 c.Check(all, DeepEquals, data)
370 // Put some more blocks
371 _, rep, err = kc.PutB([]byte("some-more-index-data"))
372 c.Check(err, Equals, nil)
374 kc.Arvados.ApiToken = arvadostest.DataManagerToken
377 for _, spec := range []struct {
382 {"", true, true}, // with no prefix
383 {hash[:3], true, false}, // with matching prefix
384 {"abcdef", false, false}, // with no such prefix
386 indexReader, err := kc.GetIndex(TestProxyUUID, spec.prefix)
387 c.Assert(err, Equals, nil)
388 indexResp, err := ioutil.ReadAll(indexReader)
389 c.Assert(err, Equals, nil)
390 locators := strings.Split(string(indexResp), "\n")
393 for _, locator := range locators {
397 c.Check(locator[:len(spec.prefix)], Equals, spec.prefix)
398 if locator[:32] == hash {
404 c.Check(gotTestHash == 2, Equals, spec.expectTestHash)
405 c.Check(gotOther > 0, Equals, spec.expectOther)
408 // GetIndex with invalid prefix
409 _, err = kc.GetIndex(TestProxyUUID, "xyz")
410 c.Assert((err != nil), Equals, true)
413 func (s *ServerRequiredSuite) TestPutAskGetInvalidToken(c *C) {
414 kc := runProxy(c, nil, false)
415 defer closeListener()
418 hash, rep, err := kc.PutB([]byte("foo"))
419 c.Check(err, Equals, nil)
420 c.Check(rep, Equals, 2)
422 for _, token := range []string{
424 "2ym314ysp27sk7h943q6vtc378srb06se3pq6ghurylyf3pdmx", // expired
426 // Change token to given bad token
427 kc.Arvados.ApiToken = token
429 // Ask should result in error
430 _, _, err = kc.Ask(hash)
432 errNotFound, _ := err.(keepclient.ErrNotFound)
433 c.Check(errNotFound.Temporary(), Equals, false)
434 c.Assert(strings.Contains(err.Error(), "HTTP 403"), Equals, true)
436 // Get should result in error
437 _, _, _, err = kc.Get(hash)
439 errNotFound, _ = err.(keepclient.ErrNotFound)
440 c.Check(errNotFound.Temporary(), Equals, false)
441 c.Assert(strings.Contains(err.Error(), "HTTP 403 \"Missing or invalid Authorization header\""), Equals, true)
445 func (s *ServerRequiredSuite) TestAskGetKeepProxyConnectionError(c *C) {
446 arv, err := arvadosclient.MakeArvadosClient()
447 c.Assert(err, Equals, nil)
449 // keepclient with no such keep server
450 kc := keepclient.New(&arv)
451 locals := map[string]string{
452 TestProxyUUID: "http://localhost:12345",
454 kc.SetServiceRoots(locals, nil, nil)
456 // Ask should result in temporary connection refused error
457 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
458 _, _, err = kc.Ask(hash)
460 errNotFound, _ := err.(*keepclient.ErrNotFound)
461 c.Check(errNotFound.Temporary(), Equals, true)
462 c.Assert(strings.Contains(err.Error(), "connection refused"), Equals, true)
464 // Get should result in temporary connection refused error
465 _, _, _, err = kc.Get(hash)
467 errNotFound, _ = err.(*keepclient.ErrNotFound)
468 c.Check(errNotFound.Temporary(), Equals, true)
469 c.Assert(strings.Contains(err.Error(), "connection refused"), Equals, true)
472 func (s *NoKeepServerSuite) TestAskGetNoKeepServerError(c *C) {
473 kc := runProxy(c, nil, false)
474 defer closeListener()
476 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
477 for _, f := range []func()error {
479 _, _, err := kc.Ask(hash)
483 _, _, _, err := kc.Get(hash)
488 c.Assert(err, NotNil)
489 errNotFound, _ := err.(*keepclient.ErrNotFound)
490 c.Check(errNotFound.Temporary(), Equals, true)
491 c.Check(err, ErrorMatches, `.*HTTP 502.*`)