7 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
8 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
9 "git.curoverse.com/arvados.git/sdk/go/keepclient"
22 // Gocheck boilerplate
23 func Test(t *testing.T) {
27 // Gocheck boilerplate
28 var _ = Suite(&ServerRequiredSuite{})
30 // Tests that require the Keep server running
31 type ServerRequiredSuite struct{}
33 // Gocheck boilerplate
34 var _ = Suite(&NoKeepServerSuite{})
36 // Test with no keepserver to simulate errors
37 type NoKeepServerSuite struct{}
39 var TestProxyUUID = "zzzzz-bi6l4-lrixqc4fxofbmzz"
41 // Wait (up to 1 second) for keepproxy to listen on a port. This
42 // avoids a race condition where we hit a "connection refused" error
43 // because we start testing the proxy too soon.
44 func waitForListener() {
48 for i := 0; listener == nil && i < 10000; i += ms {
49 time.Sleep(ms * time.Millisecond)
52 log.Fatalf("Timed out waiting for listener to start")
56 func closeListener() {
62 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
63 arvadostest.StartAPI()
64 arvadostest.StartKeep(2, false)
67 func (s *ServerRequiredSuite) SetUpTest(c *C) {
68 arvadostest.ResetEnv()
71 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
72 arvadostest.StopKeep(2)
76 func (s *NoKeepServerSuite) SetUpSuite(c *C) {
77 arvadostest.StartAPI()
78 // We need API to have some keep services listed, but the
79 // services themselves should be unresponsive.
80 arvadostest.StartKeep(2, false)
81 arvadostest.StopKeep(2)
84 func (s *NoKeepServerSuite) SetUpTest(c *C) {
85 arvadostest.ResetEnv()
88 func (s *NoKeepServerSuite) TearDownSuite(c *C) {
92 func runProxy(c *C, args []string, bogusClientToken bool) *keepclient.KeepClient {
93 args = append([]string{"keepproxy"}, args...)
94 os.Args = append(args, "-listen=:0")
99 arv, err := arvadosclient.MakeArvadosClient()
100 c.Assert(err, Equals, nil)
101 if bogusClientToken {
102 arv.ApiToken = "bogus-token"
104 kc := keepclient.New(arv)
105 sr := map[string]string{
106 TestProxyUUID: "http://" + listener.Addr().String(),
108 kc.SetServiceRoots(sr, sr, sr)
109 kc.Arvados.External = true
114 func (s *ServerRequiredSuite) TestDesiredReplicas(c *C) {
115 kc := runProxy(c, nil, false)
116 defer closeListener()
118 content := []byte("TestDesiredReplicas")
119 hash := fmt.Sprintf("%x", md5.Sum(content))
121 for _, kc.Want_replicas = range []int{0, 1, 2} {
122 locator, rep, err := kc.PutB(content)
123 c.Check(err, Equals, nil)
124 c.Check(rep, Equals, kc.Want_replicas)
126 c.Check(locator, Matches, fmt.Sprintf(`^%s\+%d(\+.+)?$`, hash, len(content)))
131 func (s *ServerRequiredSuite) TestPutWrongContentLength(c *C) {
132 kc := runProxy(c, nil, false)
133 defer closeListener()
135 content := []byte("TestPutWrongContentLength")
136 hash := fmt.Sprintf("%x", md5.Sum(content))
138 // If we use http.Client to send these requests to the network
139 // server we just started, the Go http library automatically
140 // fixes the invalid Content-Length header. In order to test
141 // our server behavior, we have to call the handler directly
142 // using an httptest.ResponseRecorder.
143 rtr := MakeRESTRouter(true, true, kc)
145 type testcase struct {
150 for _, t := range []testcase{
151 {"1", http.StatusBadRequest},
152 {"", http.StatusLengthRequired},
153 {"-1", http.StatusLengthRequired},
154 {"abcdef", http.StatusLengthRequired},
156 req, err := http.NewRequest("PUT",
157 fmt.Sprintf("http://%s/%s+%d", listener.Addr().String(), hash, len(content)),
158 bytes.NewReader(content))
160 req.Header.Set("Content-Length", t.sendLength)
161 req.Header.Set("Authorization", "OAuth2 4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
162 req.Header.Set("Content-Type", "application/octet-stream")
164 resp := httptest.NewRecorder()
165 rtr.ServeHTTP(resp, req)
166 c.Check(resp.Code, Equals, t.expectStatus)
170 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
171 kc := runProxy(c, nil, false)
172 defer closeListener()
174 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
178 _, _, err := kc.Ask(hash)
179 c.Check(err, Equals, keepclient.BlockNotFound)
180 log.Print("Finished Ask (expected BlockNotFound)")
184 reader, _, _, err := kc.Get(hash)
185 c.Check(reader, Equals, nil)
186 c.Check(err, Equals, keepclient.BlockNotFound)
187 log.Print("Finished Get (expected BlockNotFound)")
190 // Note in bug #5309 among other errors keepproxy would set
191 // Content-Length incorrectly on the 404 BlockNotFound response, this
192 // would result in a protocol violation that would prevent reuse of the
193 // connection, which would manifest by the next attempt to use the
194 // connection (in this case the PutB below) failing. So to test for
195 // that bug it's necessary to trigger an error response (such as
196 // BlockNotFound) and then do something else with the same httpClient
202 hash2, rep, err = kc.PutB([]byte("foo"))
203 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+3(\+.+)?$`, hash))
204 c.Check(rep, Equals, 2)
205 c.Check(err, Equals, nil)
206 log.Print("Finished PutB (expected success)")
210 blocklen, _, err := kc.Ask(hash2)
211 c.Assert(err, Equals, nil)
212 c.Check(blocklen, Equals, int64(3))
213 log.Print("Finished Ask (expected success)")
217 reader, blocklen, _, err := kc.Get(hash2)
218 c.Assert(err, Equals, nil)
219 all, err := ioutil.ReadAll(reader)
220 c.Check(all, DeepEquals, []byte("foo"))
221 c.Check(blocklen, Equals, int64(3))
222 log.Print("Finished Get (expected success)")
228 hash2, rep, err = kc.PutB([]byte(""))
229 c.Check(hash2, Matches, `^d41d8cd98f00b204e9800998ecf8427e\+0(\+.+)?$`)
230 c.Check(rep, Equals, 2)
231 c.Check(err, Equals, nil)
232 log.Print("Finished PutB zero block")
236 reader, blocklen, _, err := kc.Get("d41d8cd98f00b204e9800998ecf8427e")
237 c.Assert(err, Equals, nil)
238 all, err := ioutil.ReadAll(reader)
239 c.Check(all, DeepEquals, []byte(""))
240 c.Check(blocklen, Equals, int64(0))
241 log.Print("Finished Get zero block")
245 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
246 kc := runProxy(c, nil, true)
247 defer closeListener()
249 hash := fmt.Sprintf("%x", md5.Sum([]byte("bar")))
252 _, _, err := kc.Ask(hash)
253 errNotFound, _ := err.(keepclient.ErrNotFound)
254 c.Check(errNotFound, NotNil)
255 c.Assert(strings.Contains(err.Error(), "HTTP 403"), Equals, true)
260 hash2, rep, err := kc.PutB([]byte("bar"))
261 c.Check(hash2, Equals, "")
262 c.Check(rep, Equals, 0)
263 c.Check(err, Equals, keepclient.InsufficientReplicasError)
268 blocklen, _, err := kc.Ask(hash)
269 errNotFound, _ := err.(keepclient.ErrNotFound)
270 c.Check(errNotFound, NotNil)
271 c.Assert(strings.Contains(err.Error(), "HTTP 403"), Equals, true)
272 c.Check(blocklen, Equals, int64(0))
277 _, blocklen, _, err := kc.Get(hash)
278 errNotFound, _ := err.(keepclient.ErrNotFound)
279 c.Check(errNotFound, NotNil)
280 c.Assert(strings.Contains(err.Error(), "HTTP 403"), Equals, true)
281 c.Check(blocklen, Equals, int64(0))
286 func (s *ServerRequiredSuite) TestGetDisabled(c *C) {
287 kc := runProxy(c, []string{"-no-get"}, false)
288 defer closeListener()
290 hash := fmt.Sprintf("%x", md5.Sum([]byte("baz")))
293 _, _, err := kc.Ask(hash)
294 errNotFound, _ := err.(keepclient.ErrNotFound)
295 c.Check(errNotFound, NotNil)
296 c.Assert(strings.Contains(err.Error(), "HTTP 400"), Equals, true)
301 hash2, rep, err := kc.PutB([]byte("baz"))
302 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+3(\+.+)?$`, hash))
303 c.Check(rep, Equals, 2)
304 c.Check(err, Equals, nil)
309 blocklen, _, err := kc.Ask(hash)
310 errNotFound, _ := err.(keepclient.ErrNotFound)
311 c.Check(errNotFound, NotNil)
312 c.Assert(strings.Contains(err.Error(), "HTTP 400"), Equals, true)
313 c.Check(blocklen, Equals, int64(0))
318 _, blocklen, _, err := kc.Get(hash)
319 errNotFound, _ := err.(keepclient.ErrNotFound)
320 c.Check(errNotFound, NotNil)
321 c.Assert(strings.Contains(err.Error(), "HTTP 400"), Equals, true)
322 c.Check(blocklen, Equals, int64(0))
327 func (s *ServerRequiredSuite) TestPutDisabled(c *C) {
328 kc := runProxy(c, []string{"-no-put"}, false)
329 defer closeListener()
331 hash2, rep, err := kc.PutB([]byte("quux"))
332 c.Check(hash2, Equals, "")
333 c.Check(rep, Equals, 0)
334 c.Check(err, Equals, keepclient.InsufficientReplicasError)
337 func (s *ServerRequiredSuite) TestCorsHeaders(c *C) {
338 runProxy(c, nil, false)
339 defer closeListener()
342 client := http.Client{}
343 req, err := http.NewRequest("OPTIONS",
344 fmt.Sprintf("http://%s/%x+3", listener.Addr().String(), md5.Sum([]byte("foo"))),
346 req.Header.Add("Access-Control-Request-Method", "PUT")
347 req.Header.Add("Access-Control-Request-Headers", "Authorization, X-Keep-Desired-Replicas")
348 resp, err := client.Do(req)
349 c.Check(err, Equals, nil)
350 c.Check(resp.StatusCode, Equals, 200)
351 body, err := ioutil.ReadAll(resp.Body)
352 c.Check(string(body), Equals, "")
353 c.Check(resp.Header.Get("Access-Control-Allow-Methods"), Equals, "GET, HEAD, POST, PUT, OPTIONS")
354 c.Check(resp.Header.Get("Access-Control-Allow-Origin"), Equals, "*")
358 resp, err := http.Get(
359 fmt.Sprintf("http://%s/%x+3", listener.Addr().String(), md5.Sum([]byte("foo"))))
360 c.Check(err, Equals, nil)
361 c.Check(resp.Header.Get("Access-Control-Allow-Headers"), Equals, "Authorization, Content-Length, Content-Type, X-Keep-Desired-Replicas")
362 c.Check(resp.Header.Get("Access-Control-Allow-Origin"), Equals, "*")
366 func (s *ServerRequiredSuite) TestPostWithoutHash(c *C) {
367 runProxy(c, nil, false)
368 defer closeListener()
371 client := http.Client{}
372 req, err := http.NewRequest("POST",
373 "http://"+listener.Addr().String()+"/",
374 strings.NewReader("qux"))
375 req.Header.Add("Authorization", "OAuth2 4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
376 req.Header.Add("Content-Type", "application/octet-stream")
377 resp, err := client.Do(req)
378 c.Check(err, Equals, nil)
379 body, err := ioutil.ReadAll(resp.Body)
380 c.Check(err, Equals, nil)
381 c.Check(string(body), Matches,
382 fmt.Sprintf(`^%x\+3(\+.+)?$`, md5.Sum([]byte("qux"))))
386 func (s *ServerRequiredSuite) TestStripHint(c *C) {
387 c.Check(removeHint.ReplaceAllString("http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73+K@zzzzz", "$1"),
389 "http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73")
390 c.Check(removeHint.ReplaceAllString("http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+K@zzzzz+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73", "$1"),
392 "http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73")
393 c.Check(removeHint.ReplaceAllString("http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73+K@zzzzz-zzzzz-zzzzzzzzzzzzzzz", "$1"),
395 "http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73+K@zzzzz-zzzzz-zzzzzzzzzzzzzzz")
396 c.Check(removeHint.ReplaceAllString("http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+K@zzzzz-zzzzz-zzzzzzzzzzzzzzz+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73", "$1"),
398 "http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+K@zzzzz-zzzzz-zzzzzzzzzzzzzzz+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73")
403 // Put one block, with 2 replicas
404 // With no prefix (expect the block locator, twice)
405 // With an existing prefix (expect the block locator, twice)
406 // With a valid but non-existing prefix (expect "\n")
407 // With an invalid prefix (expect error)
408 func (s *ServerRequiredSuite) TestGetIndex(c *C) {
409 kc := runProxy(c, nil, false)
410 defer closeListener()
412 // Put "index-data" blocks
413 data := []byte("index-data")
414 hash := fmt.Sprintf("%x", md5.Sum(data))
416 hash2, rep, err := kc.PutB(data)
417 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+10(\+.+)?$`, hash))
418 c.Check(rep, Equals, 2)
419 c.Check(err, Equals, nil)
421 reader, blocklen, _, err := kc.Get(hash)
422 c.Assert(err, Equals, nil)
423 c.Check(blocklen, Equals, int64(10))
424 all, err := ioutil.ReadAll(reader)
425 c.Check(all, DeepEquals, data)
427 // Put some more blocks
428 _, rep, err = kc.PutB([]byte("some-more-index-data"))
429 c.Check(err, Equals, nil)
431 kc.Arvados.ApiToken = arvadostest.DataManagerToken
434 for _, spec := range []struct {
439 {"", true, true}, // with no prefix
440 {hash[:3], true, false}, // with matching prefix
441 {"abcdef", false, false}, // with no such prefix
443 indexReader, err := kc.GetIndex(TestProxyUUID, spec.prefix)
444 c.Assert(err, Equals, nil)
445 indexResp, err := ioutil.ReadAll(indexReader)
446 c.Assert(err, Equals, nil)
447 locators := strings.Split(string(indexResp), "\n")
450 for _, locator := range locators {
454 c.Check(locator[:len(spec.prefix)], Equals, spec.prefix)
455 if locator[:32] == hash {
461 c.Check(gotTestHash == 2, Equals, spec.expectTestHash)
462 c.Check(gotOther > 0, Equals, spec.expectOther)
465 // GetIndex with invalid prefix
466 _, err = kc.GetIndex(TestProxyUUID, "xyz")
467 c.Assert((err != nil), Equals, true)
470 func (s *ServerRequiredSuite) TestPutAskGetInvalidToken(c *C) {
471 kc := runProxy(c, nil, false)
472 defer closeListener()
475 hash, rep, err := kc.PutB([]byte("foo"))
476 c.Check(err, Equals, nil)
477 c.Check(rep, Equals, 2)
479 for _, token := range []string{
481 "2ym314ysp27sk7h943q6vtc378srb06se3pq6ghurylyf3pdmx", // expired
483 // Change token to given bad token
484 kc.Arvados.ApiToken = token
486 // Ask should result in error
487 _, _, err = kc.Ask(hash)
489 errNotFound, _ := err.(keepclient.ErrNotFound)
490 c.Check(errNotFound.Temporary(), Equals, false)
491 c.Assert(strings.Contains(err.Error(), "HTTP 403"), Equals, true)
493 // Get should result in error
494 _, _, _, err = kc.Get(hash)
496 errNotFound, _ = err.(keepclient.ErrNotFound)
497 c.Check(errNotFound.Temporary(), Equals, false)
498 c.Assert(strings.Contains(err.Error(), "HTTP 403 \"Missing or invalid Authorization header\""), Equals, true)
502 func (s *ServerRequiredSuite) TestAskGetKeepProxyConnectionError(c *C) {
503 arv, err := arvadosclient.MakeArvadosClient()
504 c.Assert(err, Equals, nil)
506 // keepclient with no such keep server
507 kc := keepclient.New(arv)
508 locals := map[string]string{
509 TestProxyUUID: "http://localhost:12345",
511 kc.SetServiceRoots(locals, nil, nil)
513 // Ask should result in temporary connection refused error
514 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
515 _, _, err = kc.Ask(hash)
517 errNotFound, _ := err.(*keepclient.ErrNotFound)
518 c.Check(errNotFound.Temporary(), Equals, true)
519 c.Assert(strings.Contains(err.Error(), "connection refused"), Equals, true)
521 // Get should result in temporary connection refused error
522 _, _, _, err = kc.Get(hash)
524 errNotFound, _ = err.(*keepclient.ErrNotFound)
525 c.Check(errNotFound.Temporary(), Equals, true)
526 c.Assert(strings.Contains(err.Error(), "connection refused"), Equals, true)
529 func (s *NoKeepServerSuite) TestAskGetNoKeepServerError(c *C) {
530 kc := runProxy(c, nil, false)
531 defer closeListener()
533 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
534 for _, f := range []func() error{
536 _, _, err := kc.Ask(hash)
540 _, _, _, err := kc.Get(hash)
545 c.Assert(err, NotNil)
546 errNotFound, _ := err.(*keepclient.ErrNotFound)
547 c.Check(errNotFound.Temporary(), Equals, true)
548 c.Check(err, ErrorMatches, `.*HTTP 502.*`)