@@ -50,6 +50,8 @@ func reservePort(t *testing.T) (host, port string) {
50
50
}
51
51
52
52
func Test_reflectRequestID (t * testing.T ) {
53
+ ctx := context .Background ()
54
+
53
55
dir := t .TempDir ()
54
56
m , err := minica .New (minica .WithName ("Step E2E" ))
55
57
require .NoError (t , err )
@@ -133,8 +135,11 @@ func Test_reflectRequestID(t *testing.T) {
133
135
require .ErrorIs (t , err , http .ErrServerClosed )
134
136
}()
135
137
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
+
136
142
// require OK health response as the baseline
137
- ctx := context .Background ()
138
143
healthResponse , err := caClient .HealthWithContext (ctx )
139
144
require .NoError (t , err )
140
145
if assert .NotNil (t , healthResponse ) {
@@ -262,8 +267,8 @@ func newAuthorizingServer(t *testing.T, mca *minica.CA) *httptest.Server {
262
267
263
268
srv := httptest .NewUnstartedServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
264
269
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 )
267
272
return
268
273
}
269
274
@@ -287,3 +292,30 @@ func newAuthorizingServer(t *testing.T, mca *minica.CA) *httptest.Server {
287
292
288
293
return srv
289
294
}
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