web: randomize used port in TestAgentAPIEndPoints test

To make sure we hit the right target and allow running tests in
parallel.

Signed-off-by: Mateusz Gozdek <mgozdekof@gmail.com>
pull/9717/head
Mateusz Gozdek 3 years ago committed by Julien Pivotto
parent 83086aee00
commit 79a753db7e

@ -545,8 +545,10 @@ func TestHandleMultipleQuitRequests(t *testing.T) {
func TestAgentAPIEndPoints(t *testing.T) { func TestAgentAPIEndPoints(t *testing.T) {
t.Parallel() t.Parallel()
port := fmt.Sprintf(":%d", testutil.RandomUnprivilegedPort(t))
opts := &Options{ opts := &Options{
ListenAddress: ":9090", ListenAddress: port,
ReadTimeout: 30 * time.Second, ReadTimeout: 30 * time.Second,
MaxConnections: 512, MaxConnections: 512,
Context: nil, Context: nil,
@ -559,7 +561,7 @@ func TestAgentAPIEndPoints(t *testing.T) {
EnableAdminAPI: true, EnableAdminAPI: true,
ExternalURL: &url.URL{ ExternalURL: &url.URL{
Scheme: "http", Scheme: "http",
Host: "localhost:9090", Host: "localhost" + port,
Path: "/", Path: "/",
}, },
Version: &PrometheusVersion{}, Version: &PrometheusVersion{},
@ -572,18 +574,20 @@ func TestAgentAPIEndPoints(t *testing.T) {
webHandler := New(nil, opts) webHandler := New(nil, opts)
webHandler.Ready() webHandler.Ready()
baseURL := "http://localhost" + port
// Test for non-available endpoints in the Agent mode. // Test for non-available endpoints in the Agent mode.
for _, u := range []string{ for _, u := range []string{
"http://localhost:9090/-/labels", "/-/labels",
"http://localhost:9090/label", "/label",
"http://localhost:9090/series", "/series",
"http://localhost:9090/alertmanagers", "/alertmanagers",
"http://localhost:9090/query", "/query",
"http://localhost:9090/query_range", "/query_range",
"http://localhost:9090/query_exemplars", "/query_exemplars",
} { } {
w := httptest.NewRecorder() w := httptest.NewRecorder()
req, err := http.NewRequest("GET", u, nil) req, err := http.NewRequest("GET", baseURL+u, nil)
require.NoError(t, err) require.NoError(t, err)
webHandler.router.ServeHTTP(w, req) webHandler.router.ServeHTTP(w, req)
require.Equal(t, http.StatusNotFound, w.Code) require.Equal(t, http.StatusNotFound, w.Code)
@ -591,14 +595,13 @@ func TestAgentAPIEndPoints(t *testing.T) {
// Test for available endpoints in the Agent mode. // Test for available endpoints in the Agent mode.
for _, u := range []string{ for _, u := range []string{
"http://localhost:9090/targets", "/targets",
"http://localhost:9090/status", "/status",
} { } {
w := httptest.NewRecorder() w := httptest.NewRecorder()
req, err := http.NewRequest("GET", u, nil) req, err := http.NewRequest("GET", baseURL+u, nil)
require.NoError(t, err) require.NoError(t, err)
webHandler.router.ServeHTTP(w, req) webHandler.router.ServeHTTP(w, req)
fmt.Println(u)
require.Equal(t, http.StatusOK, w.Code) require.Equal(t, http.StatusOK, w.Code)
} }
} }

Loading…
Cancel
Save