Skip to content

Commit

Permalink
[Elastic Agent] Fixed status reporting (#25584) (#25683)
Browse files Browse the repository at this point in the history
[Elastic Agent] Fixed status reporting (#25584)
  • Loading branch information
michalpristas authored May 14, 2021
1 parent 6c84fc7 commit 9e4b8ba
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
9 changes: 9 additions & 0 deletions x-pack/elastic-agent/pkg/agent/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions x-pack/elastic-agent/pkg/core/monitoring/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ 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())
default:
w.WriteHeader(http.StatusInternalServerError)

}

writeResponse(w, unexpectedErrorWithReason(err.Error()))
}
}

Expand Down
33 changes: 33 additions & 0 deletions x-pack/elastic-agent/pkg/core/monitoring/server/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package server

import (
"errors"
"net/http"
"testing"

Expand Down Expand Up @@ -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)
})
}
}

0 comments on commit 9e4b8ba

Please sign in to comment.