1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
14 "git.arvados.org/arvados.git/sdk/go/arvadostest"
18 // Gocheck boilerplate
19 func Test(t *testing.T) {
23 var _ = Suite(&ServerRequiredSuite{})
24 var _ = Suite(&UnitSuite{})
25 var _ = Suite(&MockArvadosServerSuite{})
27 // Tests that require the Keep server running
28 type ServerRequiredSuite struct{}
30 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
31 arvadostest.StartAPI()
32 arvadostest.StartKeep(2, false)
36 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
37 arvadostest.StopKeep(2)
41 func (s *ServerRequiredSuite) SetUpTest(c *C) {
42 arvadostest.ResetEnv()
45 func (s *ServerRequiredSuite) TestMakeArvadosClientSecure(c *C) {
46 os.Setenv("ARVADOS_API_HOST_INSECURE", "")
47 ac, err := MakeArvadosClient()
48 c.Assert(err, Equals, nil)
49 c.Check(ac.ApiServer, Equals, os.Getenv("ARVADOS_API_HOST"))
50 c.Check(ac.ApiToken, Equals, os.Getenv("ARVADOS_API_TOKEN"))
51 c.Check(ac.ApiInsecure, Equals, false)
54 func (s *ServerRequiredSuite) TestMakeArvadosClientInsecure(c *C) {
55 os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
56 ac, err := MakeArvadosClient()
57 c.Assert(err, Equals, nil)
58 c.Check(ac.ApiInsecure, Equals, true)
59 c.Check(ac.ApiServer, Equals, os.Getenv("ARVADOS_API_HOST"))
60 c.Check(ac.ApiToken, Equals, os.Getenv("ARVADOS_API_TOKEN"))
61 c.Check(ac.Client.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify, Equals, true)
64 func (s *ServerRequiredSuite) TestGetInvalidUUID(c *C) {
65 arv, err := MakeArvadosClient()
68 err = arv.Get("collections", "", nil, &getback)
69 c.Assert(err, Equals, ErrInvalidArgument)
70 c.Assert(len(getback), Equals, 0)
72 err = arv.Get("collections", "zebra-moose-unicorn", nil, &getback)
73 c.Assert(err, Equals, ErrInvalidArgument)
74 c.Assert(len(getback), Equals, 0)
76 err = arv.Get("collections", "acbd18db4cc2f85cedef654fccc4a4d8", nil, &getback)
77 c.Assert(err, Equals, ErrInvalidArgument)
78 c.Assert(len(getback), Equals, 0)
81 func (s *ServerRequiredSuite) TestGetValidUUID(c *C) {
82 arv, err := MakeArvadosClient()
85 err = arv.Get("collections", "zzzzz-4zz18-abcdeabcdeabcde", nil, &getback)
86 c.Assert(err, FitsTypeOf, APIServerError{})
87 c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound)
88 c.Assert(len(getback), Equals, 0)
90 err = arv.Get("collections", "acbd18db4cc2f85cedef654fccc4a4d8+3", nil, &getback)
91 c.Assert(err, FitsTypeOf, APIServerError{})
92 c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound)
93 c.Assert(len(getback), Equals, 0)
96 func (s *ServerRequiredSuite) TestInvalidResourceType(c *C) {
97 arv, err := MakeArvadosClient()
100 err = arv.Get("unicorns", "zzzzz-zebra-unicorn7unicorn", nil, &getback)
101 c.Assert(err, FitsTypeOf, APIServerError{})
102 c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound)
103 c.Assert(len(getback), Equals, 0)
105 err = arv.Update("unicorns", "zzzzz-zebra-unicorn7unicorn", nil, &getback)
106 c.Assert(err, FitsTypeOf, APIServerError{})
107 c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound)
108 c.Assert(len(getback), Equals, 0)
110 err = arv.List("unicorns", nil, &getback)
111 c.Assert(err, FitsTypeOf, APIServerError{})
112 c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound)
113 c.Assert(len(getback), Equals, 0)
116 func (s *ServerRequiredSuite) TestErrorResponse(c *C) {
117 arv, _ := MakeArvadosClient()
119 getback := make(Dict)
122 err := arv.Create("logs",
123 Dict{"log": Dict{"bogus_attr": "foo"}},
125 c.Assert(err, ErrorMatches, "arvados API server error: .*")
126 c.Assert(err, ErrorMatches, ".*unknown attribute(: | ')bogus_attr.*")
127 c.Assert(err, FitsTypeOf, APIServerError{})
128 c.Assert(err.(APIServerError).HttpStatusCode, Equals, 422)
132 err := arv.Create("bogus",
133 Dict{"bogus": Dict{}},
135 c.Assert(err, ErrorMatches, "arvados API server error: .*")
136 c.Assert(err, ErrorMatches, ".*Path not found.*")
137 c.Assert(err, FitsTypeOf, APIServerError{})
138 c.Assert(err.(APIServerError).HttpStatusCode, Equals, 404)
142 func (s *ServerRequiredSuite) TestAPIDiscovery_Get_defaultCollectionReplication(c *C) {
143 arv, err := MakeArvadosClient()
144 value, err := arv.Discovery("defaultCollectionReplication")
146 c.Assert(value, NotNil)
149 func (s *ServerRequiredSuite) TestAPIDiscovery_Get_noSuchParameter(c *C) {
150 arv, err := MakeArvadosClient()
151 value, err := arv.Discovery("noSuchParameter")
152 c.Assert(err, NotNil)
153 c.Assert(value, IsNil)
156 type UnitSuite struct{}
158 func (s *UnitSuite) TestUUIDMatch(c *C) {
159 c.Assert(UUIDMatch("zzzzz-tpzed-000000000000000"), Equals, true)
160 c.Assert(UUIDMatch("zzzzz-zebra-000000000000000"), Equals, true)
161 c.Assert(UUIDMatch("00000-00000-zzzzzzzzzzzzzzz"), Equals, true)
162 c.Assert(UUIDMatch("ZEBRA-HORSE-AFRICANELEPHANT"), Equals, false)
163 c.Assert(UUIDMatch(" zzzzz-tpzed-000000000000000"), Equals, false)
164 c.Assert(UUIDMatch("d41d8cd98f00b204e9800998ecf8427e"), Equals, false)
165 c.Assert(UUIDMatch("d41d8cd98f00b204e9800998ecf8427e+0"), Equals, false)
166 c.Assert(UUIDMatch(""), Equals, false)
169 func (s *UnitSuite) TestPDHMatch(c *C) {
170 c.Assert(PDHMatch("zzzzz-tpzed-000000000000000"), Equals, false)
171 c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e"), Equals, false)
172 c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+0"), Equals, true)
173 c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+12345"), Equals, true)
174 c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e 12345"), Equals, false)
175 c.Assert(PDHMatch("D41D8CD98F00B204E9800998ECF8427E+12345"), Equals, false)
176 c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+12345 "), Equals, false)
177 c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+abcdef"), Equals, false)
178 c.Assert(PDHMatch("da39a3ee5e6b4b0d3255bfef95601890afd80709"), Equals, false)
179 c.Assert(PDHMatch("da39a3ee5e6b4b0d3255bfef95601890afd80709+0"), Equals, false)
180 c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427+12345"), Equals, false)
181 c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+12345\n"), Equals, false)
182 c.Assert(PDHMatch("+12345"), Equals, false)
183 c.Assert(PDHMatch(""), Equals, false)
186 // Tests that use mock arvados server
187 type MockArvadosServerSuite struct{}
189 func (s *MockArvadosServerSuite) SetUpSuite(c *C) {
193 func (s *MockArvadosServerSuite) SetUpTest(c *C) {
194 arvadostest.ResetEnv()
197 type APIServer struct {
198 listener net.Listener
202 func RunFakeArvadosServer(st http.Handler) (api APIServer, err error) {
203 api.listener, err = net.ListenTCP("tcp", &net.TCPAddr{Port: 0})
207 api.url = api.listener.Addr().String()
208 go http.Serve(api.listener, st)
212 type APIStub struct {
217 responseBody []string
220 func (h *APIStub) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
221 if req.URL.Path == "/redirect-loop" {
222 http.Redirect(resp, req, "/redirect-loop", http.StatusFound)
225 if h.respStatus[h.retryAttempts] < 0 {
226 // Fail the client's Do() by starting a redirect loop
227 http.Redirect(resp, req, "/redirect-loop", http.StatusFound)
229 resp.WriteHeader(h.respStatus[h.retryAttempts])
230 resp.Write([]byte(h.responseBody[h.retryAttempts]))
235 func (s *MockArvadosServerSuite) TestWithRetries(c *C) {
236 for _, stub := range []APIStub{
238 "get", 0, 200, []int{200, 500}, []string{`{"ok":"ok"}`, ``},
241 "create", 0, 200, []int{200, 500}, []string{`{"ok":"ok"}`, ``},
244 "get", 0, 500, []int{500, 500, 500, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
247 "create", 0, 500, []int{500, 500, 500, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
250 "update", 0, 500, []int{500, 500, 500, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
253 "delete", 0, 500, []int{500, 500, 500, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
256 "get", 0, 502, []int{500, 500, 502, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
259 "create", 0, 502, []int{500, 500, 502, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
262 "get", 0, 200, []int{500, 500, 200}, []string{``, ``, `{"ok":"ok"}`},
265 "create", 0, 200, []int{500, 500, 200}, []string{``, ``, `{"ok":"ok"}`},
268 "delete", 0, 200, []int{500, 500, 200}, []string{``, ``, `{"ok":"ok"}`},
271 "update", 0, 200, []int{500, 500, 200}, []string{``, ``, `{"ok":"ok"}`},
274 "get", 0, 401, []int{401, 200}, []string{``, `{"ok":"ok"}`},
277 "create", 0, 401, []int{401, 200}, []string{``, `{"ok":"ok"}`},
280 "get", 0, 404, []int{404, 200}, []string{``, `{"ok":"ok"}`},
283 "get", 0, 401, []int{500, 401, 200}, []string{``, ``, `{"ok":"ok"}`},
286 // Response code -1 simulates an HTTP/network error
287 // (i.e., Do() returns an error; there is no HTTP
288 // response status code).
290 // Succeed on second retry
292 "get", 0, 200, []int{-1, -1, 200}, []string{``, ``, `{"ok":"ok"}`},
294 // "POST" is not safe to retry: fail after one error
296 "create", 0, -1, []int{-1, 200}, []string{``, `{"ok":"ok"}`},
299 api, err := RunFakeArvadosServer(&stub)
302 defer api.listener.Close()
304 arv := ArvadosClient{
309 Client: &http.Client{Transport: &http.Transport{}},
312 getback := make(Dict)
315 err = arv.Get("collections", "zzzzz-4zz18-znfnqtbbv4spc3w", nil, &getback)
317 err = arv.Create("collections",
318 Dict{"collection": Dict{"name": "testing"}},
321 err = arv.Update("collections", "zzzzz-4zz18-znfnqtbbv4spc3w",
322 Dict{"collection": Dict{"name": "testing"}},
325 err = arv.Delete("pipeline_templates", "zzzzz-4zz18-znfnqtbbv4spc3w", nil, &getback)
328 switch stub.expected {
331 c.Check(getback["ok"], Equals, "ok")
334 c.Check(err, ErrorMatches, `.*stopped after \d+ redirects`)
337 c.Check(err, ErrorMatches, fmt.Sprintf("arvados API server error: %d.*", stub.expected))
338 c.Check(err.(APIServerError).HttpStatusCode, Equals, stub.expected)