fix: test: do not block Prometheus port (#13945)

This commit is contained in:
Marcin Tojek
2024-07-19 16:29:10 +02:00
committed by GitHub
parent c88e4162d8
commit 40609c26e9

View File

@ -303,7 +303,7 @@ func TestProvisionerDaemon_SessionToken(t *testing.T) {
t.Run("PrometheusEnabled", func(t *testing.T) {
t.Parallel()
prometheusPort := testutil.RandomPort(t)
prometheusPort := testutil.RandomPortNoListen(t)
// Configure CLI client
client, admin := coderdenttest.New(t, &coderdenttest.Options{
@ -333,18 +333,27 @@ func TestProvisionerDaemon_SessionToken(t *testing.T) {
return false
}
return len(daemons) == 1
}, testutil.WaitShort, testutil.IntervalSlow)
}, testutil.WaitLong, testutil.IntervalSlow)
require.Equal(t, "daemon-with-prometheus", daemons[0].Name)
// Fetch metrics from Prometheus endpoint
var req *http.Request
var res *http.Response
require.Eventually(t, func() bool {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://127.0.0.1:%d", prometheusPort), nil)
assert.NoError(t, err)
req, err = http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://127.0.0.1:%d", prometheusPort), nil)
if err != nil {
t.Logf("unable to create new HTTP request: %s", err.Error())
return false
}
// nolint:bodyclose
res, err = http.DefaultClient.Do(req)
return err == nil
}, testutil.WaitShort, testutil.IntervalFast)
if err != nil {
t.Logf("unable to call Prometheus endpoint: %s", err.Error())
return false
}
return true
}, testutil.WaitShort, testutil.IntervalMedium)
defer res.Body.Close()
// Scan for metric patterns