From fa1b14e146d01577824a30af4889d13ea82ab906 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Fri, 15 Oct 2021 12:15:13 +0200 Subject: [PATCH] cmd/prometheus: randomize test port and isolate test data directory Between the tests. This enables parallelizing those tests, which should cut the test execution time. Signed-off-by: Mateusz Gozdek --- cmd/prometheus/main_test.go | 8 ++++---- cmd/prometheus/main_unix_test.go | 2 +- cmd/prometheus/query_log_test.go | 5 +++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cmd/prometheus/main_test.go b/cmd/prometheus/main_test.go index c3ada32b6..c3915c7ca 100644 --- a/cmd/prometheus/main_test.go +++ b/cmd/prometheus/main_test.go @@ -41,7 +41,6 @@ var ( promPath = os.Args[0] promConfig = filepath.Join("..", "..", "documentation", "examples", "prometheus.yml") agentConfig = filepath.Join("..", "..", "documentation", "examples", "prometheus-agent.yml") - promData = filepath.Join(os.TempDir(), "data") ) func TestMain(m *testing.M) { @@ -57,7 +56,6 @@ func TestMain(m *testing.M) { os.Setenv("no_proxy", "localhost,127.0.0.1,0.0.0.0,:") exitCode := m.Run() - os.RemoveAll(promData) os.Exit(exitCode) } @@ -207,7 +205,7 @@ func TestWALSegmentSizeBounds(t *testing.T) { } for size, expectedExitStatus := range map[string]int{"9MB": 1, "257MB": 1, "10": 2, "1GB": 1, "12MB": 0} { - prom := exec.Command(promPath, "-test.main", "--storage.tsdb.wal-segment-size="+size, "--web.listen-address=0.0.0.0:0", "--config.file="+promConfig) + prom := exec.Command(promPath, "-test.main", "--storage.tsdb.wal-segment-size="+size, "--web.listen-address=0.0.0.0:0", "--config.file="+promConfig, "--storage.tsdb.path="+filepath.Join(t.TempDir(), "data")) // Log stderr in case of failure. stderr, err := prom.StderrPipe() @@ -245,12 +243,14 @@ func TestWALSegmentSizeBounds(t *testing.T) { } func TestMaxBlockChunkSegmentSizeBounds(t *testing.T) { + t.Parallel() + if testing.Short() { t.Skip("skipping test in short mode.") } for size, expectedExitStatus := range map[string]int{"512KB": 1, "1MB": 0} { - prom := exec.Command(promPath, "-test.main", "--storage.tsdb.max-block-chunk-segment-size="+size, "--web.listen-address=0.0.0.0:0", "--config.file="+promConfig) + prom := exec.Command(promPath, "-test.main", "--storage.tsdb.max-block-chunk-segment-size="+size, "--web.listen-address=0.0.0.0:0", "--config.file="+promConfig, "--storage.tsdb.path="+filepath.Join(t.TempDir(), "data")) // Log stderr in case of failure. stderr, err := prom.StderrPipe() diff --git a/cmd/prometheus/main_unix_test.go b/cmd/prometheus/main_unix_test.go index 7724f0dd1..b76ecbcb1 100644 --- a/cmd/prometheus/main_unix_test.go +++ b/cmd/prometheus/main_unix_test.go @@ -31,7 +31,7 @@ func TestStartupInterrupt(t *testing.T) { t.Skip("skipping test in short mode.") } - prom := exec.Command(promPath, "-test.main", "--config.file="+promConfig, "--storage.tsdb.path="+promData) + prom := exec.Command(promPath, "-test.main", "--config.file="+promConfig, "--storage.tsdb.path="+t.TempDir()) err := prom.Start() if err != nil { t.Errorf("execution error: %v", err) diff --git a/cmd/prometheus/query_log_test.go b/cmd/prometheus/query_log_test.go index 4e75ecae9..1b2bd4f97 100644 --- a/cmd/prometheus/query_log_test.go +++ b/cmd/prometheus/query_log_test.go @@ -31,6 +31,8 @@ import ( "time" "github.com/stretchr/testify/require" + + "github.com/prometheus/prometheus/util/testutil" ) type origin int @@ -412,7 +414,6 @@ func TestQueryLog(t *testing.T) { cwd, err := os.Getwd() require.NoError(t, err) - port := 15000 for _, host := range []string{"127.0.0.1", "[::1]"} { for _, prefix := range []string{"", "/foobar"} { for _, enabledAtStart := range []bool{true, false} { @@ -422,7 +423,7 @@ func TestQueryLog(t *testing.T) { host: host, enabledAtStart: enabledAtStart, prefix: prefix, - port: port, + port: testutil.RandomUnprivilegedPort(t), cwd: cwd, }