ISSUE-4244 comply with --health-check-path (#4619)
This commit is contained in:
parent
d5d2b4037c
commit
203a3ed455
2 changed files with 89 additions and 75 deletions
|
@ -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,
|
||||||
)
|
)
|
||||||
|
|
|
@ -33,84 +33,97 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNginxCheck(t *testing.T) {
|
func TestNginxCheck(t *testing.T) {
|
||||||
mux := http.NewServeMux()
|
var tests = []struct {
|
||||||
|
healthzPath string
|
||||||
listener, err := net.Listen("tcp", fmt.Sprintf(":%v", nginx.StatusPort))
|
}{
|
||||||
if err != nil {
|
{"/healthz"},
|
||||||
t.Fatalf("crating tcp listener: %s", err)
|
{"/not-healthz"},
|
||||||
}
|
|
||||||
defer listener.Close()
|
|
||||||
|
|
||||||
server := &httptest.Server{
|
|
||||||
Listener: listener,
|
|
||||||
Config: &http.Server{
|
|
||||||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
fmt.Fprintf(w, "ok")
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
defer server.Close()
|
|
||||||
server.Start()
|
|
||||||
|
|
||||||
n := &NGINXController{
|
|
||||||
cfg: &Configuration{
|
|
||||||
ListenPorts: &ngx_config.ListenPorts{},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("no pid or process", func(t *testing.T) {
|
for _, tt := range tests {
|
||||||
if err := callHealthz(true, mux); err == nil {
|
testName := fmt.Sprintf("health path: %s", tt.healthzPath)
|
||||||
t.Error("expected an error but none returned")
|
t.Run(testName, func(t *testing.T) {
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// create pid file
|
mux := http.NewServeMux()
|
||||||
os.MkdirAll("/tmp", file.ReadWriteByUser)
|
|
||||||
pidFile, err := os.Create(nginx.PID)
|
listener, err := net.Listen("tcp", fmt.Sprintf(":%v", nginx.StatusPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("crating tcp listener: %s", err)
|
||||||
|
}
|
||||||
|
defer listener.Close()
|
||||||
|
|
||||||
|
server := &httptest.Server{
|
||||||
|
Listener: listener,
|
||||||
|
Config: &http.Server{
|
||||||
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
fmt.Fprintf(w, "ok")
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
defer server.Close()
|
||||||
|
server.Start()
|
||||||
|
|
||||||
|
n := &NGINXController{
|
||||||
|
cfg: &Configuration{
|
||||||
|
ListenPorts: &ngx_config.ListenPorts{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("no pid or process", func(t *testing.T) {
|
||||||
|
if err := callHealthz(true, tt.healthzPath, mux); err == nil {
|
||||||
|
t.Error("expected an error but none returned")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// create pid file
|
||||||
|
os.MkdirAll("/tmp", file.ReadWriteByUser)
|
||||||
|
pidFile, err := os.Create(nginx.PID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("no process", func(t *testing.T) {
|
||||||
|
if err := callHealthz(true, tt.healthzPath, mux); err == nil {
|
||||||
|
t.Error("expected an error but none returned")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// start dummy process to use the PID
|
||||||
|
cmd := exec.Command("sleep", "3600")
|
||||||
|
cmd.Start()
|
||||||
|
pid := cmd.Process.Pid
|
||||||
|
defer cmd.Process.Kill()
|
||||||
|
go func() {
|
||||||
|
cmd.Wait()
|
||||||
|
}()
|
||||||
|
|
||||||
|
pidFile.Write([]byte(fmt.Sprintf("%v", pid)))
|
||||||
|
pidFile.Close()
|
||||||
|
|
||||||
|
healthz.InstallPathHandler(mux, tt.healthzPath, n)
|
||||||
|
|
||||||
|
t.Run("valid request", func(t *testing.T) {
|
||||||
|
if err := callHealthz(false, tt.healthzPath, mux); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// pollute pid file
|
||||||
|
pidFile.Write([]byte(fmt.Sprint("999999")))
|
||||||
|
pidFile.Close()
|
||||||
|
|
||||||
|
t.Run("bad pid", func(t *testing.T) {
|
||||||
|
if err := callHealthz(true, tt.healthzPath, mux); err == nil {
|
||||||
|
t.Error("expected an error but none returned")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("no process", func(t *testing.T) {
|
|
||||||
if err := callHealthz(true, mux); err == nil {
|
|
||||||
t.Error("expected an error but none returned")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// start dummy process to use the PID
|
|
||||||
cmd := exec.Command("sleep", "3600")
|
|
||||||
cmd.Start()
|
|
||||||
pid := cmd.Process.Pid
|
|
||||||
defer cmd.Process.Kill()
|
|
||||||
go func() {
|
|
||||||
cmd.Wait()
|
|
||||||
}()
|
|
||||||
|
|
||||||
pidFile.Write([]byte(fmt.Sprintf("%v", pid)))
|
|
||||||
pidFile.Close()
|
|
||||||
|
|
||||||
healthz.InstallHandler(mux, n)
|
|
||||||
|
|
||||||
t.Run("valid request", func(t *testing.T) {
|
|
||||||
if err := callHealthz(false, mux); err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// pollute pid file
|
|
||||||
pidFile.Write([]byte(fmt.Sprint("999999")))
|
|
||||||
pidFile.Close()
|
|
||||||
|
|
||||||
t.Run("bad pid", func(t *testing.T) {
|
|
||||||
if err := callHealthz(true, mux); err == nil {
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue