1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
18 "git.curoverse.com/arvados.git/sdk/go/arvados"
19 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
20 "github.com/julienschmidt/httprouter"
21 check "gopkg.in/check.v1"
24 // Gocheck boilerplate
25 func Test(t *testing.T) {
29 var _ = check.Suite(&RouterSuite{})
31 type RouterSuite struct {
33 stub arvadostest.APIStub
36 func (s *RouterSuite) SetUpTest(c *check.C) {
37 s.stub = arvadostest.APIStub{}
39 mux: httprouter.New(),
45 func (s *RouterSuite) TestOptions(c *check.C) {
46 token := arvadostest.ActiveToken
47 for _, trial := range []struct {
52 shouldStatus int // zero value means 200
54 withOptions interface{}
58 path: "/arvados/v1/collections/" + arvadostest.FooCollection,
59 shouldCall: "CollectionGet",
60 withOptions: arvados.GetOptions{UUID: arvadostest.FooCollection},
64 path: "/arvados/v1/collections/" + arvadostest.FooCollection,
65 shouldCall: "CollectionUpdate",
66 withOptions: arvados.UpdateOptions{UUID: arvadostest.FooCollection},
70 path: "/arvados/v1/collections/" + arvadostest.FooCollection,
71 shouldCall: "CollectionUpdate",
72 withOptions: arvados.UpdateOptions{UUID: arvadostest.FooCollection},
76 path: "/arvados/v1/collections/" + arvadostest.FooCollection,
77 shouldCall: "CollectionDelete",
78 withOptions: arvados.DeleteOptions{UUID: arvadostest.FooCollection},
82 path: "/arvados/v1/collections",
83 shouldCall: "CollectionCreate",
84 withOptions: arvados.CreateOptions{},
88 path: "/arvados/v1/collections",
89 shouldCall: "CollectionList",
90 withOptions: arvados.ListOptions{Limit: -1},
94 path: "/arvados/v1/collections?limit=123&offset=456&include_trash=true&include_old_versions=1",
95 shouldCall: "CollectionList",
96 withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
100 path: "/arvados/v1/collections?limit=123&_method=GET",
101 body: `{"offset":456,"include_trash":true,"include_old_versions":true}`,
102 shouldCall: "CollectionList",
103 withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
107 path: "/arvados/v1/collections?limit=123",
108 body: "offset=456&include_trash=true&include_old_versions=1&_method=GET",
109 header: http.Header{"Content-Type": {"application/x-www-form-urlencoded"}},
110 shouldCall: "CollectionList",
111 withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
115 path: "/arvados/v1/collections",
116 shouldStatus: http.StatusMethodNotAllowed,
120 path: "/arvados/v1/collections",
121 shouldStatus: http.StatusMethodNotAllowed,
125 path: "/arvados/v1/collections",
126 shouldStatus: http.StatusMethodNotAllowed,
129 // Reset calls captured in previous trial
130 s.stub = arvadostest.APIStub{}
132 c.Logf("trial: %#v", trial)
133 _, rr, _ := doRequest(c, s.rtr, token, trial.method, trial.path, trial.header, bytes.NewBufferString(trial.body))
134 if trial.shouldStatus == 0 {
135 c.Check(rr.Code, check.Equals, http.StatusOK)
137 c.Check(rr.Code, check.Equals, trial.shouldStatus)
139 calls := s.stub.Calls(nil)
140 if trial.shouldCall == "" {
141 c.Check(calls, check.HasLen, 0)
142 } else if len(calls) != 1 {
143 c.Check(calls, check.HasLen, 1)
145 c.Check(calls[0].Method, isMethodNamed, trial.shouldCall)
146 c.Check(calls[0].Options, check.DeepEquals, trial.withOptions)
151 var _ = check.Suite(&RouterIntegrationSuite{})
153 type RouterIntegrationSuite struct {
157 func (s *RouterIntegrationSuite) SetUpTest(c *check.C) {
158 cluster := &arvados.Cluster{}
159 cluster.TLS.Insecure = true
160 arvadostest.SetServiceURL(&cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
164 func (s *RouterIntegrationSuite) TearDownSuite(c *check.C) {
165 err := arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil)
166 c.Check(err, check.IsNil)
169 func (s *RouterIntegrationSuite) TestCollectionResponses(c *check.C) {
170 token := arvadostest.ActiveTokenV2
172 // Check "get collection" response has "kind" key
173 _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"include_trash":true}`))
174 c.Check(rr.Code, check.Equals, http.StatusOK)
175 c.Check(jresp["items"], check.FitsTypeOf, []interface{}{})
176 c.Check(jresp["kind"], check.Equals, "arvados#collectionList")
177 c.Check(jresp["items"].([]interface{})[0].(map[string]interface{})["kind"], check.Equals, "arvados#collection")
179 // Check items in list response have a "kind" key regardless
180 // of whether a uuid/pdh is selected.
181 for _, selectj := range []string{
183 `,"select":["portable_data_hash"]`,
184 `,"select":["name"]`,
185 `,"select":["uuid"]`,
187 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"where":{"uuid":["`+arvadostest.FooCollection+`"]}`+selectj+`}`))
188 c.Check(rr.Code, check.Equals, http.StatusOK)
189 c.Check(jresp["items"], check.FitsTypeOf, []interface{}{})
190 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
191 c.Check(jresp["kind"], check.Equals, "arvados#collectionList")
192 item0 := jresp["items"].([]interface{})[0].(map[string]interface{})
193 c.Check(item0["kind"], check.Equals, "arvados#collection")
194 if selectj == "" || strings.Contains(selectj, "portable_data_hash") {
195 c.Check(item0["portable_data_hash"], check.Equals, arvadostest.FooCollectionPDH)
197 c.Check(item0["portable_data_hash"], check.IsNil)
199 if selectj == "" || strings.Contains(selectj, "name") {
200 c.Check(item0["name"], check.FitsTypeOf, "")
202 c.Check(item0["name"], check.IsNil)
204 if selectj == "" || strings.Contains(selectj, "uuid") {
205 c.Check(item0["uuid"], check.Equals, arvadostest.FooCollection)
207 c.Check(item0["uuid"], check.IsNil)
211 // Check "create collection" response has "kind" key
212 _, 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`))
213 c.Check(rr.Code, check.Equals, http.StatusOK)
214 c.Check(jresp["uuid"], check.FitsTypeOf, "")
215 c.Check(jresp["kind"], check.Equals, "arvados#collection")
218 func (s *RouterIntegrationSuite) TestContainerList(c *check.C) {
219 token := arvadostest.ActiveTokenV2
221 _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?limit=0`, nil, nil)
222 c.Check(rr.Code, check.Equals, http.StatusOK)
223 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
224 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
225 c.Check(jresp["items"], check.HasLen, 0)
227 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?limit=2&select=["uuid","command"]`, nil, nil)
228 c.Check(rr.Code, check.Equals, http.StatusOK)
229 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
230 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
231 c.Check(jresp["items"], check.HasLen, 2)
232 item0 := jresp["items"].([]interface{})[0].(map[string]interface{})
233 c.Check(item0["uuid"], check.HasLen, 27)
234 c.Check(item0["command"], check.FitsTypeOf, []interface{}{})
235 c.Check(item0["command"].([]interface{})[0], check.FitsTypeOf, "")
236 c.Check(item0["mounts"], check.IsNil)
238 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers`, nil, nil)
239 c.Check(rr.Code, check.Equals, http.StatusOK)
240 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
241 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
242 avail := int(jresp["items_available"].(float64))
243 c.Check(jresp["items"], check.HasLen, avail)
244 item0 = jresp["items"].([]interface{})[0].(map[string]interface{})
245 c.Check(item0["uuid"], check.HasLen, 27)
246 c.Check(item0["command"], check.FitsTypeOf, []interface{}{})
247 c.Check(item0["command"].([]interface{})[0], check.FitsTypeOf, "")
248 c.Check(item0["mounts"], check.NotNil)
251 func (s *RouterIntegrationSuite) TestContainerLock(c *check.C) {
252 uuid := arvadostest.QueuedContainerUUID
253 token := arvadostest.AdminToken
254 _, rr, jresp := doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
255 c.Check(rr.Code, check.Equals, http.StatusOK)
256 c.Check(jresp["uuid"], check.HasLen, 27)
257 c.Check(jresp["state"], check.Equals, "Locked")
258 _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
259 c.Check(rr.Code, check.Equals, http.StatusUnprocessableEntity)
260 c.Check(rr.Body.String(), check.Not(check.Matches), `.*"uuid":.*`)
261 _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
262 c.Check(rr.Code, check.Equals, http.StatusOK)
263 c.Check(jresp["uuid"], check.HasLen, 27)
264 c.Check(jresp["state"], check.Equals, "Queued")
265 c.Check(jresp["environment"], check.IsNil)
266 _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
267 c.Check(rr.Code, check.Equals, http.StatusUnprocessableEntity)
268 c.Check(jresp["uuid"], check.IsNil)
271 func (s *RouterIntegrationSuite) TestFullTimestampsInResponse(c *check.C) {
272 uuid := arvadostest.CollectionReplicationDesired2Confirmed2UUID
273 token := arvadostest.ActiveTokenV2
275 _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections/`+uuid, nil, nil)
276 c.Check(rr.Code, check.Equals, http.StatusOK)
277 c.Check(jresp["uuid"], check.Equals, uuid)
278 expectNS := map[string]int{
279 "created_at": 596506000, // fixture says 596506247, but truncated by postgresql
280 "modified_at": 596338000, // fixture says 596338465, but truncated by postgresql
282 for key, ns := range expectNS {
283 mt, ok := jresp[key].(string)
284 c.Logf("jresp[%q] == %q", key, mt)
285 c.Assert(ok, check.Equals, true)
286 t, err := time.Parse(time.RFC3339Nano, mt)
287 c.Check(err, check.IsNil)
288 c.Check(t.Nanosecond(), check.Equals, ns)
292 func (s *RouterIntegrationSuite) TestSelectParam(c *check.C) {
293 uuid := arvadostest.QueuedContainerUUID
294 token := arvadostest.ActiveTokenV2
295 for _, sel := range [][]string{
297 {"uuid", "command", "uuid"},
298 {"", "command", "uuid"},
300 j, err := json.Marshal(sel)
301 c.Assert(err, check.IsNil)
302 _, rr, resp := doRequest(c, s.rtr, token, "GET", "/arvados/v1/containers/"+uuid+"?select="+string(j), nil, nil)
303 c.Check(rr.Code, check.Equals, http.StatusOK)
305 c.Check(resp["kind"], check.Equals, "arvados#container")
306 c.Check(resp["uuid"], check.HasLen, 27)
307 c.Check(resp["command"], check.HasLen, 2)
308 c.Check(resp["mounts"], check.IsNil)
309 _, hasMounts := resp["mounts"]
310 c.Check(hasMounts, check.Equals, false)
314 func (s *RouterIntegrationSuite) TestRouteNotFound(c *check.C) {
315 token := arvadostest.ActiveTokenV2
318 path: "arvados/v1/collections/" + arvadostest.FooCollection + "/error404pls",
321 rr := httptest.NewRecorder()
322 s.rtr.ServeHTTP(rr, req)
323 c.Check(rr.Code, check.Equals, http.StatusNotFound)
324 c.Logf("body: %q", rr.Body.String())
325 var j map[string]interface{}
326 err := json.Unmarshal(rr.Body.Bytes(), &j)
327 c.Check(err, check.IsNil)
328 c.Logf("decoded: %v", j)
329 c.Assert(j["errors"], check.FitsTypeOf, []interface{}{})
330 c.Check(j["errors"].([]interface{})[0], check.Equals, "API endpoint not found")
333 func (s *RouterIntegrationSuite) TestCORS(c *check.C) {
334 token := arvadostest.ActiveTokenV2
337 path: "arvados/v1/collections/" + arvadostest.FooCollection,
338 header: http.Header{"Origin": {"https://example.com"}},
341 rr := httptest.NewRecorder()
342 s.rtr.ServeHTTP(rr, req)
343 c.Check(rr.Code, check.Equals, http.StatusOK)
344 c.Check(rr.Body.String(), check.HasLen, 0)
345 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "*")
346 for _, hdr := range []string{"Authorization", "Content-Type"} {
347 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Matches, ".*"+hdr+".*")
349 for _, method := range []string{"GET", "HEAD", "PUT", "POST", "DELETE"} {
350 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Matches, ".*"+method+".*")
353 for _, unsafe := range []string{"login", "logout", "auth", "auth/foo", "login/?blah"} {
357 header: http.Header{"Origin": {"https://example.com"}},
360 rr := httptest.NewRecorder()
361 s.rtr.ServeHTTP(rr, req)
362 c.Check(rr.Code, check.Equals, http.StatusOK)
363 c.Check(rr.Body.String(), check.HasLen, 0)
364 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "")
365 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Equals, "")
366 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Equals, "")
371 header: http.Header{"Origin": {"https://example.com"}},
374 rr = httptest.NewRecorder()
375 s.rtr.ServeHTTP(rr, req)
376 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "")
377 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Equals, "")
378 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Equals, "")
382 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{}) {
383 req := httptest.NewRequest(method, path, body)
384 for k, v := range hdrs {
387 req.Header.Set("Authorization", "Bearer "+token)
388 rr := httptest.NewRecorder()
389 rtr.ServeHTTP(rr, req)
390 c.Logf("response body: %s", rr.Body.String())
391 var jresp map[string]interface{}
392 err := json.Unmarshal(rr.Body.Bytes(), &jresp)
393 c.Check(err, check.IsNil)
394 return req, rr, jresp