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