@@ -22,6 +22,7 @@ import (
2222 "os"
2323 "path/filepath"
2424 "reflect"
25+ "regexp"
2526 "runtime"
2627 "strconv"
2728 "strings"
@@ -1217,24 +1218,31 @@ func TestServer(t *testing.T) {
12171218 t .Parallel ()
12181219
12191220 ctx := testutil .Context (t , testutil .WaitLong )
1220- randPort := testutil .RandomPort (t )
1221- inv , cfg := clitest .New (t ,
1221+ inv , _ := clitest .New (t ,
12221222 "server" ,
12231223 "--in-memory" ,
12241224 "--http-address" , ":0" ,
12251225 "--access-url" , "http://example.com" ,
12261226 "--provisioner-daemons" , "1" ,
12271227 "--prometheus-enable" ,
1228- "--prometheus-address" , ":" + strconv . Itoa ( randPort ) ,
1228+ "--prometheus-address" , ":0" ,
12291229 // "--prometheus-collect-db-metrics", // disabled by default
12301230 "--cache-dir" , t .TempDir (),
12311231 )
12321232
1233+ pty := ptytest .New (t )
1234+ inv .Stdout = pty .Output ()
1235+ inv .Stderr = pty .Output ()
1236+
12331237 clitest .Start (t , inv )
1234- _ = waitAccessURL (t , cfg )
1238+
1239+ // Wait until we see the prometheus address in the logs.
1240+ addrMatchExpr := `http server listening\s+addr=(\S+)\s+name=prometheus`
1241+ lineMatch := pty .ExpectRegexMatchContext (ctx , addrMatchExpr )
1242+ promAddr := regexp .MustCompile (addrMatchExpr ).FindStringSubmatch (lineMatch )[1 ]
12351243
12361244 testutil .Eventually (ctx , t , func (ctx context.Context ) bool {
1237- req , err := http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("http://127.0.0.1:%d /metrics" , randPort ), nil )
1245+ req , err := http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("http://%s /metrics" , promAddr ), nil )
12381246 if err != nil {
12391247 t .Logf ("error creating request: %s" , err .Error ())
12401248 return false
@@ -1272,24 +1280,31 @@ func TestServer(t *testing.T) {
12721280 t .Parallel ()
12731281
12741282 ctx := testutil .Context (t , testutil .WaitLong )
1275- randPort := testutil .RandomPort (t )
1276- inv , cfg := clitest .New (t ,
1283+ inv , _ := clitest .New (t ,
12771284 "server" ,
12781285 "--in-memory" ,
12791286 "--http-address" , ":0" ,
12801287 "--access-url" , "http://example.com" ,
12811288 "--provisioner-daemons" , "1" ,
12821289 "--prometheus-enable" ,
1283- "--prometheus-address" , ":" + strconv . Itoa ( randPort ) ,
1290+ "--prometheus-address" , ":0" ,
12841291 "--prometheus-collect-db-metrics" ,
12851292 "--cache-dir" , t .TempDir (),
12861293 )
12871294
1295+ pty := ptytest .New (t )
1296+ inv .Stdout = pty .Output ()
1297+ inv .Stderr = pty .Output ()
1298+
12881299 clitest .Start (t , inv )
1289- _ = waitAccessURL (t , cfg )
1300+
1301+ // Wait until we see the prometheus address in the logs.
1302+ addrMatchExpr := `http server listening\s+addr=(\S+)\s+name=prometheus`
1303+ lineMatch := pty .ExpectRegexMatchContext (ctx , addrMatchExpr )
1304+ promAddr := regexp .MustCompile (addrMatchExpr ).FindStringSubmatch (lineMatch )[1 ]
12901305
12911306 testutil .Eventually (ctx , t , func (ctx context.Context ) bool {
1292- req , err := http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("http://127.0.0.1:%d /metrics" , randPort ), nil )
1307+ req , err := http .NewRequestWithContext (ctx , "GET" , fmt .Sprintf ("http://%s /metrics" , promAddr ), nil )
12931308 if err != nil {
12941309 t .Logf ("error creating request: %s" , err .Error ())
12951310 return false
0 commit comments