http_client_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package pkg
  15. import (
  16. "fmt"
  17. "io"
  18. "math"
  19. "net/http"
  20. "net/http/httptest"
  21. "testing"
  22. "time"
  23. )
  24. func TestExpBackoff(t *testing.T) {
  25. duration := time.Millisecond
  26. max := time.Hour
  27. for i := 0; i < math.MaxUint16; i++ {
  28. duration = ExpBackoff(duration, max)
  29. if duration < 0 {
  30. t.Fatalf("duration too small: %v %v", duration, i)
  31. }
  32. if duration > max {
  33. t.Fatalf("duration too large: %v %v", duration, i)
  34. }
  35. }
  36. }
  37. // Test exponential backoff and that it continues retrying if a 5xx response is
  38. // received
  39. func TestGetURLExpBackOff(t *testing.T) {
  40. var expBackoffTests = []struct {
  41. count int
  42. body string
  43. }{
  44. {0, "number of attempts: 0"},
  45. {1, "number of attempts: 1"},
  46. {2, "number of attempts: 2"},
  47. }
  48. client := NewHTTPClient()
  49. for i, tt := range expBackoffTests {
  50. mux := http.NewServeMux()
  51. count := 0
  52. mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  53. if count == tt.count {
  54. io.WriteString(w, fmt.Sprintf("number of attempts: %d", count))
  55. return
  56. }
  57. count++
  58. http.Error(w, "", 500)
  59. })
  60. ts := httptest.NewServer(mux)
  61. defer ts.Close()
  62. data, err := client.GetRetry(ts.URL)
  63. if err != nil {
  64. t.Errorf("Test case %d produced error: %v", i, err)
  65. }
  66. if count != tt.count {
  67. t.Errorf("Test case %d failed: %d != %d", i, count, tt.count)
  68. }
  69. if string(data) != tt.body {
  70. t.Errorf("Test case %d failed: %s != %s", i, tt.body, data)
  71. }
  72. }
  73. }
  74. // Test that it stops retrying if a 4xx response comes back
  75. func TestGetURL4xx(t *testing.T) {
  76. client := NewHTTPClient()
  77. retries := 0
  78. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  79. retries++
  80. http.Error(w, "", 404)
  81. }))
  82. defer ts.Close()
  83. _, err := client.GetRetry(ts.URL)
  84. if err == nil {
  85. t.Errorf("Incorrect result\ngot: %s\nwant: %s", err.Error(), "Not found. HTTP status code: 404")
  86. }
  87. if retries > 1 {
  88. t.Errorf("Number of retries:\n%d\nExpected number of retries:\n%d", retries, 1)
  89. }
  90. }
  91. // Test that it fetches and returns user-data just fine
  92. func TestGetURL2xx(t *testing.T) {
  93. var cloudcfg = `
  94. #cloud-config
  95. coreos:
  96. oem:
  97. id: test
  98. name: CoreOS.box for Test
  99. version-id: %VERSION_ID%+%BUILD_ID%
  100. home-url: https://github.com/rancher/os/config/cloudinit
  101. bug-report-url: https://github.com/rancher/os/config/cloudinit
  102. update:
  103. reboot-strategy: best-effort
  104. `
  105. client := NewHTTPClient()
  106. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  107. fmt.Fprint(w, cloudcfg)
  108. }))
  109. defer ts.Close()
  110. data, err := client.GetRetry(ts.URL)
  111. if err != nil {
  112. t.Errorf("Incorrect result\ngot: %v\nwant: %v", err, nil)
  113. }
  114. if string(data) != cloudcfg {
  115. t.Errorf("Incorrect result\ngot: %s\nwant: %s", string(data), cloudcfg)
  116. }
  117. }
  118. // Test attempt to fetching using malformed URL
  119. func TestGetMalformedURL(t *testing.T) {
  120. client := NewHTTPClient()
  121. var tests = []struct {
  122. url string
  123. want string
  124. }{
  125. {"boo", "URL boo does not have a valid HTTP scheme. Skipping"},
  126. {"mailto://boo", "URL mailto://boo does not have a valid HTTP scheme. Skipping"},
  127. {"ftp://boo", "URL ftp://boo does not have a valid HTTP scheme. Skipping"},
  128. {"", "URL is empty. Skipping"},
  129. }
  130. for _, test := range tests {
  131. _, err := client.GetRetry(test.url)
  132. if err == nil || err.Error() != test.want {
  133. t.Errorf("Incorrect result\ngot: %v\nwant: %v", err, test.want)
  134. }
  135. }
  136. }