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