Skip to content

Commit

Permalink
change fake fleet-server requestID handling, track response bytes, al…
Browse files Browse the repository at this point in the history
…ways dump procs
  • Loading branch information
michel-laterman committed Sep 5, 2024
1 parent bfac72b commit 725cd15
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
4 changes: 1 addition & 3 deletions pkg/testing/fixture_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ func (f *Fixture) installNoPkgManager(ctx context.Context, installOpts *InstallO
f.setClient(c)

f.t.Cleanup(func() {
if f.t.Failed() {
f.DumpProcesses("-cleanup")
}
f.DumpProcesses("-pre-uninstall")
})

f.t.Cleanup(func() {
Expand Down
17 changes: 13 additions & 4 deletions testing/fleetservertest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"strconv"
"sync"
"sync/atomic"

"github.com/gofrs/uuid/v5"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -121,13 +122,18 @@ func NewRouter(handlers *Handlers) *mux.Router {

ww := &statusResponseWriter{w: w}

requestID := uuid.Must(uuid.NewV4()).String()
requestID := r.Header.Get("X-Request-Id")
if requestID == "" {
requestID = uuid.Must(uuid.NewV4()).String()
}
ww.Header().Set("X-Request-Id", requestID)

handlers.logFn("[%s] STARTING - %s %s %s %s\n",
requestID, r.Method, r.URL, r.Proto, r.RemoteAddr)
route.Handler.
ServeHTTP(ww, r)
handlers.logFn("[%s] DONE %d - %s %s %s %s\n",
requestID, ww.statusCode, r.Method, r.URL, r.Proto, r.RemoteAddr)
handlers.logFn("[%s] DONE %d - %s %s %s %s %d\n",
requestID, ww.statusCode, r.Method, r.URL, r.Proto, r.RemoteAddr, atomic.LoadUint64(&ww.byteCount))
}))
}

Expand Down Expand Up @@ -499,14 +505,17 @@ func updateLocalMetaAgentID(data []byte, agentID string) ([]byte, error) {
type statusResponseWriter struct {
w http.ResponseWriter
statusCode int
byteCount uint64
}

func (s *statusResponseWriter) Header() http.Header {
return s.w.Header()
}

func (s *statusResponseWriter) Write(bs []byte) (int, error) {
return s.w.Write(bs)
n, err := s.w.Write(bs)
atomic.AddUint64(&s.byteCount, uint64(n))
return n, err
}

func (s *statusResponseWriter) WriteHeader(statusCode int) {
Expand Down

0 comments on commit 725cd15

Please sign in to comment.