run_test_server doesn't assume ARVADOS_API_TOKEN is set.
[arvados.git] / sdk / go / keepclient / keepclient_test.go
index bab73643a25856d39db2f8416898ba1a52ba8df0..236d8dbfdab7e3940945d2bd8262db70d878b88e 100644 (file)
@@ -504,7 +504,7 @@ func (s *StandaloneSuite) TestGetWithServiceHint(c *C) {
        // This one should be used:
        ks := RunFakeKeepServer(StubGetHandler{
                c,
-               hash+"+K@"+uuid,
+               hash + "+K@" + uuid,
                "abc123",
                http.StatusOK,
                []byte("foo")})
@@ -517,7 +517,58 @@ func (s *StandaloneSuite) TestGetWithServiceHint(c *C) {
                map[string]string{"x": ks0.url},
                map[string]string{uuid: ks.url})
 
-       r, n, uri, err := kc.Get(hash+"+K@"+uuid)
+       r, n, uri, err := kc.Get(hash + "+K@" + uuid)
+       defer r.Close()
+       c.Check(err, Equals, nil)
+       c.Check(n, Equals, int64(3))
+       c.Check(uri, Equals, fmt.Sprintf("%s/%s", ks.url, hash+"+K@"+uuid))
+
+       content, err := ioutil.ReadAll(r)
+       c.Check(err, Equals, nil)
+       c.Check(content, DeepEquals, []byte("foo"))
+}
+
+// Use a service hint to fetch from a local disk service, overriding
+// rendezvous probe order.
+func (s *StandaloneSuite) TestGetWithLocalServiceHint(c *C) {
+       uuid := "zzzzz-bi6l4-zzzzzzzzzzzzzzz"
+       hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
+
+       // This one shouldn't be used, although it appears first in
+       // rendezvous probe order:
+       ks0 := RunFakeKeepServer(StubGetHandler{
+               c,
+               "error if used",
+               "abc123",
+               http.StatusOK,
+               []byte("foo")})
+       defer ks0.listener.Close()
+       // This one should be used:
+       ks := RunFakeKeepServer(StubGetHandler{
+               c,
+               hash + "+K@" + uuid,
+               "abc123",
+               http.StatusOK,
+               []byte("foo")})
+       defer ks.listener.Close()
+
+       arv, err := arvadosclient.MakeArvadosClient()
+       kc, _ := MakeKeepClient(&arv)
+       arv.ApiToken = "abc123"
+       kc.SetServiceRoots(
+               map[string]string{
+                       "zzzzz-bi6l4-yyyyyyyyyyyyyyy": ks0.url,
+                       "zzzzz-bi6l4-xxxxxxxxxxxxxxx": ks0.url,
+                       "zzzzz-bi6l4-wwwwwwwwwwwwwww": ks0.url,
+                       uuid: ks.url},
+               map[string]string{
+                       "zzzzz-bi6l4-yyyyyyyyyyyyyyy": ks0.url,
+                       "zzzzz-bi6l4-xxxxxxxxxxxxxxx": ks0.url,
+                       "zzzzz-bi6l4-wwwwwwwwwwwwwww": ks0.url,
+                       uuid: ks.url},
+       )
+
+       r, n, uri, err := kc.Get(hash + "+K@" + uuid)
        defer r.Close()
        c.Check(err, Equals, nil)
        c.Check(n, Equals, int64(3))
@@ -534,14 +585,14 @@ func (s *StandaloneSuite) TestGetWithServiceHintFailoverToLocals(c *C) {
 
        ksLocal := RunFakeKeepServer(StubGetHandler{
                c,
-               hash+"+K@"+uuid,
+               hash + "+K@" + uuid,
                "abc123",
                http.StatusOK,
                []byte("foo")})
        defer ksLocal.listener.Close()
        ksGateway := RunFakeKeepServer(StubGetHandler{
                c,
-               hash+"+K@"+uuid,
+               hash + "+K@" + uuid,
                "abc123",
                http.StatusInternalServerError,
                []byte("Error")})
@@ -554,7 +605,7 @@ func (s *StandaloneSuite) TestGetWithServiceHintFailoverToLocals(c *C) {
                map[string]string{"zzzzz-bi6l4-keepdisk0000000": ksLocal.url},
                map[string]string{uuid: ksGateway.url})
 
-       r, n, uri, err := kc.Get(hash+"+K@"+uuid)
+       r, n, uri, err := kc.Get(hash + "+K@" + uuid)
        c.Assert(err, Equals, nil)
        defer r.Close()
        c.Check(n, Equals, int64(3))
@@ -788,6 +839,16 @@ func (s *StandaloneSuite) TestMakeLocatorNoSizeHint(c *C) {
        c.Check(l.Hints, DeepEquals, []string{"Aabcde@12345678"})
 }
 
+func (s *StandaloneSuite) TestMakeLocatorPreservesUnrecognizedHints(c *C) {
+       str := "91f372a266fe2bf2823cb8ec7fda31ce+3+Unknown+Kzzzzz+Afoobar"
+       l, err := MakeLocator(str)
+       c.Check(err, Equals, nil)
+       c.Check(l.Hash, Equals, "91f372a266fe2bf2823cb8ec7fda31ce")
+       c.Check(l.Size, Equals, 3)
+       c.Check(l.Hints, DeepEquals, []string{"3", "Unknown", "Kzzzzz", "Afoobar"})
+       c.Check(l.String(), Equals, str)
+}
+
 func (s *StandaloneSuite) TestMakeLocatorInvalidInput(c *C) {
        _, err := MakeLocator("91f372a266fe2bf2823cb8ec7fda31c")
        c.Check(err, Equals, InvalidLocatorError)