@@ -22,6 +22,7 @@ import (
22
22
"os"
23
23
"path/filepath"
24
24
"reflect"
25
+ "regexp"
25
26
"runtime"
26
27
"strconv"
27
28
"strings"
@@ -1217,24 +1218,31 @@ func TestServer(t *testing.T) {
1217
1218
t .Parallel ()
1218
1219
1219
1220
ctx := testutil .Context (t , testutil .WaitLong )
1220
- randPort := testutil .RandomPort (t )
1221
- inv , cfg := clitest .New (t ,
1221
+ inv , _ := clitest .New (t ,
1222
1222
"server" ,
1223
1223
"--in-memory" ,
1224
1224
"--http-address" , ":0" ,
1225
1225
"--access-url" , "http://example.com" ,
1226
1226
"--provisioner-daemons" , "1" ,
1227
1227
"--prometheus-enable" ,
1228
- "--prometheus-address" , ":" + strconv . Itoa ( randPort ) ,
1228
+ "--prometheus-address" , ":0" ,
1229
1229
// "--prometheus-collect-db-metrics", // disabled by default
1230
1230
"--cache-dir" , t .TempDir (),
1231
1231
)
1232
1232
1233
+ pty := ptytest .New (t )
1234
+ inv .Stdout = pty .Output ()
1235
+ inv .Stderr = pty .Output ()
1236
+
1233
1237
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 ]
1235
1243
1236
1244
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 )
1238
1246
if err != nil {
1239
1247
t .Logf ("error creating request: %s" , err .Error ())
1240
1248
return false
@@ -1272,24 +1280,31 @@ func TestServer(t *testing.T) {
1272
1280
t .Parallel ()
1273
1281
1274
1282
ctx := testutil .Context (t , testutil .WaitLong )
1275
- randPort := testutil .RandomPort (t )
1276
- inv , cfg := clitest .New (t ,
1283
+ inv , _ := clitest .New (t ,
1277
1284
"server" ,
1278
1285
"--in-memory" ,
1279
1286
"--http-address" , ":0" ,
1280
1287
"--access-url" , "http://example.com" ,
1281
1288
"--provisioner-daemons" , "1" ,
1282
1289
"--prometheus-enable" ,
1283
- "--prometheus-address" , ":" + strconv . Itoa ( randPort ) ,
1290
+ "--prometheus-address" , ":0" ,
1284
1291
"--prometheus-collect-db-metrics" ,
1285
1292
"--cache-dir" , t .TempDir (),
1286
1293
)
1287
1294
1295
+ pty := ptytest .New (t )
1296
+ inv .Stdout = pty .Output ()
1297
+ inv .Stderr = pty .Output ()
1298
+
1288
1299
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 ]
1290
1305
1291
1306
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 )
1293
1308
if err != nil {
1294
1309
t .Logf ("error creating request: %s" , err .Error ())
1295
1310
return false
0 commit comments