Merge branch '15209-python-arv-deps-pinned'
[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         "os"
14         "strings"
15         "testing"
16         "time"
17
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"
22 )
23
24 // Gocheck boilerplate
25 func Test(t *testing.T) {
26         check.TestingT(t)
27 }
28
29 var _ = check.Suite(&RouterSuite{})
30
31 type RouterSuite struct {
32         rtr  *router
33         stub arvadostest.APIStub
34 }
35
36 func (s *RouterSuite) SetUpTest(c *check.C) {
37         s.stub = arvadostest.APIStub{}
38         s.rtr = &router{
39                 mux: httprouter.New(),
40                 fed: &s.stub,
41         }
42         s.rtr.addRoutes()
43 }
44
45 func (s *RouterSuite) TestOptions(c *check.C) {
46         token := arvadostest.ActiveToken
47         for _, trial := range []struct {
48                 method       string
49                 path         string
50                 header       http.Header
51                 body         string
52                 shouldStatus int // zero value means 200
53                 shouldCall   string
54                 withOptions  interface{}
55         }{
56                 {
57                         method:      "GET",
58                         path:        "/arvados/v1/collections/" + arvadostest.FooCollection,
59                         shouldCall:  "CollectionGet",
60                         withOptions: arvados.GetOptions{UUID: arvadostest.FooCollection},
61                 },
62                 {
63                         method:      "PUT",
64                         path:        "/arvados/v1/collections/" + arvadostest.FooCollection,
65                         shouldCall:  "CollectionUpdate",
66                         withOptions: arvados.UpdateOptions{UUID: arvadostest.FooCollection},
67                 },
68                 {
69                         method:      "PATCH",
70                         path:        "/arvados/v1/collections/" + arvadostest.FooCollection,
71                         shouldCall:  "CollectionUpdate",
72                         withOptions: arvados.UpdateOptions{UUID: arvadostest.FooCollection},
73                 },
74                 {
75                         method:      "DELETE",
76                         path:        "/arvados/v1/collections/" + arvadostest.FooCollection,
77                         shouldCall:  "CollectionDelete",
78                         withOptions: arvados.DeleteOptions{UUID: arvadostest.FooCollection},
79                 },
80                 {
81                         method:      "POST",
82                         path:        "/arvados/v1/collections",
83                         shouldCall:  "CollectionCreate",
84                         withOptions: arvados.CreateOptions{},
85                 },
86                 {
87                         method:      "GET",
88                         path:        "/arvados/v1/collections",
89                         shouldCall:  "CollectionList",
90                         withOptions: arvados.ListOptions{Limit: -1},
91                 },
92                 {
93                         method:      "GET",
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},
97                 },
98                 {
99                         method:      "POST",
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},
104                 },
105                 {
106                         method:      "POST",
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},
112                 },
113                 {
114                         method:       "PATCH",
115                         path:         "/arvados/v1/collections",
116                         shouldStatus: http.StatusMethodNotAllowed,
117                 },
118                 {
119                         method:       "PUT",
120                         path:         "/arvados/v1/collections",
121                         shouldStatus: http.StatusMethodNotAllowed,
122                 },
123                 {
124                         method:       "DELETE",
125                         path:         "/arvados/v1/collections",
126                         shouldStatus: http.StatusMethodNotAllowed,
127                 },
128         } {
129                 // Reset calls captured in previous trial
130                 s.stub = arvadostest.APIStub{}
131
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)
136                 } else {
137                         c.Check(rr.Code, check.Equals, trial.shouldStatus)
138                 }
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)
144                 } else {
145                         c.Check(calls[0].Method, isMethodNamed, trial.shouldCall)
146                         c.Check(calls[0].Options, check.DeepEquals, trial.withOptions)
147                 }
148         }
149 }
150
151 var _ = check.Suite(&RouterIntegrationSuite{})
152
153 type RouterIntegrationSuite struct {
154         rtr *router
155 }
156
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"))
161         s.rtr = New(cluster)
162 }
163
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)
167 }
168
169 func (s *RouterIntegrationSuite) TestCollectionResponses(c *check.C) {
170         token := arvadostest.ActiveTokenV2
171
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")
178
179         // Check items in list response have a "kind" key regardless
180         // of whether a uuid/pdh is selected.
181         for _, selectj := range []string{
182                 ``,
183                 `,"select":["portable_data_hash"]`,
184                 `,"select":["name"]`,
185                 `,"select":["uuid"]`,
186         } {
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)
196                 } else {
197                         c.Check(item0["portable_data_hash"], check.IsNil)
198                 }
199                 if selectj == "" || strings.Contains(selectj, "name") {
200                         c.Check(item0["name"], check.FitsTypeOf, "")
201                 } else {
202                         c.Check(item0["name"], check.IsNil)
203                 }
204                 if selectj == "" || strings.Contains(selectj, "uuid") {
205                         c.Check(item0["uuid"], check.Equals, arvadostest.FooCollection)
206                 } else {
207                         c.Check(item0["uuid"], check.IsNil)
208                 }
209         }
210
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")
216 }
217
218 func (s *RouterIntegrationSuite) TestContainerList(c *check.C) {
219         token := arvadostest.ActiveTokenV2
220
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)
226
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)
237
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)
249 }
250
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)
269 }
270
271 func (s *RouterIntegrationSuite) TestFullTimestampsInResponse(c *check.C) {
272         uuid := arvadostest.CollectionReplicationDesired2Confirmed2UUID
273         token := arvadostest.ActiveTokenV2
274
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
281         }
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)
289         }
290 }
291
292 func (s *RouterIntegrationSuite) TestSelectParam(c *check.C) {
293         uuid := arvadostest.QueuedContainerUUID
294         token := arvadostest.ActiveTokenV2
295         for _, sel := range [][]string{
296                 {"uuid", "command"},
297                 {"uuid", "command", "uuid"},
298                 {"", "command", "uuid"},
299         } {
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)
304
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)
311         }
312 }
313
314 func (s *RouterIntegrationSuite) TestRouteNotFound(c *check.C) {
315         token := arvadostest.ActiveTokenV2
316         req := (&testReq{
317                 method: "POST",
318                 path:   "arvados/v1/collections/" + arvadostest.FooCollection + "/error404pls",
319                 token:  token,
320         }).Request()
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")
331 }
332
333 func (s *RouterIntegrationSuite) TestCORS(c *check.C) {
334         token := arvadostest.ActiveTokenV2
335         req := (&testReq{
336                 method: "OPTIONS",
337                 path:   "arvados/v1/collections/" + arvadostest.FooCollection,
338                 header: http.Header{"Origin": {"https://example.com"}},
339                 token:  token,
340         }).Request()
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+".*")
348         }
349         for _, method := range []string{"GET", "HEAD", "PUT", "POST", "DELETE"} {
350                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Matches, ".*"+method+".*")
351         }
352
353         for _, unsafe := range []string{"login", "logout", "auth", "auth/foo", "login/?blah"} {
354                 req := (&testReq{
355                         method: "OPTIONS",
356                         path:   unsafe,
357                         header: http.Header{"Origin": {"https://example.com"}},
358                         token:  token,
359                 }).Request()
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, "")
367
368                 req = (&testReq{
369                         method: "POST",
370                         path:   unsafe,
371                         header: http.Header{"Origin": {"https://example.com"}},
372                         token:  token,
373                 }).Request()
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, "")
379         }
380 }
381
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 {
385                 req.Header[k] = v
386         }
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
395 }