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/streamer"
20 // Gocheck boilerplate
21 func Test(t *testing.T) {
25 // Gocheck boilerplate
26 var _ = Suite(&ServerRequiredSuite{})
27 var _ = Suite(&StandaloneSuite{})
29 var no_server = flag.Bool("no-server", false, "Skip 'ServerRequireSuite'")
31 // Tests that require the Keep server running
32 type ServerRequiredSuite struct{}
35 type StandaloneSuite struct{}
37 func pythonDir() string {
39 return fmt.Sprintf("%s/../../python/tests", cwd)
42 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
44 c.Skip("Skipping tests that require server")
47 arvadostest.StartAPI()
48 arvadostest.StartKeep()
51 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
55 arvadostest.StopKeep()
59 func (s *ServerRequiredSuite) TestMakeKeepClient(c *C) {
60 arv, err := arvadosclient.MakeArvadosClient()
61 c.Assert(err, Equals, nil)
63 kc, err := MakeKeepClient(&arv)
65 c.Assert(err, Equals, nil)
66 c.Check(len(kc.ServiceRoots()), Equals, 2)
67 for _, root := range kc.ServiceRoots() {
68 c.Check(root, Matches, "http://localhost:\\d+")
72 type StubPutHandler struct {
80 func (this StubPutHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
81 this.c.Check(req.URL.Path, Equals, "/"+this.expectPath)
82 this.c.Check(req.Header.Get("Authorization"), Equals, fmt.Sprintf("OAuth2 %s", this.expectApiToken))
83 body, err := ioutil.ReadAll(req.Body)
84 this.c.Check(err, Equals, nil)
85 this.c.Check(body, DeepEquals, []byte(this.expectBody))
87 this.handled <- fmt.Sprintf("http://%s", req.Host)
90 func RunFakeKeepServer(st http.Handler) (ks KeepServer) {
92 ks.listener, err = net.ListenTCP("tcp", &net.TCPAddr{Port: 0})
94 panic(fmt.Sprintf("Could not listen on any port"))
96 ks.url = fmt.Sprintf("http://%s", ks.listener.Addr().String())
97 go http.Serve(ks.listener, st)
101 func UploadToStubHelper(c *C, st http.Handler, f func(KeepClient, string,
102 io.ReadCloser, io.WriteCloser, chan uploadStatus)) {
104 ks := RunFakeKeepServer(st)
105 defer ks.listener.Close()
107 arv, _ := arvadosclient.MakeArvadosClient()
108 arv.ApiToken = "abc123"
110 kc, _ := MakeKeepClient(&arv)
112 reader, writer := io.Pipe()
113 upload_status := make(chan uploadStatus)
115 f(kc, ks.url, reader, writer, upload_status)
118 func (s *StandaloneSuite) TestUploadToStubKeepServer(c *C) {
119 log.Printf("TestUploadToStubKeepServer")
121 st := StubPutHandler{
123 "acbd18db4cc2f85cedef654fccc4a4d8",
128 UploadToStubHelper(c, st,
129 func(kc KeepClient, url string, reader io.ReadCloser,
130 writer io.WriteCloser, upload_status chan uploadStatus) {
132 go kc.uploadToKeepServer(url, st.expectPath, reader, upload_status, int64(len("foo")), "TestUploadToStubKeepServer")
134 writer.Write([]byte("foo"))
138 status := <-upload_status
139 c.Check(status, DeepEquals, uploadStatus{nil, fmt.Sprintf("%s/%s", url, st.expectPath), 200, 1, ""})
142 log.Printf("TestUploadToStubKeepServer done")
145 func (s *StandaloneSuite) TestUploadToStubKeepServerBufferReader(c *C) {
146 log.Printf("TestUploadToStubKeepServerBufferReader")
148 st := StubPutHandler{
150 "acbd18db4cc2f85cedef654fccc4a4d8",
155 UploadToStubHelper(c, st,
156 func(kc KeepClient, url string, reader io.ReadCloser,
157 writer io.WriteCloser, upload_status chan uploadStatus) {
159 tr := streamer.AsyncStreamFromReader(512, reader)
162 br1 := tr.MakeStreamReader()
164 go kc.uploadToKeepServer(url, st.expectPath, br1, upload_status, 3, "TestUploadToStubKeepServerBufferReader")
166 writer.Write([]byte("foo"))
171 status := <-upload_status
172 c.Check(status, DeepEquals, uploadStatus{nil, fmt.Sprintf("%s/%s", url, st.expectPath), 200, 1, ""})
175 log.Printf("TestUploadToStubKeepServerBufferReader done")
178 type FailHandler struct {
182 func (this FailHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
183 resp.WriteHeader(500)
184 this.handled <- fmt.Sprintf("http://%s", req.Host)
187 func (s *StandaloneSuite) TestFailedUploadToStubKeepServer(c *C) {
188 log.Printf("TestFailedUploadToStubKeepServer")
193 hash := "acbd18db4cc2f85cedef654fccc4a4d8"
195 UploadToStubHelper(c, st,
196 func(kc KeepClient, url string, reader io.ReadCloser,
197 writer io.WriteCloser, upload_status chan uploadStatus) {
199 go kc.uploadToKeepServer(url, hash, reader, upload_status, 3, "TestFailedUploadToStubKeepServer")
201 writer.Write([]byte("foo"))
206 status := <-upload_status
207 c.Check(status.url, Equals, fmt.Sprintf("%s/%s", url, hash))
208 c.Check(status.statusCode, Equals, 500)
210 log.Printf("TestFailedUploadToStubKeepServer done")
213 type KeepServer struct {
214 listener net.Listener
218 func RunSomeFakeKeepServers(st http.Handler, n int) (ks []KeepServer) {
219 ks = make([]KeepServer, n)
221 for i := 0; i < n; i += 1 {
222 ks[i] = RunFakeKeepServer(st)
228 func (s *StandaloneSuite) TestPutB(c *C) {
229 log.Printf("TestPutB")
231 hash := Md5String("foo")
233 st := StubPutHandler{
238 make(chan string, 5)}
240 arv, _ := arvadosclient.MakeArvadosClient()
241 kc, _ := MakeKeepClient(&arv)
244 arv.ApiToken = "abc123"
245 service_roots := make(map[string]string)
247 ks := RunSomeFakeKeepServers(st, 5)
249 for i, k := range ks {
250 service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = k.url
251 defer k.listener.Close()
254 kc.SetServiceRoots(service_roots)
256 kc.PutB([]byte("foo"))
258 shuff := NewRootSorter(
259 kc.ServiceRoots(), Md5String("foo")).GetSortedRoots()
263 c.Check((s1 == shuff[0] && s2 == shuff[1]) ||
264 (s1 == shuff[1] && s2 == shuff[0]),
268 log.Printf("TestPutB done")
271 func (s *StandaloneSuite) TestPutHR(c *C) {
272 log.Printf("TestPutHR")
274 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
276 st := StubPutHandler{
281 make(chan string, 5)}
283 arv, _ := arvadosclient.MakeArvadosClient()
284 kc, _ := MakeKeepClient(&arv)
287 arv.ApiToken = "abc123"
288 service_roots := make(map[string]string)
290 ks := RunSomeFakeKeepServers(st, 5)
292 for i, k := range ks {
293 service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = k.url
294 defer k.listener.Close()
297 kc.SetServiceRoots(service_roots)
299 reader, writer := io.Pipe()
302 writer.Write([]byte("foo"))
306 kc.PutHR(hash, reader, 3)
308 shuff := NewRootSorter(kc.ServiceRoots(), hash).GetSortedRoots()
314 c.Check((s1 == shuff[0] && s2 == shuff[1]) ||
315 (s1 == shuff[1] && s2 == shuff[0]),
319 log.Printf("TestPutHR done")
322 func (s *StandaloneSuite) TestPutWithFail(c *C) {
323 log.Printf("TestPutWithFail")
325 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
327 st := StubPutHandler{
332 make(chan string, 4)}
335 make(chan string, 1)}
337 arv, err := arvadosclient.MakeArvadosClient()
338 kc, _ := MakeKeepClient(&arv)
341 arv.ApiToken = "abc123"
342 service_roots := make(map[string]string)
344 ks1 := RunSomeFakeKeepServers(st, 4)
345 ks2 := RunSomeFakeKeepServers(fh, 1)
347 for i, k := range ks1 {
348 service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = k.url
349 defer k.listener.Close()
351 for i, k := range ks2 {
352 service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i+len(ks1))] = k.url
353 defer k.listener.Close()
356 kc.SetServiceRoots(service_roots)
358 shuff := NewRootSorter(
359 kc.ServiceRoots(), Md5String("foo")).GetSortedRoots()
361 phash, replicas, err := kc.PutB([]byte("foo"))
365 c.Check(err, Equals, nil)
366 c.Check(phash, Equals, "")
367 c.Check(replicas, Equals, 2)
372 c.Check((s1 == shuff[1] && s2 == shuff[2]) ||
373 (s1 == shuff[2] && s2 == shuff[1]),
378 func (s *StandaloneSuite) TestPutWithTooManyFail(c *C) {
379 log.Printf("TestPutWithTooManyFail")
381 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
383 st := StubPutHandler{
388 make(chan string, 1)}
391 make(chan string, 4)}
393 arv, err := arvadosclient.MakeArvadosClient()
394 kc, _ := MakeKeepClient(&arv)
397 arv.ApiToken = "abc123"
398 service_roots := make(map[string]string)
400 ks1 := RunSomeFakeKeepServers(st, 1)
401 ks2 := RunSomeFakeKeepServers(fh, 4)
403 for i, k := range ks1 {
404 service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = k.url
405 defer k.listener.Close()
407 for i, k := range ks2 {
408 service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i+len(ks1))] = k.url
409 defer k.listener.Close()
412 kc.SetServiceRoots(service_roots)
414 _, replicas, err := kc.PutB([]byte("foo"))
416 c.Check(err, Equals, InsufficientReplicasError)
417 c.Check(replicas, Equals, 1)
418 c.Check(<-st.handled, Equals, ks1[0].url)
420 log.Printf("TestPutWithTooManyFail done")
423 type StubGetHandler struct {
426 expectApiToken string
430 func (this StubGetHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
431 this.c.Check(req.URL.Path, Equals, "/"+this.expectPath)
432 this.c.Check(req.Header.Get("Authorization"), Equals, fmt.Sprintf("OAuth2 %s", this.expectApiToken))
433 resp.Header().Set("Content-Length", fmt.Sprintf("%d", len(this.returnBody)))
434 resp.Write(this.returnBody)
437 func (s *StandaloneSuite) TestGet(c *C) {
438 log.Printf("TestGet")
440 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
442 st := StubGetHandler{
448 ks := RunFakeKeepServer(st)
449 defer ks.listener.Close()
451 arv, err := arvadosclient.MakeArvadosClient()
452 kc, _ := MakeKeepClient(&arv)
453 arv.ApiToken = "abc123"
454 kc.SetServiceRoots(map[string]string{"x": ks.url})
456 r, n, url2, err := kc.Get(hash)
458 c.Check(err, Equals, nil)
459 c.Check(n, Equals, int64(3))
460 c.Check(url2, Equals, fmt.Sprintf("%s/%s", ks.url, hash))
462 content, err2 := ioutil.ReadAll(r)
463 c.Check(err2, Equals, nil)
464 c.Check(content, DeepEquals, []byte("foo"))
466 log.Printf("TestGet done")
469 func (s *StandaloneSuite) TestGetFail(c *C) {
470 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
472 st := FailHandler{make(chan string, 1)}
474 ks := RunFakeKeepServer(st)
475 defer ks.listener.Close()
477 arv, err := arvadosclient.MakeArvadosClient()
478 kc, _ := MakeKeepClient(&arv)
479 arv.ApiToken = "abc123"
480 kc.SetServiceRoots(map[string]string{"x": ks.url})
482 r, n, url2, err := kc.Get(hash)
483 c.Check(err, Equals, BlockNotFound)
484 c.Check(n, Equals, int64(0))
485 c.Check(url2, Equals, "")
486 c.Check(r, Equals, nil)
489 type BarHandler struct {
493 func (this BarHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
494 resp.Write([]byte("bar"))
495 this.handled <- fmt.Sprintf("http://%s", req.Host)
498 func (s *StandaloneSuite) TestChecksum(c *C) {
499 foohash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
500 barhash := fmt.Sprintf("%x", md5.Sum([]byte("bar")))
502 st := BarHandler{make(chan string, 1)}
504 ks := RunFakeKeepServer(st)
505 defer ks.listener.Close()
507 arv, err := arvadosclient.MakeArvadosClient()
508 kc, _ := MakeKeepClient(&arv)
509 arv.ApiToken = "abc123"
510 kc.SetServiceRoots(map[string]string{"x": ks.url})
512 r, n, _, err := kc.Get(barhash)
513 _, err = ioutil.ReadAll(r)
514 c.Check(n, Equals, int64(3))
515 c.Check(err, Equals, nil)
519 r, n, _, err = kc.Get(foohash)
520 _, err = ioutil.ReadAll(r)
521 c.Check(n, Equals, int64(3))
522 c.Check(err, Equals, BadChecksum)
527 func (s *StandaloneSuite) TestGetWithFailures(c *C) {
528 content := []byte("waz")
529 hash := fmt.Sprintf("%x", md5.Sum(content))
532 make(chan string, 4)}
534 st := StubGetHandler{
540 arv, err := arvadosclient.MakeArvadosClient()
541 kc, _ := MakeKeepClient(&arv)
542 arv.ApiToken = "abc123"
543 service_roots := make(map[string]string)
545 ks1 := RunSomeFakeKeepServers(st, 1)
546 ks2 := RunSomeFakeKeepServers(fh, 4)
548 for i, k := range ks1 {
549 service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = k.url
550 defer k.listener.Close()
552 for i, k := range ks2 {
553 service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i+len(ks1))] = k.url
554 defer k.listener.Close()
557 kc.SetServiceRoots(service_roots)
559 // This test works only if one of the failing services is
560 // attempted before the succeeding service. Otherwise,
561 // <-fh.handled below will just hang! (Probe order depends on
562 // the choice of block content "waz" and the UUIDs of the fake
563 // servers, so we just tried different strings until we found
564 // an example that passes this Assert.)
565 c.Assert(NewRootSorter(service_roots, hash).GetSortedRoots()[0], Not(Equals), ks1[0].url)
567 r, n, url2, err := kc.Get(hash)
570 c.Check(err, Equals, nil)
571 c.Check(n, Equals, int64(3))
572 c.Check(url2, Equals, fmt.Sprintf("%s/%s", ks1[0].url, hash))
574 read_content, err2 := ioutil.ReadAll(r)
575 c.Check(err2, Equals, nil)
576 c.Check(read_content, DeepEquals, content)
579 func (s *ServerRequiredSuite) TestPutGetHead(c *C) {
580 content := []byte("TestPutGetHead")
582 arv, err := arvadosclient.MakeArvadosClient()
583 kc, err := MakeKeepClient(&arv)
584 c.Assert(err, Equals, nil)
586 hash := fmt.Sprintf("%x", md5.Sum(content))
589 n, _, err := kc.Ask(hash)
590 c.Check(err, Equals, BlockNotFound)
591 c.Check(n, Equals, int64(0))
594 hash2, replicas, err := kc.PutB(content)
595 c.Check(hash2, Equals, fmt.Sprintf("%s+%d", hash, len(content)))
596 c.Check(replicas, Equals, 2)
597 c.Check(err, Equals, nil)
600 r, n, url2, err := kc.Get(hash)
601 c.Check(err, Equals, nil)
602 c.Check(n, Equals, int64(len(content)))
603 c.Check(url2, Matches, fmt.Sprintf("http://localhost:\\d+/%s", hash))
605 read_content, err2 := ioutil.ReadAll(r)
606 c.Check(err2, Equals, nil)
607 c.Check(read_content, DeepEquals, content)
610 n, url2, err := kc.Ask(hash)
611 c.Check(err, Equals, nil)
612 c.Check(n, Equals, int64(len(content)))
613 c.Check(url2, Matches, fmt.Sprintf("http://localhost:\\d+/%s", hash))
617 type StubProxyHandler struct {
621 func (this StubProxyHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
622 resp.Header().Set("X-Keep-Replicas-Stored", "2")
623 this.handled <- fmt.Sprintf("http://%s", req.Host)
626 func (s *StandaloneSuite) TestPutProxy(c *C) {
627 log.Printf("TestPutProxy")
629 st := StubProxyHandler{make(chan string, 1)}
631 arv, err := arvadosclient.MakeArvadosClient()
632 kc, _ := MakeKeepClient(&arv)
635 kc.Using_proxy = true
636 arv.ApiToken = "abc123"
637 service_roots := make(map[string]string)
639 ks1 := RunSomeFakeKeepServers(st, 1)
641 for i, k := range ks1 {
642 service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = k.url
643 defer k.listener.Close()
646 kc.SetServiceRoots(service_roots)
648 _, replicas, err := kc.PutB([]byte("foo"))
651 c.Check(err, Equals, nil)
652 c.Check(replicas, Equals, 2)
654 log.Printf("TestPutProxy done")
657 func (s *StandaloneSuite) TestPutProxyInsufficientReplicas(c *C) {
658 log.Printf("TestPutProxy")
660 st := StubProxyHandler{make(chan string, 1)}
662 arv, err := arvadosclient.MakeArvadosClient()
663 kc, _ := MakeKeepClient(&arv)
666 kc.Using_proxy = true
667 arv.ApiToken = "abc123"
668 service_roots := make(map[string]string)
670 ks1 := RunSomeFakeKeepServers(st, 1)
672 for i, k := range ks1 {
673 service_roots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = k.url
674 defer k.listener.Close()
676 kc.SetServiceRoots(service_roots)
678 _, replicas, err := kc.PutB([]byte("foo"))
681 c.Check(err, Equals, InsufficientReplicasError)
682 c.Check(replicas, Equals, 2)
684 log.Printf("TestPutProxy done")
687 func (s *StandaloneSuite) TestMakeLocator(c *C) {
688 l := MakeLocator("91f372a266fe2bf2823cb8ec7fda31ce+3+Aabcde@12345678")
690 c.Check(l.Hash, Equals, "91f372a266fe2bf2823cb8ec7fda31ce")
691 c.Check(l.Size, Equals, 3)
692 c.Check(l.Signature, Equals, "abcde")
693 c.Check(l.Timestamp, Equals, "12345678")