14287: Skip database reset between tests.
[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         check "gopkg.in/check.v1"
21 )
22
23 // Gocheck boilerplate
24 func Test(t *testing.T) {
25         check.TestingT(t)
26 }
27
28 var _ = check.Suite(&RouterSuite{})
29
30 type RouterSuite struct {
31         rtr *router
32 }
33
34 func (s *RouterSuite) SetUpTest(c *check.C) {
35         cluster := &arvados.Cluster{
36                 TLS: arvados.TLS{Insecure: true},
37         }
38         arvadostest.SetServiceURL(&cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
39         s.rtr = New(cluster)
40 }
41
42 func (s *RouterSuite) TearDownSuite(c *check.C) {
43         err := arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil)
44         c.Check(err, check.IsNil)
45 }
46
47 func (s *RouterSuite) doRequest(c *check.C, token, method, path string, hdrs http.Header, body io.Reader) (*http.Request, *httptest.ResponseRecorder, map[string]interface{}) {
48         req := httptest.NewRequest(method, path, body)
49         for k, v := range hdrs {
50                 req.Header[k] = v
51         }
52         req.Header.Set("Authorization", "Bearer "+token)
53         rr := httptest.NewRecorder()
54         s.rtr.ServeHTTP(rr, req)
55         c.Logf("response body: %s", rr.Body.String())
56         var jresp map[string]interface{}
57         err := json.Unmarshal(rr.Body.Bytes(), &jresp)
58         c.Check(err, check.IsNil)
59         return req, rr, jresp
60 }
61
62 func (s *RouterSuite) TestCollectionResponses(c *check.C) {
63         token := arvadostest.ActiveTokenV2
64
65         // Check "get collection" response has "kind" key
66         _, rr, jresp := s.doRequest(c, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"include_trash":true}`))
67         c.Check(rr.Code, check.Equals, http.StatusOK)
68         c.Check(jresp["items"], check.FitsTypeOf, []interface{}{})
69         c.Check(jresp["kind"], check.Equals, "arvados#collectionList")
70         c.Check(jresp["items"].([]interface{})[0].(map[string]interface{})["kind"], check.Equals, "arvados#collection")
71
72         // Check items in list response have a "kind" key regardless
73         // of whether a uuid/pdh is selected.
74         for _, selectj := range []string{
75                 ``,
76                 `,"select":["portable_data_hash"]`,
77                 `,"select":["name"]`,
78                 `,"select":["uuid"]`,
79         } {
80                 _, rr, jresp = s.doRequest(c, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"where":{"uuid":["`+arvadostest.FooCollection+`"]}`+selectj+`}`))
81                 c.Check(rr.Code, check.Equals, http.StatusOK)
82                 c.Check(jresp["items"], check.FitsTypeOf, []interface{}{})
83                 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
84                 c.Check(jresp["kind"], check.Equals, "arvados#collectionList")
85                 item0 := jresp["items"].([]interface{})[0].(map[string]interface{})
86                 c.Check(item0["kind"], check.Equals, "arvados#collection")
87                 if selectj == "" || strings.Contains(selectj, "portable_data_hash") {
88                         c.Check(item0["portable_data_hash"], check.Equals, arvadostest.FooCollectionPDH)
89                 } else {
90                         c.Check(item0["portable_data_hash"], check.IsNil)
91                 }
92                 if selectj == "" || strings.Contains(selectj, "name") {
93                         c.Check(item0["name"], check.FitsTypeOf, "")
94                 } else {
95                         c.Check(item0["name"], check.IsNil)
96                 }
97                 if selectj == "" || strings.Contains(selectj, "uuid") {
98                         c.Check(item0["uuid"], check.Equals, arvadostest.FooCollection)
99                 } else {
100                         c.Check(item0["uuid"], check.IsNil)
101                 }
102         }
103
104         // Check "create collection" response has "kind" key
105         _, rr, jresp = s.doRequest(c, token, "POST", `/arvados/v1/collections`, http.Header{"Content-Type": {"application/x-www-form-urlencoded"}}, bytes.NewBufferString(`ensure_unique_name=true`))
106         c.Check(rr.Code, check.Equals, http.StatusOK)
107         c.Check(jresp["uuid"], check.FitsTypeOf, "")
108         c.Check(jresp["kind"], check.Equals, "arvados#collection")
109 }
110
111 func (s *RouterSuite) TestContainerList(c *check.C) {
112         token := arvadostest.ActiveTokenV2
113
114         _, rr, jresp := s.doRequest(c, token, "GET", `/arvados/v1/containers?limit=0`, nil, nil)
115         c.Check(rr.Code, check.Equals, http.StatusOK)
116         c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
117         c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
118         c.Check(jresp["items"], check.HasLen, 0)
119
120         _, rr, jresp = s.doRequest(c, token, "GET", `/arvados/v1/containers?limit=2&select=["uuid","command"]`, nil, nil)
121         c.Check(rr.Code, check.Equals, http.StatusOK)
122         c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
123         c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
124         c.Check(jresp["items"], check.HasLen, 2)
125         item0 := jresp["items"].([]interface{})[0].(map[string]interface{})
126         c.Check(item0["uuid"], check.HasLen, 27)
127         c.Check(item0["command"], check.FitsTypeOf, []interface{}{})
128         c.Check(item0["command"].([]interface{})[0], check.FitsTypeOf, "")
129         c.Check(item0["mounts"], check.IsNil)
130
131         _, rr, jresp = s.doRequest(c, token, "GET", `/arvados/v1/containers`, nil, nil)
132         c.Check(rr.Code, check.Equals, http.StatusOK)
133         c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
134         c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
135         avail := int(jresp["items_available"].(float64))
136         c.Check(jresp["items"], check.HasLen, avail)
137         item0 = jresp["items"].([]interface{})[0].(map[string]interface{})
138         c.Check(item0["uuid"], check.HasLen, 27)
139         c.Check(item0["command"], check.FitsTypeOf, []interface{}{})
140         c.Check(item0["command"].([]interface{})[0], check.FitsTypeOf, "")
141         c.Check(item0["mounts"], check.NotNil)
142 }
143
144 func (s *RouterSuite) TestContainerLock(c *check.C) {
145         uuid := arvadostest.QueuedContainerUUID
146         token := arvadostest.AdminToken
147         _, rr, jresp := s.doRequest(c, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
148         c.Check(rr.Code, check.Equals, http.StatusOK)
149         c.Check(jresp["uuid"], check.HasLen, 27)
150         c.Check(jresp["state"], check.Equals, "Locked")
151         _, rr, jresp = s.doRequest(c, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
152         c.Check(rr.Code, check.Equals, http.StatusUnprocessableEntity)
153         c.Check(rr.Body.String(), check.Not(check.Matches), `.*"uuid":.*`)
154         _, rr, jresp = s.doRequest(c, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
155         c.Check(rr.Code, check.Equals, http.StatusOK)
156         c.Check(jresp["uuid"], check.HasLen, 27)
157         c.Check(jresp["state"], check.Equals, "Queued")
158         c.Check(jresp["environment"], check.IsNil)
159         _, rr, jresp = s.doRequest(c, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
160         c.Check(rr.Code, check.Equals, http.StatusUnprocessableEntity)
161         c.Check(jresp["uuid"], check.IsNil)
162 }
163
164 func (s *RouterSuite) TestFullTimestampsInResponse(c *check.C) {
165         uuid := arvadostest.CollectionReplicationDesired2Confirmed2UUID
166         token := arvadostest.ActiveTokenV2
167
168         _, rr, jresp := s.doRequest(c, token, "GET", `/arvados/v1/collections/`+uuid, nil, nil)
169         c.Check(rr.Code, check.Equals, http.StatusOK)
170         c.Check(jresp["uuid"], check.Equals, uuid)
171         expectNS := map[string]int{
172                 "created_at":  596506000, // fixture says 596506247, but truncated by postgresql
173                 "modified_at": 596338000, // fixture says 596338465, but truncated by postgresql
174         }
175         for key, ns := range expectNS {
176                 mt, ok := jresp[key].(string)
177                 c.Logf("jresp[%q] == %q", key, mt)
178                 c.Assert(ok, check.Equals, true)
179                 t, err := time.Parse(time.RFC3339Nano, mt)
180                 c.Check(err, check.IsNil)
181                 c.Check(t.Nanosecond(), check.Equals, ns)
182         }
183 }
184
185 func (s *RouterSuite) TestSelectParam(c *check.C) {
186         uuid := arvadostest.QueuedContainerUUID
187         token := arvadostest.ActiveTokenV2
188         for _, sel := range [][]string{
189                 {"uuid", "command"},
190                 {"uuid", "command", "uuid"},
191                 {"", "command", "uuid"},
192         } {
193                 j, err := json.Marshal(sel)
194                 c.Assert(err, check.IsNil)
195                 _, rr, resp := s.doRequest(c, token, "GET", "/arvados/v1/containers/"+uuid+"?select="+string(j), nil, nil)
196                 c.Check(rr.Code, check.Equals, http.StatusOK)
197
198                 c.Check(resp["kind"], check.Equals, "arvados#container")
199                 c.Check(resp["uuid"], check.HasLen, 27)
200                 c.Check(resp["command"], check.HasLen, 2)
201                 c.Check(resp["mounts"], check.IsNil)
202                 _, hasMounts := resp["mounts"]
203                 c.Check(hasMounts, check.Equals, false)
204         }
205 }
206
207 func (s *RouterSuite) TestRouteNotFound(c *check.C) {
208         token := arvadostest.ActiveTokenV2
209         req := (&testReq{
210                 method: "POST",
211                 path:   "arvados/v1/collections/" + arvadostest.FooCollection + "/error404pls",
212                 token:  token,
213         }).Request()
214         rr := httptest.NewRecorder()
215         s.rtr.ServeHTTP(rr, req)
216         c.Check(rr.Code, check.Equals, http.StatusNotFound)
217         c.Logf("body: %q", rr.Body.String())
218         var j map[string]interface{}
219         err := json.Unmarshal(rr.Body.Bytes(), &j)
220         c.Check(err, check.IsNil)
221         c.Logf("decoded: %v", j)
222         c.Assert(j["errors"], check.FitsTypeOf, []interface{}{})
223         c.Check(j["errors"].([]interface{})[0], check.Equals, "API endpoint not found")
224 }
225
226 func (s *RouterSuite) TestCORS(c *check.C) {
227         token := arvadostest.ActiveTokenV2
228         req := (&testReq{
229                 method: "OPTIONS",
230                 path:   "arvados/v1/collections/" + arvadostest.FooCollection,
231                 header: http.Header{"Origin": {"https://example.com"}},
232                 token:  token,
233         }).Request()
234         rr := httptest.NewRecorder()
235         s.rtr.ServeHTTP(rr, req)
236         c.Check(rr.Code, check.Equals, http.StatusOK)
237         c.Check(rr.Body.String(), check.HasLen, 0)
238         c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "*")
239         for _, hdr := range []string{"Authorization", "Content-Type"} {
240                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Matches, ".*"+hdr+".*")
241         }
242         for _, method := range []string{"GET", "HEAD", "PUT", "POST", "DELETE"} {
243                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Matches, ".*"+method+".*")
244         }
245
246         for _, unsafe := range []string{"login", "logout", "auth", "auth/foo", "login/?blah"} {
247                 req := (&testReq{
248                         method: "OPTIONS",
249                         path:   unsafe,
250                         header: http.Header{"Origin": {"https://example.com"}},
251                         token:  token,
252                 }).Request()
253                 rr := httptest.NewRecorder()
254                 s.rtr.ServeHTTP(rr, req)
255                 c.Check(rr.Code, check.Equals, http.StatusOK)
256                 c.Check(rr.Body.String(), check.HasLen, 0)
257                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "")
258                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Equals, "")
259                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Equals, "")
260
261                 req = (&testReq{
262                         method: "POST",
263                         path:   unsafe,
264                         header: http.Header{"Origin": {"https://example.com"}},
265                         token:  token,
266                 }).Request()
267                 rr = httptest.NewRecorder()
268                 s.rtr.ServeHTTP(rr, req)
269                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Origin"), check.Equals, "")
270                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Methods"), check.Equals, "")
271                 c.Check(rr.Result().Header.Get("Access-Control-Allow-Headers"), check.Equals, "")
272         }
273 }