X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/ffdfd0ac6e501514c5327c2d6b5f8507bb6353bc..2cf5dde4a6d56d0fb87a1dc79743753632d640a0:/sdk/go/arvadosclient/arvadosclient_test.go?ds=sidebyside diff --git a/sdk/go/arvadosclient/arvadosclient_test.go b/sdk/go/arvadosclient/arvadosclient_test.go index 20cac55092..8e32efe4f9 100644 --- a/sdk/go/arvadosclient/arvadosclient_test.go +++ b/sdk/go/arvadosclient/arvadosclient_test.go @@ -1,12 +1,12 @@ package arvadosclient import ( + "fmt" "git.curoverse.com/arvados.git/sdk/go/arvadostest" . "gopkg.in/check.v1" "net" "net/http" "os" - "strings" "testing" "time" ) @@ -29,6 +29,11 @@ func (s *ServerRequiredSuite) SetUpSuite(c *C) { RetryDelay = 0 } +func (s *ServerRequiredSuite) TearDownSuite(c *C) { + arvadostest.StopKeep(2) + arvadostest.StopAPI() +} + func (s *ServerRequiredSuite) SetUpTest(c *C) { arvadostest.ResetEnv() } @@ -249,73 +254,94 @@ func RunFakeArvadosServer(st http.Handler) (api APIServer, err error) { return } -type FailHandler struct { - status int +type APIStub struct { + method string + retryAttempts int + expected int + respStatus []int + responseBody []string } -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{Transport: &http.Transport{}}, - 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) +func (h *APIStub) ServeHTTP(resp http.ResponseWriter, req *http.Request) { + if req.URL.Path == "/redirect-loop" { + http.Redirect(resp, req, "/redirect-loop", http.StatusFound) + return } -} - -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 + if h.respStatus[h.retryAttempts] < 0 { + // Fail the client's Do() by starting a redirect loop + http.Redirect(resp, req, "/redirect-loop", http.StatusFound) } else { - resp.WriteHeader(http.StatusOK) - respJSON := []byte(`{"name":"testing"}`) - resp.Write(respJSON) + resp.WriteHeader(h.respStatus[h.retryAttempts]) + resp.Write([]byte(h.responseBody[h.retryAttempts])) } + h.retryAttempts++ } -func (s *MockArvadosServerSuite) TestFailThenSucceed(c *C) { - for _, testCase := range []string{ - "get", - "create", +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"}`}, + }, + + // Response code -1 simulates an HTTP/network error + // (i.e., Do() returns an error; there is no HTTP + // response status code). + + // Succeed on second retry + { + "get", 0, 200, []int{-1, -1, 200}, []string{``, ``, `{"ok":"ok"}`}, + }, + // "POST" is not safe to retry: fail after one error + { + "create", 0, -1, []int{-1, 200}, []string{``, `{"ok":"ok"}`}, + }, } { - stub := &FailThenSucceedHandler{0, 500} - - api, err := RunFakeArvadosServer(stub) + api, err := RunFakeArvadosServer(&stub) c.Check(err, IsNil) defer api.listener.Close() @@ -329,15 +355,32 @@ func (s *MockArvadosServerSuite) TestFailThenSucceed(c *C) { Retries: 2} getback := make(Dict) - switch testCase { + 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) + } + + switch stub.expected { + case 200: + c.Check(err, IsNil) + c.Check(getback["ok"], Equals, "ok") + case -1: + c.Check(err, NotNil) + c.Check(err, ErrorMatches, `.*stopped after \d+ redirects`) + default: + c.Check(err, NotNil) + c.Check(err, ErrorMatches, fmt.Sprintf("arvados API server error: %d.*", stub.expected)) + c.Check(err.(APIServerError).HttpStatusCode, Equals, stub.expected) } - c.Check(err, IsNil) - c.Assert(getback["name"], Equals, "testing") } }