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 {
54 shouldStatus int // zero value means 200
56 withOptions interface{}
60 path: "/arvados/v1/collections/" + arvadostest.FooCollection,
61 shouldCall: "CollectionGet",
62 withOptions: arvados.GetOptions{UUID: arvadostest.FooCollection},
66 path: "/arvados/v1/collections/" + arvadostest.FooCollection,
67 shouldCall: "CollectionUpdate",
68 withOptions: arvados.UpdateOptions{UUID: arvadostest.FooCollection},
72 path: "/arvados/v1/collections/" + arvadostest.FooCollection,
73 shouldCall: "CollectionUpdate",
74 withOptions: arvados.UpdateOptions{UUID: arvadostest.FooCollection},
78 path: "/arvados/v1/collections/" + arvadostest.FooCollection,
79 shouldCall: "CollectionDelete",
80 withOptions: arvados.DeleteOptions{UUID: arvadostest.FooCollection},
84 path: "/arvados/v1/collections",
85 shouldCall: "CollectionCreate",
86 withOptions: arvados.CreateOptions{},
90 path: "/arvados/v1/collections",
91 shouldCall: "CollectionList",
92 withOptions: arvados.ListOptions{Limit: -1},
96 path: "/arvados/v1/collections?limit=123&offset=456&include_trash=true&include_old_versions=1",
97 shouldCall: "CollectionList",
98 withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
102 path: "/arvados/v1/collections?limit=123&_method=GET",
103 body: `{"offset":456,"include_trash":true,"include_old_versions":true}`,
104 shouldCall: "CollectionList",
105 withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
109 path: "/arvados/v1/collections?limit=123",
110 body: `{"offset":456,"include_trash":true,"include_old_versions":true}`,
111 header: http.Header{"X-Http-Method-Override": {"GET"}, "Content-Type": {"application/json"}},
112 shouldCall: "CollectionList",
113 withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
117 path: "/arvados/v1/collections?limit=123",
118 body: "offset=456&include_trash=true&include_old_versions=1&_method=GET",
119 header: http.Header{"Content-Type": {"application/x-www-form-urlencoded"}},
120 shouldCall: "CollectionList",
121 withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
125 path: "/arvados/v1/collections",
126 shouldStatus: http.StatusMethodNotAllowed,
130 path: "/arvados/v1/collections",
131 shouldStatus: http.StatusMethodNotAllowed,
135 path: "/arvados/v1/collections",
136 shouldStatus: http.StatusMethodNotAllowed,
139 // Reset calls captured in previous trial
140 s.stub = arvadostest.APIStub{}
142 c.Logf("trial: %#v", trial)
143 _, rr, _ := doRequest(c, s.rtr, token, trial.method, trial.path, trial.header, bytes.NewBufferString(trial.body))
144 if trial.shouldStatus == 0 {
145 c.Check(rr.Code, check.Equals, http.StatusOK)
147 c.Check(rr.Code, check.Equals, trial.shouldStatus)
149 calls := s.stub.Calls(nil)
150 if trial.shouldCall == "" {
151 c.Check(calls, check.HasLen, 0)
152 } else if len(calls) != 1 {
153 c.Check(calls, check.HasLen, 1)
155 c.Check(calls[0].Method, isMethodNamed, trial.shouldCall)
156 c.Check(calls[0].Options, check.DeepEquals, trial.withOptions)
161 var _ = check.Suite(&RouterIntegrationSuite{})
163 type RouterIntegrationSuite struct {
167 func (s *RouterIntegrationSuite) SetUpTest(c *check.C) {
168 cluster := &arvados.Cluster{}
169 cluster.TLS.Insecure = true
170 arvadostest.SetServiceURL(&cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
171 url, _ := url.Parse("https://" + os.Getenv("ARVADOS_TEST_API_HOST"))
172 s.rtr = New(rpc.NewConn("zzzzz", url, true, rpc.PassthroughTokenProvider))
175 func (s *RouterIntegrationSuite) TearDownSuite(c *check.C) {
176 err := arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil)
177 c.Check(err, check.IsNil)
180 func (s *RouterIntegrationSuite) TestCollectionResponses(c *check.C) {
181 token := arvadostest.ActiveTokenV2
183 // Check "get collection" response has "kind" key
184 _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"include_trash":true}`))
185 c.Check(rr.Code, check.Equals, http.StatusOK)
186 c.Check(jresp["items"], check.FitsTypeOf, []interface{}{})
187 c.Check(jresp["kind"], check.Equals, "arvados#collectionList")
188 c.Check(jresp["items"].([]interface{})[0].(map[string]interface{})["kind"], check.Equals, "arvados#collection")
190 // Check items in list response have a "kind" key regardless
191 // of whether a uuid/pdh is selected.
192 for _, selectj := range []string{
194 `,"select":["portable_data_hash"]`,
195 `,"select":["name"]`,
196 `,"select":["uuid"]`,
198 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"where":{"uuid":["`+arvadostest.FooCollection+`"]}`+selectj+`}`))
199 c.Check(rr.Code, check.Equals, http.StatusOK)
200 c.Check(jresp["items"], check.FitsTypeOf, []interface{}{})
201 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
202 c.Check(jresp["kind"], check.Equals, "arvados#collectionList")
203 item0 := jresp["items"].([]interface{})[0].(map[string]interface{})
204 c.Check(item0["kind"], check.Equals, "arvados#collection")
205 if selectj == "" || strings.Contains(selectj, "portable_data_hash") {
206 c.Check(item0["portable_data_hash"], check.Equals, arvadostest.FooCollectionPDH)
208 c.Check(item0["portable_data_hash"], check.IsNil)
210 if selectj == "" || strings.Contains(selectj, "name") {
211 c.Check(item0["name"], check.FitsTypeOf, "")
213 c.Check(item0["name"], check.IsNil)
215 if selectj == "" || strings.Contains(selectj, "uuid") {
216 c.Check(item0["uuid"], check.Equals, arvadostest.FooCollection)
218 c.Check(item0["uuid"], check.IsNil)
222 // Check "create collection" response has "kind" key
223 _, 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`))
224 c.Check(rr.Code, check.Equals, http.StatusOK)
225 c.Check(jresp["uuid"], check.FitsTypeOf, "")
226 c.Check(jresp["kind"], check.Equals, "arvados#collection")
229 func (s *RouterIntegrationSuite) TestContainerList(c *check.C) {
230 token := arvadostest.ActiveTokenV2
232 _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?limit=0`, nil, nil)
233 c.Check(rr.Code, check.Equals, http.StatusOK)
234 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
235 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
236 c.Check(jresp["items"], check.NotNil)
237 c.Check(jresp["items"], check.HasLen, 0)
239 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?filters=[["uuid","in",[]]]`, nil, nil)
240 c.Check(rr.Code, check.Equals, http.StatusOK)
241 c.Check(jresp["items_available"], check.Equals, float64(0))
242 c.Check(jresp["items"], check.NotNil)
243 c.Check(jresp["items"], check.HasLen, 0)
245 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?limit=2&select=["uuid","command"]`, nil, nil)
246 c.Check(rr.Code, check.Equals, http.StatusOK)
247 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
248 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
249 c.Check(jresp["items"], check.HasLen, 2)
250 item0 := jresp["items"].([]interface{})[0].(map[string]interface{})
251 c.Check(item0["uuid"], check.HasLen, 27)
252 c.Check(item0["command"], check.FitsTypeOf, []interface{}{})
253 c.Check(item0["command"].([]interface{})[0], check.FitsTypeOf, "")
254 c.Check(item0["mounts"], check.IsNil)
256 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers`, nil, nil)
257 c.Check(rr.Code, check.Equals, http.StatusOK)
258 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
259 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
260 avail := int(jresp["items_available"].(float64))
261 c.Check(jresp["items"], check.HasLen, avail)
262 item0 = jresp["items"].([]interface{})[0].(map[string]interface{})
263 c.Check(item0["uuid"], check.HasLen, 27)
264 c.Check(item0["command"], check.FitsTypeOf, []interface{}{})
265 c.Check(item0["command"].([]interface{})[0], check.FitsTypeOf, "")
266 c.Check(item0["mounts"], check.NotNil)
269 func (s *RouterIntegrationSuite) TestContainerLock(c *check.C) {
270 uuid := arvadostest.QueuedContainerUUID
271 token := arvadostest.AdminToken
272 _, rr, jresp := doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
273 c.Check(rr.Code, check.Equals, http.StatusOK)
274 c.Check(jresp["uuid"], check.HasLen, 27)
275 c.Check(jresp["state"], check.Equals, "Locked")
276 _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
277 c.Check(rr.Code, check.Equals, http.StatusUnprocessableEntity)
278 c.Check(rr.Body.String(), check.Not(check.Matches), `.*"uuid":.*`)
279 _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
280 c.Check(rr.Code, check.Equals, http.StatusOK)
281 c.Check(jresp["uuid"], check.HasLen, 27)
282 c.Check(jresp["state"], check.Equals, "Queued")
283 c.Check(jresp["environment"], check.IsNil)
284 _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
285 c.Check(rr.Code, check.Equals, http.StatusUnprocessableEntity)
286 c.Check(jresp["uuid"], check.IsNil)
289 func (s *RouterIntegrationSuite) TestFullTimestampsInResponse(c *check.C) {
290 uuid := arvadostest.CollectionReplicationDesired2Confirmed2UUID
291 token := arvadostest.ActiveTokenV2
293 _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections/`+uuid, nil, nil)
294 c.Check(rr.Code, check.Equals, http.StatusOK)
295 c.Check(jresp["uuid"], check.Equals, uuid)
296 expectNS := map[string]int{
297 "created_at": 596506000, // fixture says 596506247, but truncated by postgresql
298 "modified_at": 596338000, // fixture says 596338465, but truncated by postgresql
300 for key, ns := range expectNS {
301 mt, ok := jresp[key].(string)
302 c.Logf("jresp[%q] == %q", key, mt)
303 c.Assert(ok, check.Equals, true)
304 t, err := time.Parse(time.RFC3339Nano, mt)
305 c.Check(err, check.IsNil)
306 c.Check(t.Nanosecond(), check.Equals, ns)
310 func (s *RouterIntegrationSuite) TestSelectParam(c *check.C) {
311 uuid := arvadostest.QueuedContainerUUID
312 token := arvadostest.ActiveTokenV2
313 for _, sel := range [][]string{
315 {"uuid", "command", "uuid"},
316 {"", "command", "uuid"},
318 j, err := json.Marshal(sel)
319 c.Assert(err, check.IsNil)
320 _, rr, resp := doRequest(c, s.rtr, token, "GET", "/arvados/v1/containers/"+uuid+"?select="+string(j), nil, nil)
321 c.Check(rr.Code, check.Equals, http.StatusOK)
323 c.Check(resp["kind"], check.Equals, "arvados#container")
324 c.Check(resp["uuid"], check.HasLen, 27)
325 c.Check(resp["command"], check.HasLen, 2)
326 c.Check(resp["mounts"], check.IsNil)
327 _, hasMounts := resp["mounts"]
328 c.Check(hasMounts, check.Equals, false)
332 func (s *RouterIntegrationSuite) TestHEAD(c *check.C) {
333 _, rr, _ := doRequest(c, s.rtr, arvadostest.ActiveTokenV2, "HEAD", "/arvados/v1/containers/"+arvadostest.QueuedContainerUUID, nil, nil)
334 c.Check(rr.Code, check.Equals, http.StatusOK)
337 func (s *RouterIntegrationSuite) TestRouteNotFound(c *check.C) {
338 token := arvadostest.ActiveTokenV2
341 path: "arvados/v1/collections/" + arvadostest.FooCollection + "/error404pls",
344 rr := httptest.NewRecorder()
345 s.rtr.ServeHTTP(rr, req)
346 c.Check(rr.Code, check.Equals, http.StatusNotFound)
347 c.Logf("body: %q", rr.Body.String())
348 var j map[string]interface{}
349 err := json.Unmarshal(rr.Body.Bytes(), &j)
350 c.Check(err, check.IsNil)
351 c.Logf("decoded: %v", j)
352 c.Assert(j["errors"], check.FitsTypeOf, []interface{}{})
353 c.Check(j["errors"].([]interface{})[0], check.Equals, "API endpoint not found")
356 func (s *RouterIntegrationSuite) TestCORS(c *check.C) {
357 token := arvadostest.ActiveTokenV2
360 path: "arvados/v1/collections/" + arvadostest.FooCollection,
361 header: http.Header{"Origin": {"https://example.com"}},
364 rr := httptest.NewRecorder()
365 s.rtr.ServeHTTP(rr, req)
366 c.Check(rr.Code, check.Equals, http.StatusOK)
367 c.Check(rr.Body.String(), check.HasLen, 0)
368 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "*")
369 for _, hdr := range []string{"Authorization", "Content-Type"} {
370 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Matches, ".*"+hdr+".*")
372 for _, method := range []string{"GET", "HEAD", "PUT", "POST", "PATCH", "DELETE"} {
373 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Matches, ".*"+method+".*")
376 for _, unsafe := range []string{"login", "logout", "auth", "auth/foo", "login/?blah"} {
380 header: http.Header{"Origin": {"https://example.com"}},
383 rr := httptest.NewRecorder()
384 s.rtr.ServeHTTP(rr, req)
385 c.Check(rr.Code, check.Equals, http.StatusOK)
386 c.Check(rr.Body.String(), check.HasLen, 0)
387 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "")
388 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Equals, "")
389 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Equals, "")
394 header: http.Header{"Origin": {"https://example.com"}},
397 rr = httptest.NewRecorder()
398 s.rtr.ServeHTTP(rr, req)
399 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "")
400 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Equals, "")
401 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Equals, "")
405 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{}) {
406 req := httptest.NewRequest(method, path, body)
407 for k, v := range hdrs {
410 req.Header.Set("Authorization", "Bearer "+token)
411 rr := httptest.NewRecorder()
412 rtr.ServeHTTP(rr, req)
413 c.Logf("response body: %s", rr.Body.String())
414 var jresp map[string]interface{}
415 err := json.Unmarshal(rr.Body.Bytes(), &jresp)
416 c.Check(err, check.IsNil)
417 return req, rr, jresp