From b2c933e317e0afc910fa2fe3fc0a149565046cf8 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Fri, 15 Oct 2021 12:25:29 +0200 Subject: [PATCH] web: randomize used listening ports for tests So they can be parallelized to speed up tests execution. Signed-off-by: Mateusz Gozdek --- web/web_test.go | 95 ++++++++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 40 deletions(-) diff --git a/web/web_test.go b/web/web_test.go index 868fd08e6..eeda68a28 100644 --- a/web/web_test.go +++ b/web/web_test.go @@ -40,6 +40,7 @@ import ( "github.com/prometheus/prometheus/rules" "github.com/prometheus/prometheus/scrape" "github.com/prometheus/prometheus/tsdb" + "github.com/prometheus/prometheus/util/testutil" ) func TestMain(m *testing.M) { @@ -118,8 +119,10 @@ func TestReadyAndHealthy(t *testing.T) { db, err := tsdb.Open(dbDir, nil, nil, nil, nil) require.NoError(t, err) + port := fmt.Sprintf(":%d", testutil.RandomUnprivilegedPort(t)) + opts := &Options{ - ListenAddress: ":9090", + ListenAddress: port, ReadTimeout: 30 * time.Second, MaxConnections: 512, Context: nil, @@ -134,7 +137,7 @@ func TestReadyAndHealthy(t *testing.T) { EnableAdminAPI: true, ExternalURL: &url.URL{ Scheme: "http", - Host: "localhost:9090", + Host: "localhost" + port, Path: "/", }, Version: &PrometheusVersion{}, @@ -165,20 +168,22 @@ func TestReadyAndHealthy(t *testing.T) { // to be up before starting tests. time.Sleep(5 * time.Second) - resp, err := http.Get("http://localhost:9090/-/healthy") + baseURL := "http://localhost" + port + + resp, err := http.Get(baseURL + "/-/healthy") require.NoError(t, err) require.Equal(t, http.StatusOK, resp.StatusCode) cleanupTestResponse(t, resp) for _, u := range []string{ - "http://localhost:9090/-/ready", - "http://localhost:9090/classic/graph", - "http://localhost:9090/classic/flags", - "http://localhost:9090/classic/rules", - "http://localhost:9090/classic/service-discovery", - "http://localhost:9090/classic/targets", - "http://localhost:9090/classic/status", - "http://localhost:9090/classic/config", + baseURL + "/-/ready", + baseURL + "/classic/graph", + baseURL + "/classic/flags", + baseURL + "/classic/rules", + baseURL + "/classic/service-discovery", + baseURL + "/classic/targets", + baseURL + "/classic/status", + baseURL + "/classic/config", } { resp, err = http.Get(u) require.NoError(t, err) @@ -186,12 +191,12 @@ func TestReadyAndHealthy(t *testing.T) { cleanupTestResponse(t, resp) } - resp, err = http.Post("http://localhost:9090/api/v1/admin/tsdb/snapshot", "", strings.NewReader("")) + resp, err = http.Post(baseURL+"/api/v1/admin/tsdb/snapshot", "", strings.NewReader("")) require.NoError(t, err) require.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) cleanupTestResponse(t, resp) - resp, err = http.Post("http://localhost:9090/api/v1/admin/tsdb/delete_series", "", strings.NewReader("{}")) + resp, err = http.Post(baseURL+"/api/v1/admin/tsdb/delete_series", "", strings.NewReader("{}")) require.NoError(t, err) require.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) cleanupTestResponse(t, resp) @@ -200,15 +205,15 @@ func TestReadyAndHealthy(t *testing.T) { webHandler.Ready() for _, u := range []string{ - "http://localhost:9090/-/healthy", - "http://localhost:9090/-/ready", - "http://localhost:9090/classic/graph", - "http://localhost:9090/classic/flags", - "http://localhost:9090/classic/rules", - "http://localhost:9090/classic/service-discovery", - "http://localhost:9090/classic/targets", - "http://localhost:9090/classic/status", - "http://localhost:9090/classic/config", + baseURL + "/-/healthy", + baseURL + "/-/ready", + baseURL + "/classic/graph", + baseURL + "/classic/flags", + baseURL + "/classic/rules", + baseURL + "/classic/service-discovery", + baseURL + "/classic/targets", + baseURL + "/classic/status", + baseURL + "/classic/config", } { resp, err = http.Get(u) require.NoError(t, err) @@ -216,13 +221,13 @@ func TestReadyAndHealthy(t *testing.T) { cleanupTestResponse(t, resp) } - resp, err = http.Post("http://localhost:9090/api/v1/admin/tsdb/snapshot", "", strings.NewReader("")) + resp, err = http.Post(baseURL+"/api/v1/admin/tsdb/snapshot", "", strings.NewReader("")) require.NoError(t, err) require.Equal(t, http.StatusOK, resp.StatusCode) cleanupSnapshot(t, dbDir, resp) cleanupTestResponse(t, resp) - resp, err = http.Post("http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]=up", "", nil) + resp, err = http.Post(baseURL+"/api/v1/admin/tsdb/delete_series?match[]=up", "", nil) require.NoError(t, err) require.Equal(t, http.StatusNoContent, resp.StatusCode) cleanupTestResponse(t, resp) @@ -237,8 +242,10 @@ func TestRoutePrefix(t *testing.T) { db, err := tsdb.Open(dbDir, nil, nil, nil, nil) require.NoError(t, err) + port := fmt.Sprintf(":%d", testutil.RandomUnprivilegedPort(t)) + opts := &Options{ - ListenAddress: ":9091", + ListenAddress: port, ReadTimeout: 30 * time.Second, MaxConnections: 512, Context: nil, @@ -252,7 +259,7 @@ func TestRoutePrefix(t *testing.T) { RoutePrefix: "/prometheus", EnableAdminAPI: true, ExternalURL: &url.URL{ - Host: "localhost.localdomain:9090", + Host: "localhost.localdomain" + port, Scheme: "http", }, } @@ -277,22 +284,24 @@ func TestRoutePrefix(t *testing.T) { // to be up before starting tests. time.Sleep(5 * time.Second) - resp, err := http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/healthy") + baseURL := "http://localhost" + port + + resp, err := http.Get(baseURL + opts.RoutePrefix + "/-/healthy") require.NoError(t, err) require.Equal(t, http.StatusOK, resp.StatusCode) cleanupTestResponse(t, resp) - resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready") + resp, err = http.Get(baseURL + opts.RoutePrefix + "/-/ready") require.NoError(t, err) require.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) cleanupTestResponse(t, resp) - resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v1/admin/tsdb/snapshot", "", strings.NewReader("")) + resp, err = http.Post(baseURL+opts.RoutePrefix+"/api/v1/admin/tsdb/snapshot", "", strings.NewReader("")) require.NoError(t, err) require.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) cleanupTestResponse(t, resp) - resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v1/admin/tsdb/delete_series", "", strings.NewReader("{}")) + resp, err = http.Post(baseURL+opts.RoutePrefix+"/api/v1/admin/tsdb/delete_series", "", strings.NewReader("{}")) require.NoError(t, err) require.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) cleanupTestResponse(t, resp) @@ -300,23 +309,23 @@ func TestRoutePrefix(t *testing.T) { // Set to ready. webHandler.Ready() - resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/healthy") + resp, err = http.Get(baseURL + opts.RoutePrefix + "/-/healthy") require.NoError(t, err) require.Equal(t, http.StatusOK, resp.StatusCode) cleanupTestResponse(t, resp) - resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready") + resp, err = http.Get(baseURL + opts.RoutePrefix + "/-/ready") require.NoError(t, err) require.Equal(t, http.StatusOK, resp.StatusCode) cleanupTestResponse(t, resp) - resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v1/admin/tsdb/snapshot", "", strings.NewReader("")) + resp, err = http.Post(baseURL+opts.RoutePrefix+"/api/v1/admin/tsdb/snapshot", "", strings.NewReader("")) require.NoError(t, err) require.Equal(t, http.StatusOK, resp.StatusCode) cleanupSnapshot(t, dbDir, resp) cleanupTestResponse(t, resp) - resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v1/admin/tsdb/delete_series?match[]=up", "", nil) + resp, err = http.Post(baseURL+opts.RoutePrefix+"/api/v1/admin/tsdb/delete_series?match[]=up", "", nil) require.NoError(t, err) require.Equal(t, http.StatusNoContent, resp.StatusCode) cleanupTestResponse(t, resp) @@ -404,8 +413,10 @@ func TestShutdownWithStaleConnection(t *testing.T) { timeout := 10 * time.Second + port := fmt.Sprintf(":%d", testutil.RandomUnprivilegedPort(t)) + opts := &Options{ - ListenAddress: ":9090", + ListenAddress: port, ReadTimeout: timeout, MaxConnections: 512, Context: nil, @@ -419,7 +430,7 @@ func TestShutdownWithStaleConnection(t *testing.T) { RoutePrefix: "/", ExternalURL: &url.URL{ Scheme: "http", - Host: "localhost:9090", + Host: "localhost" + port, Path: "/", }, Version: &PrometheusVersion{}, @@ -454,7 +465,7 @@ func TestShutdownWithStaleConnection(t *testing.T) { // Open a socket, and don't use it. This connection should then be closed // after the ReadTimeout. - c, err := net.Dial("tcp", "localhost:9090") + c, err := net.Dial("tcp", opts.ExternalURL.Host) require.NoError(t, err) t.Cleanup(func() { require.NoError(t, c.Close()) }) @@ -469,14 +480,16 @@ func TestShutdownWithStaleConnection(t *testing.T) { } func TestHandleMultipleQuitRequests(t *testing.T) { + port := fmt.Sprintf(":%d", testutil.RandomUnprivilegedPort(t)) + opts := &Options{ - ListenAddress: ":9090", + ListenAddress: port, MaxConnections: 512, EnableLifecycle: true, RoutePrefix: "/", ExternalURL: &url.URL{ Scheme: "http", - Host: "localhost:9090", + Host: "localhost" + port, Path: "/", }, } @@ -501,6 +514,8 @@ func TestHandleMultipleQuitRequests(t *testing.T) { // to be up before starting tests. time.Sleep(5 * time.Second) + baseURL := opts.ExternalURL.Scheme + "://" + opts.ExternalURL.Host + start := make(chan struct{}) var wg sync.WaitGroup for i := 0; i < 3; i++ { @@ -508,7 +523,7 @@ func TestHandleMultipleQuitRequests(t *testing.T) { go func() { defer wg.Done() <-start - resp, err := http.Post("http://localhost:9090/-/quit", "", strings.NewReader("")) + resp, err := http.Post(baseURL+"/-/quit", "", strings.NewReader("")) require.NoError(t, err) require.Equal(t, http.StatusOK, resp.StatusCode) }()