5538: correct retryable list and use it to determine whether to close idle connection...
[arvados.git] / sdk / go / arvadosclient / arvadosclient_test.go
1 package arvadosclient
2
3 import (
4         "fmt"
5         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
6         . "gopkg.in/check.v1"
7         "net"
8         "net/http"
9         "os"
10         "strings"
11         "testing"
12         "time"
13 )
14
15 // Gocheck boilerplate
16 func Test(t *testing.T) {
17         TestingT(t)
18 }
19
20 var _ = Suite(&ServerRequiredSuite{})
21 var _ = Suite(&UnitSuite{})
22 var _ = Suite(&MockArvadosServerSuite{})
23
24 // Tests that require the Keep server running
25 type ServerRequiredSuite struct{}
26
27 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
28         arvadostest.StartAPI()
29         arvadostest.StartKeep(2, false)
30         RetryDelay = 0
31 }
32
33 func (s *ServerRequiredSuite) SetUpTest(c *C) {
34         arvadostest.ResetEnv()
35 }
36
37 func (s *ServerRequiredSuite) TestMakeArvadosClientSecure(c *C) {
38         os.Setenv("ARVADOS_API_HOST_INSECURE", "")
39         kc, err := MakeArvadosClient()
40         c.Assert(err, Equals, nil)
41         c.Check(kc.ApiServer, Equals, os.Getenv("ARVADOS_API_HOST"))
42         c.Check(kc.ApiToken, Equals, os.Getenv("ARVADOS_API_TOKEN"))
43         c.Check(kc.ApiInsecure, Equals, false)
44 }
45
46 func (s *ServerRequiredSuite) TestMakeArvadosClientInsecure(c *C) {
47         os.Setenv("ARVADOS_API_HOST_INSECURE", "true")
48         kc, err := MakeArvadosClient()
49         c.Assert(err, Equals, nil)
50         c.Check(kc.ApiInsecure, Equals, true)
51         c.Check(kc.ApiServer, Equals, os.Getenv("ARVADOS_API_HOST"))
52         c.Check(kc.ApiToken, Equals, os.Getenv("ARVADOS_API_TOKEN"))
53         c.Check(kc.Client.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify, Equals, true)
54 }
55
56 func (s *ServerRequiredSuite) TestGetInvalidUUID(c *C) {
57         arv, err := MakeArvadosClient()
58
59         getback := make(Dict)
60         err = arv.Get("collections", "", nil, &getback)
61         c.Assert(err, Equals, ErrInvalidArgument)
62         c.Assert(len(getback), Equals, 0)
63
64         err = arv.Get("collections", "zebra-moose-unicorn", nil, &getback)
65         c.Assert(err, Equals, ErrInvalidArgument)
66         c.Assert(len(getback), Equals, 0)
67
68         err = arv.Get("collections", "acbd18db4cc2f85cedef654fccc4a4d8", nil, &getback)
69         c.Assert(err, Equals, ErrInvalidArgument)
70         c.Assert(len(getback), Equals, 0)
71 }
72
73 func (s *ServerRequiredSuite) TestGetValidUUID(c *C) {
74         arv, err := MakeArvadosClient()
75
76         getback := make(Dict)
77         err = arv.Get("collections", "zzzzz-4zz18-abcdeabcdeabcde", nil, &getback)
78         c.Assert(err, FitsTypeOf, APIServerError{})
79         c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound)
80         c.Assert(len(getback), Equals, 0)
81
82         err = arv.Get("collections", "acbd18db4cc2f85cedef654fccc4a4d8+3", nil, &getback)
83         c.Assert(err, FitsTypeOf, APIServerError{})
84         c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound)
85         c.Assert(len(getback), Equals, 0)
86 }
87
88 func (s *ServerRequiredSuite) TestInvalidResourceType(c *C) {
89         arv, err := MakeArvadosClient()
90
91         getback := make(Dict)
92         err = arv.Get("unicorns", "zzzzz-zebra-unicorn7unicorn", nil, &getback)
93         c.Assert(err, FitsTypeOf, APIServerError{})
94         c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound)
95         c.Assert(len(getback), Equals, 0)
96
97         err = arv.Update("unicorns", "zzzzz-zebra-unicorn7unicorn", nil, &getback)
98         c.Assert(err, FitsTypeOf, APIServerError{})
99         c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound)
100         c.Assert(len(getback), Equals, 0)
101
102         err = arv.List("unicorns", nil, &getback)
103         c.Assert(err, FitsTypeOf, APIServerError{})
104         c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound)
105         c.Assert(len(getback), Equals, 0)
106 }
107
108 func (s *ServerRequiredSuite) TestCreatePipelineTemplate(c *C) {
109         arv, err := MakeArvadosClient()
110
111         for _, idleConnections := range []bool{
112                 false,
113                 true,
114         } {
115                 if idleConnections {
116                         arv.lastClosedIdlesAt = time.Now().Add(-time.Minute)
117                 } else {
118                         arv.lastClosedIdlesAt = time.Now()
119                 }
120
121                 getback := make(Dict)
122                 err = arv.Create("pipeline_templates",
123                         Dict{"pipeline_template": Dict{
124                                 "name": "tmp",
125                                 "components": Dict{
126                                         "c1": map[string]string{"script": "script1"},
127                                         "c2": map[string]string{"script": "script2"}}}},
128                         &getback)
129                 c.Assert(err, Equals, nil)
130                 c.Assert(getback["name"], Equals, "tmp")
131                 c.Assert(getback["components"].(map[string]interface{})["c2"].(map[string]interface{})["script"], Equals, "script2")
132
133                 uuid := getback["uuid"].(string)
134
135                 getback = make(Dict)
136                 err = arv.Get("pipeline_templates", uuid, nil, &getback)
137                 c.Assert(err, Equals, nil)
138                 c.Assert(getback["name"], Equals, "tmp")
139                 c.Assert(getback["components"].(map[string]interface{})["c1"].(map[string]interface{})["script"], Equals, "script1")
140
141                 getback = make(Dict)
142                 err = arv.Update("pipeline_templates", uuid,
143                         Dict{
144                                 "pipeline_template": Dict{"name": "tmp2"}},
145                         &getback)
146                 c.Assert(err, Equals, nil)
147                 c.Assert(getback["name"], Equals, "tmp2")
148
149                 c.Assert(getback["uuid"].(string), Equals, uuid)
150                 getback = make(Dict)
151                 err = arv.Delete("pipeline_templates", uuid, nil, &getback)
152                 c.Assert(err, Equals, nil)
153                 c.Assert(getback["name"], Equals, "tmp2")
154         }
155 }
156
157 func (s *ServerRequiredSuite) TestErrorResponse(c *C) {
158         arv, _ := MakeArvadosClient()
159
160         getback := make(Dict)
161
162         {
163                 err := arv.Create("logs",
164                         Dict{"log": Dict{"bogus_attr": "foo"}},
165                         &getback)
166                 c.Assert(err, ErrorMatches, "arvados API server error: .*")
167                 c.Assert(err, ErrorMatches, ".*unknown attribute: bogus_attr.*")
168                 c.Assert(err, FitsTypeOf, APIServerError{})
169                 c.Assert(err.(APIServerError).HttpStatusCode, Equals, 422)
170         }
171
172         {
173                 err := arv.Create("bogus",
174                         Dict{"bogus": Dict{}},
175                         &getback)
176                 c.Assert(err, ErrorMatches, "arvados API server error: .*")
177                 c.Assert(err, ErrorMatches, ".*Path not found.*")
178                 c.Assert(err, FitsTypeOf, APIServerError{})
179                 c.Assert(err.(APIServerError).HttpStatusCode, Equals, 404)
180         }
181 }
182
183 func (s *ServerRequiredSuite) TestAPIDiscovery_Get_defaultCollectionReplication(c *C) {
184         arv, err := MakeArvadosClient()
185         value, err := arv.Discovery("defaultCollectionReplication")
186         c.Assert(err, IsNil)
187         c.Assert(value, NotNil)
188 }
189
190 func (s *ServerRequiredSuite) TestAPIDiscovery_Get_noSuchParameter(c *C) {
191         arv, err := MakeArvadosClient()
192         value, err := arv.Discovery("noSuchParameter")
193         c.Assert(err, NotNil)
194         c.Assert(value, IsNil)
195 }
196
197 type UnitSuite struct{}
198
199 func (s *UnitSuite) TestUUIDMatch(c *C) {
200         c.Assert(UUIDMatch("zzzzz-tpzed-000000000000000"), Equals, true)
201         c.Assert(UUIDMatch("zzzzz-zebra-000000000000000"), Equals, true)
202         c.Assert(UUIDMatch("00000-00000-zzzzzzzzzzzzzzz"), Equals, true)
203         c.Assert(UUIDMatch("ZEBRA-HORSE-AFRICANELEPHANT"), Equals, false)
204         c.Assert(UUIDMatch(" zzzzz-tpzed-000000000000000"), Equals, false)
205         c.Assert(UUIDMatch("d41d8cd98f00b204e9800998ecf8427e"), Equals, false)
206         c.Assert(UUIDMatch("d41d8cd98f00b204e9800998ecf8427e+0"), Equals, false)
207         c.Assert(UUIDMatch(""), Equals, false)
208 }
209
210 func (s *UnitSuite) TestPDHMatch(c *C) {
211         c.Assert(PDHMatch("zzzzz-tpzed-000000000000000"), Equals, false)
212         c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e"), Equals, false)
213         c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+0"), Equals, true)
214         c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+12345"), Equals, true)
215         c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e 12345"), Equals, false)
216         c.Assert(PDHMatch("D41D8CD98F00B204E9800998ECF8427E+12345"), Equals, false)
217         c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+12345 "), Equals, false)
218         c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+abcdef"), Equals, false)
219         c.Assert(PDHMatch("da39a3ee5e6b4b0d3255bfef95601890afd80709"), Equals, false)
220         c.Assert(PDHMatch("da39a3ee5e6b4b0d3255bfef95601890afd80709+0"), Equals, false)
221         c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427+12345"), Equals, false)
222         c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+12345\n"), Equals, false)
223         c.Assert(PDHMatch("+12345"), Equals, false)
224         c.Assert(PDHMatch(""), Equals, false)
225 }
226
227 // Tests that use mock arvados server
228 type MockArvadosServerSuite struct{}
229
230 func (s *MockArvadosServerSuite) SetUpSuite(c *C) {
231         RetryDelay = 0
232 }
233
234 func (s *MockArvadosServerSuite) SetUpTest(c *C) {
235         arvadostest.ResetEnv()
236 }
237
238 type APIServer struct {
239         listener net.Listener
240         url      string
241 }
242
243 func RunFakeArvadosServer(st http.Handler) (api APIServer, err error) {
244         api.listener, err = net.ListenTCP("tcp", &net.TCPAddr{Port: 0})
245         if err != nil {
246                 return
247         }
248         api.url = api.listener.Addr().String()
249         go http.Serve(api.listener, st)
250         return
251 }
252
253 func (h *APIStub) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
254         resp.WriteHeader(h.respStatus[h.count])
255
256         if h.respStatus[h.count] == 200 {
257                 resp.Write([]byte(`{"ok":"ok"}`))
258         } else {
259                 resp.Write([]byte(``))
260         }
261
262         h.count++
263 }
264
265 type APIStub struct {
266         method       string
267         count        int
268         expected     int
269         respStatus   []int
270         responseBody []string
271 }
272
273 func (s *MockArvadosServerSuite) TestWithRetries(c *C) {
274         for _, stub := range []APIStub{
275                 {
276                         "get", 0, 200, []int{200, 500}, []string{`{"ok":"ok"}`, ``},
277                 },
278                 {
279                         "create", 0, 200, []int{200, 500}, []string{`{"ok":"ok"}`, ``},
280                 },
281                 {
282                         "get", 0, 500, []int{500, 500, 500, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
283                 },
284                 {
285                         "create", 0, 500, []int{500, 500, 500, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
286                 },
287                 {
288                         "update", 0, 500, []int{500, 500, 500, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
289                 },
290                 {
291                         "delete", 0, 500, []int{500, 500, 500, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
292                 },
293                 {
294                         "get", 0, 502, []int{500, 500, 502, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
295                 },
296                 {
297                         "create", 0, 502, []int{500, 500, 502, 200}, []string{``, ``, ``, `{"ok":"ok"}`},
298                 },
299                 {
300                         "get", 0, 200, []int{500, 500, 200}, []string{``, ``, `{"ok":"ok"}`},
301                 },
302                 {
303                         "create", 0, 200, []int{500, 500, 200}, []string{``, ``, `{"ok":"ok"}`},
304                 },
305                 {
306                         "delete", 0, 200, []int{500, 500, 200}, []string{``, ``, `{"ok":"ok"}`},
307                 },
308                 {
309                         "update", 0, 200, []int{500, 500, 200}, []string{``, ``, `{"ok":"ok"}`},
310                 },
311                 {
312                         "get", 0, 401, []int{401, 200}, []string{``, `{"ok":"ok"}`},
313                 },
314                 {
315                         "create", 0, 401, []int{401, 200}, []string{``, `{"ok":"ok"}`},
316                 },
317                 {
318                         "get", 0, 404, []int{404, 200}, []string{``, `{"ok":"ok"}`},
319                 },
320                 {
321                         "get", 0, 401, []int{500, 401, 200}, []string{``, ``, `{"ok":"ok"}`},
322                 },
323         } {
324                 api, err := RunFakeArvadosServer(&stub)
325                 c.Check(err, IsNil)
326
327                 defer api.listener.Close()
328
329                 arv := ArvadosClient{
330                         Scheme:      "http",
331                         ApiServer:   api.url,
332                         ApiToken:    "abc123",
333                         ApiInsecure: true,
334                         Client:      &http.Client{Transport: &http.Transport{}},
335                         Retries:     2}
336
337                 getback := make(Dict)
338                 switch stub.method {
339                 case "get":
340                         err = arv.Get("collections", "zzzzz-4zz18-znfnqtbbv4spc3w", nil, &getback)
341                 case "create":
342                         err = arv.Create("collections",
343                                 Dict{"collection": Dict{"name": "testing"}},
344                                 &getback)
345                 case "update":
346                         err = arv.Update("collections", "zzzzz-4zz18-znfnqtbbv4spc3w",
347                                 Dict{"collection": Dict{"name": "testing"}},
348                                 &getback)
349                 case "delete":
350                         err = arv.Delete("pipeline_templates", "zzzzz-4zz18-znfnqtbbv4spc3w", nil, &getback)
351                 }
352
353                 if stub.expected == 200 {
354                         c.Check(err, IsNil)
355                         c.Assert(getback["ok"], Equals, "ok")
356                 } else {
357                         c.Check(err, NotNil)
358                         c.Check(strings.Contains(err.Error(), fmt.Sprintf("%s%d", "arvados API server error: ", stub.expected)), Equals, true)
359                         c.Assert(err.(APIServerError).HttpStatusCode, Equals, stub.expected)
360                 }
361         }
362 }