15720: Merge branch 'master' into 15720-fed-user-list
[arvados.git] / lib / controller / router / router_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package router
6
7 import (
8         "bytes"
9         "encoding/json"
10         "io"
11         "net/http"
12         "net/http/httptest"
13         "net/url"
14         "os"
15         "strings"
16         "testing"
17         "time"
18
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/gorilla/mux"
23         check "gopkg.in/check.v1"
24 )
25
26 // Gocheck boilerplate
27 func Test(t *testing.T) {
28         check.TestingT(t)
29 }
30
31 var _ = check.Suite(&RouterSuite{})
32
33 type RouterSuite struct {
34         rtr  *router
35         stub arvadostest.APIStub
36 }
37
38 func (s *RouterSuite) SetUpTest(c *check.C) {
39         s.stub = arvadostest.APIStub{}
40         s.rtr = &router{
41                 mux: mux.NewRouter(),
42                 fed: &s.stub,
43         }
44         s.rtr.addRoutes()
45 }
46
47 func (s *RouterSuite) TestOptions(c *check.C) {
48         token := arvadostest.ActiveToken
49         for _, trial := range []struct {
50                 method       string
51                 path         string
52                 header       http.Header
53                 body         string
54                 shouldStatus int // zero value means 200
55                 shouldCall   string
56                 withOptions  interface{}
57         }{
58                 {
59                         method:      "GET",
60                         path:        "/arvados/v1/collections/" + arvadostest.FooCollection,
61                         shouldCall:  "CollectionGet",
62                         withOptions: arvados.GetOptions{UUID: arvadostest.FooCollection},
63                 },
64                 {
65                         method:      "PUT",
66                         path:        "/arvados/v1/collections/" + arvadostest.FooCollection,
67                         shouldCall:  "CollectionUpdate",
68                         withOptions: arvados.UpdateOptions{UUID: arvadostest.FooCollection},
69                 },
70                 {
71                         method:      "PATCH",
72                         path:        "/arvados/v1/collections/" + arvadostest.FooCollection,
73                         shouldCall:  "CollectionUpdate",
74                         withOptions: arvados.UpdateOptions{UUID: arvadostest.FooCollection},
75                 },
76                 {
77                         method:      "DELETE",
78                         path:        "/arvados/v1/collections/" + arvadostest.FooCollection,
79                         shouldCall:  "CollectionDelete",
80                         withOptions: arvados.DeleteOptions{UUID: arvadostest.FooCollection},
81                 },
82                 {
83                         method:      "POST",
84                         path:        "/arvados/v1/collections",
85                         shouldCall:  "CollectionCreate",
86                         withOptions: arvados.CreateOptions{},
87                 },
88                 {
89                         method:      "GET",
90                         path:        "/arvados/v1/collections",
91                         shouldCall:  "CollectionList",
92                         withOptions: arvados.ListOptions{Limit: -1},
93                 },
94                 {
95                         method:      "GET",
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},
99                 },
100                 {
101                         method:      "POST",
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},
106                 },
107                 {
108                         method:      "POST",
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},
114                 },
115                 {
116                         method:       "PATCH",
117                         path:         "/arvados/v1/collections",
118                         shouldStatus: http.StatusMethodNotAllowed,
119                 },
120                 {
121                         method:       "PUT",
122                         path:         "/arvados/v1/collections",
123                         shouldStatus: http.StatusMethodNotAllowed,
124                 },
125                 {
126                         method:       "DELETE",
127                         path:         "/arvados/v1/collections",
128                         shouldStatus: http.StatusMethodNotAllowed,
129                 },
130         } {
131                 // Reset calls captured in previous trial
132                 s.stub = arvadostest.APIStub{}
133
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)
138                 } else {
139                         c.Check(rr.Code, check.Equals, trial.shouldStatus)
140                 }
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)
146                 } else {
147                         c.Check(calls[0].Method, isMethodNamed, trial.shouldCall)
148                         c.Check(calls[0].Options, check.DeepEquals, trial.withOptions)
149                 }
150         }
151 }
152
153 var _ = check.Suite(&RouterIntegrationSuite{})
154
155 type RouterIntegrationSuite struct {
156         rtr *router
157 }
158
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))
165 }
166
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)
170 }
171
172 func (s *RouterIntegrationSuite) TestCollectionResponses(c *check.C) {
173         token := arvadostest.ActiveTokenV2
174
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")
181
182         // Check items in list response have a "kind" key regardless
183         // of whether a uuid/pdh is selected.
184         for _, selectj := range []string{
185                 ``,
186                 `,"select":["portable_data_hash"]`,
187                 `,"select":["name"]`,
188                 `,"select":["uuid"]`,
189         } {
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)
199                 } else {
200                         c.Check(item0["portable_data_hash"], check.IsNil)
201                 }
202                 if selectj == "" || strings.Contains(selectj, "name") {
203                         c.Check(item0["name"], check.FitsTypeOf, "")
204                 } else {
205                         c.Check(item0["name"], check.IsNil)
206                 }
207                 if selectj == "" || strings.Contains(selectj, "uuid") {
208                         c.Check(item0["uuid"], check.Equals, arvadostest.FooCollection)
209                 } else {
210                         c.Check(item0["uuid"], check.IsNil)
211                 }
212         }
213
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")
219 }
220
221 func (s *RouterIntegrationSuite) TestContainerList(c *check.C) {
222         token := arvadostest.ActiveTokenV2
223
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.NotNil)
229         c.Check(jresp["items"], check.HasLen, 0)
230
231         _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?filters=[["uuid","in",[]]]`, nil, nil)
232         c.Check(rr.Code, check.Equals, http.StatusOK)
233         c.Check(jresp["items_available"], check.Equals, float64(0))
234         c.Check(jresp["items"], check.NotNil)
235         c.Check(jresp["items"], check.HasLen, 0)
236
237         _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers?limit=2&select=["uuid","command"]`, nil, nil)
238         c.Check(rr.Code, check.Equals, http.StatusOK)
239         c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
240         c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
241         c.Check(jresp["items"], check.HasLen, 2)
242         item0 := jresp["items"].([]interface{})[0].(map[string]interface{})
243         c.Check(item0["uuid"], check.HasLen, 27)
244         c.Check(item0["command"], check.FitsTypeOf, []interface{}{})
245         c.Check(item0["command"].([]interface{})[0], check.FitsTypeOf, "")
246         c.Check(item0["mounts"], check.IsNil)
247
248         _, rr, jresp = doRequest(c, s.rtr, token, "GET", `/arvados/v1/containers`, nil, nil)
249         c.Check(rr.Code, check.Equals, http.StatusOK)
250         c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
251         c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
252         avail := int(jresp["items_available"].(float64))
253         c.Check(jresp["items"], check.HasLen, avail)
254         item0 = jresp["items"].([]interface{})[0].(map[string]interface{})
255         c.Check(item0["uuid"], check.HasLen, 27)
256         c.Check(item0["command"], check.FitsTypeOf, []interface{}{})
257         c.Check(item0["command"].([]interface{})[0], check.FitsTypeOf, "")
258         c.Check(item0["mounts"], check.NotNil)
259 }
260
261 func (s *RouterIntegrationSuite) TestContainerLock(c *check.C) {
262         uuid := arvadostest.QueuedContainerUUID
263         token := arvadostest.AdminToken
264         _, rr, jresp := doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", 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, "Locked")
268         _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
269         c.Check(rr.Code, check.Equals, http.StatusUnprocessableEntity)
270         c.Check(rr.Body.String(), check.Not(check.Matches), `.*"uuid":.*`)
271         _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
272         c.Check(rr.Code, check.Equals, http.StatusOK)
273         c.Check(jresp["uuid"], check.HasLen, 27)
274         c.Check(jresp["state"], check.Equals, "Queued")
275         c.Check(jresp["environment"], check.IsNil)
276         _, rr, jresp = doRequest(c, s.rtr, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
277         c.Check(rr.Code, check.Equals, http.StatusUnprocessableEntity)
278         c.Check(jresp["uuid"], check.IsNil)
279 }
280
281 func (s *RouterIntegrationSuite) TestFullTimestampsInResponse(c *check.C) {
282         uuid := arvadostest.CollectionReplicationDesired2Confirmed2UUID
283         token := arvadostest.ActiveTokenV2
284
285         _, rr, jresp := doRequest(c, s.rtr, token, "GET", `/arvados/v1/collections/`+uuid, nil, nil)
286         c.Check(rr.Code, check.Equals, http.StatusOK)
287         c.Check(jresp["uuid"], check.Equals, uuid)
288         expectNS := map[string]int{
289                 "created_at":  596506000, // fixture says 596506247, but truncated by postgresql
290                 "modified_at": 596338000, // fixture says 596338465, but truncated by postgresql
291         }
292         for key, ns := range expectNS {
293                 mt, ok := jresp[key].(string)
294                 c.Logf("jresp[%q] == %q", key, mt)
295                 c.Assert(ok, check.Equals, true)
296                 t, err := time.Parse(time.RFC3339Nano, mt)
297                 c.Check(err, check.IsNil)
298                 c.Check(t.Nanosecond(), check.Equals, ns)
299         }
300 }
301
302 func (s *RouterIntegrationSuite) TestSelectParam(c *check.C) {
303         uuid := arvadostest.QueuedContainerUUID
304         token := arvadostest.ActiveTokenV2
305         for _, sel := range [][]string{
306                 {"uuid", "command"},
307                 {"uuid", "command", "uuid"},
308                 {"", "command", "uuid"},
309         } {
310                 j, err := json.Marshal(sel)
311                 c.Assert(err, check.IsNil)
312                 _, rr, resp := doRequest(c, s.rtr, token, "GET", "/arvados/v1/containers/"+uuid+"?select="+string(j), nil, nil)
313                 c.Check(rr.Code, check.Equals, http.StatusOK)
314
315                 c.Check(resp["kind"], check.Equals, "arvados#container")
316                 c.Check(resp["uuid"], check.HasLen, 27)
317                 c.Check(resp["command"], check.HasLen, 2)
318                 c.Check(resp["mounts"], check.IsNil)
319                 _, hasMounts := resp["mounts"]
320                 c.Check(hasMounts, check.Equals, false)
321         }
322 }
323
324 func (s *RouterIntegrationSuite) TestRouteNotFound(c *check.C) {
325         token := arvadostest.ActiveTokenV2
326         req := (&testReq{
327                 method: "POST",
328                 path:   "arvados/v1/collections/" + arvadostest.FooCollection + "/error404pls",
329                 token:  token,
330         }).Request()
331         rr := httptest.NewRecorder()
332         s.rtr.ServeHTTP(rr, req)
333         c.Check(rr.Code, check.Equals, http.StatusNotFound)
334         c.Logf("body: %q", rr.Body.String())
335         var j map[string]interface{}
336         err := json.Unmarshal(rr.Body.Bytes(), &j)
337         c.Check(err, check.IsNil)
338         c.Logf("decoded: %v", j)
339         c.Assert(j["errors"], check.FitsTypeOf, []interface{}{})
340         c.Check(j["errors"].([]interface{})[0], check.Equals, "API endpoint not found")
341 }
342
343 func (s *RouterIntegrationSuite) TestCORS(c *check.C) {
344         token := arvadostest.ActiveTokenV2
345         req := (&testReq{
346                 method: "OPTIONS",
347                 path:   "arvados/v1/collections/" + arvadostest.FooCollection,
348                 header: http.Header{"Origin": {"https://example.com"}},
349                 token:  token,
350         }).Request()
351         rr := httptest.NewRecorder()
352         s.rtr.ServeHTTP(rr, req)
353         c.Check(rr.Code, check.Equals, http.StatusOK)
354         c.Check(rr.Body.String(), check.HasLen, 0)
355         c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "*")
356         for _, hdr := range []string{"Authorization", "Content-Type"} {
357                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Matches, ".*"+hdr+".*")
358         }
359         for _, method := range []string{"GET", "HEAD", "PUT", "POST", "DELETE"} {
360                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Matches, ".*"+method+".*")
361         }
362
363         for _, unsafe := range []string{"login", "logout", "auth", "auth/foo", "login/?blah"} {
364                 req := (&testReq{
365                         method: "OPTIONS",
366                         path:   unsafe,
367                         header: http.Header{"Origin": {"https://example.com"}},
368                         token:  token,
369                 }).Request()
370                 rr := httptest.NewRecorder()
371                 s.rtr.ServeHTTP(rr, req)
372                 c.Check(rr.Code, check.Equals, http.StatusOK)
373                 c.Check(rr.Body.String(), check.HasLen, 0)
374                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "")
375                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Equals, "")
376                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Equals, "")
377
378                 req = (&testReq{
379                         method: "POST",
380                         path:   unsafe,
381                         header: http.Header{"Origin": {"https://example.com"}},
382                         token:  token,
383                 }).Request()
384                 rr = httptest.NewRecorder()
385                 s.rtr.ServeHTTP(rr, req)
386                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "")
387                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Equals, "")
388                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Equals, "")
389         }
390 }
391
392 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{}) {
393         req := httptest.NewRequest(method, path, body)
394         for k, v := range hdrs {
395                 req.Header[k] = v
396         }
397         req.Header.Set("Authorization", "Bearer "+token)
398         rr := httptest.NewRecorder()
399         rtr.ServeHTTP(rr, req)
400         c.Logf("response body: %s", rr.Body.String())
401         var jresp map[string]interface{}
402         err := json.Unmarshal(rr.Body.Bytes(), &jresp)
403         c.Check(err, check.IsNil)
404         return req, rr, jresp
405 }