Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick #25584 to 7.13: Fixed status reporting #25683

Merged
merged 2 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}
}