4 "git.curoverse.com/arvados.git/sdk/go/keepclient"
5 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
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 func pythonDir() string {
35 return fmt.Sprintf("%s/../../sdk/python/tests", cwd)
38 // Wait (up to 1 second) for keepproxy to listen on a port. This
39 // avoids a race condition where we hit a "connection refused" error
40 // because we start testing the proxy too soon.
41 func waitForListener() {
43 for i := 0; listener == nil && i < 1000; i += ms {
44 time.Sleep(ms * time.Millisecond)
47 log.Fatalf("Timed out waiting for listener to start")
51 func closeListener() {
57 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
63 cmd := exec.Command("python", "run_test_server.py", "start")
64 stderr, err := cmd.StderrPipe()
66 log.Fatalf("Setting up stderr pipe: %s", err)
68 go io.Copy(os.Stderr, stderr)
69 if err := cmd.Run(); err != nil {
70 panic(fmt.Sprintf("'python run_test_server.py start' returned error %s", err))
74 cmd := exec.Command("python", "run_test_server.py", "start_keep")
75 stderr, err := cmd.StderrPipe()
77 log.Fatalf("Setting up stderr pipe: %s", err)
79 go io.Copy(os.Stderr, stderr)
80 if err := cmd.Run(); err != nil {
81 panic(fmt.Sprintf("'python run_test_server.py start_keep' returned error %s", err))
85 os.Setenv("ARVADOS_API_HOST", "localhost:3000")
86 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
87 os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
90 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
95 exec.Command("python", "run_test_server.py", "stop_keep").Run()
96 exec.Command("python", "run_test_server.py", "stop").Run()
99 func setupProxyService() {
101 client := &http.Client{Transport: &http.Transport{
102 TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
104 var req *http.Request
106 if req, err = http.NewRequest("POST", fmt.Sprintf("https://%s/arvados/v1/keep_services", os.Getenv("ARVADOS_API_HOST")), nil); err != nil {
109 req.Header.Add("Authorization", fmt.Sprintf("OAuth2 %s", os.Getenv("ARVADOS_API_TOKEN")))
111 reader, writer := io.Pipe()
117 data.Set("keep_service", `{
118 "service_host": "localhost",
119 "service_port": 29950,
120 "service_ssl_flag": false,
121 "service_type": "proxy"
124 writer.Write([]byte(data.Encode()))
128 var resp *http.Response
129 if resp, err = client.Do(req); err != nil {
132 if resp.StatusCode != 200 {
137 func runProxy(c *C, args []string, token string, port int) keepclient.KeepClient {
138 os.Args = append(args, fmt.Sprintf("-listen=:%v", port))
139 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
142 time.Sleep(100 * time.Millisecond)
144 os.Setenv("ARVADOS_KEEP_PROXY", fmt.Sprintf("http://localhost:%v", port))
145 os.Setenv("ARVADOS_API_TOKEN", token)
146 arv, err := arvadosclient.MakeArvadosClient()
147 c.Assert(err, Equals, nil)
148 kc, err := keepclient.MakeKeepClient(&arv)
149 c.Assert(err, Equals, nil)
150 c.Check(kc.Using_proxy, Equals, true)
151 c.Check(len(kc.ServiceRoots()), Equals, 1)
152 for _, root := range(kc.ServiceRoots()) {
153 c.Check(root, Equals, fmt.Sprintf("http://localhost:%v", port))
155 os.Setenv("ARVADOS_KEEP_PROXY", "")
156 log.Print("keepclient created")
160 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
161 log.Print("TestPutAndGet start")
163 os.Args = []string{"keepproxy", "-listen=:29950"}
164 os.Setenv("ARVADOS_API_TOKEN", "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
166 time.Sleep(100 * time.Millisecond)
170 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
171 arv, err := arvadosclient.MakeArvadosClient()
172 c.Assert(err, Equals, nil)
173 kc, err := keepclient.MakeKeepClient(&arv)
174 c.Assert(err, Equals, nil)
175 c.Check(kc.Arvados.External, Equals, true)
176 c.Check(kc.Using_proxy, Equals, true)
177 c.Check(len(kc.ServiceRoots()), Equals, 1)
178 for _, root := range kc.ServiceRoots() {
179 c.Check(root, Equals, "http://localhost:29950")
181 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
182 log.Print("keepclient created")
185 defer closeListener()
187 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
191 _, _, err := kc.Ask(hash)
192 c.Check(err, Equals, keepclient.BlockNotFound)
199 hash2, rep, err = kc.PutB([]byte("foo"))
200 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+3(\+.+)?$`, hash))
201 c.Check(rep, Equals, 2)
202 c.Check(err, Equals, nil)
207 blocklen, _, err := kc.Ask(hash2)
208 c.Assert(err, Equals, nil)
209 c.Check(blocklen, Equals, int64(3))
214 reader, blocklen, _, err := kc.Get(hash2)
215 c.Assert(err, Equals, nil)
216 all, err := ioutil.ReadAll(reader)
217 c.Check(all, DeepEquals, []byte("foo"))
218 c.Check(blocklen, Equals, int64(3))
222 log.Print("TestPutAndGet done")
225 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
226 log.Print("TestPutAskGetForbidden start")
228 kc := runProxy(c, []string{"keepproxy"}, "123abc", 29951)
230 defer closeListener()
232 log.Print("keepclient created")
234 hash := fmt.Sprintf("%x", md5.Sum([]byte("bar")))
237 _, _, err := kc.Ask(hash)
238 c.Check(err, Equals, keepclient.BlockNotFound)
243 hash2, rep, err := kc.PutB([]byte("bar"))
244 c.Check(hash2, Equals, "")
245 c.Check(rep, Equals, 0)
246 c.Check(err, Equals, keepclient.InsufficientReplicasError)
251 blocklen, _, err := kc.Ask(hash)
252 c.Assert(err, Equals, keepclient.BlockNotFound)
253 c.Check(blocklen, Equals, int64(0))
258 _, blocklen, _, err := kc.Get(hash)
259 c.Assert(err, Equals, keepclient.BlockNotFound)
260 c.Check(blocklen, Equals, int64(0))
264 log.Print("TestPutAskGetForbidden done")
267 func (s *ServerRequiredSuite) TestGetDisabled(c *C) {
268 log.Print("TestGetDisabled start")
270 kc := runProxy(c, []string{"keepproxy", "-no-get"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29952)
272 defer closeListener()
274 hash := fmt.Sprintf("%x", md5.Sum([]byte("baz")))
277 _, _, err := kc.Ask(hash)
278 c.Check(err, Equals, keepclient.BlockNotFound)
283 hash2, rep, err := kc.PutB([]byte("baz"))
284 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+3(\+.+)?$`, hash))
285 c.Check(rep, Equals, 2)
286 c.Check(err, Equals, nil)
291 blocklen, _, err := kc.Ask(hash)
292 c.Assert(err, Equals, keepclient.BlockNotFound)
293 c.Check(blocklen, Equals, int64(0))
298 _, blocklen, _, err := kc.Get(hash)
299 c.Assert(err, Equals, keepclient.BlockNotFound)
300 c.Check(blocklen, Equals, int64(0))
304 log.Print("TestGetDisabled done")
307 func (s *ServerRequiredSuite) TestPutDisabled(c *C) {
308 log.Print("TestPutDisabled start")
310 kc := runProxy(c, []string{"keepproxy", "-no-put"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29953)
312 defer closeListener()
315 hash2, rep, err := kc.PutB([]byte("quux"))
316 c.Check(hash2, Equals, "")
317 c.Check(rep, Equals, 0)
318 c.Check(err, Equals, keepclient.InsufficientReplicasError)
322 log.Print("TestPutDisabled done")
325 func (s *ServerRequiredSuite) TestCorsHeaders(c *C) {
326 runProxy(c, []string{"keepproxy"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29954)
328 defer closeListener()
331 client := http.Client{}
332 req, err := http.NewRequest("OPTIONS",
333 fmt.Sprintf("http://localhost:29954/%x+3",
334 md5.Sum([]byte("foo"))),
336 req.Header.Add("Access-Control-Request-Method", "PUT")
337 req.Header.Add("Access-Control-Request-Headers", "Authorization, X-Keep-Desired-Replicas")
338 resp, err := client.Do(req)
339 c.Check(err, Equals, nil)
340 c.Check(resp.StatusCode, Equals, 200)
341 body, err := ioutil.ReadAll(resp.Body)
342 c.Check(string(body), Equals, "")
343 c.Check(resp.Header.Get("Access-Control-Allow-Methods"), Equals, "GET, HEAD, POST, PUT, OPTIONS")
344 c.Check(resp.Header.Get("Access-Control-Allow-Origin"), Equals, "*")
348 resp, err := http.Get(
349 fmt.Sprintf("http://localhost:29954/%x+3",
350 md5.Sum([]byte("foo"))))
351 c.Check(err, Equals, nil)
352 c.Check(resp.Header.Get("Access-Control-Allow-Headers"), Equals, "Authorization, Content-Length, Content-Type, X-Keep-Desired-Replicas")
353 c.Check(resp.Header.Get("Access-Control-Allow-Origin"), Equals, "*")
357 func (s *ServerRequiredSuite) TestPostWithoutHash(c *C) {
358 runProxy(c, []string{"keepproxy"}, "4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h", 29955)
360 defer closeListener()
363 client := http.Client{}
364 req, err := http.NewRequest("POST",
365 "http://localhost:29955/",
366 strings.NewReader("qux"))
367 req.Header.Add("Authorization", "OAuth2 4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
368 req.Header.Add("Content-Type", "application/octet-stream")
369 resp, err := client.Do(req)
370 c.Check(err, Equals, nil)
371 body, err := ioutil.ReadAll(resp.Body)
372 c.Check(err, Equals, nil)
373 c.Check(string(body), Equals,
374 fmt.Sprintf("%x+%d", md5.Sum([]byte("qux")), 3))