2017-10-23 18:39:15 +00:00
|
|
|
package health
|
2016-12-22 17:07:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2017-03-04 00:49:42 +00:00
|
|
|
"github.com/go-kit/kit/log"
|
2016-12-22 17:07:47 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-10-23 18:39:15 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-12-22 17:07:47 +00:00
|
|
|
)
|
|
|
|
|
2017-10-23 18:39:15 +00:00
|
|
|
func TestCheckHealth(t *testing.T) {
|
|
|
|
checkers := map[string]Checker{
|
|
|
|
"fail": fail{},
|
|
|
|
"pass": Nop(),
|
|
|
|
}
|
|
|
|
|
|
|
|
healthy := CheckHealth(log.NewNopLogger(), checkers)
|
|
|
|
require.False(t, healthy)
|
|
|
|
|
|
|
|
checkers = map[string]Checker{
|
|
|
|
"pass": Nop(),
|
|
|
|
}
|
|
|
|
healthy = CheckHealth(log.NewNopLogger(), checkers)
|
|
|
|
require.True(t, healthy)
|
|
|
|
}
|
|
|
|
|
|
|
|
type fail struct{}
|
|
|
|
|
|
|
|
func (c fail) HealthCheck() error {
|
|
|
|
return errors.New("fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHealthzHandler(t *testing.T) {
|
2017-03-04 00:49:42 +00:00
|
|
|
logger := log.NewNopLogger()
|
2022-07-01 11:08:03 +00:00
|
|
|
failCheck := healthcheckFunc(func() error {
|
|
|
|
return errors.New("health check failed")
|
|
|
|
})
|
|
|
|
passCheck := healthcheckFunc(func() error {
|
|
|
|
return nil
|
|
|
|
})
|
2017-10-23 18:39:15 +00:00
|
|
|
|
2022-07-01 11:08:03 +00:00
|
|
|
fail := Handler(logger, map[string]Checker{
|
|
|
|
"mock": failCheck,
|
|
|
|
})
|
|
|
|
pass := Handler(logger, map[string]Checker{
|
|
|
|
"mock": passCheck,
|
|
|
|
})
|
|
|
|
both := Handler(logger, map[string]Checker{
|
|
|
|
"pass": passCheck,
|
|
|
|
"fail": failCheck,
|
|
|
|
})
|
2016-12-22 17:07:47 +00:00
|
|
|
|
2022-07-01 11:08:03 +00:00
|
|
|
httpTests := []struct {
|
2016-12-22 17:07:47 +00:00
|
|
|
handler http.Handler
|
2022-07-01 11:08:03 +00:00
|
|
|
path string
|
|
|
|
wantHeader int
|
2016-12-22 17:07:47 +00:00
|
|
|
}{
|
2022-07-01 11:08:03 +00:00
|
|
|
{pass, "/healthz", http.StatusOK},
|
|
|
|
{fail, "/healthz", http.StatusInternalServerError},
|
|
|
|
|
|
|
|
// Empty check name
|
|
|
|
{pass, "/healthz?check=mock&check=", http.StatusBadRequest},
|
|
|
|
// Bad check name
|
|
|
|
{pass, "/healthz?check=mock&check=bad", http.StatusBadRequest},
|
|
|
|
// Passing and failing checks
|
|
|
|
{both, "/healthz", http.StatusInternalServerError},
|
|
|
|
// Passing and failing checks
|
|
|
|
{both, "/healthz?check=pass&check=fail", http.StatusInternalServerError},
|
|
|
|
// Only run passing
|
|
|
|
{both, "/healthz?check=pass", http.StatusOK},
|
|
|
|
// Only run failing
|
|
|
|
{both, "/healthz?check=fail", http.StatusInternalServerError},
|
2016-12-22 17:07:47 +00:00
|
|
|
}
|
|
|
|
for _, tt := range httpTests {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
rr := httptest.NewRecorder()
|
2022-07-01 11:08:03 +00:00
|
|
|
req := httptest.NewRequest("GET", tt.path, nil)
|
2016-12-22 17:07:47 +00:00
|
|
|
tt.handler.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, rr.Code, tt.wantHeader)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type healthcheckFunc func() error
|
|
|
|
|
|
|
|
func (fn healthcheckFunc) HealthCheck() error {
|
|
|
|
return fn()
|
|
|
|
}
|