1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.arvados.org/arvados.git/lib/controller/rpc"
20 "git.arvados.org/arvados.git/sdk/go/arvados"
21 "git.arvados.org/arvados.git/sdk/go/arvadostest"
22 "github.com/gorilla/mux"
23 check "gopkg.in/check.v1"
26 // Gocheck boilerplate
27 func Test(t *testing.T) {
31 var _ = check.Suite(&RouterSuite{})
33 type RouterSuite struct {
35 stub arvadostest.APIStub
38 func (s *RouterSuite) SetUpTest(c *check.C) {
39 s.stub = arvadostest.APIStub{}
47 func (s *RouterSuite) TestOptions(c *check.C) {
48 token := arvadostest.ActiveToken
49 for _, trial := range []struct {
50 comment string // unparsed -- only used to help match test failures to trials
55 shouldStatus int // zero value means 200
57 withOptions interface{}
61 path: "/arvados/v1/collections/" + arvadostest.FooCollection,
62 shouldCall: "CollectionGet",
63 withOptions: arvados.GetOptions{UUID: arvadostest.FooCollection},
67 path: "/arvados/v1/collections/" + arvadostest.FooCollection,
68 shouldCall: "CollectionUpdate",
69 withOptions: arvados.UpdateOptions{UUID: arvadostest.FooCollection},
73 path: "/arvados/v1/collections/" + arvadostest.FooCollection,
74 shouldCall: "CollectionUpdate",
75 withOptions: arvados.UpdateOptions{UUID: arvadostest.FooCollection},
79 path: "/arvados/v1/collections/" + arvadostest.FooCollection,
80 shouldCall: "CollectionDelete",
81 withOptions: arvados.DeleteOptions{UUID: arvadostest.FooCollection},
85 path: "/arvados/v1/collections",
86 shouldCall: "CollectionCreate",
87 withOptions: arvados.CreateOptions{},
91 path: "/arvados/v1/collections",
92 shouldCall: "CollectionList",
93 withOptions: arvados.ListOptions{Limit: -1},
97 path: "/arvados/v1/api_client_authorizations",
98 shouldCall: "APIClientAuthorizationList",
99 withOptions: arvados.ListOptions{Limit: -1},
103 path: "/arvados/v1/collections?limit=123&offset=456&include_trash=true&include_old_versions=1",
104 shouldCall: "CollectionList",
105 withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
109 path: "/arvados/v1/collections?limit=123&_method=GET",
110 body: `{"offset":456,"include_trash":true,"include_old_versions":true}`,
111 shouldCall: "CollectionList",
112 withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
116 path: "/arvados/v1/collections?limit=123",
117 body: `{"offset":456,"include_trash":true,"include_old_versions":true}`,
118 header: http.Header{"X-Http-Method-Override": {"GET"}, "Content-Type": {"application/json"}},
119 shouldCall: "CollectionList",
120 withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
124 path: "/arvados/v1/collections?limit=123",
125 body: "offset=456&include_trash=true&include_old_versions=1&_method=GET",
126 header: http.Header{"Content-Type": {"application/x-www-form-urlencoded"}},
127 shouldCall: "CollectionList",
128 withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
131 comment: "form-encoded expression filter in query string",
133 path: "/arvados/v1/collections?filters=[%22(foo<bar)%22]",
134 shouldCall: "CollectionList",
135 withOptions: arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"(foo<bar)", "=", true}}},
138 comment: "form-encoded expression filter in POST body",
140 path: "/arvados/v1/collections",
141 body: "filters=[\"(foo<bar)\"]&_method=GET",
142 header: http.Header{"Content-Type": {"application/x-www-form-urlencoded"}},
143 shouldCall: "CollectionList",
144 withOptions: arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"(foo<bar)", "=", true}}},
147 comment: "json-encoded expression filter in POST body",
149 path: "/arvados/v1/collections?_method=GET",
150 body: `{"filters":["(foo<bar)",["bar","=","baz"]],"limit":2}`,
151 header: http.Header{"Content-Type": {"application/json"}},
152 shouldCall: "CollectionList",
153 withOptions: arvados.ListOptions{Limit: 2, Filters: []arvados.Filter{{"(foo<bar)", "=", true}, {"bar", "=", "baz"}}},
156 comment: "json-encoded select param in query string",
158 path: "/arvados/v1/collections/" + arvadostest.FooCollection + "?select=[%22portable_data_hash%22]",
159 shouldCall: "CollectionGet",
160 withOptions: arvados.GetOptions{UUID: arvadostest.FooCollection, Select: []string{"portable_data_hash"}},
164 path: "/arvados/v1/collections",
165 shouldStatus: http.StatusMethodNotAllowed,
169 path: "/arvados/v1/collections",
170 shouldStatus: http.StatusMethodNotAllowed,
174 path: "/arvados/v1/collections",
175 shouldStatus: http.StatusMethodNotAllowed,
178 // Reset calls captured in previous trial
179 s.stub = arvadostest.APIStub{}
181 c.Logf("trial: %+v", trial)
182 comment := check.Commentf("trial comment: %s", trial.comment)
184 _, rr, _ := doRequest(c, s.rtr, token, trial.method, trial.path, trial.header, bytes.NewBufferString(trial.body))
185 if trial.shouldStatus == 0 {
186 c.Check(rr.Code, check.Equals, http.StatusOK, comment)
188 c.Check(rr.Code, check.Equals, trial.shouldStatus, comment)
190 calls := s.stub.Calls(nil)
191 if trial.shouldCall == "" {
192 c.Check(calls, check.HasLen, 0, comment)
193 } else if len(calls) != 1 {
194 c.Check(calls, check.HasLen, 1, comment)
196 c.Check(calls[0].Method, isMethodNamed, trial.shouldCall, comment)
197 c.Check(calls[0].Options, check.DeepEquals, trial.withOptions, comment)
202 var _ = check.Suite(&RouterIntegrationSuite{})
204 type RouterIntegrationSuite struct {
208 func (s *RouterIntegrationSuite) SetUpTest(c *check.C) {
209 cluster := &arvados.Cluster{}
210 cluster.TLS.Insecure = true
211 arvadostest.SetServiceURL(&cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
212 url, _ := url.Parse("https://" + os.Getenv("ARVADOS_TEST_API_HOST"))
213 s.rtr = New(rpc.NewConn("zzzzz", url, true, rpc.PassthroughTokenProvider), Config{})
216 func (s *RouterIntegrationSuite) TearDownSuite(c *check.C) {
217 err := arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil)
218 c.Check(err, check.IsNil)
221 func (s *RouterIntegrationSuite) TestCollectionResponses(c *check.C) {
222 token := arvadostest.ActiveTokenV2
224 // Check "get collection" response has "kind" key
225 _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"include_trash":true}`))
226 c.Check(rr.Code, check.Equals, http.StatusOK)
227 c.Check(jresp["items"], check.FitsTypeOf, []interface{}{})
228 c.Check(jresp["kind"], check.Equals, "arvados#collectionList")
229 c.Check(jresp["items"].([]interface{})[0].(map[string]interface{})["kind"], check.Equals, "arvados#collection")
231 // Check items in list response have a "kind" key regardless
232 // of whether a uuid/pdh is selected.
233 for _, selectj := range []string{
235 `,"select":["portable_data_hash"]`,
236 `,"select":["name"]`,
237 `,"select":["uuid"]`,
239 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"where":{"uuid":["`+arvadostest.FooCollection+`"]}`+selectj+`}`))
240 c.Check(rr.Code, check.Equals, http.StatusOK)
241 c.Check(jresp["items"], check.FitsTypeOf, []interface{}{})
242 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
243 c.Check(jresp["kind"], check.Equals, "arvados#collectionList")
244 item0 := jresp["items"].([]interface{})[0].(map[string]interface{})
245 c.Check(item0["kind"], check.Equals, "arvados#collection")
246 if selectj == "" || strings.Contains(selectj, "portable_data_hash") {
247 c.Check(item0["portable_data_hash"], check.Equals, arvadostest.FooCollectionPDH)
249 c.Check(item0["portable_data_hash"], check.IsNil)
251 if selectj == "" || strings.Contains(selectj, "name") {
252 c.Check(item0["name"], check.FitsTypeOf, "")
254 c.Check(item0["name"], check.IsNil)
256 if selectj == "" || strings.Contains(selectj, "uuid") {
257 c.Check(item0["uuid"], check.Equals, arvadostest.FooCollection)
259 c.Check(item0["uuid"], check.IsNil)
263 // Check "create collection" response has "kind" key
264 _, rr, jresp = doRequest(c, s.rtr, token, "POST", `/arvados/v1/collections`, http.Header{"Content-Type": {"application/x-www-form-urlencoded"}}, bytes.NewBufferString(`ensure_unique_name=true`))
265 c.Check(rr.Code, check.Equals, http.StatusOK)
266 c.Check(jresp["uuid"], check.FitsTypeOf, "")
267 c.Check(jresp["kind"], check.Equals, "arvados#collection")
270 func (s *RouterIntegrationSuite) TestMaxRequestSize(c *check.C) {
271 token := arvadostest.ActiveTokenV2
272 for _, maxRequestSize := range []int{
273 // Ensure 5M limit is enforced.
275 // Ensure 50M limit is enforced, and that a >25M body
276 // is accepted even though the default Go request size
280 s.rtr.config.MaxRequestSize = maxRequestSize
282 for len(okstr) < maxRequestSize/2 {
283 okstr = okstr + okstr
286 hdr := http.Header{"Content-Type": {"application/x-www-form-urlencoded"}}
288 body := bytes.NewBufferString(url.Values{"foo_bar": {okstr}}.Encode())
289 _, rr, _ := doRequest(c, s.rtr, token, "POST", `/arvados/v1/collections`, hdr, body)
290 c.Check(rr.Code, check.Equals, http.StatusOK)
292 body = bytes.NewBufferString(url.Values{"foo_bar": {okstr + okstr}}.Encode())
293 _, rr, _ = doRequest(c, s.rtr, token, "POST", `/arvados/v1/collections`, hdr, body)
294 c.Check(rr.Code, check.Equals, http.StatusRequestEntityTooLarge)
298 func (s *RouterIntegrationSuite) TestContainerList(c *check.C) {
299 token := arvadostest.ActiveTokenV2
301 _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?limit=0`, nil, nil)
302 c.Check(rr.Code, check.Equals, http.StatusOK)
303 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
304 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
305 c.Check(jresp["items"], check.NotNil)
306 c.Check(jresp["items"], check.HasLen, 0)
308 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?filters=[["uuid","in",[]]]`, nil, nil)
309 c.Check(rr.Code, check.Equals, http.StatusOK)
310 c.Check(jresp["items_available"], check.Equals, float64(0))
311 c.Check(jresp["items"], check.NotNil)
312 c.Check(jresp["items"], check.HasLen, 0)
314 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?limit=2&select=["uuid","command"]`, nil, nil)
315 c.Check(rr.Code, check.Equals, http.StatusOK)
316 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
317 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
318 c.Check(jresp["items"], check.HasLen, 2)
319 item0 := jresp["items"].([]interface{})[0].(map[string]interface{})
320 c.Check(item0["uuid"], check.HasLen, 27)
321 c.Check(item0["command"], check.FitsTypeOf, []interface{}{})
322 c.Check(item0["command"].([]interface{})[0], check.FitsTypeOf, "")
323 c.Check(item0["mounts"], check.IsNil)
325 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers`, nil, nil)
326 c.Check(rr.Code, check.Equals, http.StatusOK)
327 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
328 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
329 avail := int(jresp["items_available"].(float64))
330 c.Check(jresp["items"], check.HasLen, avail)
331 item0 = jresp["items"].([]interface{})[0].(map[string]interface{})
332 c.Check(item0["uuid"], check.HasLen, 27)
333 c.Check(item0["command"], check.FitsTypeOf, []interface{}{})
334 c.Check(item0["command"].([]interface{})[0], check.FitsTypeOf, "")
335 c.Check(item0["mounts"], check.NotNil)
338 func (s *RouterIntegrationSuite) TestContainerLock(c *check.C) {
339 uuid := arvadostest.QueuedContainerUUID
340 token := arvadostest.AdminToken
341 _, rr, jresp := doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
342 c.Check(rr.Code, check.Equals, http.StatusOK)
343 c.Check(jresp["uuid"], check.HasLen, 27)
344 c.Check(jresp["state"], check.Equals, "Locked")
345 _, rr, _ = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
346 c.Check(rr.Code, check.Equals, http.StatusUnprocessableEntity)
347 c.Check(rr.Body.String(), check.Not(check.Matches), `.*"uuid":.*`)
348 _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
349 c.Check(rr.Code, check.Equals, http.StatusOK)
350 c.Check(jresp["uuid"], check.HasLen, 27)
351 c.Check(jresp["state"], check.Equals, "Queued")
352 c.Check(jresp["environment"], check.IsNil)
353 _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
354 c.Check(rr.Code, check.Equals, http.StatusUnprocessableEntity)
355 c.Check(jresp["uuid"], check.IsNil)
358 func (s *RouterIntegrationSuite) TestWritableBy(c *check.C) {
359 _, rr, jresp := doRequest(c, s.rtr, arvadostest.ActiveTokenV2, "GET", `/arvados/v1/users/`+arvadostest.ActiveUserUUID, nil, nil)
360 c.Check(rr.Code, check.Equals, http.StatusOK)
361 c.Check(jresp["writable_by"], check.DeepEquals, []interface{}{"zzzzz-tpzed-000000000000000", "zzzzz-tpzed-xurymjxw79nv3jz", "zzzzz-j7d0g-48foin4vonvc2at"})
364 func (s *RouterIntegrationSuite) TestFullTimestampsInResponse(c *check.C) {
365 uuid := arvadostest.CollectionReplicationDesired2Confirmed2UUID
366 token := arvadostest.ActiveTokenV2
368 _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections/`+uuid, nil, nil)
369 c.Check(rr.Code, check.Equals, http.StatusOK)
370 c.Check(jresp["uuid"], check.Equals, uuid)
371 expectNS := map[string]int{
372 "created_at": 596506000, // fixture says 596506247, but truncated by postgresql
373 "modified_at": 596338000, // fixture says 596338465, but truncated by postgresql
375 for key, ns := range expectNS {
376 mt, ok := jresp[key].(string)
377 c.Logf("jresp[%q] == %q", key, mt)
378 c.Assert(ok, check.Equals, true)
379 t, err := time.Parse(time.RFC3339Nano, mt)
380 c.Check(err, check.IsNil)
381 c.Check(t.Nanosecond(), check.Equals, ns)
385 func (s *RouterIntegrationSuite) TestSelectParam(c *check.C) {
386 uuid := arvadostest.QueuedContainerUUID
387 token := arvadostest.ActiveTokenV2
389 for _, sel := range [][]string{
391 {"uuid", "command", "uuid"},
393 j, err := json.Marshal(sel)
394 c.Assert(err, check.IsNil)
395 _, rr, resp := doRequest(c, s.rtr, token, "GET", "/arvados/v1/containers/"+uuid+"?select="+string(j), nil, nil)
396 c.Check(rr.Code, check.Equals, http.StatusOK)
398 c.Check(resp["kind"], check.Equals, "arvados#container")
399 c.Check(resp["uuid"], check.HasLen, 27)
400 c.Check(resp["command"], check.HasLen, 2)
401 c.Check(resp["mounts"], check.IsNil)
402 _, hasMounts := resp["mounts"]
403 c.Check(hasMounts, check.Equals, false)
406 uuid = arvadostest.FooCollection
407 j, err := json.Marshal([]string{"uuid", "description"})
408 c.Assert(err, check.IsNil)
409 for _, method := range []string{"PUT", "POST"} {
410 desc := "Today is " + time.Now().String()
411 reqBody := "{\"description\":\"" + desc + "\"}"
412 var resp map[string]interface{}
413 var rr *httptest.ResponseRecorder
415 _, rr, resp = doRequest(c, s.rtr, token, method, "/arvados/v1/collections/"+uuid+"?select="+string(j), nil, bytes.NewReader([]byte(reqBody)))
417 _, rr, resp = doRequest(c, s.rtr, token, method, "/arvados/v1/collections?select="+string(j), nil, bytes.NewReader([]byte(reqBody)))
419 c.Check(rr.Code, check.Equals, http.StatusOK)
420 c.Check(resp["kind"], check.Equals, "arvados#collection")
421 c.Check(resp["uuid"], check.HasLen, 27)
422 c.Check(resp["description"], check.Equals, desc)
423 c.Check(resp["manifest_text"], check.IsNil)
427 func (s *RouterIntegrationSuite) TestHEAD(c *check.C) {
428 _, rr, _ := doRequest(c, s.rtr, arvadostest.ActiveTokenV2, "HEAD", "/arvados/v1/containers/"+arvadostest.QueuedContainerUUID, nil, nil)
429 c.Check(rr.Code, check.Equals, http.StatusOK)
432 func (s *RouterIntegrationSuite) TestRouteNotFound(c *check.C) {
433 token := arvadostest.ActiveTokenV2
436 path: "arvados/v1/collections/" + arvadostest.FooCollection + "/error404pls",
439 rr := httptest.NewRecorder()
440 s.rtr.ServeHTTP(rr, req)
441 c.Check(rr.Code, check.Equals, http.StatusNotFound)
442 c.Logf("body: %q", rr.Body.String())
443 var j map[string]interface{}
444 err := json.Unmarshal(rr.Body.Bytes(), &j)
445 c.Check(err, check.IsNil)
446 c.Logf("decoded: %v", j)
447 c.Assert(j["errors"], check.FitsTypeOf, []interface{}{})
448 c.Check(j["errors"].([]interface{})[0], check.Equals, "API endpoint not found")
451 func (s *RouterIntegrationSuite) TestCORS(c *check.C) {
452 token := arvadostest.ActiveTokenV2
455 path: "arvados/v1/collections/" + arvadostest.FooCollection,
456 header: http.Header{"Origin": {"https://example.com"}},
459 rr := httptest.NewRecorder()
460 s.rtr.ServeHTTP(rr, req)
461 c.Check(rr.Code, check.Equals, http.StatusOK)
462 c.Check(rr.Body.String(), check.HasLen, 0)
463 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "*")
464 for _, hdr := range []string{"Authorization", "Content-Type"} {
465 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Matches, ".*"+hdr+".*")
467 for _, method := range []string{"GET", "HEAD", "PUT", "POST", "PATCH", "DELETE"} {
468 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Matches, ".*"+method+".*")
471 for _, unsafe := range []string{"login", "logout", "auth", "auth/foo", "login/?blah"} {
475 header: http.Header{"Origin": {"https://example.com"}},
478 rr := httptest.NewRecorder()
479 s.rtr.ServeHTTP(rr, req)
480 c.Check(rr.Code, check.Equals, http.StatusOK)
481 c.Check(rr.Body.String(), check.HasLen, 0)
482 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "")
483 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Equals, "")
484 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Equals, "")
489 header: http.Header{"Origin": {"https://example.com"}},
492 rr = httptest.NewRecorder()
493 s.rtr.ServeHTTP(rr, req)
494 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "")
495 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Equals, "")
496 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Equals, "")
500 func doRequest(c *check.C, rtr http.Handler, token, method, path string, hdrs http.Header, body io.Reader) (*http.Request, *httptest.ResponseRecorder, map[string]interface{}) {
501 req := httptest.NewRequest(method, path, body)
502 for k, v := range hdrs {
505 req.Header.Set("Authorization", "Bearer "+token)
506 rr := httptest.NewRecorder()
507 rtr.ServeHTTP(rr, req)
508 c.Logf("response body: %s", rr.Body.String())
509 var jresp map[string]interface{}
510 err := json.Unmarshal(rr.Body.Bytes(), &jresp)
511 c.Check(err, check.IsNil)
512 return req, rr, jresp