Use a random port in cmd/prometheus tests

There are a few tests that will run prometheus command.
This can test if there's already something listening on port :9090 since --web.listen-address defaults to 0.0.0.0:9090.
To fix that we can tell prometheus to use a random port on loopback interface.

Signed-off-by: Łukasz Mierzwa <l.mierzwa@gmail.com>
This commit is contained in:
Łukasz Mierzwa 2023-03-31 14:31:14 +01:00
parent 408f31f786
commit f2b9a39a48
1 changed files with 5 additions and 5 deletions

View File

@ -121,7 +121,7 @@ func TestFailedStartupExitCode(t *testing.T) {
fakeInputFile := "fake-input-file" fakeInputFile := "fake-input-file"
expectedExitStatus := 2 expectedExitStatus := 2
prom := exec.Command(promPath, "-test.main", "--config.file="+fakeInputFile) prom := exec.Command(promPath, "-test.main", "--web.listen-address=0.0.0.0:0", "--config.file="+fakeInputFile)
err := prom.Run() err := prom.Run()
require.Error(t, err) require.Error(t, err)
@ -358,7 +358,7 @@ func getCurrentGaugeValuesFor(t *testing.T, reg prometheus.Gatherer, metricNames
} }
func TestAgentSuccessfulStartup(t *testing.T) { func TestAgentSuccessfulStartup(t *testing.T) {
prom := exec.Command(promPath, "-test.main", "--enable-feature=agent", "--config.file="+agentConfig) prom := exec.Command(promPath, "-test.main", "--enable-feature=agent", "--web.listen-address=0.0.0.0:0", "--config.file="+agentConfig)
require.NoError(t, prom.Start()) require.NoError(t, prom.Start())
actualExitStatus := 0 actualExitStatus := 0
@ -376,7 +376,7 @@ func TestAgentSuccessfulStartup(t *testing.T) {
} }
func TestAgentFailedStartupWithServerFlag(t *testing.T) { func TestAgentFailedStartupWithServerFlag(t *testing.T) {
prom := exec.Command(promPath, "-test.main", "--enable-feature=agent", "--storage.tsdb.path=.", "--config.file="+promConfig) prom := exec.Command(promPath, "-test.main", "--enable-feature=agent", "--storage.tsdb.path=.", "--web.listen-address=0.0.0.0:0", "--config.file="+promConfig)
output := bytes.Buffer{} output := bytes.Buffer{}
prom.Stderr = &output prom.Stderr = &output
@ -403,7 +403,7 @@ func TestAgentFailedStartupWithServerFlag(t *testing.T) {
} }
func TestAgentFailedStartupWithInvalidConfig(t *testing.T) { func TestAgentFailedStartupWithInvalidConfig(t *testing.T) {
prom := exec.Command(promPath, "-test.main", "--enable-feature=agent", "--config.file="+promConfig) prom := exec.Command(promPath, "-test.main", "--enable-feature=agent", "--web.listen-address=0.0.0.0:0", "--config.file="+promConfig)
require.NoError(t, prom.Start()) require.NoError(t, prom.Start())
actualExitStatus := 0 actualExitStatus := 0
@ -438,7 +438,7 @@ func TestModeSpecificFlags(t *testing.T) {
for _, tc := range testcases { for _, tc := range testcases {
t.Run(fmt.Sprintf("%s mode with option %s", tc.mode, tc.arg), func(t *testing.T) { t.Run(fmt.Sprintf("%s mode with option %s", tc.mode, tc.arg), func(t *testing.T) {
args := []string{"-test.main", tc.arg, t.TempDir()} args := []string{"-test.main", tc.arg, t.TempDir(), "--web.listen-address=0.0.0.0:0"}
if tc.mode == "agent" { if tc.mode == "agent" {
args = append(args, "--enable-feature=agent", "--config.file="+agentConfig) args = append(args, "--enable-feature=agent", "--config.file="+agentConfig)