Fix some logs and commend informations for healthz

Signed-off-by: yuexiao-wang <wang.yuexiao@zte.com.cn>
This commit is contained in:
yuexiao-wang 2016-10-09 17:24:50 +08:00
parent 853ee4c9f7
commit 0c4974a31a
2 changed files with 8 additions and 8 deletions

View File

@ -23,7 +23,7 @@ import (
"sync" "sync"
) )
// HealthzChecker is a named healthz check. // HealthzChecker is a named healthz checker.
type HealthzChecker interface { type HealthzChecker interface {
Name() string Name() string
Check(req *http.Request) error Check(req *http.Request) error
@ -41,7 +41,7 @@ func DefaultHealthz(checks ...HealthzChecker) {
// PingHealthz returns true automatically when checked // PingHealthz returns true automatically when checked
var PingHealthz HealthzChecker = ping{} var PingHealthz HealthzChecker = ping{}
// ping implements the simplest possible health checker. // ping implements the simplest possible healthz checker.
type ping struct{} type ping struct{}
func (ping) Name() string { func (ping) Name() string {
@ -53,7 +53,7 @@ func (ping) Check(_ *http.Request) error {
return nil return nil
} }
// NamedCheck returns a health checker for the given name and function. // NamedCheck returns a healthz checker for the given name and function.
func NamedCheck(name string, check func(r *http.Request) error) HealthzChecker { func NamedCheck(name string, check func(r *http.Request) error) HealthzChecker {
return &healthzCheck{name, check} return &healthzCheck{name, check}
} }
@ -125,7 +125,7 @@ func adaptCheckToHandler(c func(r *http.Request) error) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := c(r) err := c(r)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("Internal server error: %v", err), http.StatusInternalServerError) http.Error(w, fmt.Sprintf("internal server error: %v", err), http.StatusInternalServerError)
} else { } else {
fmt.Fprint(w, "ok") fmt.Fprint(w, "ok")
} }

View File

@ -29,15 +29,15 @@ func TestInstallHandler(t *testing.T) {
InstallHandler(mux) InstallHandler(mux)
req, err := http.NewRequest("GET", "http://example.com/healthz", nil) req, err := http.NewRequest("GET", "http://example.com/healthz", nil)
if err != nil { if err != nil {
t.Fatalf("Unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
w := httptest.NewRecorder() w := httptest.NewRecorder()
mux.ServeHTTP(w, req) mux.ServeHTTP(w, req)
if w.Code != http.StatusOK { if w.Code != http.StatusOK {
t.Errorf("Expected %v, got %v", http.StatusOK, w.Code) t.Errorf("expected %v, got %v", http.StatusOK, w.Code)
} }
if w.Body.String() != "ok" { if w.Body.String() != "ok" {
t.Errorf("Expected %v, got %v", "ok", w.Body.String()) t.Errorf("expected %v, got %v", "ok", w.Body.String())
} }
} }
@ -53,7 +53,7 @@ func TestMulitipleChecks(t *testing.T) {
{"/healthz", "ok", http.StatusOK, false}, {"/healthz", "ok", http.StatusOK, false},
{"/healthz?verbose", "[+]ping ok\n[-]bad failed: this will fail\nhealthz check failed\n", http.StatusInternalServerError, true}, {"/healthz?verbose", "[+]ping ok\n[-]bad failed: this will fail\nhealthz check failed\n", http.StatusInternalServerError, true},
{"/healthz/ping", "ok", http.StatusOK, true}, {"/healthz/ping", "ok", http.StatusOK, true},
{"/healthz/bad", "Internal server error: this will fail\n", http.StatusInternalServerError, true}, {"/healthz/bad", "internal server error: this will fail\n", http.StatusInternalServerError, true},
{"/healthz", "[+]ping ok\n[-]bad failed: this will fail\nhealthz check failed\n", http.StatusInternalServerError, true}, {"/healthz", "[+]ping ok\n[-]bad failed: this will fail\nhealthz check failed\n", http.StatusInternalServerError, true},
} }