e0a41c924bb3de6a0f9ae7826fd4abd2febfd5eb
[arvados.git] / sdk / go / arvadosclient / arvadosclient_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package arvadosclient
6
7 import (
8         "fmt"
9         "net"
10         "net/http"
11         "os"
12         "testing"
13
14         "git.arvados.org/arvados.git/sdk/go/arvadostest"
15         . "gopkg.in/check.v1"
16 )
17
18 // Gocheck boilerplate
19 func Test(t *testing.T) {
20         TestingT(t)
21 }
22
23 var _ = Suite(&ServerRequiredSuite{})
24 var _ = Suite(&UnitSuite{})
25 var _ = Suite(&MockArvadosServerSuite{})
26
27 // Tests that require the Keep server running
28 type ServerRequiredSuite struct{}
29
30 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
31         arvadostest.StartAPI()
32         arvadostest.StartKeep(2, false)
33         RetryDelay = 0
34 }
35
36 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
37         arvadostest.StopKeep(2)
38         arvadostest.StopAPI()
39 }
40
41 func (s *ServerRequiredSuite) SetUpTest(c *C) {
42         arvadostest.ResetEnv()
43 }
44
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)
52 }
53
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)
62 }
63
64 func (s *ServerRequiredSuite) TestGetInvalidUUID(c *C) {
65         arv, err := MakeArvadosClient()
66
67         getback := make(Dict)
68         err = arv.Get("collections", "", nil, &getback)
69         c.Assert(err, Equals, ErrInvalidArgument)
70         c.Assert(len(getback), Equals, 0)
71
72         err = arv.Get("collections", "zebra-moose-unicorn", nil, &getback)
73         c.Assert(err, Equals, ErrInvalidArgument)
74         c.Assert(len(getback), Equals, 0)
75
76         err = arv.Get("collections", "acbd18db4cc2f85cedef654fccc4a4d8", nil, &getback)
77         c.Assert(err, Equals, ErrInvalidArgument)
78         c.Assert(len(getback), Equals, 0)
79 }
80
81 func (s *ServerRequiredSuite) TestGetValidUUID(c *C) {
82         arv, err := MakeArvadosClient()
83
84         getback := make(Dict)
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)
89
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)
94 }
95
96 func (s *ServerRequiredSuite) TestInvalidResourceType(c *C) {
97         arv, err := MakeArvadosClient()
98
99         getback := make(Dict)
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)
104
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)
109
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)
114 }
115
116 func (s *ServerRequiredSuite) TestErrorResponse(c *C) {
117         arv, _ := MakeArvadosClient()
118
119         getback := make(Dict)
120
121         {
122                 err := arv.Create("logs",
123                         Dict{"log": Dict{"bogus_attr": "foo"}},
124                         &getback)
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)
129         }
130
131         {
132                 err := arv.Create("bogus",
133                         Dict{"bogus": Dict{}},
134                         &getback)
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)
139         }
140 }
141
142 func (s *ServerRequiredSuite) TestAPIDiscovery_Get_defaultCollectionReplication(c *C) {
143         arv, err := MakeArvadosClient()
144         value, err := arv.Discovery("defaultCollectionReplication")
145         c.Assert(err, IsNil)
146         c.Assert(value, NotNil)
147 }
148
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)
154 }
155
156 type UnitSuite struct{}
157
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)
167 }
168
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)
184 }
185
186 // Tests that use mock arvados server
187 type MockArvadosServerSuite struct{}
188
189 func (s *MockArvadosServerSuite) SetUpSuite(c *C) {
190         RetryDelay = 0
191 }
192
193 func (s *MockArvadosServerSuite) SetUpTest(c *C) {
194         arvadostest.ResetEnv()
195 }
196
197 type APIServer struct {
198         listener net.Listener
199         url      string
200 }
201
202 func RunFakeArvadosServer(st http.Handler) (api APIServer, err error) {
203         api.listener, err = net.ListenTCP("tcp", &net.TCPAddr{Port: 0})
204         if err != nil {
205                 return
206         }
207         api.url = api.listener.Addr().String()
208         go http.Serve(api.listener, st)
209         return
210 }
211
212 type APIStub struct {
213         method        string
214         retryAttempts int
215         expected      int
216         respStatus    []int
217         responseBody  []string
218 }
219
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)
223                 return
224         }
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)
228         } else {
229                 resp.WriteHeader(h.respStatus[h.retryAttempts])
230                 resp.Write([]byte(h.responseBody[h.retryAttempts]))
231         }
232         h.retryAttempts++
233 }
234
235 func (s *MockArvadosServerSuite) TestWithRetries(c *C) {
236         for _, stub := range []APIStub{
237                 {
238                         "get", 0, 200, []int{200, 500}, []string{`{"ok":"ok"}`, ``},
239                 },
240                 {
241                         "create", 0, 200, []int{200, 500}, []string{`{"ok":"ok"}`, ``},
242                 },
243                 {
244                         "get", 0, 500, []int{500, 500, 500, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
245                 },
246                 {
247                         "create", 0, 500, []int{500, 500, 500, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
248                 },
249                 {
250                         "update", 0, 500, []int{500, 500, 500, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
251                 },
252                 {
253                         "delete", 0, 500, []int{500, 500, 500, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
254                 },
255                 {
256                         "get", 0, 502, []int{500, 500, 502, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
257                 },
258                 {
259                         "create", 0, 502, []int{500, 500, 502, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
260                 },
261                 {
262                         "get", 0, 200, []int{500, 500, 200}, []string{``, ``, `{"ok":"ok"}`},
263                 },
264                 {
265                         "create", 0, 200, []int{500, 500, 200}, []string{``, ``, `{"ok":"ok"}`},
266                 },
267                 {
268                         "delete", 0, 200, []int{500, 500, 200}, []string{``, ``, `{"ok":"ok"}`},
269                 },
270                 {
271                         "update", 0, 200, []int{500, 500, 200}, []string{``, ``, `{"ok":"ok"}`},
272                 },
273                 {
274                         "get", 0, 401, []int{401, 200}, []string{``, `{"ok":"ok"}`},
275                 },
276                 {
277                         "create", 0, 401, []int{401, 200}, []string{``, `{"ok":"ok"}`},
278                 },
279                 {
280                         "get", 0, 404, []int{404, 200}, []string{``, `{"ok":"ok"}`},
281                 },
282                 {
283                         "get", 0, 401, []int{500, 401, 200}, []string{``, ``, `{"ok":"ok"}`},
284                 },
285
286                 // Response code -1 simulates an HTTP/network error
287                 // (i.e., Do() returns an error; there is no HTTP
288                 // response status code).
289
290                 // Succeed on second retry
291                 {
292                         "get", 0, 200, []int{-1, -1, 200}, []string{``, ``, `{"ok":"ok"}`},
293                 },
294                 // "POST" is not safe to retry: fail after one error
295                 {
296                         "create", 0, -1, []int{-1, 200}, []string{``, `{"ok":"ok"}`},
297                 },
298         } {
299                 api, err := RunFakeArvadosServer(&stub)
300                 c.Check(err, IsNil)
301
302                 defer api.listener.Close()
303
304                 arv := ArvadosClient{
305                         Scheme:      "http",
306                         ApiServer:   api.url,
307                         ApiToken:    "abc123",
308                         ApiInsecure: true,
309                         Client:      &http.Client{Transport: &http.Transport{}},
310                         Retries:     2}
311
312                 getback := make(Dict)
313                 switch stub.method {
314                 case "get":
315                         err = arv.Get("collections", "zzzzz-4zz18-znfnqtbbv4spc3w", nil, &getback)
316                 case "create":
317                         err = arv.Create("collections",
318                                 Dict{"collection": Dict{"name": "testing"}},
319                                 &getback)
320                 case "update":
321                         err = arv.Update("collections", "zzzzz-4zz18-znfnqtbbv4spc3w",
322                                 Dict{"collection": Dict{"name": "testing"}},
323                                 &getback)
324                 case "delete":
325                         err = arv.Delete("pipeline_templates", "zzzzz-4zz18-znfnqtbbv4spc3w", nil, &getback)
326                 }
327
328                 switch stub.expected {
329                 case 200:
330                         c.Check(err, IsNil)
331                         c.Check(getback["ok"], Equals, "ok")
332                 case -1:
333                         c.Check(err, NotNil)
334                         c.Check(err, ErrorMatches, `.*stopped after \d+ redirects`)
335                 default:
336                         c.Check(err, NotNil)
337                         c.Check(err, ErrorMatches, fmt.Sprintf("arvados API server error: %d.*", stub.expected))
338                         c.Check(err.(APIServerError).HttpStatusCode, Equals, stub.expected)
339                 }
340         }
341 }