X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/3ded4c192b1fc655d9fffa225f792055f10f78c9..dfb9a5e285e4275c26b6f959a04babd5a8ad836e:/sdk/go/arvadosclient/arvadosclient_test.go diff --git a/sdk/go/arvadosclient/arvadosclient_test.go b/sdk/go/arvadosclient/arvadosclient_test.go index bc88c44022..8e32efe4f9 100644 --- a/sdk/go/arvadosclient/arvadosclient_test.go +++ b/sdk/go/arvadosclient/arvadosclient_test.go @@ -7,7 +7,6 @@ import ( "net" "net/http" "os" - "strings" "testing" "time" ) @@ -30,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() } @@ -250,24 +254,27 @@ func RunFakeArvadosServer(st http.Handler) (api APIServer, err error) { return } -func (h *APIStub) ServeHTTP(resp http.ResponseWriter, req *http.Request) { - resp.WriteHeader(h.respStatus[h.count]) +type APIStub struct { + method string + retryAttempts int + expected int + respStatus []int + responseBody []string +} - if h.respStatus[h.count] == 200 { - resp.Write([]byte(`{"ok":"ok"}`)) +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 + } + 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.Write([]byte(``)) + resp.WriteHeader(h.respStatus[h.retryAttempts]) + resp.Write([]byte(h.responseBody[h.retryAttempts])) } - - h.count++ -} - -type APIStub struct { - method string - count int - expected int - respStatus []int - responseBody []string + h.retryAttempts++ } func (s *MockArvadosServerSuite) TestWithRetries(c *C) { @@ -320,6 +327,19 @@ func (s *MockArvadosServerSuite) TestWithRetries(c *C) { { "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"}`}, + }, } { api, err := RunFakeArvadosServer(&stub) c.Check(err, IsNil) @@ -350,13 +370,17 @@ func (s *MockArvadosServerSuite) TestWithRetries(c *C) { err = arv.Delete("pipeline_templates", "zzzzz-4zz18-znfnqtbbv4spc3w", nil, &getback) } - if stub.expected == 200 { + switch stub.expected { + case 200: c.Check(err, IsNil) - c.Assert(getback["ok"], Equals, "ok") - } else { + 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(strings.Contains(err.Error(), fmt.Sprintf("%s%d", "arvados API server error: ", stub.expected)), Equals, true) - c.Assert(err.(APIServerError).HttpStatusCode, Equals, stub.expected) + c.Check(err, ErrorMatches, fmt.Sprintf("arvados API server error: %d.*", stub.expected)) + c.Check(err.(APIServerError).HttpStatusCode, Equals, stub.expected) } } }