Skip to content

Commit

Permalink
server: Apply review comments and split basic handler
Browse files Browse the repository at this point in the history
  • Loading branch information
serathius committed Mar 14, 2022
1 parent c38cfa9 commit ed13c11
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
3 changes: 2 additions & 1 deletion server/embed/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 7 additions & 5 deletions server/etcdserver/api/etcdhttp/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,28 @@ func TestHealthHandler(t *testing.T) {
expectHealth: "true",
},
{
name: "Healthy even if authentication failed",
healthCheckURL: "/health",
apiError: auth.ErrUserEmpty,
expectStatusCode: http.StatusOK,
expectHealth: "true",
},
{
healthCheckURL: "/health",
name: "Healthy even if authorization failed",
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,
expectHealth: "false",
},
}

for i, tt := range tests {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mux := http.NewServeMux()
HandleHealth(zap.NewExample(), mux, &fakeHealthServer{
Expand All @@ -147,14 +149,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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/framework/integration/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,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{
Expand Down

0 comments on commit ed13c11

Please sign in to comment.