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 kc.SetServiceRoots(map[string]string{
119 "proxy": fmt.Sprintf("http://localhost:%v", port),
121 c.Check(kc.Using_proxy, Equals, true)
122 c.Check(len(kc.LocalRoots()), Equals, 1)
123 for _, root := range kc.LocalRoots() {
124 c.Check(root, Equals, fmt.Sprintf("http://localhost:%v", port))
126 log.Print("keepclient created")
127 if bogusClientToken {
128 arvadostest.ResetEnv()
132 os.Args = append(args, fmt.Sprintf("-listen=:%v", port))
140 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
141 log.Print("TestPutAndGet start")
143 os.Args = []string{"keepproxy", "-listen=:29950"}
146 time.Sleep(100 * time.Millisecond)
150 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "true")
151 arv, err := arvadosclient.MakeArvadosClient()
152 c.Assert(err, Equals, nil)
153 kc, err := keepclient.MakeKeepClient(&arv)
154 c.Assert(err, Equals, nil)
155 c.Check(kc.Arvados.External, Equals, true)
156 c.Check(kc.Using_proxy, Equals, true)
157 c.Check(len(kc.LocalRoots()), Equals, 1)
158 for _, root := range kc.LocalRoots() {
159 c.Check(root, Equals, "http://localhost:29950")
161 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
164 defer closeListener()
166 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
170 _, _, err := kc.Ask(hash)
171 c.Check(err, Equals, keepclient.BlockNotFound)
172 log.Print("Finished Ask (expected BlockNotFound)")
176 reader, _, _, err := kc.Get(hash)
177 c.Check(reader, Equals, nil)
178 c.Check(err, Equals, keepclient.BlockNotFound)
179 log.Print("Finished Get (expected BlockNotFound)")
182 // Note in bug #5309 among other errors keepproxy would set
183 // Content-Length incorrectly on the 404 BlockNotFound response, this
184 // would result in a protocol violation that would prevent reuse of the
185 // connection, which would manifest by the next attempt to use the
186 // connection (in this case the PutB below) failing. So to test for
187 // that bug it's necessary to trigger an error response (such as
188 // BlockNotFound) and then do something else with the same httpClient
194 hash2, rep, err = kc.PutB([]byte("foo"))
195 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+3(\+.+)?$`, hash))
196 c.Check(rep, Equals, 2)
197 c.Check(err, Equals, nil)
198 log.Print("Finished PutB (expected success)")
202 blocklen, _, err := kc.Ask(hash2)
203 c.Assert(err, Equals, nil)
204 c.Check(blocklen, Equals, int64(3))
205 log.Print("Finished Ask (expected success)")
209 reader, blocklen, _, err := kc.Get(hash2)
210 c.Assert(err, Equals, nil)
211 all, err := ioutil.ReadAll(reader)
212 c.Check(all, DeepEquals, []byte("foo"))
213 c.Check(blocklen, Equals, int64(3))
214 log.Print("Finished Get (expected success)")
220 hash2, rep, err = kc.PutB([]byte(""))
221 c.Check(hash2, Matches, `^d41d8cd98f00b204e9800998ecf8427e\+0(\+.+)?$`)
222 c.Check(rep, Equals, 2)
223 c.Check(err, Equals, nil)
224 log.Print("Finished PutB zero block")
228 reader, blocklen, _, err := kc.Get("d41d8cd98f00b204e9800998ecf8427e")
229 c.Assert(err, Equals, nil)
230 all, err := ioutil.ReadAll(reader)
231 c.Check(all, DeepEquals, []byte(""))
232 c.Check(blocklen, Equals, int64(0))
233 log.Print("Finished Get zero block")
236 log.Print("TestPutAndGet done")
239 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
240 log.Print("TestPutAskGetForbidden start")
242 kc := runProxy(c, []string{"keepproxy"}, 29951, true)
244 defer closeListener()
246 hash := fmt.Sprintf("%x", md5.Sum([]byte("bar")))
249 _, _, err := kc.Ask(hash)
250 c.Check(err, Equals, keepclient.BlockNotFound)
255 hash2, rep, err := kc.PutB([]byte("bar"))
256 c.Check(hash2, Equals, "")
257 c.Check(rep, Equals, 0)
258 c.Check(err, Equals, keepclient.InsufficientReplicasError)
263 blocklen, _, err := kc.Ask(hash)
264 c.Assert(err, Equals, keepclient.BlockNotFound)
265 c.Check(blocklen, Equals, int64(0))
270 _, blocklen, _, err := kc.Get(hash)
271 c.Assert(err, Equals, keepclient.BlockNotFound)
272 c.Check(blocklen, Equals, int64(0))
276 log.Print("TestPutAskGetForbidden done")
279 func (s *ServerRequiredSuite) TestGetDisabled(c *C) {
280 log.Print("TestGetDisabled start")
282 kc := runProxy(c, []string{"keepproxy", "-no-get"}, 29952, false)
284 defer closeListener()
286 hash := fmt.Sprintf("%x", md5.Sum([]byte("baz")))
289 _, _, err := kc.Ask(hash)
290 c.Check(err, Equals, keepclient.BlockNotFound)
295 hash2, rep, err := kc.PutB([]byte("baz"))
296 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+3(\+.+)?$`, hash))
297 c.Check(rep, Equals, 2)
298 c.Check(err, Equals, nil)
303 blocklen, _, err := kc.Ask(hash)
304 c.Assert(err, Equals, keepclient.BlockNotFound)
305 c.Check(blocklen, Equals, int64(0))
310 _, blocklen, _, err := kc.Get(hash)
311 c.Assert(err, Equals, keepclient.BlockNotFound)
312 c.Check(blocklen, Equals, int64(0))
316 log.Print("TestGetDisabled done")
319 func (s *ServerRequiredSuite) TestPutDisabled(c *C) {
320 log.Print("TestPutDisabled start")
322 kc := runProxy(c, []string{"keepproxy", "-no-put"}, 29953, false)
324 defer closeListener()
327 hash2, rep, err := kc.PutB([]byte("quux"))
328 c.Check(hash2, Equals, "")
329 c.Check(rep, Equals, 0)
330 c.Check(err, Equals, keepclient.InsufficientReplicasError)
334 log.Print("TestPutDisabled done")
337 func (s *ServerRequiredSuite) TestCorsHeaders(c *C) {
338 runProxy(c, []string{"keepproxy"}, 29954, false)
340 defer closeListener()
343 client := http.Client{}
344 req, err := http.NewRequest("OPTIONS",
345 fmt.Sprintf("http://localhost:29954/%x+3",
346 md5.Sum([]byte("foo"))),
348 req.Header.Add("Access-Control-Request-Method", "PUT")
349 req.Header.Add("Access-Control-Request-Headers", "Authorization, X-Keep-Desired-Replicas")
350 resp, err := client.Do(req)
351 c.Check(err, Equals, nil)
352 c.Check(resp.StatusCode, Equals, 200)
353 body, err := ioutil.ReadAll(resp.Body)
354 c.Check(string(body), Equals, "")
355 c.Check(resp.Header.Get("Access-Control-Allow-Methods"), Equals, "GET, HEAD, POST, PUT, OPTIONS")
356 c.Check(resp.Header.Get("Access-Control-Allow-Origin"), Equals, "*")
360 resp, err := http.Get(
361 fmt.Sprintf("http://localhost:29954/%x+3",
362 md5.Sum([]byte("foo"))))
363 c.Check(err, Equals, nil)
364 c.Check(resp.Header.Get("Access-Control-Allow-Headers"), Equals, "Authorization, Content-Length, Content-Type, X-Keep-Desired-Replicas")
365 c.Check(resp.Header.Get("Access-Control-Allow-Origin"), Equals, "*")
369 func (s *ServerRequiredSuite) TestPostWithoutHash(c *C) {
370 runProxy(c, []string{"keepproxy"}, 29955, false)
372 defer closeListener()
375 client := http.Client{}
376 req, err := http.NewRequest("POST",
377 "http://localhost:29955/",
378 strings.NewReader("qux"))
379 req.Header.Add("Authorization", "OAuth2 4axaw8zxe0qm22wa6urpp5nskcne8z88cvbupv653y1njyi05h")
380 req.Header.Add("Content-Type", "application/octet-stream")
381 resp, err := client.Do(req)
382 c.Check(err, Equals, nil)
383 body, err := ioutil.ReadAll(resp.Body)
384 c.Check(err, Equals, nil)
385 c.Check(string(body), Equals,
386 fmt.Sprintf("%x+%d", md5.Sum([]byte("qux")), 3))