+func SetupProxyService() {
+
+ client := &http.Client{Transport: &http.Transport{
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
+
+ var req *http.Request
+ var err error
+ if req, err = http.NewRequest("POST", fmt.Sprintf("https://%s/arvados/v1/keep_services", os.Getenv("ARVADOS_API_HOST")), nil); err != nil {
+ panic(err.Error())
+ }
+ req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", os.Getenv("ARVADOS_API_TOKEN")))
+
+ reader, writer := io.Pipe()
+
+ req.Body = reader
+
+ go func() {
+ data := url.Values{}
+ data.Set("keep_service", `{
+ "service_host": "localhost",
+ "service_port": 29950,
+ "service_ssl_flag": false,
+ "service_type": "proxy"
+}`)
+
+ writer.Write([]byte(data.Encode()))
+ writer.Close()
+ }()
+
+ var resp *http.Response
+ if resp, err = client.Do(req); err != nil {
+ panic(err.Error())
+ }
+ if resp.StatusCode != 200 {
+ panic(resp.Status)
+ }
+}
+
+func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
+ log.Print("TestPutAndGet start")
+
+ os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
+ kc, err := keepclient.MakeKeepClient()
+ c.Check(kc.External, Equals, true)
+ c.Check(kc.Using_proxy, Equals, true)
+ c.Check(len(kc.ServiceRoots()), Equals, 1)
+ c.Check(kc.ServiceRoots()[0], Equals, "http://localhost:29950")
+ c.Check(err, Equals, nil)
+ os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
+
+ log.Print("keepclient created")
+
+ hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
+
+ {
+ _, _, err := kc.Ask(hash)
+ c.Check(err, Equals, keepclient.BlockNotFound)
+ log.Print("Ask 1")
+ }
+
+ {
+ hash2, rep, err := kc.PutB([]byte("foo"))
+ c.Check(hash2, Equals, hash)
+ c.Check(rep, Equals, 2)
+ c.Check(err, Equals, nil)
+ log.Print("PutB")
+ }
+
+ {
+ blocklen, _, err := kc.Ask(hash)
+ c.Assert(err, Equals, nil)
+ c.Check(blocklen, Equals, int64(3))
+ log.Print("Ask 2")
+ }
+
+ {
+ reader, blocklen, _, err := kc.Get(hash)
+ c.Assert(err, Equals, nil)
+ all, err := ioutil.ReadAll(reader)
+ c.Check(all, DeepEquals, []byte("foo"))
+ c.Check(blocklen, Equals, int64(3))
+ log.Print("Get")
+ }
+
+ log.Print("TestPutAndGet done")
+}
+
+func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
+ log.Print("TestPutAndGet start")
+
+ os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
+ kc, err := keepclient.MakeKeepClient()
+ kc.ApiToken = "123xyz"
+ c.Check(kc.External, Equals, true)
+ c.Check(kc.Using_proxy, Equals, true)
+ c.Check(len(kc.ServiceRoots()), Equals, 1)
+ c.Check(kc.ServiceRoots()[0], Equals, "http://localhost:29950")
+ c.Check(err, Equals, nil)
+ os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
+
+ log.Print("keepclient created")
+
+ hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
+
+ {
+ _, _, err := kc.Ask(hash)
+ c.Check(err, Equals, keepclient.BlockNotFound)
+ log.Print("Ask 1")
+ }
+
+ {
+ hash2, rep, err := kc.PutB([]byte("foo"))
+ c.Check(hash2, Equals, hash)
+ c.Check(rep, Equals, 0)
+ c.Check(err, Equals, keepclient.InsufficientReplicasError)
+ log.Print("PutB")
+ }
+
+ {
+ blocklen, _, err := kc.Ask(hash)
+ c.Assert(err, Equals, keepclient.BlockNotFound)
+ c.Check(blocklen, Equals, int64(0))
+ log.Print("Ask 2")
+ }
+
+ {
+ _, blocklen, _, err := kc.Get(hash)
+ c.Assert(err, Equals, keepclient.BlockNotFound)
+ c.Check(blocklen, Equals, int64(0))
+ log.Print("Get")
+ }
+
+ log.Print("TestPutAndGetForbidden done")