ISSUE-4244 comply with --health-check-path (#4619)

This commit is contained in:
Andrea Spacca 2019-09-29 19:37:57 +02:00 committed by Manuel Alejandro de Brito Fontes
parent d5d2b4037c
commit 203a3ed455
2 changed files with 89 additions and 75 deletions

View file

@ -135,7 +135,7 @@ func main() {
go registerProfiler() go registerProfiler()
} }
registerHealthz(ngx, mux) registerHealthz(nginx.HealthPath, ngx, mux)
registerMetrics(reg, mux) registerMetrics(reg, mux)
registerHandlers(mux) registerHandlers(mux)
@ -247,9 +247,10 @@ func registerHandlers(mux *http.ServeMux) {
}) })
} }
func registerHealthz(ic *controller.NGINXController, mux *http.ServeMux) { func registerHealthz(healthPath string, ic *controller.NGINXController, mux *http.ServeMux) {
// expose health check endpoint (/healthz) // expose health check endpoint (/healthz)
healthz.InstallHandler(mux, healthz.InstallPathHandler(mux,
healthPath,
healthz.PingHealthz, healthz.PingHealthz,
ic, ic,
) )

View file

@ -33,6 +33,17 @@ import (
) )
func TestNginxCheck(t *testing.T) { func TestNginxCheck(t *testing.T) {
var tests = []struct {
healthzPath string
}{
{"/healthz"},
{"/not-healthz"},
}
for _, tt := range tests {
testName := fmt.Sprintf("health path: %s", tt.healthzPath)
t.Run(testName, func(t *testing.T) {
mux := http.NewServeMux() mux := http.NewServeMux()
listener, err := net.Listen("tcp", fmt.Sprintf(":%v", nginx.StatusPort)) listener, err := net.Listen("tcp", fmt.Sprintf(":%v", nginx.StatusPort))
@ -60,7 +71,7 @@ func TestNginxCheck(t *testing.T) {
} }
t.Run("no pid or process", func(t *testing.T) { t.Run("no pid or process", func(t *testing.T) {
if err := callHealthz(true, mux); err == nil { if err := callHealthz(true, tt.healthzPath, mux); err == nil {
t.Error("expected an error but none returned") t.Error("expected an error but none returned")
} }
}) })
@ -73,7 +84,7 @@ func TestNginxCheck(t *testing.T) {
} }
t.Run("no process", func(t *testing.T) { t.Run("no process", func(t *testing.T) {
if err := callHealthz(true, mux); err == nil { if err := callHealthz(true, tt.healthzPath, mux); err == nil {
t.Error("expected an error but none returned") t.Error("expected an error but none returned")
} }
}) })
@ -90,10 +101,10 @@ func TestNginxCheck(t *testing.T) {
pidFile.Write([]byte(fmt.Sprintf("%v", pid))) pidFile.Write([]byte(fmt.Sprintf("%v", pid)))
pidFile.Close() pidFile.Close()
healthz.InstallHandler(mux, n) healthz.InstallPathHandler(mux, tt.healthzPath, n)
t.Run("valid request", func(t *testing.T) { t.Run("valid request", func(t *testing.T) {
if err := callHealthz(false, mux); err != nil { if err := callHealthz(false, tt.healthzPath, mux); err != nil {
t.Error(err) t.Error(err)
} }
}) })
@ -103,14 +114,16 @@ func TestNginxCheck(t *testing.T) {
pidFile.Close() pidFile.Close()
t.Run("bad pid", func(t *testing.T) { t.Run("bad pid", func(t *testing.T) {
if err := callHealthz(true, mux); err == nil { if err := callHealthz(true, tt.healthzPath, mux); err == nil {
t.Error("expected an error but none returned") t.Error("expected an error but none returned")
} }
}) })
})
}
} }
func callHealthz(expErr bool, mux *http.ServeMux) error { func callHealthz(expErr bool, healthzPath string, mux *http.ServeMux) error {
req, err := http.NewRequest("GET", "/healthz", nil) req, err := http.NewRequest("GET", healthzPath, nil)
if err != nil { if err != nil {
return fmt.Errorf("healthz error: %v", err) return fmt.Errorf("healthz error: %v", err)
} }