server: Apply review comments and split basic handler

This commit is contained in:
Marek Siarkowicz 2022-02-25 14:54:57 +01:00
parent fb361e43f0
commit 26f42e7a9e
4 changed files with 11 additions and 11 deletions

View File

@ -701,7 +701,8 @@ func (e *Etcd) serveClients() (err error) {
// Start a client server goroutine for each listen address
mux := http.NewServeMux()
etcdhttp.HandleBasic(e.cfg.logger, mux, e.Server)
etcdhttp.HandleDebug(mux)
etcdhttp.HandleVersion(mux, e.Server)
etcdhttp.HandleMetrics(mux)
etcdhttp.HandleHealth(e.cfg.logger, mux, e.Server)

View File

@ -115,18 +115,21 @@ func TestHealthHandler(t *testing.T) {
expectHealth: "true",
},
{
name: "Healthy even if authentication failed",
healthCheckURL: "/health",
apiError: auth.ErrUserEmpty,
expectStatusCode: http.StatusOK,
expectHealth: "true",
},
{
name: "Healthy even if authorization failed",
healthCheckURL: "/health",
apiError: auth.ErrPermissionDenied,
expectStatusCode: http.StatusOK,
expectHealth: "true",
},
{
name: "Unhealthy if api is not available",
healthCheckURL: "/health",
apiError: fmt.Errorf("Unexpected error"),
expectStatusCode: http.StatusServiceUnavailable,
@ -134,7 +137,7 @@ func TestHealthHandler(t *testing.T) {
},
}
for i, tt := range tests {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mux := http.NewServeMux()
HandleHealth(zaptest.NewLogger(t), mux, &fakeHealthServer{
@ -147,14 +150,14 @@ func TestHealthHandler(t *testing.T) {
res, err := ts.Client().Do(&http.Request{Method: http.MethodGet, URL: testutil.MustNewURL(t, ts.URL+tt.healthCheckURL)})
if err != nil {
t.Errorf("fail serve http request %s %v in test case #%d", tt.healthCheckURL, err, i+1)
t.Errorf("fail serve http request %s %v", tt.healthCheckURL, err)
}
if res == nil {
t.Errorf("got nil http response with http request %s in test case #%d", tt.healthCheckURL, i+1)
t.Errorf("got nil http response with http request %s", tt.healthCheckURL)
return
}
if res.StatusCode != tt.expectStatusCode {
t.Errorf("want statusCode %d but got %d in test case #%d", tt.expectStatusCode, res.StatusCode, i+1)
t.Errorf("want statusCode %d but got %d", tt.expectStatusCode, res.StatusCode)
}
health, err := parseHealthOutput(res.Body)
if err != nil {

View File

@ -23,11 +23,6 @@ import (
"go.uber.org/zap"
)
func HandleBasic(lg *zap.Logger, mux *http.ServeMux, peer etcdserver.ServerPeer) {
HandleDebug(mux)
HandleVersion(mux, peer)
}
func allowMethod(w http.ResponseWriter, r *http.Request, m string) bool {
if m == r.Method {
return true

View File

@ -992,7 +992,8 @@ func (m *Member) Launch() error {
}
for _, ln := range m.ClientListeners {
handler := http.NewServeMux()
etcdhttp.HandleBasic(m.Logger, handler, m.Server)
etcdhttp.HandleDebug(handler)
etcdhttp.HandleVersion(handler, m.Server)
etcdhttp.HandleMetrics(handler)
etcdhttp.HandleHealth(m.Logger, handler, m.Server)
hs := &httptest.Server{