1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.curoverse.com/arvados.git/lib/controller/rpc"
20 "git.curoverse.com/arvados.git/sdk/go/arvados"
21 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
22 "github.com/julienschmidt/httprouter"
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{}
41 mux: httprouter.New(),
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=1&_method=GET",
111 header: http.Header{"Content-Type": {"application/x-www-form-urlencoded"}},
112 shouldCall: "CollectionList",
113 withOptions: arvados.ListOptions{Limit: 123, Offset: 456, IncludeTrash: true, IncludeOldVersions: true},
117 path: "/arvados/v1/collections",
118 shouldStatus: http.StatusMethodNotAllowed,
122 path: "/arvados/v1/collections",
123 shouldStatus: http.StatusMethodNotAllowed,
127 path: "/arvados/v1/collections",
128 shouldStatus: http.StatusMethodNotAllowed,
131 // Reset calls captured in previous trial
132 s.stub = arvadostest.APIStub{}
134 c.Logf("trial: %#v", trial)
135 _, rr, _ := doRequest(c, s.rtr, token, trial.method, trial.path, trial.header, bytes.NewBufferString(trial.body))
136 if trial.shouldStatus == 0 {
137 c.Check(rr.Code, check.Equals, http.StatusOK)
139 c.Check(rr.Code, check.Equals, trial.shouldStatus)
141 calls := s.stub.Calls(nil)
142 if trial.shouldCall == "" {
143 c.Check(calls, check.HasLen, 0)
144 } else if len(calls) != 1 {
145 c.Check(calls, check.HasLen, 1)
147 c.Check(calls[0].Method, isMethodNamed, trial.shouldCall)
148 c.Check(calls[0].Options, check.DeepEquals, trial.withOptions)
153 var _ = check.Suite(&RouterIntegrationSuite{})
155 type RouterIntegrationSuite struct {
159 func (s *RouterIntegrationSuite) SetUpTest(c *check.C) {
160 cluster := &arvados.Cluster{}
161 cluster.TLS.Insecure = true
162 arvadostest.SetServiceURL(&cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
163 url, _ := url.Parse("https://" + os.Getenv("ARVADOS_TEST_API_HOST"))
164 s.rtr = New(rpc.NewConn("zzzzz", url, true, rpc.PassthroughTokenProvider))
167 func (s *RouterIntegrationSuite) TearDownSuite(c *check.C) {
168 err := arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil)
169 c.Check(err, check.IsNil)
172 func (s *RouterIntegrationSuite) TestCollectionResponses(c *check.C) {
173 token := arvadostest.ActiveTokenV2
175 // Check "get collection" response has "kind" key
176 _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"include_trash":true}`))
177 c.Check(rr.Code, check.Equals, http.StatusOK)
178 c.Check(jresp["items"], check.FitsTypeOf, []interface{}{})
179 c.Check(jresp["kind"], check.Equals, "arvados#collectionList")
180 c.Check(jresp["items"].([]interface{})[0].(map[string]interface{})["kind"], check.Equals, "arvados#collection")
182 // Check items in list response have a "kind" key regardless
183 // of whether a uuid/pdh is selected.
184 for _, selectj := range []string{
186 `,"select":["portable_data_hash"]`,
187 `,"select":["name"]`,
188 `,"select":["uuid"]`,
190 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"where":{"uuid":["`+arvadostest.FooCollection+`"]}`+selectj+`}`))
191 c.Check(rr.Code, check.Equals, http.StatusOK)
192 c.Check(jresp["items"], check.FitsTypeOf, []interface{}{})
193 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
194 c.Check(jresp["kind"], check.Equals, "arvados#collectionList")
195 item0 := jresp["items"].([]interface{})[0].(map[string]interface{})
196 c.Check(item0["kind"], check.Equals, "arvados#collection")
197 if selectj == "" || strings.Contains(selectj, "portable_data_hash") {
198 c.Check(item0["portable_data_hash"], check.Equals, arvadostest.FooCollectionPDH)
200 c.Check(item0["portable_data_hash"], check.IsNil)
202 if selectj == "" || strings.Contains(selectj, "name") {
203 c.Check(item0["name"], check.FitsTypeOf, "")
205 c.Check(item0["name"], check.IsNil)
207 if selectj == "" || strings.Contains(selectj, "uuid") {
208 c.Check(item0["uuid"], check.Equals, arvadostest.FooCollection)
210 c.Check(item0["uuid"], check.IsNil)
214 // Check "create collection" response has "kind" key
215 _, 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`))
216 c.Check(rr.Code, check.Equals, http.StatusOK)
217 c.Check(jresp["uuid"], check.FitsTypeOf, "")
218 c.Check(jresp["kind"], check.Equals, "arvados#collection")
221 func (s *RouterIntegrationSuite) TestContainerList(c *check.C) {
222 token := arvadostest.ActiveTokenV2
224 _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?limit=0`, nil, nil)
225 c.Check(rr.Code, check.Equals, http.StatusOK)
226 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
227 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
228 c.Check(jresp["items"], check.HasLen, 0)
230 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?limit=2&select=["uuid","command"]`, nil, nil)
231 c.Check(rr.Code, check.Equals, http.StatusOK)
232 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
233 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
234 c.Check(jresp["items"], check.HasLen, 2)
235 item0 := jresp["items"].([]interface{})[0].(map[string]interface{})
236 c.Check(item0["uuid"], check.HasLen, 27)
237 c.Check(item0["command"], check.FitsTypeOf, []interface{}{})
238 c.Check(item0["command"].([]interface{})[0], check.FitsTypeOf, "")
239 c.Check(item0["mounts"], check.IsNil)
241 _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers`, nil, nil)
242 c.Check(rr.Code, check.Equals, http.StatusOK)
243 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
244 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
245 avail := int(jresp["items_available"].(float64))
246 c.Check(jresp["items"], check.HasLen, avail)
247 item0 = jresp["items"].([]interface{})[0].(map[string]interface{})
248 c.Check(item0["uuid"], check.HasLen, 27)
249 c.Check(item0["command"], check.FitsTypeOf, []interface{}{})
250 c.Check(item0["command"].([]interface{})[0], check.FitsTypeOf, "")
251 c.Check(item0["mounts"], check.NotNil)
254 func (s *RouterIntegrationSuite) TestContainerLock(c *check.C) {
255 uuid := arvadostest.QueuedContainerUUID
256 token := arvadostest.AdminToken
257 _, rr, jresp := doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
258 c.Check(rr.Code, check.Equals, http.StatusOK)
259 c.Check(jresp["uuid"], check.HasLen, 27)
260 c.Check(jresp["state"], check.Equals, "Locked")
261 _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
262 c.Check(rr.Code, check.Equals, http.StatusUnprocessableEntity)
263 c.Check(rr.Body.String(), check.Not(check.Matches), `.*"uuid":.*`)
264 _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
265 c.Check(rr.Code, check.Equals, http.StatusOK)
266 c.Check(jresp["uuid"], check.HasLen, 27)
267 c.Check(jresp["state"], check.Equals, "Queued")
268 c.Check(jresp["environment"], check.IsNil)
269 _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
270 c.Check(rr.Code, check.Equals, http.StatusUnprocessableEntity)
271 c.Check(jresp["uuid"], check.IsNil)
274 func (s *RouterIntegrationSuite) TestFullTimestampsInResponse(c *check.C) {
275 uuid := arvadostest.CollectionReplicationDesired2Confirmed2UUID
276 token := arvadostest.ActiveTokenV2
278 _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections/`+uuid, nil, nil)
279 c.Check(rr.Code, check.Equals, http.StatusOK)
280 c.Check(jresp["uuid"], check.Equals, uuid)
281 expectNS := map[string]int{
282 "created_at": 596506000, // fixture says 596506247, but truncated by postgresql
283 "modified_at": 596338000, // fixture says 596338465, but truncated by postgresql
285 for key, ns := range expectNS {
286 mt, ok := jresp[key].(string)
287 c.Logf("jresp[%q] == %q", key, mt)
288 c.Assert(ok, check.Equals, true)
289 t, err := time.Parse(time.RFC3339Nano, mt)
290 c.Check(err, check.IsNil)
291 c.Check(t.Nanosecond(), check.Equals, ns)
295 func (s *RouterIntegrationSuite) TestSelectParam(c *check.C) {
296 uuid := arvadostest.QueuedContainerUUID
297 token := arvadostest.ActiveTokenV2
298 for _, sel := range [][]string{
300 {"uuid", "command", "uuid"},
301 {"", "command", "uuid"},
303 j, err := json.Marshal(sel)
304 c.Assert(err, check.IsNil)
305 _, rr, resp := doRequest(c, s.rtr, token, "GET", "/arvados/v1/containers/"+uuid+"?select="+string(j), nil, nil)
306 c.Check(rr.Code, check.Equals, http.StatusOK)
308 c.Check(resp["kind"], check.Equals, "arvados#container")
309 c.Check(resp["uuid"], check.HasLen, 27)
310 c.Check(resp["command"], check.HasLen, 2)
311 c.Check(resp["mounts"], check.IsNil)
312 _, hasMounts := resp["mounts"]
313 c.Check(hasMounts, check.Equals, false)
317 func (s *RouterIntegrationSuite) TestRouteNotFound(c *check.C) {
318 token := arvadostest.ActiveTokenV2
321 path: "arvados/v1/collections/" + arvadostest.FooCollection + "/error404pls",
324 rr := httptest.NewRecorder()
325 s.rtr.ServeHTTP(rr, req)
326 c.Check(rr.Code, check.Equals, http.StatusNotFound)
327 c.Logf("body: %q", rr.Body.String())
328 var j map[string]interface{}
329 err := json.Unmarshal(rr.Body.Bytes(), &j)
330 c.Check(err, check.IsNil)
331 c.Logf("decoded: %v", j)
332 c.Assert(j["errors"], check.FitsTypeOf, []interface{}{})
333 c.Check(j["errors"].([]interface{})[0], check.Equals, "API endpoint not found")
336 func (s *RouterIntegrationSuite) TestCORS(c *check.C) {
337 token := arvadostest.ActiveTokenV2
340 path: "arvados/v1/collections/" + arvadostest.FooCollection,
341 header: http.Header{"Origin": {"https://example.com"}},
344 rr := httptest.NewRecorder()
345 s.rtr.ServeHTTP(rr, req)
346 c.Check(rr.Code, check.Equals, http.StatusOK)
347 c.Check(rr.Body.String(), check.HasLen, 0)
348 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "*")
349 for _, hdr := range []string{"Authorization", "Content-Type"} {
350 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Matches, ".*"+hdr+".*")
352 for _, method := range []string{"GET", "HEAD", "PUT", "POST", "DELETE"} {
353 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Matches, ".*"+method+".*")
356 for _, unsafe := range []string{"login", "logout", "auth", "auth/foo", "login/?blah"} {
360 header: http.Header{"Origin": {"https://example.com"}},
363 rr := httptest.NewRecorder()
364 s.rtr.ServeHTTP(rr, req)
365 c.Check(rr.Code, check.Equals, http.StatusOK)
366 c.Check(rr.Body.String(), check.HasLen, 0)
367 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "")
368 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Equals, "")
369 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Equals, "")
374 header: http.Header{"Origin": {"https://example.com"}},
377 rr = httptest.NewRecorder()
378 s.rtr.ServeHTTP(rr, req)
379 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "")
380 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Equals, "")
381 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Equals, "")
385 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{}) {
386 req := httptest.NewRequest(method, path, body)
387 for k, v := range hdrs {
390 req.Header.Set("Authorization", "Bearer "+token)
391 rr := httptest.NewRecorder()
392 rtr.ServeHTTP(rr, req)
393 c.Logf("response body: %s", rr.Body.String())
394 var jresp map[string]interface{}
395 err := json.Unmarshal(rr.Body.Bytes(), &jresp)
396 c.Check(err, check.IsNil)
397 return req, rr, jresp