cmd/prometheus: use random listen port in TestStartupInterrupt test

So it can be run in parallel safely.

Signed-off-by: Mateusz Gozdek <mgozdekof@gmail.com>
pull/9717/head
Mateusz Gozdek 2021-11-10 11:33:46 +01:00 committed by Julien Pivotto
parent 7bd7573891
commit c08bb86be0
1 changed files with 9 additions and 2 deletions

View File

@ -17,11 +17,14 @@
package main package main
import ( import (
"fmt"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"testing" "testing"
"time" "time"
"github.com/prometheus/prometheus/util/testutil"
) )
// As soon as prometheus starts responding to http request it should be able to // As soon as prometheus starts responding to http request it should be able to
@ -31,7 +34,9 @@ func TestStartupInterrupt(t *testing.T) {
t.Skip("skipping test in short mode.") t.Skip("skipping test in short mode.")
} }
prom := exec.Command(promPath, "-test.main", "--config.file="+promConfig, "--storage.tsdb.path="+t.TempDir()) port := fmt.Sprintf(":%d", testutil.RandomUnprivilegedPort(t))
prom := exec.Command(promPath, "-test.main", "--config.file="+promConfig, "--storage.tsdb.path="+t.TempDir(), "--web.listen-address=0.0.0.0"+port)
err := prom.Start() err := prom.Start()
if err != nil { if err != nil {
t.Fatalf("execution error: %v", err) t.Fatalf("execution error: %v", err)
@ -45,11 +50,13 @@ func TestStartupInterrupt(t *testing.T) {
var startedOk bool var startedOk bool
var stoppedErr error var stoppedErr error
url := "http://localhost" + port + "/graph"
Loop: Loop:
for x := 0; x < 10; x++ { for x := 0; x < 10; x++ {
// error=nil means prometheus has started so we can send the interrupt // error=nil means prometheus has started so we can send the interrupt
// signal and wait for the graceful shutdown. // signal and wait for the graceful shutdown.
if _, err := http.Get("http://localhost:9090/graph"); err == nil { if _, err := http.Get(url); err == nil {
startedOk = true startedOk = true
prom.Process.Signal(os.Interrupt) prom.Process.Signal(os.Interrupt)
select { select {