Skip to content

Commit c2a6b5b

Browse files
committed
Wait till a connection to the CA host/port can be made
1 parent 98ddef5 commit c2a6b5b

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

test/integration/requestid_test.go

+35-3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ func reservePort(t *testing.T) (host, port string) {
5050
}
5151

5252
func Test_reflectRequestID(t *testing.T) {
53+
ctx := context.Background()
54+
5355
dir := t.TempDir()
5456
m, err := minica.New(minica.WithName("Step E2E"))
5557
require.NoError(t, err)
@@ -133,8 +135,11 @@ func Test_reflectRequestID(t *testing.T) {
133135
require.ErrorIs(t, err, http.ErrServerClosed)
134136
}()
135137

138+
// require the CA server to be available within 10 seconds,
139+
// failing the test if it doesn't.
140+
requireCAServerToBeAvailable(t, net.JoinHostPort("localhost", port), 10*time.Second)
141+
136142
// require OK health response as the baseline
137-
ctx := context.Background()
138143
healthResponse, err := caClient.HealthWithContext(ctx)
139144
require.NoError(t, err)
140145
if assert.NotNil(t, healthResponse) {
@@ -262,8 +267,8 @@ func newAuthorizingServer(t *testing.T, mca *minica.CA) *httptest.Server {
262267

263268
srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
264269
if assert.Equal(t, "signRequestID", r.Header.Get("X-Request-Id")) {
265-
json.NewEncoder(w).Encode(struct{ Allow bool }{Allow: true})
266-
w.WriteHeader(http.StatusOK)
270+
err := json.NewEncoder(w).Encode(struct{ Allow bool }{Allow: true})
271+
require.NoError(t, err)
267272
return
268273
}
269274

@@ -287,3 +292,30 @@ func newAuthorizingServer(t *testing.T, mca *minica.CA) *httptest.Server {
287292

288293
return srv
289294
}
295+
296+
func requireCAServerToBeAvailable(t *testing.T, address string, timeout time.Duration) {
297+
t.Helper()
298+
299+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
300+
defer cancel()
301+
302+
for !canConnect(ctx, address) {
303+
select {
304+
case <-ctx.Done():
305+
require.FailNow(t, fmt.Sprintf("CA server failed to start at https://%s within %s", address, timeout.String()))
306+
case <-time.After(100 * time.Millisecond):
307+
}
308+
}
309+
}
310+
311+
func canConnect(ctx context.Context, address string) bool {
312+
d := net.Dialer{Timeout: 5 * time.Second}
313+
conn, err := d.DialContext(ctx, "tcp", address)
314+
if err != nil {
315+
return false
316+
}
317+
318+
conn.Close()
319+
320+
return true
321+
}

0 commit comments

Comments
 (0)