Skip to content

Commit

Permalink
Add /_/health endpoint to of-watchdog
Browse files Browse the repository at this point in the history
Introduce new endpoint `/_/health` to watchdog for health status of
functions which checks for `/tmp/.lock` file

Added tests for healthHandler

Issues: openfaas/faas#547

Signed-off-by: Vivek Singh <vivekkmr45@yahoo.in>
  • Loading branch information
viveksyngh authored and alexellis committed Sep 17, 2018
1 parent c96acf7 commit 10751f5
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
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 @@ -227,3 +228,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) {
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
}

0 comments on commit 10751f5

Please sign in to comment.