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/collections?limit=123&offset=456&include_trash=true&include_old_versions=1",
98 shouldCall: "CollectionList",
99 withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
103 path: "/arvados/v1/collections?limit=123&_method=GET",
104 body: `{"offset":456,"include_trash":true,"include_old_versions":true}`,
105 shouldCall: "CollectionList",
106 withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
110 path: "/arvados/v1/collections?limit=123",
111 body: `{"offset":456,"include_trash":true,"include_old_versions":true}`,
112 header: http.Header{"X-Http-Method-Override": {"GET"}, "Content-Type": {"application/json"}},
113 shouldCall: "CollectionList",
114 withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
118 path: "/arvados/v1/collections?limit=123",
119 body: "offset=456&include_trash=true&include_old_versions=1&_method=GET",
120 header: http.Header{"Content-Type": {"application/x-www-form-urlencoded"}},
121 shouldCall: "CollectionList",
122 withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
125 comment: "form-encoded expression filter in query string",
127 path: "/arvados/v1/collections?filters=[%22(foo<bar)%22]",
128 header: http.Header{"Content-Type": {"application/x-www-form-urlencoded"}},
129 shouldCall: "CollectionList",
130 withOptions: arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"(foo<bar)", "=", true}}},
133 comment: "form-encoded expression filter in POST body",
135 path: "/arvados/v1/collections",
136 body: "filters=[\"(foo<bar)\"]&_method=GET",
137 header: http.Header{"Content-Type": {"application/x-www-form-urlencoded"}},
138 shouldCall: "CollectionList",
139 withOptions: arvados.ListOptions{Limit: -1, Filters: []arvados.Filter{{"(foo<bar)", "=", true}}},
142 comment: "json-encoded expression filter in POST body",
144 path: "/arvados/v1/collections?_method=GET",
145 body: `{"filters":["(foo<bar)",["bar","=","baz"]],"limit":2}`,
146 header: http.Header{"Content-Type": {"application/json"}},
147 shouldCall: "CollectionList",
148 withOptions: arvados.ListOptions{Limit: 2, Filters: []arvados.Filter{{"(foo<bar)", "=", true}, {"bar", "=", "baz"}}},
152 path: "/arvados/v1/collections",
153 shouldStatus: http.StatusMethodNotAllowed,
157 path: "/arvados/v1/collections",
158 shouldStatus: http.StatusMethodNotAllowed,
162 path: "/arvados/v1/collections",
163 shouldStatus: http.StatusMethodNotAllowed,
166 // Reset calls captured in previous trial
167 s.stub = arvadostest.APIStub{}
169 c.Logf("trial: %+v", trial)
170 comment := check.Commentf("trial comment: %s", trial.comment)
172 _, rr, _ := doRequest(c, s.rtr, token, trial.method, trial.path, trial.header, bytes.NewBufferString(trial.body))
173 if trial.shouldStatus == 0 {
174 c.Check(rr.Code, check.Equals, http.StatusOK, comment)
176 c.Check(rr.Code, check.Equals, trial.shouldStatus, comment)
178 calls := s.stub.Calls(nil)
179 if trial.shouldCall == "" {
180 c.Check(calls, check.HasLen, 0, comment)
181 } else if len(calls) != 1 {
182 c.Check(calls, check.HasLen, 1, comment)
184 c.Check(calls[0].Method, isMethodNamed, trial.shouldCall, comment)
185 c.Check(calls[0].Options, check.DeepEquals, trial.withOptions, comment)
190 var _ = check.Suite(&RouterIntegrationSuite{})
192 type RouterIntegrationSuite struct {
196 func (s *RouterIntegrationSuite) SetUpTest(c *check.C) {
197 cluster := &arvados.Cluster{}
198 cluster.TLS.Insecure = true
199 arvadostest.SetServiceURL(&cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
200 url, _ := url.Parse("https://" + os.Getenv("ARVADOS_TEST_API_HOST"))
201 s.rtr = New(rpc.NewConn("zzzzz", url, true, rpc.PassthroughTokenProvider), Config{})
204 func (s *RouterIntegrationSuite) TearDownSuite(c *check.C) {
205 err := arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil)
206 c.Check(err, check.IsNil)
209 func (s *RouterIntegrationSuite) TestCollectionResponses(c *check.C) {
210 token := arvadostest.ActiveTokenV2
212 // Check "get collection" response has "kind" key
213 _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"include_trash":true}`))
214 c.Check(rr.Code, check.Equals, http.StatusOK)
215 c.Check(jresp["items"], check.FitsTypeOf, []interface{}{})
216 c.Check(jresp["kind"], check.Equals, "arvados#collectionList")
217 c.Check(jresp["items"].([]interface{})[0].(map[string]interface{})["kind"], check.Equals, "arvados#collection")
219 // Check items in list response have a "kind" key regardless
220 // of whether a uuid/pdh is selected.
221 for _, selectj := range []string{
223 `,"select":["portable_data_hash"]`,
224 `,"select":["name"]`,
225 `,"select":["uuid"]`,
227 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"where":{"uuid":["`+arvadostest.FooCollection+`"]}`+selectj+`}`))
228 c.Check(rr.Code, check.Equals, http.StatusOK)
229 c.Check(jresp["items"], check.FitsTypeOf, []interface{}{})
230 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
231 c.Check(jresp["kind"], check.Equals, "arvados#collectionList")
232 item0 := jresp["items"].([]interface{})[0].(map[string]interface{})
233 c.Check(item0["kind"], check.Equals, "arvados#collection")
234 if selectj == "" || strings.Contains(selectj, "portable_data_hash") {
235 c.Check(item0["portable_data_hash"], check.Equals, arvadostest.FooCollectionPDH)
237 c.Check(item0["portable_data_hash"], check.IsNil)
239 if selectj == "" || strings.Contains(selectj, "name") {
240 c.Check(item0["name"], check.FitsTypeOf, "")
242 c.Check(item0["name"], check.IsNil)
244 if selectj == "" || strings.Contains(selectj, "uuid") {
245 c.Check(item0["uuid"], check.Equals, arvadostest.FooCollection)
247 c.Check(item0["uuid"], check.IsNil)
251 // Check "create collection" response has "kind" key
252 _, 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`))
253 c.Check(rr.Code, check.Equals, http.StatusOK)
254 c.Check(jresp["uuid"], check.FitsTypeOf, "")
255 c.Check(jresp["kind"], check.Equals, "arvados#collection")
258 func (s *RouterIntegrationSuite) TestMaxRequestSize(c *check.C) {
259 token := arvadostest.ActiveTokenV2
260 for _, maxRequestSize := range []int{
261 // Ensure 5M limit is enforced.
263 // Ensure 50M limit is enforced, and that a >25M body
264 // is accepted even though the default Go request size
268 s.rtr.config.MaxRequestSize = maxRequestSize
270 for len(okstr) < maxRequestSize/2 {
271 okstr = okstr + okstr
274 hdr := http.Header{"Content-Type": {"application/x-www-form-urlencoded"}}
276 body := bytes.NewBufferString(url.Values{"foo_bar": {okstr}}.Encode())
277 _, rr, _ := doRequest(c, s.rtr, token, "POST", `/arvados/v1/collections`, hdr, body)
278 c.Check(rr.Code, check.Equals, http.StatusOK)
280 body = bytes.NewBufferString(url.Values{"foo_bar": {okstr + okstr}}.Encode())
281 _, rr, _ = doRequest(c, s.rtr, token, "POST", `/arvados/v1/collections`, hdr, body)
282 c.Check(rr.Code, check.Equals, http.StatusRequestEntityTooLarge)
286 func (s *RouterIntegrationSuite) TestContainerList(c *check.C) {
287 token := arvadostest.ActiveTokenV2
289 _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?limit=0`, nil, nil)
290 c.Check(rr.Code, check.Equals, http.StatusOK)
291 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
292 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
293 c.Check(jresp["items"], check.NotNil)
294 c.Check(jresp["items"], check.HasLen, 0)
296 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?filters=[["uuid","in",[]]]`, nil, nil)
297 c.Check(rr.Code, check.Equals, http.StatusOK)
298 c.Check(jresp["items_available"], check.Equals, float64(0))
299 c.Check(jresp["items"], check.NotNil)
300 c.Check(jresp["items"], check.HasLen, 0)
302 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?limit=2&select=["uuid","command"]`, nil, nil)
303 c.Check(rr.Code, check.Equals, http.StatusOK)
304 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
305 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
306 c.Check(jresp["items"], check.HasLen, 2)
307 item0 := jresp["items"].([]interface{})[0].(map[string]interface{})
308 c.Check(item0["uuid"], check.HasLen, 27)
309 c.Check(item0["command"], check.FitsTypeOf, []interface{}{})
310 c.Check(item0["command"].([]interface{})[0], check.FitsTypeOf, "")
311 c.Check(item0["mounts"], check.IsNil)
313 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers`, nil, nil)
314 c.Check(rr.Code, check.Equals, http.StatusOK)
315 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
316 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
317 avail := int(jresp["items_available"].(float64))
318 c.Check(jresp["items"], check.HasLen, avail)
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.NotNil)
326 func (s *RouterIntegrationSuite) TestContainerLock(c *check.C) {
327 uuid := arvadostest.QueuedContainerUUID
328 token := arvadostest.AdminToken
329 _, rr, jresp := doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
330 c.Check(rr.Code, check.Equals, http.StatusOK)
331 c.Check(jresp["uuid"], check.HasLen, 27)
332 c.Check(jresp["state"], check.Equals, "Locked")
333 _, rr, _ = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
334 c.Check(rr.Code, check.Equals, http.StatusUnprocessableEntity)
335 c.Check(rr.Body.String(), check.Not(check.Matches), `.*"uuid":.*`)
336 _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
337 c.Check(rr.Code, check.Equals, http.StatusOK)
338 c.Check(jresp["uuid"], check.HasLen, 27)
339 c.Check(jresp["state"], check.Equals, "Queued")
340 c.Check(jresp["environment"], check.IsNil)
341 _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
342 c.Check(rr.Code, check.Equals, http.StatusUnprocessableEntity)
343 c.Check(jresp["uuid"], check.IsNil)
346 func (s *RouterIntegrationSuite) TestWritableBy(c *check.C) {
347 _, rr, jresp := doRequest(c, s.rtr, arvadostest.ActiveTokenV2, "GET", `/arvados/v1/users/`+arvadostest.ActiveUserUUID, nil, nil)
348 c.Check(rr.Code, check.Equals, http.StatusOK)
349 c.Check(jresp["writable_by"], check.DeepEquals, []interface{}{"zzzzz-tpzed-000000000000000", "zzzzz-tpzed-xurymjxw79nv3jz", "zzzzz-j7d0g-48foin4vonvc2at"})
352 func (s *RouterIntegrationSuite) TestFullTimestampsInResponse(c *check.C) {
353 uuid := arvadostest.CollectionReplicationDesired2Confirmed2UUID
354 token := arvadostest.ActiveTokenV2
356 _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections/`+uuid, nil, nil)
357 c.Check(rr.Code, check.Equals, http.StatusOK)
358 c.Check(jresp["uuid"], check.Equals, uuid)
359 expectNS := map[string]int{
360 "created_at": 596506000, // fixture says 596506247, but truncated by postgresql
361 "modified_at": 596338000, // fixture says 596338465, but truncated by postgresql
363 for key, ns := range expectNS {
364 mt, ok := jresp[key].(string)
365 c.Logf("jresp[%q] == %q", key, mt)
366 c.Assert(ok, check.Equals, true)
367 t, err := time.Parse(time.RFC3339Nano, mt)
368 c.Check(err, check.IsNil)
369 c.Check(t.Nanosecond(), check.Equals, ns)
373 func (s *RouterIntegrationSuite) TestSelectParam(c *check.C) {
374 uuid := arvadostest.QueuedContainerUUID
375 token := arvadostest.ActiveTokenV2
376 for _, sel := range [][]string{
378 {"uuid", "command", "uuid"},
379 {"", "command", "uuid"},
381 j, err := json.Marshal(sel)
382 c.Assert(err, check.IsNil)
383 _, rr, resp := doRequest(c, s.rtr, token, "GET", "/arvados/v1/containers/"+uuid+"?select="+string(j), nil, nil)
384 c.Check(rr.Code, check.Equals, http.StatusOK)
386 c.Check(resp["kind"], check.Equals, "arvados#container")
387 c.Check(resp["etag"], check.FitsTypeOf, "")
388 c.Check(resp["etag"], check.Not(check.Equals), "")
389 c.Check(resp["uuid"], check.HasLen, 27)
390 c.Check(resp["command"], check.HasLen, 2)
391 c.Check(resp["mounts"], check.IsNil)
392 _, hasMounts := resp["mounts"]
393 c.Check(hasMounts, check.Equals, false)
397 func (s *RouterIntegrationSuite) TestHEAD(c *check.C) {
398 _, rr, _ := doRequest(c, s.rtr, arvadostest.ActiveTokenV2, "HEAD", "/arvados/v1/containers/"+arvadostest.QueuedContainerUUID, nil, nil)
399 c.Check(rr.Code, check.Equals, http.StatusOK)
402 func (s *RouterIntegrationSuite) TestRouteNotFound(c *check.C) {
403 token := arvadostest.ActiveTokenV2
406 path: "arvados/v1/collections/" + arvadostest.FooCollection + "/error404pls",
409 rr := httptest.NewRecorder()
410 s.rtr.ServeHTTP(rr, req)
411 c.Check(rr.Code, check.Equals, http.StatusNotFound)
412 c.Logf("body: %q", rr.Body.String())
413 var j map[string]interface{}
414 err := json.Unmarshal(rr.Body.Bytes(), &j)
415 c.Check(err, check.IsNil)
416 c.Logf("decoded: %v", j)
417 c.Assert(j["errors"], check.FitsTypeOf, []interface{}{})
418 c.Check(j["errors"].([]interface{})[0], check.Equals, "API endpoint not found")
421 func (s *RouterIntegrationSuite) TestCORS(c *check.C) {
422 token := arvadostest.ActiveTokenV2
425 path: "arvados/v1/collections/" + arvadostest.FooCollection,
426 header: http.Header{"Origin": {"https://example.com"}},
429 rr := httptest.NewRecorder()
430 s.rtr.ServeHTTP(rr, req)
431 c.Check(rr.Code, check.Equals, http.StatusOK)
432 c.Check(rr.Body.String(), check.HasLen, 0)
433 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "*")
434 for _, hdr := range []string{"Authorization", "Content-Type"} {
435 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Matches, ".*"+hdr+".*")
437 for _, method := range []string{"GET", "HEAD", "PUT", "POST", "PATCH", "DELETE"} {
438 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Matches, ".*"+method+".*")
441 for _, unsafe := range []string{"login", "logout", "auth", "auth/foo", "login/?blah"} {
445 header: http.Header{"Origin": {"https://example.com"}},
448 rr := httptest.NewRecorder()
449 s.rtr.ServeHTTP(rr, req)
450 c.Check(rr.Code, check.Equals, http.StatusOK)
451 c.Check(rr.Body.String(), check.HasLen, 0)
452 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "")
453 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Equals, "")
454 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Equals, "")
459 header: http.Header{"Origin": {"https://example.com"}},
462 rr = httptest.NewRecorder()
463 s.rtr.ServeHTTP(rr, req)
464 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "")
465 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Equals, "")
466 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Equals, "")
470 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{}) {
471 req := httptest.NewRequest(method, path, body)
472 for k, v := range hdrs {
475 req.Header.Set("Authorization", "Bearer "+token)
476 rr := httptest.NewRecorder()
477 rtr.ServeHTTP(rr, req)
478 c.Logf("response body: %s", rr.Body.String())
479 var jresp map[string]interface{}
480 err := json.Unmarshal(rr.Body.Bytes(), &jresp)
481 c.Check(err, check.IsNil)
482 return req, rr, jresp