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