From e1562d05d6999136ef6c670b27b958ac0f90677f Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Thu, 6 May 2021 14:21:20 +0200 Subject: [PATCH 1/2] fixed --- x-pack/elastic-agent/pkg/agent/cmd/run.go | 9 +++++++++ .../elastic-agent/pkg/core/monitoring/server/handler.go | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/x-pack/elastic-agent/pkg/agent/cmd/run.go b/x-pack/elastic-agent/pkg/agent/cmd/run.go index e49d60b59370..5fa7ad112216 100644 --- a/x-pack/elastic-agent/pkg/agent/cmd/run.go +++ b/x-pack/elastic-agent/pkg/agent/cmd/run.go @@ -215,6 +215,15 @@ func reexecPath() (string, error) { } func getOverwrites(rawConfig *config.Config) error { + cfg, err := configuration.NewFromConfig(rawConfig) + if err != nil { + return err + } + + if !cfg.Fleet.Enabled { + // overrides should apply only for fleet mode + return nil + } path := paths.AgentConfigFile() store := storage.NewDiskStore(path) diff --git a/x-pack/elastic-agent/pkg/core/monitoring/server/handler.go b/x-pack/elastic-agent/pkg/core/monitoring/server/handler.go index dfb50d7e0246..482ce82b819a 100644 --- a/x-pack/elastic-agent/pkg/core/monitoring/server/handler.go +++ b/x-pack/elastic-agent/pkg/core/monitoring/server/handler.go @@ -27,8 +27,6 @@ type apiHandler struct { func (h *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { err := h.innerFn(w, r) if err != nil { - writeResponse(w, unexpectedErrorWithReason(err.Error())) - switch e := err.(type) { case apiError: w.WriteHeader(e.Status()) @@ -36,6 +34,8 @@ func (h *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) } + + writeResponse(w, unexpectedErrorWithReason(err.Error())) } } From 018e4874fb0a446d521090489e8916852564571a Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Thu, 6 May 2021 16:21:52 +0200 Subject: [PATCH 2/2] unit test --- .../core/monitoring/server/process_test.go | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/x-pack/elastic-agent/pkg/core/monitoring/server/process_test.go b/x-pack/elastic-agent/pkg/core/monitoring/server/process_test.go index e518322749b8..029516ca9e93 100644 --- a/x-pack/elastic-agent/pkg/core/monitoring/server/process_test.go +++ b/x-pack/elastic-agent/pkg/core/monitoring/server/process_test.go @@ -4,6 +4,7 @@ package server import ( + "errors" "net/http" "testing" @@ -54,3 +55,35 @@ func TestParseID(t *testing.T) { }) } } + +func TestStatusErr(t *testing.T) { + cases := map[string]struct { + Error error + ExpectedStatusCode int + }{ + "no error": {nil, 0}, + "normal error": {errors.New("something bad happened"), http.StatusInternalServerError}, + "status bound err - not found": {errorWithStatus(http.StatusNotFound, errors.New("something was not found")), http.StatusNotFound}, + "status bound err - internal": {errorWithStatus(http.StatusInternalServerError, errors.New("something was not found")), http.StatusInternalServerError}, + "status bound err - bad request": {errorWithStatus(http.StatusBadRequest, errors.New("something really bad happened")), http.StatusBadRequest}, + } + + dummyHandler := func(err error) func(w http.ResponseWriter, r *http.Request) error { + return func(w http.ResponseWriter, r *http.Request) error { + return err + } + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + h := createHandler(dummyHandler(tc.Error)) + tw := &testWriter{} + r, err := http.NewRequest("GET", "", nil) + require.NoError(t, err) + + h.ServeHTTP(tw, r) + + require.Equal(t, tc.ExpectedStatusCode, tw.statusCode) + }) + } +}