4 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
14 // Gocheck boilerplate
15 func Test(t *testing.T) {
19 var _ = Suite(&ServerRequiredSuite{})
20 var _ = Suite(&UnitSuite{})
21 var _ = Suite(&MockArvadosServerSuite{})
23 // Tests that require the Keep server running
24 type ServerRequiredSuite struct{}
26 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
27 arvadostest.StartAPI()
28 arvadostest.StartKeep(2, false)
31 func (s *ServerRequiredSuite) SetUpTest(c *C) {
32 arvadostest.ResetEnv()
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)
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)
54 func (s *ServerRequiredSuite) TestGetInvalidUUID(c *C) {
55 arv, err := MakeArvadosClient()
58 err = arv.Get("collections", "", nil, &getback)
59 c.Assert(err, Equals, ErrInvalidArgument)
60 c.Assert(len(getback), Equals, 0)
62 err = arv.Get("collections", "zebra-moose-unicorn", nil, &getback)
63 c.Assert(err, Equals, ErrInvalidArgument)
64 c.Assert(len(getback), Equals, 0)
66 err = arv.Get("collections", "acbd18db4cc2f85cedef654fccc4a4d8", nil, &getback)
67 c.Assert(err, Equals, ErrInvalidArgument)
68 c.Assert(len(getback), Equals, 0)
71 func (s *ServerRequiredSuite) TestGetValidUUID(c *C) {
72 arv, err := MakeArvadosClient()
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)
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)
86 func (s *ServerRequiredSuite) TestInvalidResourceType(c *C) {
87 arv, err := MakeArvadosClient()
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)
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)
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)
106 func (s *ServerRequiredSuite) TestCreatePipelineTemplate(c *C) {
107 arv, err := MakeArvadosClient()
109 for _, idleConnections := range []bool{
114 arv.lastClosedIdlesAt = time.Now().Add(-time.Minute)
116 arv.lastClosedIdlesAt = time.Now()
119 getback := make(Dict)
120 err = arv.Create("pipeline_templates",
121 Dict{"pipeline_template": Dict{
124 "c1": map[string]string{"script": "script1"},
125 "c2": map[string]string{"script": "script2"}}}},
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")
131 uuid := getback["uuid"].(string)
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")
140 err = arv.Update("pipeline_templates", uuid,
142 "pipeline_template": Dict{"name": "tmp2"}},
144 c.Assert(err, Equals, nil)
145 c.Assert(getback["name"], Equals, "tmp2")
147 c.Assert(getback["uuid"].(string), Equals, uuid)
149 err = arv.Delete("pipeline_templates", uuid, nil, &getback)
150 c.Assert(err, Equals, nil)
151 c.Assert(getback["name"], Equals, "tmp2")
155 func (s *ServerRequiredSuite) TestErrorResponse(c *C) {
156 arv, _ := MakeArvadosClient()
158 getback := make(Dict)
161 err := arv.Create("logs",
162 Dict{"log": Dict{"bogus_attr": "foo"}},
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)
171 err := arv.Create("bogus",
172 Dict{"bogus": Dict{}},
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)
181 func (s *ServerRequiredSuite) TestAPIDiscovery_Get_defaultCollectionReplication(c *C) {
182 arv, err := MakeArvadosClient()
183 value, err := arv.Discovery("defaultCollectionReplication")
185 c.Assert(value, NotNil)
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)
195 type UnitSuite struct{}
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)
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)
225 // Tests that use mock arvados server
226 type MockArvadosServerSuite struct{}
228 func (s *MockArvadosServerSuite) SetUpSuite(c *C) {
231 func (s *MockArvadosServerSuite) SetUpTest(c *C) {
232 arvadostest.ResetEnv()
235 type APIServer struct {
236 listener net.Listener
240 func RunFakeArvadosServer(st http.Handler) (api APIServer, err error) {
241 api.listener, err = net.ListenTCP("tcp", &net.TCPAddr{Port: 0})
245 api.url = api.listener.Addr().String()
246 go http.Serve(api.listener, st)
250 type FailHandler struct {
254 func (h FailHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
255 resp.WriteHeader(h.status)
258 func (s *MockArvadosServerSuite) TestFailWithRetries(c *C) {
259 for _, testCase := range []string{
263 stub := FailHandler{500}
265 api, err := RunFakeArvadosServer(stub)
268 defer api.listener.Close()
270 arv := ArvadosClient{
275 Client: &http.Client{},
278 getback := make(Dict)
281 err = arv.Get("collections", "zzzzz-4zz18-znfnqtbbv4spc3w", nil, &getback)
283 err = arv.Create("collections",
284 Dict{"collection": Dict{"name": "testing"}},
288 c.Check(strings.Contains(err.Error(), "arvados API server error: 500"), Equals, true)
289 c.Assert(err.(APIServerError).HttpStatusCode, Equals, 500)
293 type FailThenSucceedHandler struct {
298 func (h *FailThenSucceedHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
300 resp.WriteHeader(h.failStatus)
303 resp.WriteHeader(http.StatusOK)
304 respJSON := []byte(`{"name":"testing"}`)
309 func (s *MockArvadosServerSuite) TestFailThenSucceed(c *C) {
310 for _, testCase := range []string{
314 stub := &FailThenSucceedHandler{0, 500}
316 api, err := RunFakeArvadosServer(stub)
319 defer api.listener.Close()
321 arv := ArvadosClient{
326 Client: &http.Client{},
329 getback := make(Dict)
332 err = arv.Get("collections", "zzzzz-4zz18-znfnqtbbv4spc3w", nil, &getback)
334 err = arv.Create("collections",
335 Dict{"collection": Dict{"name": "testing"}},
339 c.Assert(getback["name"], Equals, "testing")