handler_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2017 by the contributors.
  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 healthcheck
  15. import (
  16. "errors"
  17. "net/http"
  18. "net/http/httptest"
  19. "testing"
  20. "github.com/stretchr/testify/assert"
  21. )
  22. func TestNewHandler(t *testing.T) {
  23. tests := []struct {
  24. name string
  25. method string
  26. path string
  27. live bool
  28. ready bool
  29. expect int
  30. expectBody string
  31. }{
  32. {
  33. name: "GET /foo should generate a 404",
  34. method: "POST",
  35. path: "/foo",
  36. live: true,
  37. ready: true,
  38. expect: http.StatusNotFound,
  39. },
  40. {
  41. name: "POST /live should generate a 405 Method Not Allowed",
  42. method: "POST",
  43. path: "/live",
  44. live: true,
  45. ready: true,
  46. expect: http.StatusMethodNotAllowed,
  47. },
  48. {
  49. name: "POST /ready should generate a 405 Method Not Allowed",
  50. method: "POST",
  51. path: "/ready",
  52. live: true,
  53. ready: true,
  54. expect: http.StatusMethodNotAllowed,
  55. },
  56. {
  57. name: "with no checks, /live should succeed",
  58. method: "GET",
  59. path: "/live",
  60. live: true,
  61. ready: true,
  62. expect: http.StatusOK,
  63. expectBody: "{}\n",
  64. },
  65. {
  66. name: "with no checks, /ready should succeed",
  67. method: "GET",
  68. path: "/ready",
  69. live: true,
  70. ready: true,
  71. expect: http.StatusOK,
  72. expectBody: "{}\n",
  73. },
  74. {
  75. name: "with a failing readiness check, /live should still succeed",
  76. method: "GET",
  77. path: "/live?full=1",
  78. live: true,
  79. ready: false,
  80. expect: http.StatusOK,
  81. expectBody: "{}\n",
  82. },
  83. {
  84. name: "with a failing readiness check, /ready should fail",
  85. method: "GET",
  86. path: "/ready?full=1",
  87. live: true,
  88. ready: false,
  89. expect: http.StatusServiceUnavailable,
  90. expectBody: "{\n \"test-readiness-check\": \"failed readiness check\"\n}\n",
  91. },
  92. {
  93. name: "with a failing liveness check, /live should fail",
  94. method: "GET",
  95. path: "/live?full=1",
  96. live: false,
  97. ready: true,
  98. expect: http.StatusServiceUnavailable,
  99. expectBody: "{\n \"test-liveness-check\": \"failed liveness check\"\n}\n",
  100. },
  101. {
  102. name: "with a failing liveness check, /ready should fail",
  103. method: "GET",
  104. path: "/ready?full=1",
  105. live: false,
  106. ready: true,
  107. expect: http.StatusServiceUnavailable,
  108. expectBody: "{\n \"test-liveness-check\": \"failed liveness check\"\n}\n",
  109. },
  110. {
  111. name: "with a failing liveness check, /ready without full=1 should fail with an empty body",
  112. method: "GET",
  113. path: "/ready",
  114. live: false,
  115. ready: true,
  116. expect: http.StatusServiceUnavailable,
  117. expectBody: "{}\n",
  118. },
  119. }
  120. for _, tt := range tests {
  121. t.Run(tt.name, func(t *testing.T) {
  122. h := NewHandler()
  123. if !tt.live {
  124. h.AddLivenessCheck("test-liveness-check", func() error {
  125. return errors.New("failed liveness check")
  126. })
  127. }
  128. if !tt.ready {
  129. h.AddReadinessCheck("test-readiness-check", func() error {
  130. return errors.New("failed readiness check")
  131. })
  132. }
  133. req, err := http.NewRequest(tt.method, tt.path, nil)
  134. assert.NoError(t, err)
  135. reqStr := tt.method + " " + tt.path
  136. rr := httptest.NewRecorder()
  137. h.ServeHTTP(rr, req)
  138. assert.Equal(t, tt.expect, rr.Code, "wrong code for %q", reqStr)
  139. if tt.expectBody != "" {
  140. assert.Equal(t, tt.expectBody, rr.Body.String(), "wrong body for %q", reqStr)
  141. }
  142. })
  143. }
  144. }