From a93d51b2c437975342c9220976fa7c08f9059b3b Mon Sep 17 00:00:00 2001 From: Herman Slatman Date: Tue, 7 Jan 2025 01:41:02 +0100 Subject: [PATCH] Wait and retry connection to test CA server instead of failing (immediately) --- test/integration/requestid_test.go | 35 +++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/test/integration/requestid_test.go b/test/integration/requestid_test.go index 54fd2eb07..1ef44bf8b 100644 --- a/test/integration/requestid_test.go +++ b/test/integration/requestid_test.go @@ -50,6 +50,8 @@ func reservePort(t *testing.T) (host, port string) { } func Test_reflectRequestID(t *testing.T) { + ctx := context.Background() + dir := t.TempDir() m, err := minica.New(minica.WithName("Step E2E")) require.NoError(t, err) @@ -133,12 +135,29 @@ func Test_reflectRequestID(t *testing.T) { require.ErrorIs(t, err, http.ErrServerClosed) }() - // require OK health response as the baseline - ctx := context.Background() - healthResponse, err := caClient.HealthWithContext(ctx) - require.NoError(t, err) - if assert.NotNil(t, healthResponse) { - require.Equal(t, "ok", healthResponse.Status) + // require OK health response from the CA server within + // 10 seconds, retrying every ~100 milliseconds. + connected := false + stopTime := time.Now().Add(10 * time.Second) + for { + healthResponse, err := caClient.HealthWithContext(ctx) + if err == nil { + require.NotNil(t, healthResponse) + require.Equal(t, "ok", healthResponse.Status) + connected = true + } + + if connected || time.Now().After(stopTime) { + break + } + + time.Sleep(100 * time.Millisecond) + } + + // fail the test if CA client fails to connect to the + // CA server at least once within 10 seconds. + if !connected { + require.FailNow(t, fmt.Sprintf("CA client failed to connect to CA server at https://localhost:%s", port)) } // expect an error when retrieving an invalid root @@ -262,8 +281,8 @@ func newAuthorizingServer(t *testing.T, mca *minica.CA) *httptest.Server { srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if assert.Equal(t, "signRequestID", r.Header.Get("X-Request-Id")) { - json.NewEncoder(w).Encode(struct{ Allow bool }{Allow: true}) - w.WriteHeader(http.StatusOK) + err := json.NewEncoder(w).Encode(struct{ Allow bool }{Allow: true}) + require.NoError(t, err) return }