Skip to content

Commit

Permalink
add logging to flakey test case (#5316) (#5698)
Browse files Browse the repository at this point in the history
* wip: add logging to flakey test case

* log fleet-server port and proxy requests for all tests

* change fake fleet-server requestID handling, track response bytes, always dump procs

* Apply suggestions from code review

Co-authored-by: Paolo Chilà <paolo.chila@elastic.co>
Co-authored-by: Andrzej Stencel <andrzej.stencel@elastic.co>

* change to atomic.Uint64

---------

Co-authored-by: Paolo Chilà <paolo.chila@elastic.co>
Co-authored-by: Andrzej Stencel <andrzej.stencel@elastic.co>
(cherry picked from commit 99c7e1e)

Co-authored-by: Michel Laterman <82832767+michel-laterman@users.noreply.github.com>
  • Loading branch information
mergify[bot] and michel-laterman authored Oct 4, 2024
1 parent 757cb82 commit 53e6e19
Show file tree
Hide file tree
Showing 3 changed files with 18 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("-post-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, ww.byteCount.Load())
}))
}

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 atomic.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)
s.byteCount.Add(uint64(n))
return n, err
}

func (s *statusResponseWriter) WriteHeader(statusCode int) {
Expand Down
4 changes: 4 additions & 0 deletions testing/integration/proxy_url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ func TestProxyURL(t *testing.T) {

// Specific testcase setup and map of created proxies
proxies, args := tt.setup(t, mockFleet)
t.Logf("Fleet-server port: %s", mockFleet.fleetServer.Port)

fixture, err := define.NewFixtureFromLocalBuild(t,
define.Version(),
Expand Down Expand Up @@ -732,6 +733,9 @@ func TestProxyURL(t *testing.T) {
Key: args.key,
}})
t.Logf("elastic-agent install output: \n%s\n", string(out))
for proxyName, proxy := range proxies {
t.Logf("Proxy %s requests: %v", proxyName, proxy.ProxiedRequests())
}
if tt.wantErr(t, err, "elastic-agent install returned an unexpected error") {
tt.assertFunc(ctx, t, fixture, proxies, mockFleet)
}
Expand Down

0 comments on commit 53e6e19

Please sign in to comment.