1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
18 "git.curoverse.com/arvados.git/sdk/go/arvados"
19 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
20 check "gopkg.in/check.v1"
23 // Gocheck boilerplate
24 func Test(t *testing.T) {
28 var _ = check.Suite(&RouterSuite{})
30 type RouterSuite struct {
34 func (s *RouterSuite) SetUpTest(c *check.C) {
35 cluster := &arvados.Cluster{
36 TLS: arvados.TLS{Insecure: true},
38 arvadostest.SetServiceURL(&cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
42 func (s *RouterSuite) TearDownTest(c *check.C) {
43 err := arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil)
44 c.Check(err, check.IsNil)
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 {
52 req.Header.Set("Authorization", "Bearer "+token)
53 rw := httptest.NewRecorder()
54 s.rtr.ServeHTTP(rw, req)
55 c.Logf("response body: %s", rw.Body.String())
56 var jresp map[string]interface{}
57 err := json.Unmarshal(rw.Body.Bytes(), &jresp)
58 c.Check(err, check.IsNil)
62 func (s *RouterSuite) TestCollectionResponses(c *check.C) {
63 token := arvadostest.ActiveTokenV2
65 // Check "get collection" response has "kind" key
66 _, rw, jresp := s.doRequest(c, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"include_trash":true}`))
67 c.Check(rw.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")
72 // Check items in list response have a "kind" key regardless
73 // of whether a uuid/pdh is selected.
74 for _, selectj := range []string{
76 `,"select":["portable_data_hash"]`,
80 _, rw, jresp = s.doRequest(c, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"where":{"uuid":["`+arvadostest.FooCollection+`"]}`+selectj+`}`))
81 c.Check(rw.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)
90 c.Check(item0["portable_data_hash"], check.IsNil)
92 if selectj == "" || strings.Contains(selectj, "name") {
93 c.Check(item0["name"], check.FitsTypeOf, "")
95 c.Check(item0["name"], check.IsNil)
97 if selectj == "" || strings.Contains(selectj, "uuid") {
98 c.Check(item0["uuid"], check.Equals, arvadostest.FooCollection)
100 c.Check(item0["uuid"], check.IsNil)
104 // Check "create collection" response has "kind" key
105 _, rw, 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(rw.Code, check.Equals, http.StatusOK)
107 c.Check(jresp["uuid"], check.FitsTypeOf, "")
108 c.Check(jresp["kind"], check.Equals, "arvados#collection")
111 func (s *RouterSuite) TestContainerList(c *check.C) {
112 token := arvadostest.ActiveTokenV2
114 _, rw, jresp := s.doRequest(c, token, "GET", `/arvados/v1/containers?limit=0`, nil, nil)
115 c.Check(rw.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)
120 _, rw, jresp = s.doRequest(c, token, "GET", `/arvados/v1/containers?limit=2&select=["uuid","command"]`, nil, nil)
121 c.Check(rw.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)
131 _, rw, jresp = s.doRequest(c, token, "GET", `/arvados/v1/containers`, nil, nil)
132 c.Check(rw.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)
144 func (s *RouterSuite) TestContainerLock(c *check.C) {
145 uuid := arvadostest.QueuedContainerUUID
146 token := arvadostest.AdminToken
147 _, rw, jresp := s.doRequest(c, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
148 c.Check(rw.Code, check.Equals, http.StatusOK)
149 c.Check(jresp["uuid"], check.HasLen, 27)
150 c.Check(jresp["state"], check.Equals, "Locked")
151 _, rw, jresp = s.doRequest(c, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
152 c.Check(rw.Code, check.Equals, http.StatusUnprocessableEntity)
153 c.Check(rw.Body.String(), check.Not(check.Matches), `.*"uuid":.*`)
154 _, rw, jresp = s.doRequest(c, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
155 c.Check(rw.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 _, rw, jresp = s.doRequest(c, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
160 c.Check(rw.Code, check.Equals, http.StatusUnprocessableEntity)
161 c.Check(jresp["uuid"], check.IsNil)
164 func (s *RouterSuite) TestFullTimestampsInResponse(c *check.C) {
165 uuid := arvadostest.CollectionReplicationDesired2Confirmed2UUID
166 token := arvadostest.ActiveTokenV2
168 _, rw, jresp := s.doRequest(c, token, "GET", `/arvados/v1/collections/`+uuid, nil, nil)
169 c.Check(rw.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
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)
185 func (s *RouterSuite) TestSelectParam(c *check.C) {
186 uuid := arvadostest.QueuedContainerUUID
187 token := arvadostest.ActiveTokenV2
188 for _, sel := range [][]string{
190 {"uuid", "command", "uuid"},
191 {"", "command", "uuid"},
193 j, err := json.Marshal(sel)
194 c.Assert(err, check.IsNil)
195 _, rw, resp := s.doRequest(c, token, "GET", "/arvados/v1/containers/"+uuid+"?select="+string(j), nil, nil)
196 c.Check(rw.Code, check.Equals, http.StatusOK)
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)