4
0

handler_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package logging
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "github.com/smallstep/assert"
  8. "github.com/sirupsen/logrus"
  9. "github.com/sirupsen/logrus/hooks/test"
  10. )
  11. // TestHealthOKHandling ensures that http requests from the Kubernetes
  12. // liveness/readiness probes are only logged at Trace level if they are HTTP
  13. // 200 (which is normal operation) and the user has opted-in. If the user has
  14. // not opted-in then they continue to be logged at Info level.
  15. func TestHealthOKHandling(t *testing.T) {
  16. statusHandler := func(statusCode int) http.HandlerFunc {
  17. return func(w http.ResponseWriter, _ *http.Request) {
  18. w.WriteHeader(statusCode)
  19. fmt.Fprint(w, "{}")
  20. }
  21. }
  22. tests := []struct {
  23. name string
  24. path string
  25. options options
  26. handler http.HandlerFunc
  27. want logrus.Level
  28. }{
  29. {
  30. name: "200 should be logged at Info level for /health request without explicit opt-in",
  31. path: "/health",
  32. handler: statusHandler(http.StatusOK),
  33. want: logrus.InfoLevel,
  34. },
  35. {
  36. name: "200 should be logged only at Trace level for /health request if opt-in",
  37. path: "/health",
  38. options: options{
  39. onlyTraceHealthEndpoint: true,
  40. },
  41. handler: statusHandler(http.StatusOK),
  42. want: logrus.TraceLevel,
  43. },
  44. }
  45. for _, tt := range tests {
  46. t.Run(tt.name, func(t *testing.T) {
  47. logger, hook := test.NewNullLogger()
  48. logger.SetLevel(logrus.TraceLevel)
  49. l := &LoggerHandler{
  50. logger: logger,
  51. options: tt.options,
  52. next: tt.handler,
  53. }
  54. r := httptest.NewRequest("GET", tt.path, http.NoBody)
  55. w := httptest.NewRecorder()
  56. l.ServeHTTP(w, r)
  57. if assert.Equals(t, 1, len(hook.AllEntries())) {
  58. assert.Equals(t, tt.want, hook.LastEntry().Level)
  59. }
  60. })
  61. }
  62. }
  63. // TestHandlingRegardlessOfOptions ensures that http requests are treated like
  64. // any other request if they are for a non-health uri or fall within the
  65. // warn/error ranges of the http status codes, regardless of the
  66. // "onlyTraceHealthEndpoint" option.
  67. func TestHandlingRegardlessOfOptions(t *testing.T) {
  68. statusHandler := func(statusCode int) http.HandlerFunc {
  69. return func(w http.ResponseWriter, _ *http.Request) {
  70. w.WriteHeader(statusCode)
  71. fmt.Fprint(w, "{}")
  72. }
  73. }
  74. tests := []struct {
  75. name string
  76. path string
  77. handler http.HandlerFunc
  78. want logrus.Level
  79. }{
  80. {
  81. name: "200 should be logged at Info level for non-health requests",
  82. path: "/info",
  83. handler: statusHandler(http.StatusOK),
  84. want: logrus.InfoLevel,
  85. },
  86. {
  87. name: "400 should be logged at Warn level for non-health requests",
  88. path: "/info",
  89. handler: statusHandler(http.StatusBadRequest),
  90. want: logrus.WarnLevel,
  91. },
  92. {
  93. name: "500 should be logged at Error level for non-health requests",
  94. path: "/info",
  95. handler: statusHandler(http.StatusInternalServerError),
  96. want: logrus.ErrorLevel,
  97. },
  98. {
  99. name: "400 should be logged at Warn level even for /health requests",
  100. path: "/health",
  101. handler: statusHandler(http.StatusBadRequest),
  102. want: logrus.WarnLevel,
  103. },
  104. {
  105. name: "500 should be logged at Error level even for /health requests",
  106. path: "/health",
  107. handler: statusHandler(http.StatusInternalServerError),
  108. want: logrus.ErrorLevel,
  109. },
  110. }
  111. for _, tt := range tests {
  112. t.Run(tt.name, func(t *testing.T) {
  113. for _, b := range []bool{true, false} {
  114. logger, hook := test.NewNullLogger()
  115. logger.SetLevel(logrus.TraceLevel)
  116. l := &LoggerHandler{
  117. logger: logger,
  118. options: options{
  119. onlyTraceHealthEndpoint: b,
  120. },
  121. next: tt.handler,
  122. }
  123. r := httptest.NewRequest("GET", tt.path, http.NoBody)
  124. w := httptest.NewRecorder()
  125. l.ServeHTTP(w, r)
  126. if assert.Equals(t, 1, len(hook.AllEntries())) {
  127. assert.Equals(t, tt.want, hook.LastEntry().Level)
  128. }
  129. }
  130. })
  131. }
  132. }