X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/f81a5a1adb403f9c81159239e92880843dca3c93..cabef98fc6cec3b9616d0c76ba469184b9dce693:/sdk/go/arvadosclient/arvadosclient_test.go diff --git a/sdk/go/arvadosclient/arvadosclient_test.go b/sdk/go/arvadosclient/arvadosclient_test.go index 21eff20355..26a4349acd 100644 --- a/sdk/go/arvadosclient/arvadosclient_test.go +++ b/sdk/go/arvadosclient/arvadosclient_test.go @@ -3,8 +3,10 @@ package arvadosclient import ( "git.curoverse.com/arvados.git/sdk/go/arvadostest" . "gopkg.in/check.v1" + "net" "net/http" "os" + "strings" "testing" ) @@ -14,13 +16,15 @@ func Test(t *testing.T) { } var _ = Suite(&ServerRequiredSuite{}) +var _ = Suite(&UnitSuite{}) +var _ = Suite(&MockArvadosServerSuite{}) // Tests that require the Keep server running type ServerRequiredSuite struct{} func (s *ServerRequiredSuite) SetUpSuite(c *C) { arvadostest.StartAPI() - arvadostest.StartKeep() + arvadostest.StartKeep(2, false) } func (s *ServerRequiredSuite) SetUpTest(c *C) { @@ -46,6 +50,58 @@ func (s *ServerRequiredSuite) TestMakeArvadosClientInsecure(c *C) { c.Check(kc.Client.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify, Equals, true) } +func (s *ServerRequiredSuite) TestGetInvalidUUID(c *C) { + arv, err := MakeArvadosClient() + + getback := make(Dict) + err = arv.Get("collections", "", nil, &getback) + c.Assert(err, Equals, ErrInvalidArgument) + c.Assert(len(getback), Equals, 0) + + err = arv.Get("collections", "zebra-moose-unicorn", nil, &getback) + c.Assert(err, Equals, ErrInvalidArgument) + c.Assert(len(getback), Equals, 0) + + err = arv.Get("collections", "acbd18db4cc2f85cedef654fccc4a4d8", nil, &getback) + c.Assert(err, Equals, ErrInvalidArgument) + c.Assert(len(getback), Equals, 0) +} + +func (s *ServerRequiredSuite) TestGetValidUUID(c *C) { + arv, err := MakeArvadosClient() + + getback := make(Dict) + err = arv.Get("collections", "zzzzz-4zz18-abcdeabcdeabcde", nil, &getback) + c.Assert(err, FitsTypeOf, APIServerError{}) + c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound) + c.Assert(len(getback), Equals, 0) + + err = arv.Get("collections", "acbd18db4cc2f85cedef654fccc4a4d8+3", nil, &getback) + c.Assert(err, FitsTypeOf, APIServerError{}) + c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound) + c.Assert(len(getback), Equals, 0) +} + +func (s *ServerRequiredSuite) TestInvalidResourceType(c *C) { + arv, err := MakeArvadosClient() + + getback := make(Dict) + err = arv.Get("unicorns", "zzzzz-zebra-unicorn7unicorn", nil, &getback) + c.Assert(err, FitsTypeOf, APIServerError{}) + c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound) + c.Assert(len(getback), Equals, 0) + + err = arv.Update("unicorns", "zzzzz-zebra-unicorn7unicorn", nil, &getback) + c.Assert(err, FitsTypeOf, APIServerError{}) + c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound) + c.Assert(len(getback), Equals, 0) + + err = arv.List("unicorns", nil, &getback) + c.Assert(err, FitsTypeOf, APIServerError{}) + c.Assert(err.(APIServerError).HttpStatusCode, Equals, http.StatusNotFound) + c.Assert(len(getback), Equals, 0) +} + func (s *ServerRequiredSuite) TestCreatePipelineTemplate(c *C) { arv, err := MakeArvadosClient() @@ -62,6 +118,13 @@ func (s *ServerRequiredSuite) TestCreatePipelineTemplate(c *C) { c.Assert(getback["components"].(map[string]interface{})["c2"].(map[string]interface{})["script"], Equals, "script2") uuid := getback["uuid"].(string) + + getback = make(Dict) + err = arv.Get("pipeline_templates", uuid, nil, &getback) + c.Assert(err, Equals, nil) + c.Assert(getback["name"], Equals, "tmp") + c.Assert(getback["components"].(map[string]interface{})["c1"].(map[string]interface{})["script"], Equals, "script1") + getback = make(Dict) err = arv.Update("pipeline_templates", uuid, Dict{ @@ -102,3 +165,165 @@ func (s *ServerRequiredSuite) TestErrorResponse(c *C) { c.Assert(err.(APIServerError).HttpStatusCode, Equals, 404) } } + +func (s *ServerRequiredSuite) TestAPIDiscovery_Get_defaultCollectionReplication(c *C) { + arv, err := MakeArvadosClient() + value, err := arv.Discovery("defaultCollectionReplication") + c.Assert(err, IsNil) + c.Assert(value, NotNil) +} + +func (s *ServerRequiredSuite) TestAPIDiscovery_Get_noSuchParameter(c *C) { + arv, err := MakeArvadosClient() + value, err := arv.Discovery("noSuchParameter") + c.Assert(err, NotNil) + c.Assert(value, IsNil) +} + +type UnitSuite struct{} + +func (s *UnitSuite) TestUUIDMatch(c *C) { + c.Assert(UUIDMatch("zzzzz-tpzed-000000000000000"), Equals, true) + c.Assert(UUIDMatch("zzzzz-zebra-000000000000000"), Equals, true) + c.Assert(UUIDMatch("00000-00000-zzzzzzzzzzzzzzz"), Equals, true) + c.Assert(UUIDMatch("ZEBRA-HORSE-AFRICANELEPHANT"), Equals, false) + c.Assert(UUIDMatch(" zzzzz-tpzed-000000000000000"), Equals, false) + c.Assert(UUIDMatch("d41d8cd98f00b204e9800998ecf8427e"), Equals, false) + c.Assert(UUIDMatch("d41d8cd98f00b204e9800998ecf8427e+0"), Equals, false) + c.Assert(UUIDMatch(""), Equals, false) +} + +func (s *UnitSuite) TestPDHMatch(c *C) { + c.Assert(PDHMatch("zzzzz-tpzed-000000000000000"), Equals, false) + c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e"), Equals, false) + c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+0"), Equals, true) + c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+12345"), Equals, true) + c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e 12345"), Equals, false) + c.Assert(PDHMatch("D41D8CD98F00B204E9800998ECF8427E+12345"), Equals, false) + c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+12345 "), Equals, false) + c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+abcdef"), Equals, false) + c.Assert(PDHMatch("da39a3ee5e6b4b0d3255bfef95601890afd80709"), Equals, false) + c.Assert(PDHMatch("da39a3ee5e6b4b0d3255bfef95601890afd80709+0"), Equals, false) + c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427+12345"), Equals, false) + c.Assert(PDHMatch("d41d8cd98f00b204e9800998ecf8427e+12345\n"), Equals, false) + c.Assert(PDHMatch("+12345"), Equals, false) + c.Assert(PDHMatch(""), Equals, false) +} + +// Tests that use mock arvados server +type MockArvadosServerSuite struct{} + +func (s *MockArvadosServerSuite) SetUpSuite(c *C) { +} + +func (s *MockArvadosServerSuite) SetUpTest(c *C) { + arvadostest.ResetEnv() +} + +type APIServer struct { + listener net.Listener + url string +} + +func RunFakeArvadosServer(st http.Handler) (api APIServer, err error) { + api.listener, err = net.ListenTCP("tcp", &net.TCPAddr{Port: 0}) + if err != nil { + return + } + api.url = api.listener.Addr().String() + go http.Serve(api.listener, st) + return +} + +type FailHandler struct { + status int +} + +func (h FailHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { + resp.WriteHeader(h.status) +} + +func (s *MockArvadosServerSuite) TestFailWithRetries(c *C) { + for _, testCase := range []string{ + "get", + "create", + } { + stub := FailHandler{500} + + api, err := RunFakeArvadosServer(stub) + c.Check(err, IsNil) + + defer api.listener.Close() + + arv := ArvadosClient{ + Scheme: "http", + ApiServer: api.url, + ApiToken: "abc123", + ApiInsecure: true, + Client: &http.Client{}, + Retries: 2} + + getback := make(Dict) + switch testCase { + case "get": + err = arv.Get("collections", "zzzzz-4zz18-znfnqtbbv4spc3w", nil, &getback) + case "create": + err = arv.Create("collections", + Dict{"collection": Dict{"name": "testing"}}, + &getback) + } + c.Check(err, NotNil) + c.Check(strings.Contains(err.Error(), "arvados API server error: 500"), Equals, true) + c.Assert(err.(APIServerError).HttpStatusCode, Equals, 500) + } +} + +type FailThenSucceedHandler struct { + count int + failStatus int +} + +func (h *FailThenSucceedHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { + if h.count == 0 { + resp.WriteHeader(h.failStatus) + h.count += 1 + } else { + resp.WriteHeader(http.StatusOK) + respJSON := []byte(`{"name":"testing"}`) + resp.Write(respJSON) + } +} + +func (s *MockArvadosServerSuite) TestFailThenSucceed(c *C) { + for _, testCase := range []string{ + "get", + "create", + } { + stub := &FailThenSucceedHandler{0, 500} + + api, err := RunFakeArvadosServer(stub) + c.Check(err, IsNil) + + defer api.listener.Close() + + arv := ArvadosClient{ + Scheme: "http", + ApiServer: api.url, + ApiToken: "abc123", + ApiInsecure: true, + Client: &http.Client{}, + Retries: 2} + + getback := make(Dict) + switch testCase { + case "get": + err = arv.Get("collections", "zzzzz-4zz18-znfnqtbbv4spc3w", nil, &getback) + case "create": + err = arv.Create("collections", + Dict{"collection": Dict{"name": "testing"}}, + &getback) + } + c.Check(err, IsNil) + c.Assert(getback["name"], Equals, "testing") + } +}