From 05721a85d79599d97912c49d66d22b18d9e02428 Mon Sep 17 00:00:00 2001 From: Julien Pivotto <291750+roidelapluie@users.noreply.github.com> Date: Mon, 30 Mar 2026 09:53:48 +0200 Subject: [PATCH] cmd/prometheus: fix flaky TestQueryLog console test - Use an absolute path for the console templates directory to align with rules. - Rewrite waitForPrometheus using require.Eventually for cleaner error reporting and to fix a bug where the function could return nil even when the server never became ready. Signed-off-by: Julien Pivotto <291750+roidelapluie@users.noreply.github.com> --- cmd/prometheus/query_log_test.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/cmd/prometheus/query_log_test.go b/cmd/prometheus/query_log_test.go index e410f836a9..b3fd5e1bdb 100644 --- a/cmd/prometheus/query_log_test.go +++ b/cmd/prometheus/query_log_test.go @@ -68,16 +68,16 @@ func (p *queryLogTest) skip(t *testing.T) { } // waitForPrometheus waits for Prometheus to be ready. -func (p *queryLogTest) waitForPrometheus() error { - var err error - for range 20 { - var r *http.Response - if r, err = http.Get(fmt.Sprintf("http://%s:%d%s/-/ready", p.host, p.port, p.prefix)); err == nil && r.StatusCode == http.StatusOK { - break +func (p *queryLogTest) waitForPrometheus(t *testing.T) { + t.Helper() + require.Eventually(t, func() bool { + r, err := http.Get(fmt.Sprintf("http://%s:%d%s/-/ready", p.host, p.port, p.prefix)) + if err != nil { + return false } - time.Sleep(500 * time.Millisecond) - } - return err + r.Body.Close() + return r.StatusCode == http.StatusOK + }, 20*time.Second, 500*time.Millisecond, "prometheus at %s:%d did not become ready in time", p.host, p.port) } // setQueryLog alters the configuration file to enable or disable the query log, @@ -250,7 +250,7 @@ func (p *queryLogTest) params() []string { s = append(s, "--web.route-prefix="+p.prefix) } if p.origin == consoleOrigin { - s = append(s, "--web.console.templates="+filepath.Join("testdata", "consoles")) + s = append(s, "--web.console.templates="+filepath.Join(p.cwd, "testdata", "consoles")) } return s } @@ -323,7 +323,7 @@ func (p *queryLogTest) run(t *testing.T) { prom.Process.Kill() prom.Wait() }() - require.NoError(t, p.waitForPrometheus()) + p.waitForPrometheus(t) if !p.enabledAtStart { p.query(t)