5 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
16 // Gocheck boilerplate
17 func Test(t *testing.T) {
21 var _ = Suite(&ServerRequiredSuite{})
22 var _ = Suite(&UnitSuite{})
23 var _ = Suite(&MockArvadosServerSuite{})
25 // Tests that require the Keep server running
26 type ServerRequiredSuite struct{}
28 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
29 arvadostest.StartAPI()
30 arvadostest.StartKeep(2, false)
34 func (s *ServerRequiredSuite) SetUpTest(c *C) {
35 arvadostest.ResetEnv()
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)
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)
57 func (s *ServerRequiredSuite) TestGetInvalidUUID(c *C) {
58 arv, err := MakeArvadosClient()
61 err = arv.Get("collections", "", nil, &getback)
62 c.Assert(err, Equals, ErrInvalidArgument)
63 c.Assert(len(getback), Equals, 0)
65 err = arv.Get("collections", "zebra-moose-unicorn", nil, &getback)
66 c.Assert(err, Equals, ErrInvalidArgument)
67 c.Assert(len(getback), Equals, 0)
69 err = arv.Get("collections", "acbd18db4cc2f85cedef654fccc4a4d8", nil, &getback)
70 c.Assert(err, Equals, ErrInvalidArgument)
71 c.Assert(len(getback), Equals, 0)
74 func (s *ServerRequiredSuite) TestGetValidUUID(c *C) {
75 arv, err := MakeArvadosClient()
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)
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)
89 func (s *ServerRequiredSuite) TestInvalidResourceType(c *C) {
90 arv, err := MakeArvadosClient()
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)
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)
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)
109 func (s *ServerRequiredSuite) TestCreatePipelineTemplate(c *C) {
110 arv, err := MakeArvadosClient()
112 for _, idleConnections := range []bool{
117 arv.lastClosedIdlesAt = time.Now().Add(-time.Minute)
119 arv.lastClosedIdlesAt = time.Now()
122 getback := make(Dict)
123 err = arv.Create("pipeline_templates",
124 Dict{"pipeline_template": Dict{
127 "c1": map[string]string{"script": "script1"},
128 "c2": map[string]string{"script": "script2"}}}},
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")
134 uuid := getback["uuid"].(string)
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")
143 err = arv.Update("pipeline_templates", uuid,
145 "pipeline_template": Dict{"name": "tmp2"}},
147 c.Assert(err, Equals, nil)
148 c.Assert(getback["name"], Equals, "tmp2")
150 c.Assert(getback["uuid"].(string), Equals, uuid)
152 err = arv.Delete("pipeline_templates", uuid, nil, &getback)
153 c.Assert(err, Equals, nil)
154 c.Assert(getback["name"], Equals, "tmp2")
158 func (s *ServerRequiredSuite) TestErrorResponse(c *C) {
159 arv, _ := MakeArvadosClient()
161 getback := make(Dict)
164 err := arv.Create("logs",
165 Dict{"log": Dict{"bogus_attr": "foo"}},
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)
174 err := arv.Create("bogus",
175 Dict{"bogus": Dict{}},
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)
184 func (s *ServerRequiredSuite) TestAPIDiscovery_Get_defaultCollectionReplication(c *C) {
185 arv, err := MakeArvadosClient()
186 value, err := arv.Discovery("defaultCollectionReplication")
188 c.Assert(value, NotNil)
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)
198 type UnitSuite struct{}
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)
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)
228 // Tests that use mock arvados server
229 type MockArvadosServerSuite struct{}
231 func (s *MockArvadosServerSuite) SetUpSuite(c *C) {
235 func (s *MockArvadosServerSuite) SetUpTest(c *C) {
236 arvadostest.ResetEnv()
239 type APIServer struct {
240 listener net.Listener
244 func RunFakeArvadosServer(st http.Handler) (api APIServer, err error) {
245 api.listener, err = net.ListenTCP("tcp", &net.TCPAddr{Port: 0})
249 api.url = api.listener.Addr().String()
250 go http.Serve(api.listener, st)
254 type APIStub struct {
259 func (h *APIStub) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
260 resp.WriteHeader(h.respStatus[h.count])
262 if h.respStatus[h.count] == 200 {
263 resp.Write([]byte(`{"ok":"ok"}`))
265 resp.Write([]byte(``))
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.
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}},
296 var statusCodes []int
299 for key, value := range testCase {
300 method = key[:strings.Index(key, ":")]
301 expected = key[strings.Index(key, ":")+1:]
305 stub := &APIStub{0, statusCodes}
307 api, err := RunFakeArvadosServer(stub)
310 defer api.listener.Close()
312 arv := ArvadosClient{
317 Client: &http.Client{Transport: &http.Transport{}},
320 getback := make(Dict)
323 err = arv.Get("collections", "zzzzz-4zz18-znfnqtbbv4spc3w", nil, &getback)
325 err = arv.Create("collections",
326 Dict{"collection": Dict{"name": "testing"}},
329 err = arv.Update("collections", "zzzzz-4zz18-znfnqtbbv4spc3w",
330 Dict{"collection": Dict{"name": "testing"}},
333 err = arv.Delete("pipeline_templates", "zzzzz-4zz18-znfnqtbbv4spc3w", nil, &getback)
336 if expected == "success" {
338 c.Assert(getback["ok"], Equals, "ok")
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)