visitor_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package resource
  14. import (
  15. "bytes"
  16. "fmt"
  17. "io"
  18. "io/ioutil"
  19. "testing"
  20. "github.com/stretchr/testify/assert"
  21. )
  22. func TestVisitorHttpGet(t *testing.T) {
  23. // Test retries on errors
  24. i := 0
  25. expectedErr := fmt.Errorf("Failed to get http")
  26. actualBytes, actualErr := readHttpWithRetries(func(url string) (int, string, io.ReadCloser, error) {
  27. assert.Equal(t, "hello", url)
  28. i++
  29. if i > 2 {
  30. return 0, "", nil, expectedErr
  31. }
  32. return 0, "", nil, fmt.Errorf("Unexpected error")
  33. }, 0, "hello", 3)
  34. assert.Equal(t, expectedErr, actualErr)
  35. assert.Nil(t, actualBytes)
  36. assert.Equal(t, 3, i)
  37. // Test that 500s are retried.
  38. i = 0
  39. actualBytes, actualErr = readHttpWithRetries(func(url string) (int, string, io.ReadCloser, error) {
  40. assert.Equal(t, "hello", url)
  41. i++
  42. return 501, "Status", nil, nil
  43. }, 0, "hello", 3)
  44. assert.Error(t, actualErr)
  45. assert.Nil(t, actualBytes)
  46. assert.Equal(t, 3, i)
  47. // Test that 300s are not retried
  48. i = 0
  49. actualBytes, actualErr = readHttpWithRetries(func(url string) (int, string, io.ReadCloser, error) {
  50. assert.Equal(t, "hello", url)
  51. i++
  52. return 300, "Status", nil, nil
  53. }, 0, "hello", 3)
  54. assert.Error(t, actualErr)
  55. assert.Nil(t, actualBytes)
  56. assert.Equal(t, 1, i)
  57. // Test attempt count is respected
  58. i = 0
  59. actualBytes, actualErr = readHttpWithRetries(func(url string) (int, string, io.ReadCloser, error) {
  60. assert.Equal(t, "hello", url)
  61. i++
  62. return 501, "Status", nil, nil
  63. }, 0, "hello", 1)
  64. assert.Error(t, actualErr)
  65. assert.Nil(t, actualBytes)
  66. assert.Equal(t, 1, i)
  67. // Test attempts less than 1 results in an error
  68. i = 0
  69. b := bytes.Buffer{}
  70. actualBytes, actualErr = readHttpWithRetries(func(url string) (int, string, io.ReadCloser, error) {
  71. return 200, "Status", ioutil.NopCloser(&b), nil
  72. }, 0, "hello", 0)
  73. assert.Error(t, actualErr)
  74. assert.Nil(t, actualBytes)
  75. assert.Equal(t, 0, i)
  76. // Test Success
  77. i = 0
  78. b = bytes.Buffer{}
  79. actualBytes, actualErr = readHttpWithRetries(func(url string) (int, string, io.ReadCloser, error) {
  80. assert.Equal(t, "hello", url)
  81. i++
  82. if i > 1 {
  83. return 200, "Status", ioutil.NopCloser(&b), nil
  84. }
  85. return 501, "Status", nil, nil
  86. }, 0, "hello", 3)
  87. assert.Nil(t, actualErr)
  88. assert.NotNil(t, actualBytes)
  89. assert.Equal(t, 2, i)
  90. }