This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add deeper healthcheck for agent (#268)
Currently the agent health endpoint just checks that it can return some metadata from AWS. I would also like to be able to check that the agent is able to communticate succsessfully with the server. The motivation is that we are renewing certificates externally and when the client cert expires the agent will fail to talk to the server. In this case I want the healthcheck to fail so that kubernetes will restart the agent and cause it to re-read (the now renewed certs) from disk. I've implemented this as a URL query param so that doing /health will continue to function the same but /health?deep=anything will also do a grpc call to the server and assert the health endpoint there returns "ok"
- Loading branch information
1 parent
6057c19
commit 3ea5e8e
Showing
4 changed files
with
139 additions
and
5 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,91 @@ | ||
package metadata | ||
|
||
import ( | ||
"github.com/fortytw2/leaktest" | ||
"github.com/gorilla/mux" | ||
st "github.com/uswitch/kiam/pkg/testutil/server" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestHealthReturn(t *testing.T) { | ||
defer leaktest.Check(t)() | ||
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
res.WriteHeader(http.StatusOK) | ||
res.Write([]byte("i-12345")) | ||
})) | ||
defer func() { testServer.Close() }() | ||
|
||
r, err := http.NewRequest("GET", "/health", nil) | ||
if err != nil { | ||
t.Error("Error creating http request") | ||
} | ||
rr := httptest.NewRecorder() | ||
handler := newHealthHandler(st.NewStubClient(), testServer.URL) | ||
router := mux.NewRouter() | ||
handler.Install(router) | ||
router.ServeHTTP(rr, r) | ||
if rr.Code != http.StatusOK { | ||
t.Error("expected 200 response, was", rr.Code) | ||
} | ||
body, err := ioutil.ReadAll(rr.Body) | ||
if err != nil { | ||
t.Error("error reading body of metadata response") | ||
} | ||
if string(body) != "i-12345" { | ||
t.Error("instance-id not returned correctly") | ||
} | ||
} | ||
|
||
func TestDeepHealthBadReturn(t *testing.T) { | ||
defer leaktest.Check(t)() | ||
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
res.WriteHeader(http.StatusOK) | ||
res.Write([]byte("i-12345")) | ||
})) | ||
defer func() { testServer.Close() }() | ||
|
||
r, err := http.NewRequest("GET", "/health?deep=true", nil) | ||
if err != nil { | ||
t.Error("Error creating http request") | ||
} | ||
rr := httptest.NewRecorder() | ||
handler := newHealthHandler(st.NewStubClient().WithHealth("bad"), testServer.URL) | ||
router := mux.NewRouter() | ||
handler.Install(router) | ||
router.ServeHTTP(rr, r) | ||
if rr.Code != http.StatusInternalServerError { | ||
t.Error("expected 500 response, was", rr.Code) | ||
} | ||
} | ||
|
||
func TestDeepHealthReturn(t *testing.T) { | ||
defer leaktest.Check(t)() | ||
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
res.WriteHeader(http.StatusOK) | ||
res.Write([]byte("i-12345")) | ||
})) | ||
defer func() { testServer.Close() }() | ||
|
||
r, err := http.NewRequest("GET", "/health?deep=true", nil) | ||
if err != nil { | ||
t.Error("Error creating http request") | ||
} | ||
rr := httptest.NewRecorder() | ||
handler := newHealthHandler(st.NewStubClient().WithHealth("ok"), testServer.URL) | ||
router := mux.NewRouter() | ||
handler.Install(router) | ||
router.ServeHTTP(rr, r) | ||
if rr.Code != http.StatusOK { | ||
t.Error("expected 200 response, was", rr.Code) | ||
} | ||
body, err := ioutil.ReadAll(rr.Body) | ||
if err != nil { | ||
t.Error("error reading body of metadata response") | ||
} | ||
if string(body) != "i-12345" { | ||
t.Error("instance-id not returned correctly") | ||
} | ||
} |
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