Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 4244 #4619

Merged
merged 1 commit into from
Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cmd/nginx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func main() {
go registerProfiler()
}

registerHealthz(ngx, mux)
registerHealthz(nginx.HealthPath, ngx, mux)
registerMetrics(reg, mux)
registerHandlers(mux)

Expand Down Expand Up @@ -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)
healthz.InstallHandler(mux,
healthz.InstallPathHandler(mux,
healthPath,
healthz.PingHealthz,
ic,
)
Expand Down
159 changes: 86 additions & 73 deletions internal/ingress/controller/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,84 +33,97 @@ import (
)

func TestNginxCheck(t *testing.T) {
mux := http.NewServeMux()

listener, err := net.Listen("tcp", fmt.Sprintf(":%v", nginx.StatusPort))
if err != nil {
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{},
},
var tests = []struct {
healthzPath string
}{
{"/healthz"},
{"/not-healthz"},
}

t.Run("no pid or process", func(t *testing.T) {
if err := callHealthz(true, 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)
for _, tt := range tests {
testName := fmt.Sprintf("health path: %s", tt.healthzPath)
t.Run(testName, func(t *testing.T) {

mux := http.NewServeMux()

listener, err := net.Listen("tcp", fmt.Sprintf(":%v", nginx.StatusPort))
if err != nil {
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 {
req, err := http.NewRequest("GET", "/healthz", nil)
func callHealthz(expErr bool, healthzPath string, mux *http.ServeMux) error {
req, err := http.NewRequest("GET", healthzPath, nil)
if err != nil {
return fmt.Errorf("healthz error: %v", err)
}
Expand Down