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

Add /_/health endpoint to of-watchdog #13

Merged
merged 1 commit into from
Sep 17, 2018
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
27 changes: 27 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func main() {
}

http.HandleFunc("/", requestHandler)
http.HandleFunc("/_/health", makeHealthHandler())
log.Fatal(s.ListenAndServe())
}

Expand Down Expand Up @@ -223,3 +224,29 @@ func makeHTTPRequestHandler(watchdogConfig config.WatchdogConfig) func(http.Resp

}
}

func lockFilePresent() bool {
path := filepath.Join(os.TempDir(), ".lock")
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}

func makeHealthHandler() func(w http.ResponseWriter, r *http.Request) {
Copy link
Contributor

@johnmccabe johnmccabe Apr 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this not be the same as the old watchdog implementation? The acceptingConnections check is missing here and from the lock() function. Why is that no longer needed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The acceptingConnections check is part of the graceful shutdown that's currently only in the original watchdog. There's an issue to port it over here: #10

I'm starting to work on that. Hoping to have a PR tonight (US time)

return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
if lockFilePresent() == false {
w.WriteHeader(http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
break
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
}
83 changes: 83 additions & 0 deletions requesthandler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)

func TestHealthHandler_StatusOK_LockFilePresent(t *testing.T) {
rr := httptest.NewRecorder()

present := lockFilePresent()

if present == false {
if err := lock(); err != nil {
t.Fatal(err)
}
}

req, err := http.NewRequest(http.MethodGet, "/_/health", nil)
if err != nil {
t.Fatal(err)
}
handler := makeHealthHandler()
handler(rr, req)

required := http.StatusOK
if status := rr.Code; status != required {
t.Errorf("handler returned wrong status code - want: %v, got: %v", required, status)
}

}

func TestHealthHandler_StatusInternalServerError_LockFileNotPresent(t *testing.T) {
rr := httptest.NewRecorder()

if lockFilePresent() == true {
if err := removeLockFile(); err != nil {
t.Fatal(err)
}
}

req, err := http.NewRequest(http.MethodGet, "/_/health", nil)
if err != nil {
t.Fatal(err)
}
handler := makeHealthHandler()
handler(rr, req)

required := http.StatusInternalServerError
if status := rr.Code; status != required {
t.Errorf("handler returned wrong status code - want: %v, got: %v", required, status)
}
}

func TestHealthHandler_StatusMethodNotAllowed_ForWriteableVerbs(t *testing.T) {
rr := httptest.NewRecorder()

verbs := []string{http.MethodPost, http.MethodPut, http.MethodDelete}

for _, verb := range verbs {
req, err := http.NewRequest(verb, "/_/health", nil)
if err != nil {
t.Fatal(err)
}

handler := makeHealthHandler()
handler(rr, req)

required := http.StatusMethodNotAllowed
if status := rr.Code; status != required {
t.Errorf("handler returned wrong status code - want: %v, got: %v", required, status)
}
}
}

func removeLockFile() error {
path := filepath.Join(os.TempDir(), ".lock")
removeErr := os.Remove(path)
return removeErr
}