X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/45f10d80d1b584808a6e375214b5be6bc7d2a730..b5c48c07a2618740eab353150a450fb62bb4ceee:/sdk/go/arvadosclient/arvadosclient_test.go?ds=inline diff --git a/sdk/go/arvadosclient/arvadosclient_test.go b/sdk/go/arvadosclient/arvadosclient_test.go index d35f6dacb7..045acf81e7 100644 --- a/sdk/go/arvadosclient/arvadosclient_test.go +++ b/sdk/go/arvadosclient/arvadosclient_test.go @@ -1,11 +1,14 @@ package arvadosclient import ( + "fmt" "git.curoverse.com/arvados.git/sdk/go/arvadostest" . "gopkg.in/check.v1" + "net" "net/http" "os" "testing" + "time" ) // Gocheck boilerplate @@ -15,13 +18,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) + RetryDelay = 0 } func (s *ServerRequiredSuite) SetUpTest(c *C) { @@ -102,39 +107,50 @@ func (s *ServerRequiredSuite) TestInvalidResourceType(c *C) { func (s *ServerRequiredSuite) TestCreatePipelineTemplate(c *C) { arv, err := MakeArvadosClient() - getback := make(Dict) - err = arv.Create("pipeline_templates", - Dict{"pipeline_template": Dict{ - "name": "tmp", - "components": Dict{ - "c1": map[string]string{"script": "script1"}, - "c2": map[string]string{"script": "script2"}}}}, - &getback) - c.Assert(err, Equals, nil) - c.Assert(getback["name"], Equals, "tmp") - 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{ - "pipeline_template": Dict{"name": "tmp2"}}, - &getback) - c.Assert(err, Equals, nil) - c.Assert(getback["name"], Equals, "tmp2") - - c.Assert(getback["uuid"].(string), Equals, uuid) - getback = make(Dict) - err = arv.Delete("pipeline_templates", uuid, nil, &getback) - c.Assert(err, Equals, nil) - c.Assert(getback["name"], Equals, "tmp2") + for _, idleConnections := range []bool{ + false, + true, + } { + if idleConnections { + arv.lastClosedIdlesAt = time.Now().Add(-time.Minute) + } else { + arv.lastClosedIdlesAt = time.Now() + } + + getback := make(Dict) + err = arv.Create("pipeline_templates", + Dict{"pipeline_template": Dict{ + "name": "tmp", + "components": Dict{ + "c1": map[string]string{"script": "script1"}, + "c2": map[string]string{"script": "script2"}}}}, + &getback) + c.Assert(err, Equals, nil) + c.Assert(getback["name"], Equals, "tmp") + 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{ + "pipeline_template": Dict{"name": "tmp2"}}, + &getback) + c.Assert(err, Equals, nil) + c.Assert(getback["name"], Equals, "tmp2") + + c.Assert(getback["uuid"].(string), Equals, uuid) + getback = make(Dict) + err = arv.Delete("pipeline_templates", uuid, nil, &getback) + c.Assert(err, Equals, nil) + c.Assert(getback["name"], Equals, "tmp2") + } } func (s *ServerRequiredSuite) TestErrorResponse(c *C) { @@ -206,3 +222,153 @@ func (s *UnitSuite) TestPDHMatch(c *C) { 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) { + RetryDelay = 0 +} + +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 APIStub struct { + method string + retryAttempts int + expected int + respStatus []int + responseBody []string +} + +func (h *APIStub) ServeHTTP(resp http.ResponseWriter, req *http.Request) { + resp.WriteHeader(h.respStatus[h.retryAttempts]) + resp.Write([]byte(h.responseBody[h.retryAttempts])) + h.retryAttempts++ +} + +func (s *MockArvadosServerSuite) TestWithRetries(c *C) { + for _, stub := range []APIStub{ + { + "get", 0, 200, []int{200, 500}, []string{`{"ok":"ok"}`, ``}, + }, + { + "create", 0, 200, []int{200, 500}, []string{`{"ok":"ok"}`, ``}, + }, + { + "get", 0, 500, []int{500, 500, 500, 200}, []string{``, ``, ``, `{"ok":"ok"}`}, + }, + { + "create", 0, 500, []int{500, 500, 500, 200}, []string{``, ``, ``, `{"ok":"ok"}`}, + }, + { + "update", 0, 500, []int{500, 500, 500, 200}, []string{``, ``, ``, `{"ok":"ok"}`}, + }, + { + "delete", 0, 500, []int{500, 500, 500, 200}, []string{``, ``, ``, `{"ok":"ok"}`}, + }, + { + "get", 0, 502, []int{500, 500, 502, 200}, []string{``, ``, ``, `{"ok":"ok"}`}, + }, + { + "create", 0, 502, []int{500, 500, 502, 200}, []string{``, ``, ``, `{"ok":"ok"}`}, + }, + { + "get", 0, 200, []int{500, 500, 200}, []string{``, ``, `{"ok":"ok"}`}, + }, + { + "create", 0, 200, []int{500, 500, 200}, []string{``, ``, `{"ok":"ok"}`}, + }, + { + "delete", 0, 200, []int{500, 500, 200}, []string{``, ``, `{"ok":"ok"}`}, + }, + { + "update", 0, 200, []int{500, 500, 200}, []string{``, ``, `{"ok":"ok"}`}, + }, + { + "get", 0, 401, []int{401, 200}, []string{``, `{"ok":"ok"}`}, + }, + { + "create", 0, 401, []int{401, 200}, []string{``, `{"ok":"ok"}`}, + }, + { + "get", 0, 404, []int{404, 200}, []string{``, `{"ok":"ok"}`}, + }, + { + "get", 0, 401, []int{500, 401, 200}, []string{``, ``, `{"ok":"ok"}`}, + }, + // Use nil responseBody to simulate error during request processing + // Even though retryable, the simulated error applies during reties also, and hence "get" also eventually fails in this test. + { + "get", 0, -1, nil, nil, + }, + { + "create", 0, -1, nil, nil, + }, + } { + 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{Transport: &http.Transport{}}, + Retries: 2} + + // We use nil responseBody to look for errors during request processing + // Simulate an error using https (but the arv.Client transport used does not support it) + if stub.responseBody == nil { + arv.Scheme = "https" + } + + getback := make(Dict) + switch stub.method { + case "get": + err = arv.Get("collections", "zzzzz-4zz18-znfnqtbbv4spc3w", nil, &getback) + case "create": + err = arv.Create("collections", + Dict{"collection": Dict{"name": "testing"}}, + &getback) + case "update": + err = arv.Update("collections", "zzzzz-4zz18-znfnqtbbv4spc3w", + Dict{"collection": Dict{"name": "testing"}}, + &getback) + case "delete": + err = arv.Delete("pipeline_templates", "zzzzz-4zz18-znfnqtbbv4spc3w", nil, &getback) + } + + if stub.expected == 200 { + c.Check(err, IsNil) + c.Assert(getback["ok"], Equals, "ok") + } else { + c.Check(err, NotNil) + + if stub.responseBody == nil { // test uses empty responseBody to look for errors during request processing + c.Assert(err, ErrorMatches, "* oversized record received.*") + } else { + c.Assert(err, ErrorMatches, fmt.Sprintf("%s%d.*", "arvados API server error: ", stub.expected)) + c.Assert(err.(APIServerError).HttpStatusCode, Equals, stub.expected) + } + } + } +}