1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.curoverse.com/arvados.git/sdk/go/arvados"
18 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
19 check "gopkg.in/check.v1"
22 // Gocheck boilerplate
23 func Test(t *testing.T) {
27 var _ = check.Suite(&RouterSuite{})
29 type RouterSuite struct {
33 func (s *RouterSuite) SetUpTest(c *check.C) {
34 cluster := &arvados.Cluster{
35 TLS: arvados.TLS{Insecure: true},
37 arvadostest.SetServiceURL(&cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
41 func (s *RouterSuite) TearDownTest(c *check.C) {
42 err := arvados.NewClientFromEnv().RequestAndDecode(nil, "POST", "database/reset", nil, nil)
43 c.Check(err, check.IsNil)
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 {
51 req.Header.Set("Authorization", "Bearer "+token)
52 rw := httptest.NewRecorder()
53 s.rtr.ServeHTTP(rw, req)
54 c.Logf("response body: %s", rw.Body.String())
55 var jresp map[string]interface{}
56 err := json.Unmarshal(rw.Body.Bytes(), &jresp)
57 c.Check(err, check.IsNil)
61 func (s *RouterSuite) TestCollectionParams(c *check.C) {
62 token := arvadostest.ActiveTokenV2
64 _, rw, jresp := s.doRequest(c, token, "GET", `/arvados/v1/collections?include_trash=true`, nil, nil)
65 c.Check(rw.Code, check.Equals, http.StatusOK)
66 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
67 c.Check(jresp["kind"], check.Equals, "arvados#collectionList")
68 c.Check(jresp["items"].([]interface{})[0].(map[string]interface{})["kind"], check.Equals, "arvados#collection")
70 _, rw, jresp = s.doRequest(c, token, "GET", `/arvados/v1/collections`, nil, bytes.NewBufferString(`{"include_trash":true}`))
71 c.Check(rw.Code, check.Equals, http.StatusOK)
72 c.Check(jresp["items"], check.FitsTypeOf, []interface{}{})
73 c.Check(jresp["kind"], check.Equals, "arvados#collectionList")
74 c.Check(jresp["items"].([]interface{})[0].(map[string]interface{})["kind"], check.Equals, "arvados#collection")
76 _, 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`))
77 c.Check(rw.Code, check.Equals, http.StatusOK)
78 c.Check(jresp["uuid"], check.FitsTypeOf, "")
79 c.Check(jresp["kind"], check.Equals, "arvados#collection")
81 _, rw, jresp = s.doRequest(c, token, "POST", `/arvados/v1/collections?ensure_unique_name=true`, nil, nil)
82 c.Check(rw.Code, check.Equals, http.StatusOK)
83 c.Check(jresp["uuid"], check.FitsTypeOf, "")
84 c.Check(jresp["kind"], check.Equals, "arvados#collection")
87 func (s *RouterSuite) TestContainerList(c *check.C) {
88 token := arvadostest.ActiveTokenV2
90 _, rw, jresp := s.doRequest(c, token, "GET", `/arvados/v1/containers?limit=0`, nil, nil)
91 c.Check(rw.Code, check.Equals, http.StatusOK)
92 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
93 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
94 c.Check(jresp["items"], check.HasLen, 0)
96 _, rw, jresp = s.doRequest(c, token, "GET", `/arvados/v1/containers?limit=2&select=["uuid","command"]`, nil, nil)
97 c.Check(rw.Code, check.Equals, http.StatusOK)
98 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
99 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
100 c.Check(jresp["items"], check.HasLen, 2)
101 item0 := jresp["items"].([]interface{})[0].(map[string]interface{})
102 c.Check(item0["uuid"], check.HasLen, 27)
103 c.Check(item0["command"], check.FitsTypeOf, []interface{}{})
104 c.Check(item0["command"].([]interface{})[0], check.FitsTypeOf, "")
105 c.Check(item0["mounts"], check.IsNil)
107 _, rw, jresp = s.doRequest(c, token, "GET", `/arvados/v1/containers`, nil, nil)
108 c.Check(rw.Code, check.Equals, http.StatusOK)
109 c.Check(jresp["items_available"], check.FitsTypeOf, float64(0))
110 c.Check(jresp["items_available"].(float64) > 2, check.Equals, true)
111 avail := int(jresp["items_available"].(float64))
112 c.Check(jresp["items"], check.HasLen, avail)
113 item0 = jresp["items"].([]interface{})[0].(map[string]interface{})
114 c.Check(item0["uuid"], check.HasLen, 27)
115 c.Check(item0["command"], check.FitsTypeOf, []interface{}{})
116 c.Check(item0["command"].([]interface{})[0], check.FitsTypeOf, "")
117 c.Check(item0["mounts"], check.NotNil)
120 func (s *RouterSuite) TestContainerLock(c *check.C) {
121 uuid := arvadostest.QueuedContainerUUID
122 token := arvadostest.AdminToken
123 _, rw, jresp := s.doRequest(c, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
124 c.Check(rw.Code, check.Equals, http.StatusOK)
125 c.Check(jresp["uuid"], check.HasLen, 27)
126 c.Check(jresp["state"], check.Equals, "Locked")
127 _, rw, jresp = s.doRequest(c, token, "POST", "/arvados/v1/containers/"+uuid+"/lock", nil, nil)
128 c.Check(rw.Code, check.Equals, http.StatusUnprocessableEntity)
129 c.Check(rw.Body.String(), check.Not(check.Matches), `.*"uuid":.*`)
130 _, rw, jresp = s.doRequest(c, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
131 c.Check(rw.Code, check.Equals, http.StatusOK)
132 c.Check(jresp["uuid"], check.HasLen, 27)
133 c.Check(jresp["state"], check.Equals, "Queued")
134 c.Check(jresp["environment"], check.IsNil)
135 _, rw, jresp = s.doRequest(c, token, "POST", "/arvados/v1/containers/"+uuid+"/unlock", nil, nil)
136 c.Check(rw.Code, check.Equals, http.StatusUnprocessableEntity)
137 c.Check(jresp["uuid"], check.IsNil)
140 func (s *RouterSuite) TestFullTimestampsInResponse(c *check.C) {
141 uuid := arvadostest.CollectionReplicationDesired2Confirmed2UUID
142 token := arvadostest.ActiveTokenV2
144 _, rw, jresp := s.doRequest(c, token, "GET", `/arvados/v1/collections/`+uuid, nil, nil)
145 c.Check(rw.Code, check.Equals, http.StatusOK)
146 c.Check(jresp["uuid"], check.Equals, uuid)
147 expectNS := map[string]int{
148 "created_at": 596506000, // fixture says 596506247, but truncated by postgresql
149 "modified_at": 596338000, // fixture says 596338465, but truncated by postgresql
151 for key, ns := range expectNS {
152 mt, ok := jresp[key].(string)
153 c.Logf("jresp[%q] == %q", key, mt)
154 c.Assert(ok, check.Equals, true)
155 t, err := time.Parse(time.RFC3339Nano, mt)
156 c.Check(err, check.IsNil)
157 c.Check(t.Nanosecond(), check.Equals, ns)
161 func (s *RouterSuite) TestSelectParam(c *check.C) {
162 uuid := arvadostest.QueuedContainerUUID
163 token := arvadostest.ActiveTokenV2
164 for _, sel := range [][]string{
166 {"uuid", "command", "uuid"},
167 {"", "command", "uuid"},
169 j, err := json.Marshal(sel)
170 c.Assert(err, check.IsNil)
171 _, rw, resp := s.doRequest(c, token, "GET", "/arvados/v1/containers/"+uuid+"?select="+string(j), nil, nil)
172 c.Check(rw.Code, check.Equals, http.StatusOK)
174 c.Check(resp["kind"], check.Equals, "arvados#container")
175 c.Check(resp["uuid"], check.HasLen, 27)
176 c.Check(resp["command"], check.HasLen, 2)
177 c.Check(resp["mounts"], check.IsNil)
178 _, hasMounts := resp["mounts"]
179 c.Check(hasMounts, check.Equals, false)