mirror of https://github.com/prometheus/prometheus
fix TestAgentAPIEndPoints (#9882)
* fix TestAgentAPIEndPoints Signed-off-by: renzheng.wang <wangrzneu@gmail.com> * fix TestAgentAPIEndPoints Signed-off-by: renzheng.wang <wangrzneu@gmail.com>pull/10047/head
parent
c92673fb14
commit
3462b79f4b
|
@ -567,39 +567,68 @@ func TestAgentAPIEndPoints(t *testing.T) {
|
||||||
|
|
||||||
webHandler := New(nil, opts)
|
webHandler := New(nil, opts)
|
||||||
webHandler.Ready()
|
webHandler.Ready()
|
||||||
|
webHandler.config = &config.Config{}
|
||||||
baseURL := "http://localhost" + port
|
webHandler.notifier = ¬ifier.Manager{}
|
||||||
|
l, err := webHandler.Listener()
|
||||||
// Test for non-available endpoints in the Agent mode.
|
if err != nil {
|
||||||
for _, u := range []string{
|
panic(fmt.Sprintf("Unable to start web listener: %s", err))
|
||||||
"/-/labels",
|
|
||||||
"/label",
|
|
||||||
"/series",
|
|
||||||
"/alertmanagers",
|
|
||||||
"/query",
|
|
||||||
"/query_range",
|
|
||||||
"/query_exemplars",
|
|
||||||
"/graph",
|
|
||||||
"/rules",
|
|
||||||
} {
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
req, err := http.NewRequest("GET", baseURL+u, nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
webHandler.router.ServeHTTP(w, req)
|
|
||||||
require.Equal(t, http.StatusNotFound, w.Code)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test for available endpoints in the Agent mode.
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
for _, u := range []string{
|
defer cancel()
|
||||||
"/agent",
|
go func() {
|
||||||
"/targets",
|
err := webHandler.Run(ctx, l, "")
|
||||||
"/status",
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("Can't start web handler:%s", err))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Give some time for the web goroutine to run since we need the server
|
||||||
|
// to be up before starting tests.
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
baseURL := "http://localhost" + port + "/api/v1"
|
||||||
|
|
||||||
|
// Test for non-available endpoints in the Agent mode.
|
||||||
|
for path, methods := range map[string][]string{
|
||||||
|
"/labels": {http.MethodGet, http.MethodPost},
|
||||||
|
"/label/:name/values": {http.MethodGet},
|
||||||
|
"/series": {http.MethodGet, http.MethodPost, http.MethodDelete},
|
||||||
|
"/alertmanagers": {http.MethodGet},
|
||||||
|
"/query": {http.MethodGet, http.MethodPost},
|
||||||
|
"/query_range": {http.MethodGet, http.MethodPost},
|
||||||
|
"/query_exemplars": {http.MethodGet, http.MethodPost},
|
||||||
|
"/status/tsdb": {http.MethodGet},
|
||||||
|
"/alerts": {http.MethodGet},
|
||||||
|
"/rules": {http.MethodGet},
|
||||||
|
"/admin/tsdb/delete_series": {http.MethodPost, http.MethodPut},
|
||||||
|
"/admin/tsdb/clean_tombstones": {http.MethodPost, http.MethodPut},
|
||||||
|
"/admin/tsdb/snapshot": {http.MethodPost, http.MethodPut},
|
||||||
} {
|
} {
|
||||||
w := httptest.NewRecorder()
|
for _, m := range methods {
|
||||||
req, err := http.NewRequest("GET", baseURL+u, nil)
|
req, err := http.NewRequest(m, baseURL+path, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
webHandler.router.ServeHTTP(w, req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
require.Equal(t, http.StatusOK, w.Code)
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test for some of available endpoints in the Agent mode.
|
||||||
|
for path, methods := range map[string][]string{
|
||||||
|
"/targets": {http.MethodGet},
|
||||||
|
"/targets/metadata": {http.MethodGet},
|
||||||
|
"/metadata": {http.MethodGet},
|
||||||
|
"/status/config": {http.MethodGet},
|
||||||
|
"/status/runtimeinfo": {http.MethodGet},
|
||||||
|
"/status/flags": {http.MethodGet},
|
||||||
|
} {
|
||||||
|
for _, m := range methods {
|
||||||
|
req, err := http.NewRequest(m, baseURL+path, nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue