X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/c1d63408c611a5ce65dc4de41aeb323c06da2bbc..3b4da80a23136c0cec743ee4cdad9b22b51ac3e0:/sdk/go/arvadosclient/arvadosclient_test.go diff --git a/sdk/go/arvadosclient/arvadosclient_test.go b/sdk/go/arvadosclient/arvadosclient_test.go index 045acf81e7..372f09d14b 100644 --- a/sdk/go/arvadosclient/arvadosclient_test.go +++ b/sdk/go/arvadosclient/arvadosclient_test.go @@ -1,14 +1,19 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: Apache-2.0 + package arvadosclient import ( "fmt" - "git.curoverse.com/arvados.git/sdk/go/arvadostest" - . "gopkg.in/check.v1" "net" "net/http" "os" "testing" "time" + + "git.curoverse.com/arvados.git/sdk/go/arvadostest" + . "gopkg.in/check.v1" ) // Gocheck boilerplate @@ -29,27 +34,32 @@ 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() } func (s *ServerRequiredSuite) TestMakeArvadosClientSecure(c *C) { os.Setenv("ARVADOS_API_HOST_INSECURE", "") - kc, err := MakeArvadosClient() + ac, err := MakeArvadosClient() c.Assert(err, Equals, nil) - c.Check(kc.ApiServer, Equals, os.Getenv("ARVADOS_API_HOST")) - c.Check(kc.ApiToken, Equals, os.Getenv("ARVADOS_API_TOKEN")) - c.Check(kc.ApiInsecure, Equals, false) + c.Check(ac.ApiServer, Equals, os.Getenv("ARVADOS_API_HOST")) + c.Check(ac.ApiToken, Equals, os.Getenv("ARVADOS_API_TOKEN")) + c.Check(ac.ApiInsecure, Equals, false) } func (s *ServerRequiredSuite) TestMakeArvadosClientInsecure(c *C) { os.Setenv("ARVADOS_API_HOST_INSECURE", "true") - kc, err := MakeArvadosClient() + ac, err := MakeArvadosClient() c.Assert(err, Equals, nil) - c.Check(kc.ApiInsecure, Equals, true) - c.Check(kc.ApiServer, Equals, os.Getenv("ARVADOS_API_HOST")) - c.Check(kc.ApiToken, Equals, os.Getenv("ARVADOS_API_TOKEN")) - c.Check(kc.Client.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify, Equals, true) + c.Check(ac.ApiInsecure, Equals, true) + c.Check(ac.ApiServer, Equals, os.Getenv("ARVADOS_API_HOST")) + c.Check(ac.ApiToken, Equals, os.Getenv("ARVADOS_API_TOKEN")) + c.Check(ac.Client.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify, Equals, true) } func (s *ServerRequiredSuite) TestGetInvalidUUID(c *C) { @@ -163,7 +173,7 @@ func (s *ServerRequiredSuite) TestErrorResponse(c *C) { Dict{"log": Dict{"bogus_attr": "foo"}}, &getback) c.Assert(err, ErrorMatches, "arvados API server error: .*") - c.Assert(err, ErrorMatches, ".*unknown attribute: bogus_attr.*") + c.Assert(err, ErrorMatches, ".*unknown attribute(: | ')bogus_attr.*") c.Assert(err, FitsTypeOf, APIServerError{}) c.Assert(err.(APIServerError).HttpStatusCode, Equals, 422) } @@ -258,8 +268,17 @@ type APIStub struct { } func (h *APIStub) ServeHTTP(resp http.ResponseWriter, req *http.Request) { - resp.WriteHeader(h.respStatus[h.retryAttempts]) - resp.Write([]byte(h.responseBody[h.retryAttempts])) + 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.WriteHeader(h.respStatus[h.retryAttempts]) + resp.Write([]byte(h.responseBody[h.retryAttempts])) + } h.retryAttempts++ } @@ -313,13 +332,18 @@ func (s *MockArvadosServerSuite) TestWithRetries(c *C) { { "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. + + // 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, -1, nil, nil, + "get", 0, 200, []int{-1, -1, 200}, []string{``, ``, `{"ok":"ok"}`}, }, + // "POST" is not safe to retry: fail after one error { - "create", 0, -1, nil, nil, + "create", 0, -1, []int{-1, 200}, []string{``, `{"ok":"ok"}`}, }, } { api, err := RunFakeArvadosServer(&stub) @@ -335,12 +359,6 @@ func (s *MockArvadosServerSuite) TestWithRetries(c *C) { 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": @@ -357,18 +375,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) - - 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) - } + 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) } } }