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 // Wait (up to 1 second) for keepproxy to listen on a port. This
34 // avoids a race condition where we hit a "connection refused" error
35 // because we start testing the proxy too soon.
36 func waitForListener() {
40 for i := 0; listener == nil && i < 1000; i += ms {
41 time.Sleep(ms * time.Millisecond)
44 log.Fatalf("Timed out waiting for listener to start")
48 func closeListener() {
54 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
55 arvadostest.StartAPI()
56 arvadostest.StartKeep()
59 func (s *ServerRequiredSuite) SetUpTest(c *C) {
60 arvadostest.ResetEnv()
63 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
64 arvadostest.StopKeep()
68 func setupProxyService() {
70 client := &http.Client{Transport: &http.Transport{
71 TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
75 if req, err = http.NewRequest("POST", fmt.Sprintf("https://%s/arvados/v1/keep_services", os.Getenv("ARVADOS_API_HOST")), nil); err != nil {
78 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", os.Getenv("ARVADOS_API_TOKEN")))
80 reader, writer := io.Pipe()
86 data.Set("keep_service", `{
87 "service_host": "localhost",
88 "service_port": 29950,
89 "service_ssl_flag": false,
90 "service_type": "proxy"
93 writer.Write([]byte(data.Encode()))
97 var resp *http.Response
98 if resp, err = client.Do(req); err != nil {
101 if resp.StatusCode != 200 {
106 func runProxy(c *C, args []string, port int, bogusClientToken bool) keepclient.KeepClient {
107 if bogusClientToken {
108 os.Setenv("ARVADOS_API_TOKEN", "bogus-token")
110 arv, err := arvadosclient.MakeArvadosClient()
111 c.Assert(err, Equals, nil)
112 kc := keepclient.KeepClient{
116 Client: &http.Client{},
118 locals := map[string]string{
119 "proxy": fmt.Sprintf("http://localhost:%v", port),
121 writableLocals := map[string]string{
122 "proxy": fmt.Sprintf("http://localhost:%v", port),
124 kc.SetServiceRoots(locals, writableLocals, nil)
125 c.Check(kc.Using_proxy, Equals, true)
126 c.Check(len(kc.LocalRoots()), Equals, 1)
127 for _, root := range kc.LocalRoots() {
128 c.Check(root, Equals, fmt.Sprintf("http://localhost:%v", port))
130 log.Print("keepclient created")
131 if bogusClientToken {
132 arvadostest.ResetEnv()
136 os.Args = append(args, fmt.Sprintf("-listen=:%v", port))
144 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
145 log.Print("TestPutAndGet start")
147 os.Args = []string{"keepproxy", "-listen=:29950"}
150 time.Sleep(100 * time.Millisecond)
154 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
155 arv, err := arvadosclient.MakeArvadosClient()
156 c.Assert(err, Equals, nil)
157 kc, err := keepclient.MakeKeepClient(&arv)
158 c.Assert(err, Equals, nil)
159 c.Check(kc.Arvados.External, Equals, true)
160 c.Check(kc.Using_proxy, Equals, true)
161 c.Check(len(kc.LocalRoots()), Equals, 1)
162 for _, root := range kc.LocalRoots() {
163 c.Check(root, Equals, "http://localhost:29950")
165 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
168 defer closeListener()
170 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
174 _, _, err := kc.Ask(hash)
175 c.Check(err, Equals, keepclient.BlockNotFound)
176 log.Print("Finished Ask (expected BlockNotFound)")
180 reader, _, _, err := kc.Get(hash)
181 c.Check(reader, Equals, nil)
182 c.Check(err, Equals, keepclient.BlockNotFound)
183 log.Print("Finished Get (expected BlockNotFound)")
186 // Note in bug #5309 among other errors keepproxy would set
187 // Content-Length incorrectly on the 404 BlockNotFound response, this
188 // would result in a protocol violation that would prevent reuse of the
189 // connection, which would manifest by the next attempt to use the
190 // connection (in this case the PutB below) failing. So to test for
191 // that bug it's necessary to trigger an error response (such as
192 // BlockNotFound) and then do something else with the same httpClient
198 hash2, rep, err = kc.PutB([]byte("foo"))
199 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+3(\+.+)?$`, hash))
200 c.Check(rep, Equals, 2)
201 c.Check(err, Equals, nil)
202 log.Print("Finished PutB (expected success)")
206 blocklen, _, err := kc.Ask(hash2)
207 c.Assert(err, Equals, nil)
208 c.Check(blocklen, Equals, int64(3))
209 log.Print("Finished Ask (expected success)")
213 reader, blocklen, _, err := kc.Get(hash2)
214 c.Assert(err, Equals, nil)
215 all, err := ioutil.ReadAll(reader)
216 c.Check(all, DeepEquals, []byte("foo"))
217 c.Check(blocklen, Equals, int64(3))
218 log.Print("Finished Get (expected success)")
224 hash2, rep, err = kc.PutB([]byte(""))
225 c.Check(hash2, Matches, `^d41d8cd98f00b204e9800998ecf8427e\+0(\+.+)?$`)
226 c.Check(rep, Equals, 2)
227 c.Check(err, Equals, nil)
228 log.Print("Finished PutB zero block")
232 reader, blocklen, _, err := kc.Get("d41d8cd98f00b204e9800998ecf8427e")
233 c.Assert(err, Equals, nil)
234 all, err := ioutil.ReadAll(reader)
235 c.Check(all, DeepEquals, []byte(""))
236 c.Check(blocklen, Equals, int64(0))
237 log.Print("Finished Get zero block")
240 log.Print("TestPutAndGet done")
243 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
244 log.Print("TestPutAskGetForbidden start")
246 kc := runProxy(c, []string{"keepproxy"}, 29951, true)
248 defer closeListener()
250 hash := fmt.Sprintf("%x", md5.Sum([]byte("bar")))
253 _, _, err := kc.Ask(hash)
254 c.Check(err, Equals, keepclient.BlockNotFound)
259 hash2, rep, err := kc.PutB([]byte("bar"))
260 c.Check(hash2, Equals, "")
261 c.Check(rep, Equals, 0)
262 c.Check(err, Equals, keepclient.InsufficientReplicasError)
267 blocklen, _, err := kc.Ask(hash)
268 c.Assert(err, Equals, keepclient.BlockNotFound)
269 c.Check(blocklen, Equals, int64(0))
274 _, blocklen, _, err := kc.Get(hash)
275 c.Assert(err, Equals, keepclient.BlockNotFound)
276 c.Check(blocklen, Equals, int64(0))
280 log.Print("TestPutAskGetForbidden done")
283 func (s *ServerRequiredSuite) TestGetDisabled(c *C) {
284 log.Print("TestGetDisabled start")
286 kc := runProxy(c, []string{"keepproxy", "-no-get"}, 29952, false)
288 defer closeListener()
290 hash := fmt.Sprintf("%x", md5.Sum([]byte("baz")))
293 _, _, err := kc.Ask(hash)
294 c.Check(err, Equals, keepclient.BlockNotFound)
299 hash2, rep, err := kc.PutB([]byte("baz"))
300 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+3(\+.+)?$`, hash))
301 c.Check(rep, Equals, 2)
302 c.Check(err, Equals, nil)
307 blocklen, _, err := kc.Ask(hash)
308 c.Assert(err, Equals, keepclient.BlockNotFound)
309 c.Check(blocklen, Equals, int64(0))
314 _, blocklen, _, err := kc.Get(hash)
315 c.Assert(err, Equals, keepclient.BlockNotFound)
316 c.Check(blocklen, Equals, int64(0))
320 log.Print("TestGetDisabled done")
323 func (s *ServerRequiredSuite) TestPutDisabled(c *C) {
324 log.Print("TestPutDisabled start")
326 kc := runProxy(c, []string{"keepproxy", "-no-put"}, 29953, false)
328 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)
338 log.Print("TestPutDisabled done")
341 func (s *ServerRequiredSuite) TestCorsHeaders(c *C) {
342 runProxy(c, []string{"keepproxy"}, 29954, false)
344 defer closeListener()
347 client := http.Client{}
348 req, err := http.NewRequest("OPTIONS",
349 fmt.Sprintf("http://localhost:29954/%x+3",
350 md5.Sum([]byte("foo"))),
352 req.Header.Add("Access-Control-Request-Method", "PUT")
353 req.Header.Add("Access-Control-Request-Headers", "Authorization, X-Keep-Desired-Replicas")
354 resp, err := client.Do(req)
355 c.Check(err, Equals, nil)
356 c.Check(resp.StatusCode, Equals, 200)
357 body, err := ioutil.ReadAll(resp.Body)
358 c.Check(string(body), Equals, "")
359 c.Check(resp.Header.Get("Access-Control-Allow-Methods"), Equals, "GET, HEAD, POST, PUT, OPTIONS")
360 c.Check(resp.Header.Get("Access-Control-Allow-Origin"), Equals, "*")
364 resp, err := http.Get(
365 fmt.Sprintf("http://localhost:29954/%x+3",
366 md5.Sum([]byte("foo"))))
367 c.Check(err, Equals, nil)
368 c.Check(resp.Header.Get("Access-Control-Allow-Headers"), Equals, "Authorization, Content-Length, Content-Type, X-Keep-Desired-Replicas")
369 c.Check(resp.Header.Get("Access-Control-Allow-Origin"), Equals, "*")
373 func (s *ServerRequiredSuite) TestPostWithoutHash(c *C) {
374 runProxy(c, []string{"keepproxy"}, 29955, false)
376 defer closeListener()
379 client := http.Client{}
380 req, err := http.NewRequest("POST",
381 "http://localhost:29955/",
382 strings.NewReader("qux"))
383 req.Header.Add("Authorization", "OAuth2 4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
384 req.Header.Add("Content-Type", "application/octet-stream")
385 resp, err := client.Do(req)
386 c.Check(err, Equals, nil)
387 body, err := ioutil.ReadAll(resp.Body)
388 c.Check(err, Equals, nil)
389 c.Check(string(body), Matches,
390 fmt.Sprintf(`^%x\+3(\+.+)?$`, md5.Sum([]byte("qux"))))
394 func (s *ServerRequiredSuite) TestStripHint(c *C) {
395 c.Check(removeHint.ReplaceAllString("http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73+K@zzzzz", "$1"),
397 "http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73")
398 c.Check(removeHint.ReplaceAllString("http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+K@zzzzz+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73", "$1"),
400 "http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73")
401 c.Check(removeHint.ReplaceAllString("http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73+K@zzzzz-zzzzz-zzzzzzzzzzzzzzz", "$1"),
403 "http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73+K@zzzzz-zzzzz-zzzzzzzzzzzzzzz")
404 c.Check(removeHint.ReplaceAllString("http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+K@zzzzz-zzzzz-zzzzzzzzzzzzzzz+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73", "$1"),
406 "http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+K@zzzzz-zzzzz-zzzzzzzzzzzzzzz+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73")
411 // Put one block, with 2 replicas
412 // With no prefix (expect the block locator, twice)
413 // With an existing prefix (expect the block locator, twice)
414 // With a valid but non-existing prefix (expect "\n")
415 // With an invalid prefix (expect error)
416 func (s *ServerRequiredSuite) TestGetIndex(c *C) {
417 kc := runProxy(c, []string{"keepproxy"}, 28852, false)
419 defer closeListener()
421 // Put "index-data" blocks
422 data := []byte("index-data")
423 hash := fmt.Sprintf("%x", md5.Sum(data))
425 hash2, rep, err := kc.PutB(data)
426 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+10(\+.+)?$`, hash))
427 c.Check(rep, Equals, 2)
428 c.Check(err, Equals, nil)
430 reader, blocklen, _, err := kc.Get(hash)
431 c.Assert(err, Equals, nil)
432 c.Check(blocklen, Equals, int64(10))
433 all, err := ioutil.ReadAll(reader)
434 c.Check(all, DeepEquals, data)
436 // Put some more blocks
437 _, rep, err = kc.PutB([]byte("some-more-index-data"))
438 c.Check(err, Equals, nil)
441 for _, spec := range []struct {
446 {"", true, true}, // with no prefix
447 {hash[:3], true, false}, // with matching prefix
448 {"abcdef", false, false}, // with no such prefix
450 indexReader, err := kc.GetIndex("proxy", spec.prefix)
451 c.Assert(err, Equals, nil)
452 indexResp, err := ioutil.ReadAll(indexReader)
453 c.Assert(err, Equals, nil)
454 locators := strings.Split(string(indexResp), "\n")
457 for _, locator := range locators {
461 c.Check(locator[:len(spec.prefix)], Equals, spec.prefix)
462 if locator[:32] == hash {
468 c.Check(gotTestHash == 2, Equals, spec.expectTestHash)
469 c.Check(gotOther > 0, Equals, spec.expectOther)
472 // GetIndex with invalid prefix
473 _, err = kc.GetIndex("proxy", "xyz")
474 c.Assert((err != nil), Equals, true)