1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
23 check "gopkg.in/check.v1"
26 type stubTransport struct {
27 Responses map[string]string
28 Requests []http.Request
32 func (stub *stubTransport) RoundTrip(req *http.Request) (*http.Response, error) {
34 stub.Requests = append(stub.Requests, *req)
37 resp := &http.Response{
45 str := stub.Responses[req.URL.Path]
47 resp.Status = "404 Not Found"
51 buf := bytes.NewBufferString(str)
52 resp.Body = ioutil.NopCloser(buf)
53 resp.ContentLength = int64(buf.Len())
57 type errorTransport struct{}
59 func (stub *errorTransport) RoundTrip(req *http.Request) (*http.Response, error) {
60 return nil, fmt.Errorf("something awful happened")
63 type timeoutTransport struct {
67 func (stub *timeoutTransport) RoundTrip(req *http.Request) (*http.Response, error) {
68 return &http.Response{
75 Body: ioutil.NopCloser(iotest.TimeoutReader(bytes.NewReader(stub.response))),
79 var _ = check.Suite(&clientSuite{})
81 type clientSuite struct{}
83 func (*clientSuite) TestCurrentUser(c *check.C) {
84 stub := &stubTransport{
85 Responses: map[string]string{
86 "/arvados/v1/users/current": `{"uuid":"zzzzz-abcde-012340123401234"}`,
93 APIHost: "zzzzz.arvadosapi.com",
96 u, err := client.CurrentUser()
97 c.Check(err, check.IsNil)
98 c.Check(u.UUID, check.Equals, "zzzzz-abcde-012340123401234")
99 c.Check(stub.Requests, check.Not(check.HasLen), 0)
100 hdr := stub.Requests[len(stub.Requests)-1].Header
101 c.Check(hdr.Get("Authorization"), check.Equals, "OAuth2 xyzzy")
103 client.Client.Transport = &errorTransport{}
104 u, err = client.CurrentUser()
105 c.Check(err, check.NotNil)
108 func (*clientSuite) TestAnythingToValues(c *check.C) {
109 type testCase struct {
111 // ok==nil means anythingToValues should return an
112 // error, otherwise it's a func that returns true if
114 ok func(out url.Values) bool
116 for _, tc := range []testCase{
118 in: map[string]interface{}{"foo": "bar"},
119 ok: func(out url.Values) bool {
120 return out.Get("foo") == "bar"
124 in: map[string]interface{}{"foo": 2147483647},
125 ok: func(out url.Values) bool {
126 return out.Get("foo") == "2147483647"
130 in: map[string]interface{}{"foo": 1.234},
131 ok: func(out url.Values) bool {
132 return out.Get("foo") == "1.234"
136 in: map[string]interface{}{"foo": "1.234"},
137 ok: func(out url.Values) bool {
138 return out.Get("foo") == "1.234"
142 in: map[string]interface{}{"foo": map[string]interface{}{"bar": 1.234}},
143 ok: func(out url.Values) bool {
144 return out.Get("foo") == `{"bar":1.234}`
148 in: url.Values{"foo": {"bar"}},
149 ok: func(out url.Values) bool {
150 return out.Get("foo") == "bar"
163 out, err := anythingToValues(tc.in)
165 c.Check(err, check.NotNil)
168 c.Check(err, check.IsNil)
169 c.Check(tc.ok(out), check.Equals, true)
173 func (*clientSuite) TestLoadConfig(c *check.C) {
174 oldenv := os.Environ()
177 for _, s := range oldenv {
178 i := strings.IndexRune(s, '=')
179 os.Setenv(s[:i], s[i+1:])
184 os.Setenv("HOME", tmp)
185 for _, s := range os.Environ() {
186 if strings.HasPrefix(s, "ARVADOS_") {
187 i := strings.IndexRune(s, '=')
191 os.Mkdir(tmp+"/.config", 0777)
192 os.Mkdir(tmp+"/.config/arvados", 0777)
194 // Use $HOME/.config/arvados/settings.conf if no env vars are
196 os.WriteFile(tmp+"/.config/arvados/settings.conf", []byte(`
197 ARVADOS_API_HOST = localhost:1
198 ARVADOS_API_TOKEN = token_from_settings_file1
200 client := NewClientFromEnv()
201 c.Check(client.AuthToken, check.Equals, "token_from_settings_file1")
202 c.Check(client.APIHost, check.Equals, "localhost:1")
203 c.Check(client.Insecure, check.Equals, false)
205 // ..._INSECURE=true, comments, ignored lines in settings.conf
206 os.WriteFile(tmp+"/.config/arvados/settings.conf", []byte(`
207 (ignored) = (ignored)
208 #ARVADOS_API_HOST = localhost:2
209 ARVADOS_API_TOKEN = token_from_settings_file2
210 ARVADOS_API_HOST_INSECURE = true
212 client = NewClientFromEnv()
213 c.Check(client.AuthToken, check.Equals, "token_from_settings_file2")
214 c.Check(client.APIHost, check.Equals, "")
215 c.Check(client.Insecure, check.Equals, true)
217 // Environment variables override settings.conf
218 os.Setenv("ARVADOS_API_HOST", "[::]:3")
219 os.Setenv("ARVADOS_API_HOST_INSECURE", "0")
220 client = NewClientFromEnv()
221 c.Check(client.AuthToken, check.Equals, "token_from_settings_file2")
222 c.Check(client.APIHost, check.Equals, "[::]:3")
223 c.Check(client.Insecure, check.Equals, false)
226 var _ = check.Suite(&clientRetrySuite{})
228 type clientRetrySuite struct {
229 server *httptest.Server
233 respDelay time.Duration
235 origLimiterQuietPeriod time.Duration
238 func (s *clientRetrySuite) SetUpTest(c *check.C) {
239 // Test server: delay and return errors until a final status
240 // appears on the respStatus channel.
241 s.origLimiterQuietPeriod = requestLimiterQuietPeriod
242 requestLimiterQuietPeriod = time.Second / 100
243 s.server = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
244 s.reqs = append(s.reqs, r)
247 delay = time.Duration(rand.Int63n(int64(time.Second / 10)))
249 timer := time.NewTimer(delay)
252 case code, ok := <-s.respStatus:
257 w.Write([]byte(`{}`))
259 w.WriteHeader(http.StatusServiceUnavailable)
263 s.respStatus = make(chan int, 1)
265 APIHost: s.server.URL[8:],
268 Timeout: 2 * time.Second,
272 func (s *clientRetrySuite) TearDownTest(c *check.C) {
274 requestLimiterQuietPeriod = s.origLimiterQuietPeriod
277 func (s *clientRetrySuite) TestOK(c *check.C) {
278 s.respStatus <- http.StatusOK
279 err := s.client.RequestAndDecode(&struct{}{}, http.MethodGet, "test", nil, nil)
280 c.Check(err, check.IsNil)
281 c.Check(s.reqs, check.HasLen, 1)
284 func (s *clientRetrySuite) TestNetworkError(c *check.C) {
285 // Close the stub server to produce a "connection refused" error.
289 timeout := time.Second
290 ctx, cancel := context.WithDeadline(context.Background(), start.Add(timeout))
292 s.client.Timeout = timeout * 2
293 err := s.client.RequestAndDecodeContext(ctx, &struct{}{}, http.MethodGet, "test", nil, nil)
294 c.Check(err, check.ErrorMatches, `.*dial tcp .* connection refused.*`)
295 delta := time.Since(start)
296 c.Check(delta > timeout, check.Equals, true, check.Commentf("time.Since(start) == %v, timeout = %v", delta, timeout))
299 func (s *clientRetrySuite) TestNonRetryableError(c *check.C) {
300 s.respStatus <- http.StatusBadRequest
301 err := s.client.RequestAndDecode(&struct{}{}, http.MethodGet, "test", nil, nil)
302 c.Check(err, check.ErrorMatches, `.*400 Bad Request.*`)
303 c.Check(s.reqs, check.HasLen, 1)
306 func (s *clientRetrySuite) TestNonRetryableAfter503s(c *check.C) {
307 time.AfterFunc(time.Second, func() { s.respStatus <- http.StatusNotFound })
308 err := s.client.RequestAndDecode(&struct{}{}, http.MethodGet, "test", nil, nil)
309 c.Check(err, check.ErrorMatches, `.*404 Not Found.*`)
312 func (s *clientRetrySuite) TestOKAfter503s(c *check.C) {
315 time.AfterFunc(delay, func() { s.respStatus <- http.StatusOK })
316 err := s.client.RequestAndDecode(&struct{}{}, http.MethodGet, "test", nil, nil)
317 c.Check(err, check.IsNil)
318 c.Check(len(s.reqs) > 1, check.Equals, true, check.Commentf("len(s.reqs) == %d", len(s.reqs)))
319 c.Check(time.Since(start) > delay, check.Equals, true)
322 func (s *clientRetrySuite) TestTimeoutAfter503(c *check.C) {
323 s.respStatus <- http.StatusServiceUnavailable
324 s.respDelay = time.Second * 2
325 s.client.Timeout = time.Second / 2
326 err := s.client.RequestAndDecode(&struct{}{}, http.MethodGet, "test", nil, nil)
327 c.Check(err, check.ErrorMatches, `.*503 Service Unavailable.*`)
328 c.Check(s.reqs, check.HasLen, 2)
331 func (s *clientRetrySuite) Test503Forever(c *check.C) {
332 err := s.client.RequestAndDecode(&struct{}{}, http.MethodGet, "test", nil, nil)
333 c.Check(err, check.ErrorMatches, `.*503 Service Unavailable.*`)
334 c.Check(len(s.reqs) > 1, check.Equals, true, check.Commentf("len(s.reqs) == %d", len(s.reqs)))
337 func (s *clientRetrySuite) TestContextAlreadyCanceled(c *check.C) {
338 ctx, cancel := context.WithCancel(context.Background())
340 err := s.client.RequestAndDecodeContext(ctx, &struct{}{}, http.MethodGet, "test", nil, nil)
341 c.Check(err, check.Equals, context.Canceled)
344 func (s *clientRetrySuite) TestExponentialBackoff(c *check.C) {
345 var min, max time.Duration
346 min, max = time.Second, 64*time.Second
348 t := exponentialBackoff(min, max, 0, nil)
349 c.Check(t, check.Equals, min)
351 for e := float64(1); e < 5; e += 1 {
353 for i := 0; i < 20; i++ {
354 t = exponentialBackoff(min, max, int(e), nil)
355 // Every returned value must be between min and min(2^e, max)
356 c.Check(t >= min, check.Equals, true)
357 c.Check(t <= min*time.Duration(math.Pow(2, e)), check.Equals, true)
358 c.Check(t <= max, check.Equals, true)
359 // Check that jitter is actually happening by
360 // checking that at least one in 20 trials is
361 // between min*2^(e-.75) and min*2^(e-.25)
362 jittermin := time.Duration(float64(min) * math.Pow(2, e-0.75))
363 jittermax := time.Duration(float64(min) * math.Pow(2, e-0.25))
364 c.Logf("min %v max %v e %v jittermin %v jittermax %v t %v", min, max, e, jittermin, jittermax, t)
365 if t > jittermin && t < jittermax {
370 c.Check(ok, check.Equals, true)
373 for i := 0; i < 20; i++ {
374 t := exponentialBackoff(min, max, 100, nil)
375 c.Check(t < max, check.Equals, true)
378 for _, trial := range []struct {
382 {"1", time.Second * 4}, // minimum enforced
383 {"5", time.Second * 5}, // header used
384 {"55", time.Second * 10}, // maximum enforced
385 {"eleventy-nine", time.Second * 4}, // invalid header, exponential backoff used
386 {time.Now().UTC().Add(time.Second).Format(time.RFC1123), time.Second * 4}, // minimum enforced
387 {time.Now().UTC().Add(time.Minute).Format(time.RFC1123), time.Second * 10}, // maximum enforced
388 {time.Now().UTC().Add(-time.Minute).Format(time.RFC1123), time.Second * 4}, // minimum enforced
390 c.Logf("trial %+v", trial)
391 t := exponentialBackoff(time.Second*4, time.Second*10, 0, &http.Response{
392 StatusCode: http.StatusTooManyRequests,
393 Header: http.Header{"Retry-After": {trial.retryAfter}}})
394 c.Check(t, check.Equals, trial.expect)
396 t = exponentialBackoff(time.Second*4, time.Second*10, 0, &http.Response{
397 StatusCode: http.StatusTooManyRequests,
399 c.Check(t, check.Equals, time.Second*4)
401 t = exponentialBackoff(0, max, 0, nil)
402 c.Check(t, check.Equals, time.Duration(0))
403 t = exponentialBackoff(0, max, 1, nil)
404 c.Check(t, check.Not(check.Equals), time.Duration(0))