mirror of https://github.com/hashicorp/consul
Browse Source
AutopilotServerHealthy now handles the 429 status code Previously we would error out and not parse the response. Now either a 200 or 429 status code are considered expected statuses and will result in the method returning the reply allowing API consumers to not only see if the system is healthy or not but which server is unhealthy.pull/9107/head
Matt Keeler
4 years ago
committed by
GitHub
5 changed files with 159 additions and 1 deletions
@ -0,0 +1,3 @@
|
||||
```release-note:improvement |
||||
api: `AutopilotServerHelath` now handles the 429 status code returned by the v1/operator/autopilot/health endpoint and still returned the parsed reply which will indicate server healthiness |
||||
``` |
@ -0,0 +1,82 @@
|
||||
package api |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"io" |
||||
"io/ioutil" |
||||
"net/http" |
||||
"net/http/httptest" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/mock" |
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
type mockAPI struct { |
||||
ts *httptest.Server |
||||
t *testing.T |
||||
mock.Mock |
||||
} |
||||
|
||||
func setupMockAPI(t *testing.T) (*mockAPI, *Client) { |
||||
mapi := mockAPI{t: t} |
||||
mapi.Test(t) |
||||
mapi.ts = httptest.NewServer(&mapi) |
||||
t.Cleanup(func() { |
||||
mapi.ts.Close() |
||||
mapi.Mock.AssertExpectations(t) |
||||
}) |
||||
|
||||
cfg := DefaultConfig() |
||||
cfg.Address = mapi.ts.URL |
||||
|
||||
client, err := NewClient(cfg) |
||||
require.NoError(t, err) |
||||
return &mapi, client |
||||
} |
||||
|
||||
func (m *mockAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
||||
var body interface{} |
||||
|
||||
if r.Body != nil { |
||||
bodyBytes, err := ioutil.ReadAll(r.Body) |
||||
if err == nil && len(bodyBytes) > 0 { |
||||
body = bodyBytes |
||||
|
||||
var bodyMap map[string]interface{} |
||||
if err := json.Unmarshal(bodyBytes, &bodyMap); err != nil { |
||||
body = bodyMap |
||||
} |
||||
} |
||||
} |
||||
|
||||
ret := m.Called(r.Method, r.URL.Path, body) |
||||
|
||||
if replyFn, ok := ret.Get(0).(func(http.ResponseWriter, *http.Request)); ok { |
||||
replyFn(w, r) |
||||
return |
||||
} |
||||
} |
||||
|
||||
func (m *mockAPI) static(method string, path string, body interface{}) *mock.Call { |
||||
return m.On("ServeHTTP", method, path, body) |
||||
} |
||||
|
||||
func (m *mockAPI) withReply(method, path string, body interface{}, status int, reply interface{}) *mock.Call { |
||||
return m.static(method, path, body).Return(func(w http.ResponseWriter, r *http.Request) { |
||||
w.WriteHeader(status) |
||||
|
||||
if reply == nil { |
||||
return |
||||
} |
||||
|
||||
rdr, ok := reply.(io.Reader) |
||||
if ok { |
||||
io.Copy(w, rdr) |
||||
return |
||||
} |
||||
|
||||
enc := json.NewEncoder(w) |
||||
require.NoError(m.t, enc.Encode(reply)) |
||||
}) |
||||
} |
Loading…
Reference in new issue