http_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright 2015 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 http
  14. import (
  15. "fmt"
  16. "net"
  17. "net/http"
  18. "net/http/httptest"
  19. "net/url"
  20. "strconv"
  21. "strings"
  22. "testing"
  23. "time"
  24. "k8s.io/kubernetes/pkg/probe"
  25. )
  26. const FailureCode int = -1
  27. func TestHTTPProbeChecker(t *testing.T) {
  28. handleReq := func(s int, body string) func(w http.ResponseWriter, r *http.Request) {
  29. return func(w http.ResponseWriter, r *http.Request) {
  30. w.WriteHeader(s)
  31. w.Write([]byte(body))
  32. }
  33. }
  34. prober := New()
  35. testCases := []struct {
  36. handler func(w http.ResponseWriter, r *http.Request)
  37. reqHeaders http.Header
  38. health probe.Result
  39. accBody string
  40. }{
  41. // The probe will be filled in below. This is primarily testing that an HTTP GET happens.
  42. {
  43. handler: handleReq(http.StatusOK, "ok body"),
  44. health: probe.Success,
  45. accBody: "ok body",
  46. },
  47. {
  48. // Echo handler that returns the contents of request headers in the body
  49. handler: func(w http.ResponseWriter, r *http.Request) {
  50. w.WriteHeader(200)
  51. output := ""
  52. for k, arr := range r.Header {
  53. for _, v := range arr {
  54. output += fmt.Sprintf("%s: %s\n", k, v)
  55. }
  56. }
  57. w.Write([]byte(output))
  58. },
  59. reqHeaders: http.Header{
  60. "X-Muffins-Or-Cupcakes": {"muffins"},
  61. },
  62. health: probe.Success,
  63. accBody: "X-Muffins-Or-Cupcakes: muffins",
  64. },
  65. {
  66. // Echo handler that returns the contents of Host in the body
  67. handler: func(w http.ResponseWriter, r *http.Request) {
  68. w.WriteHeader(200)
  69. w.Write([]byte(r.Host))
  70. },
  71. reqHeaders: http.Header{
  72. "Host": {"muffins.cupcakes.org"},
  73. },
  74. health: probe.Success,
  75. accBody: "muffins.cupcakes.org",
  76. },
  77. {
  78. handler: handleReq(FailureCode, "fail body"),
  79. health: probe.Failure,
  80. },
  81. {
  82. handler: handleReq(http.StatusInternalServerError, "fail body"),
  83. health: probe.Failure,
  84. },
  85. {
  86. handler: func(w http.ResponseWriter, r *http.Request) {
  87. time.Sleep(3 * time.Second)
  88. },
  89. health: probe.Failure,
  90. },
  91. }
  92. for i, test := range testCases {
  93. func() {
  94. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  95. test.handler(w, r)
  96. }))
  97. defer server.Close()
  98. u, err := url.Parse(server.URL)
  99. if err != nil {
  100. t.Errorf("case %d: unexpected error: %v", i, err)
  101. }
  102. _, port, err := net.SplitHostPort(u.Host)
  103. if err != nil {
  104. t.Errorf("case %d: unexpected error: %v", i, err)
  105. }
  106. _, err = strconv.Atoi(port)
  107. if err != nil {
  108. t.Errorf("case %d: unexpected error: %v", i, err)
  109. }
  110. health, output, err := prober.Probe(u, test.reqHeaders, 1*time.Second)
  111. if test.health == probe.Unknown && err == nil {
  112. t.Errorf("case %d: expected error", i)
  113. }
  114. if test.health != probe.Unknown && err != nil {
  115. t.Errorf("case %d: unexpected error: %v", i, err)
  116. }
  117. if health != test.health {
  118. t.Errorf("case %d: expected %v, got %v", i, test.health, health)
  119. }
  120. if health != probe.Failure && test.health != probe.Failure {
  121. if !strings.Contains(output, test.accBody) {
  122. t.Errorf("Expected response body to contain %v, got %v", test.accBody, output)
  123. }
  124. }
  125. }()
  126. }
  127. }