-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add /_/health endpoint to of-watchdog
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
1 parent
c96acf7
commit 10751f5
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |